1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AntonyThorpe\SilverShopProductModel; |
4
|
|
|
|
5
|
|
|
use AntonyThorpe\SilverShopProductModel\ProductModel; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\GridField\GridField; |
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
9
|
|
|
use SilverStripe\Forms\HeaderField; |
10
|
|
|
use SilverStripe\Forms\LabelField; |
11
|
|
|
use SilverStripe\ORM\ArrayList; |
12
|
|
|
use SilverStripe\ORM\DataExtension; |
13
|
|
|
use SilverStripe\ORM\GroupedList; |
14
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Extends SilverShop\Page\ProductCategory to enable the adding of Models |
18
|
|
|
*/ |
19
|
|
|
class ProductCategoryExtension extends DataExtension |
20
|
|
|
{ |
21
|
|
|
private static $has_many = array ( |
|
|
|
|
22
|
|
|
'ProductModels' => ProductModel::class |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
public function updateCMSFields(FieldList $fields) |
26
|
|
|
{ |
27
|
|
|
$fields->addFieldToTab( |
28
|
|
|
'Root.' . _t(\SilverShop\Page\ProductCategory::class . 'Models', 'Models'), |
29
|
|
|
HeaderField::create( |
30
|
|
|
'ModelHeading', |
31
|
|
|
_t(\SilverShop\Page\ProductCategory::class . 'ModelsHeading', 'Specify the models for this Product Category') |
32
|
|
|
) |
33
|
|
|
); |
34
|
|
|
$fields->addFieldToTab( |
35
|
|
|
'Root.' . _t(\SilverShop\Page\ProductCategory::class . 'Models', 'Models'), |
36
|
|
|
LabelField::create( |
37
|
|
|
'ModelLabel', |
38
|
|
|
_t( |
39
|
|
|
\SilverShop\Page\ProductCategory::class . "ModelsLabel", |
40
|
|
|
"The below entries determine the order of Models, and their associated products, on the Product Category Pages. Also, sets the Models to appear in dropdown on each Product's page." |
41
|
|
|
) |
42
|
|
|
) |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$fields->addFieldToTab( |
46
|
|
|
'Root.' . _t(\SilverShop\Page\ProductCategory::class . 'Models', 'Models'), |
47
|
|
|
GridField::create( |
48
|
|
|
'Models', |
49
|
|
|
_t(\SilverShop\Page\ProductCategory::class . 'Models', 'Models'), |
50
|
|
|
$this->owner->ProductModels()->sort('Sort', 'ASC'), |
51
|
|
|
$config = GridFieldConfig_RecordEditor::create() |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
// Add reorder capabilties |
55
|
|
|
if ($this->owner->ProductModels()->count() >= 2) { |
56
|
|
|
$config->addComponent(new GridFieldOrderableRows('Sort')); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create a grouped list for presentation in a table |
62
|
|
|
* @return GroupedList |
63
|
|
|
*/ |
64
|
|
|
public function getGroupedProductsByModel() |
65
|
|
|
{ |
66
|
|
|
$list = $this->owner->ProductsShowable(); |
67
|
|
|
$sortedList = new ArrayList(); |
68
|
|
|
|
69
|
|
|
foreach ($this->owner->ProductModels()->sort('Sort') as $model) { |
70
|
|
|
foreach ($list as $product) { |
71
|
|
|
if ($product->Model == $model->Title) { |
72
|
|
|
$sortedList->push($product); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return GroupedList::create($sortedList); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|