1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package FoxyStripe |
5
|
|
|
* @method Products|SS_List $Products |
6
|
|
|
*/ |
7
|
|
|
class ProductHolder extends Page |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private static $singular_name = 'Product Group'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private static $plural_name = 'Product Groups'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $description = 'Display a list of related products'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $allowed_children = [ |
|
|
|
|
29
|
|
|
'ProductHolder', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private static $many_many = [ |
36
|
|
|
'Products' => 'FoxyStripeProduct', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private static $many_many_extraFields = array( |
43
|
|
|
'Products' => array( |
44
|
|
|
'SortOrder' => 'Int' |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return FieldList |
50
|
|
|
*/ |
51
|
1 |
|
public function getCMSFields() |
52
|
|
|
{ |
53
|
1 |
|
$fields = parent::getCMSFields(); |
54
|
|
|
|
55
|
1 |
|
$config = GridFieldConfig_RelationEditor::create(); |
56
|
1 |
|
if (class_exists('GridFieldSortableRows')) { |
57
|
|
|
$config->addComponent(new GridFieldSortableRows('SortOrder')); |
58
|
|
|
} |
59
|
1 |
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
60
|
1 |
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
61
|
1 |
|
$fields->addFieldToTab( |
62
|
1 |
|
'Root.Products', |
63
|
1 |
|
GridField::create( |
64
|
1 |
|
'Products', |
65
|
1 |
|
_t('ProductHolder.Products', 'Products'), |
66
|
1 |
|
$this->Products(), |
67
|
|
|
$config |
68
|
1 |
|
) |
69
|
1 |
|
); |
70
|
|
|
|
71
|
1 |
|
$this->extend('updateCMSFields', $fields); |
72
|
|
|
|
73
|
1 |
|
return $fields; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* loadDescendantProductGroupIDListInto function. |
78
|
|
|
* |
79
|
|
|
* @access public |
80
|
|
|
* @param mixed &$idList |
81
|
|
|
*/ |
82
|
1 |
|
public function loadDescendantProductGroupIDListInto(&$idList) |
83
|
|
|
{ |
84
|
|
|
if ($children = $this->AllChildren()) { |
85
|
1 |
|
foreach ($children as $child) { |
86
|
|
|
if (in_array($child->ID, $idList)) continue; |
87
|
|
|
|
88
|
|
|
if ($child instanceof ProductHolder) { |
89
|
|
|
$idList[] = $child->ID; |
90
|
|
|
$child->loadDescendantProductGroupIDListInto($idList); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* ProductGroupIDs function. |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function ProductGroupIDs() |
103
|
|
|
{ |
104
|
|
|
$holderIDs = []; |
105
|
|
|
$this->loadDescendantProductGroupIDListInto($holderIDs); |
106
|
|
|
return $holderIDs; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param int $limit |
111
|
|
|
* @return PaginatedList |
112
|
|
|
*/ |
113
|
1 |
|
public function getPaginatedProducts($limit = 10) |
114
|
|
|
{ |
115
|
1 |
|
return PaginatedList::create($this->Products()->sort('SortOrder'))->setPageLength($limit); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
class ProductHolder_Controller extends Page_Controller |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
private static $allowed_actions = [ |
123
|
|
|
'product', |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
*/ |
129
|
|
|
public function init() |
130
|
|
|
{ |
131
|
|
|
parent::init(); |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function product(SS_HTTPRequest $request) |
136
|
|
|
{ |
137
|
|
|
|
138
|
|
|
if (!$slug = $request->latestParam('ID')) { |
139
|
|
|
return $this->httpError(404); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (!$product = FoxyStripeProduct::get()->byUrlSegment($slug)) { |
143
|
|
|
return $this->httpError(404); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->customise([ |
147
|
|
|
'Product' => $product, |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.