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