1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class CatalogProduct extends DataObject implements PermissionProvider, Dynamic\ViewableDataObject\VDOInterfaces\ViewableDataObjectInterface |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* @var string |
7
|
|
|
*/ |
8
|
|
|
private static $singular_name = 'Product'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private static $plural_name = 'Products'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private static $db = array( |
19
|
|
|
'Title' => 'Varchar(255)', |
20
|
|
|
'Content' => 'HTMLText', |
21
|
|
|
'QuickFeatures' => 'HTMLText', |
22
|
|
|
'Dimensions' => 'Varchar(255)', |
23
|
|
|
'SKU' => 'Varchar(50)', |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private static $many_many = array( |
30
|
|
|
'Categories' => 'CatalogCategory', |
31
|
|
|
'CareCleaningDocs' => 'CareCleaningDoc', |
32
|
|
|
'OperationManuals' => 'OperationManual', |
33
|
|
|
'SpecSheets' => 'SpecSheet', |
34
|
|
|
'Warranties' => 'Warranty', |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private static $many_many_extraFields = array( |
41
|
|
|
'Categories' => array( |
42
|
|
|
'SortOrder' => 'Int', |
43
|
|
|
), |
44
|
|
|
'CareCleaningDocs' => array( |
45
|
|
|
'Sort' => 'Int', |
46
|
|
|
), |
47
|
|
|
'OperationManuals' => array( |
48
|
|
|
'Sort' => 'Int', |
49
|
|
|
), |
50
|
|
|
'SpecSheets' => array( |
51
|
|
|
'Sort' => 'Int', |
52
|
|
|
), |
53
|
|
|
'Warranties' => array( |
54
|
|
|
'Sort' => 'Int', |
55
|
|
|
), |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
private static $summary_fields = array( |
62
|
|
|
'ProductThumbnail' => 'Image', |
63
|
|
|
'Title' => 'Title', |
64
|
|
|
'Type' => 'Type', |
65
|
|
|
'CategoryList' => 'Categories', |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
private static $searchable_fields = array( |
72
|
|
|
'Title' => array( |
73
|
|
|
'title' => 'Product Name', |
74
|
|
|
), |
75
|
|
|
'Categories.ID' => array( |
76
|
|
|
'title' => 'Category', |
77
|
|
|
), |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
private static $extensions = [ |
84
|
|
|
'VersionedDataObject', |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param bool $includerelations |
89
|
|
|
* @return array|string |
90
|
|
|
*/ |
91
|
2 |
|
public function fieldLabels($includerelations = true) |
92
|
|
|
{ |
93
|
2 |
|
$labels = parent::fieldLabels($includerelations); |
94
|
|
|
|
95
|
2 |
|
$labels['Title'] = 'Product Name'; |
96
|
2 |
|
$labels['Categories.ID'] = 'Category'; |
97
|
|
|
|
98
|
2 |
|
return $labels; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
1 |
|
public function CategoryList() |
105
|
|
|
{ |
106
|
1 |
|
$list = ''; |
107
|
|
|
|
108
|
1 |
|
if ($this->Categories()) { |
|
|
|
|
109
|
1 |
|
$i = 0; |
110
|
1 |
|
foreach ($this->Categories()->sort('SortOrder') as $category) { |
|
|
|
|
111
|
1 |
|
$list .= $category->Title; |
112
|
1 |
|
if(++$i !== $this->Categories()->Count()) { |
|
|
|
|
113
|
|
|
$list .= ", "; |
114
|
|
|
} |
115
|
1 |
|
} |
116
|
1 |
|
} |
117
|
1 |
|
return $list; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
1 |
|
public function getImage() |
124
|
|
|
{ |
125
|
1 |
|
if ($this->Slides()->exists()) { |
|
|
|
|
126
|
|
|
return $this->Slides()->first(); |
|
|
|
|
127
|
|
|
} |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function Image() |
134
|
|
|
{ |
135
|
|
|
return $this->getImage(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
public function ProductThumbnail() |
142
|
|
|
{ |
143
|
|
|
$image = $this->getImage(); |
144
|
|
|
$thumb = Image::get()->byID($image->ID); |
145
|
|
|
return $thumb->CMSThumbnail(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return FieldList |
150
|
|
|
*/ |
151
|
1 |
|
public function getCMSFields() |
152
|
|
|
{ |
153
|
1 |
|
$fields = parent::getCMSFields(); |
154
|
|
|
|
155
|
|
|
$remove = [ |
156
|
1 |
|
'Categories', |
157
|
1 |
|
'CareCleaningDocs', |
158
|
1 |
|
'OperationManuals', |
159
|
1 |
|
'SpecSheets', |
160
|
1 |
|
'Warranties', |
161
|
1 |
|
'DisabledBlocks', |
162
|
1 |
|
]; |
163
|
|
|
|
164
|
1 |
|
if (!$this->ID) { |
165
|
1 |
|
$remove[] = 'Blocks'; |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
|
$fields->removeByName($remove); |
169
|
|
|
|
170
|
1 |
|
$fields->insertBefore( |
171
|
1 |
|
$fields->dataFieldByName('SKU'), |
172
|
|
|
'Content' |
|
|
|
|
173
|
1 |
|
); |
174
|
|
|
|
175
|
1 |
|
$fields->addFieldsToTab('Root.Info', array( |
176
|
1 |
|
TextField::create('Dimensions'), |
177
|
1 |
|
HTMLEditorField::create('QuickFeatures'), |
178
|
1 |
|
)); |
179
|
|
|
|
180
|
1 |
|
if ($this->ID) { |
181
|
|
|
// Categories |
182
|
1 |
|
$config = GridFieldConfig_RelationEditor::create(); |
183
|
1 |
|
$config->addComponent(new GridFieldOrderableRows('SortOrder')); |
184
|
1 |
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
185
|
1 |
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
186
|
1 |
|
$config->removeComponentsByType('GridFieldAddNewButton'); |
187
|
1 |
|
$categories = $this->Categories()->sort('SortOrder'); |
|
|
|
|
188
|
1 |
|
$categoryField = GridField::create('Categories', 'Categories', $categories, $config); |
189
|
|
|
|
190
|
1 |
|
$fields->addFieldsToTab('Root.Categories.Categories', array( |
191
|
1 |
|
$categoryField, |
192
|
1 |
|
)); |
193
|
|
|
|
194
|
|
|
// Care and Cleaning |
195
|
1 |
|
$config = GridFieldConfig_RecordEditor::create(); |
196
|
1 |
|
$config->addComponent(new GridFieldOrderableRows('Sort')); |
197
|
1 |
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
198
|
1 |
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
199
|
1 |
|
$operation = GridField::create('CareCleaningDocs', 'Care and Cleaning', $this->CareCleaningDocs()->sort('Sort'), $config); |
|
|
|
|
200
|
1 |
|
$fields->addFieldsToTab('Root.Files.Care', array( |
201
|
1 |
|
$operation, |
202
|
1 |
|
)); |
203
|
|
|
|
204
|
|
|
// Operation Manuals |
205
|
1 |
|
$config = GridFieldConfig_RecordEditor::create(); |
206
|
1 |
|
$config->addComponent(new GridFieldOrderableRows('Sort')); |
207
|
1 |
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
208
|
1 |
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
209
|
1 |
|
$operation = GridField::create('OperationManuals', 'Operation Manuals', $this->OperationManuals()->sort('Sort'), $config); |
|
|
|
|
210
|
1 |
|
$fields->addFieldsToTab('Root.Files.Operation', array( |
211
|
1 |
|
$operation, |
212
|
1 |
|
)); |
213
|
|
|
|
214
|
|
|
// Spec Sheets |
215
|
1 |
|
$config = GridFieldConfig_RecordEditor::create(); |
216
|
1 |
|
$config->addComponent(new GridFieldOrderableRows('Sort')); |
217
|
1 |
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
218
|
1 |
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
219
|
1 |
|
$specsheets = GridField::create('SpecSheets', 'Spec Sheets', $this->SpecSheets()->sort('Sort'), $config); |
|
|
|
|
220
|
1 |
|
$fields->addFieldsToTab('Root.Files.SpecSheets', array( |
221
|
1 |
|
$specsheets, |
222
|
1 |
|
)); |
223
|
|
|
|
224
|
|
|
// Warranties |
225
|
1 |
|
$config = GridFieldConfig_RecordEditor::create(); |
226
|
1 |
|
$config->addComponent(new GridFieldOrderableRows('Sort')); |
227
|
1 |
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
228
|
1 |
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
229
|
1 |
|
$warranties = GridField::create('Warranties', 'Warranties', $this->Warranties()->sort('Sort'), $config); |
|
|
|
|
230
|
1 |
|
$fields->addFieldsToTab('Root.Files.Warranty', array( |
231
|
1 |
|
$warranties, |
232
|
1 |
|
)); |
233
|
1 |
|
} |
234
|
|
|
|
235
|
1 |
|
return $fields; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return CatalogCategory |
240
|
|
|
*/ |
241
|
2 |
|
public function getPrimaryCategory() |
242
|
|
|
{ |
243
|
2 |
|
if ($this->Categories()->exists()) { |
|
|
|
|
244
|
2 |
|
$category = $this->Categories()->sort('SortOrder')->first(); |
|
|
|
|
245
|
2 |
|
} else { |
246
|
1 |
|
$category = CatalogCategory::get()->first(); |
247
|
|
|
} |
248
|
2 |
|
return $category; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* set ParentPage for ViewableDataobject |
253
|
|
|
* |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
1 |
|
public function getParentPage() |
257
|
|
|
{ |
258
|
1 |
|
return $this->getPrimaryCategory(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* set ViewAction for ViewableDataobject |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
1 |
|
public function getViewAction() |
267
|
|
|
{ |
268
|
1 |
|
return 'view'; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* set Ancestors, needed for Blocks on Dataobjects |
273
|
|
|
* |
274
|
|
|
* @return DataList |
275
|
|
|
*/ |
276
|
|
|
public function getAncestors() |
277
|
|
|
{ |
278
|
|
|
return CatalogCategory::get(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
1 |
|
public function providePermissions() |
285
|
|
|
{ |
286
|
|
|
return array( |
287
|
1 |
|
'Product_EDIT' => 'Edit a Product', |
288
|
1 |
|
'Product_DELETE' => 'Delete a Product', |
289
|
1 |
|
'Product_CREATE' => 'Create a Product', |
290
|
1 |
|
); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param null $member |
295
|
|
|
* |
296
|
|
|
* @return bool|int |
297
|
|
|
*/ |
298
|
1 |
|
public function canEdit($member = null) |
299
|
|
|
{ |
300
|
1 |
|
return Permission::check('Product_EDIT', 'any', $member); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param null $member |
305
|
|
|
* |
306
|
|
|
* @return bool|int |
307
|
|
|
*/ |
308
|
1 |
|
public function canDelete($member = null) |
309
|
|
|
{ |
310
|
1 |
|
return Permission::check('Product_DELETE', 'any', $member); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param null $member |
315
|
|
|
* |
316
|
|
|
* @return bool|int |
317
|
|
|
*/ |
318
|
1 |
|
public function canCreate($member = null) |
319
|
|
|
{ |
320
|
1 |
|
return Permission::check('Product_CREATE', 'any', $member); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param null $member |
325
|
|
|
* |
326
|
|
|
* @return bool |
327
|
|
|
*/ |
328
|
1 |
|
public function canView($member = null) |
329
|
|
|
{ |
330
|
1 |
|
return true; |
331
|
|
|
} |
332
|
|
|
} |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: