Completed
Pull Request — master (#30)
by Jason
17:47 queued 02:47
created

CatalogProduct::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
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';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
31
32
    /**
33
     * @var string
34
     */
35
    private static $plural_name = 'Products';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
36
37
    /**
38
     * @var array
39
     */
40
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
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(
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
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(
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
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';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
84
85
    /**
86
     * @var bool
87
     */
88
    private static $versioned_gridfield_extensions = true;
0 ignored issues
show
introduced by
The private property $versioned_gridfield_extensions is not used, and could be removed.
Loading history...
89
90
    /**
91
     * @var array
92
     */
93
    private static $summary_fields = array(
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
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(
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
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';
0 ignored issues
show
introduced by
The private property $slide_tab_title is not used, and could be removed.
Loading history...
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()) {
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on Dynamic\ProductCatalog\ORM\CatalogProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
        if ($this->/** @scrutinizer ignore-call */ Categories()) {
Loading history...
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()) {
0 ignored issues
show
Bug introduced by
The method Slides() does not exist on Dynamic\ProductCatalog\ORM\CatalogProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        if ($this->/** @scrutinizer ignore-call */ Slides()->exists()) {
Loading history...
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'
0 ignored issues
show
Bug introduced by
'Content' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

214
            /** @scrutinizer ignore-type */ 'Content'
Loading history...
215
        );
216
217
        $fields->addFieldsToTab('Root.Info', array(
218
            TextField::create('Dimensions'),
0 ignored issues
show
Bug introduced by
'Dimensions' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
            TextField::create(/** @scrutinizer ignore-type */ 'Dimensions'),
Loading history...
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'),
0 ignored issues
show
Bug introduced by
The method CareCleaningDocs() does not exist on Dynamic\ProductCatalog\ORM\CatalogProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

245
                $this->/** @scrutinizer ignore-call */ 
246
                       CareCleaningDocs()->sort('Sort'),
Loading history...
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'),
0 ignored issues
show
Bug introduced by
The method OperationManuals() does not exist on Dynamic\ProductCatalog\ORM\CatalogProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
                $this->/** @scrutinizer ignore-call */ 
261
                       OperationManuals()->sort('Sort'),
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method SpecSheets() does not exist on Dynamic\ProductCatalog\ORM\CatalogProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

272
            $specsheets = GridField::create('SpecSheets', 'Spec Sheets', $this->/** @scrutinizer ignore-call */ SpecSheets()->sort('Sort'), $config);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method Warranties() does not exist on Dynamic\ProductCatalog\ORM\CatalogProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

282
            $warranties = GridField::create('Warranties', 'Warranties', $this->/** @scrutinizer ignore-call */ Warranties()->sort('Sort'), $config);
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
347
     *
348
     * @return bool|int
349
     */
350
    public function canEdit($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

350
    public function canEdit($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
351
    {
352
        return Permission::check('Product_EDIT', 'any', $member);
353
    }
354
355
    /**
356
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
357
     *
358
     * @return bool|int
359
     */
360
    public function canDelete($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

360
    public function canDelete($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
361
    {
362
        return Permission::check('Product_DELETE', 'any', $member);
363
    }
364
365
    /**
366
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
377
     *
378
     * @return bool
379
     */
380
    public function canView($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

380
    public function canView($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
381
    {
382
        return true;
383
    }
384
385
    /**
386
     * @return array
387
     */
388
    public function allowedChildren()
389
    {
390
        return [];
391
    }
392
}
393