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