Completed
Pull Request — master (#30)
by Jason
14:29
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\Dev\Debug;
13
use SilverStripe\Forms\GridField\GridField;
14
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
15
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
16
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
17
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
18
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
19
use SilverStripe\Forms\TextField;
20
use SilverStripe\ORM\DataObject;
21
use SilverStripe\Security\Permission;
22
use SilverStripe\Security\PermissionProvider;
23
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
24
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
25
26
class CatalogProduct extends DataObject implements PermissionProvider, ViewableDataObjectInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    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...
32
33
    /**
34
     * @var string
35
     */
36
    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...
37
38
    /**
39
     * @var array
40
     */
41
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
42
        'Title' => 'Varchar(255)',
43
        'Content' => 'HTMLText',
44
        'QuickFeatures' => 'HTMLText',
45
        'Dimensions' => 'Varchar(255)',
46
        'SKU' => 'Varchar(50)',
47
    );
48
49
    /**
50
     * @var array
51
     */
52
    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...
53
        'Categories' => CatalogCategory::class,
54
        'CareCleaningDocs' => CareCleaningDoc::class,
55
        'OperationManuals' => OperationManual::class,
56
        'SpecSheets' => SpecSheet::class,
57
        'Warranties' => Warranty::class,
58
    );
59
60
    /**
61
     * @var array
62
     */
63
    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...
64
        'Categories' => array(
65
            'SortOrder' => 'Int',
66
        ),
67
        'CareCleaningDocs' => array(
68
            'Sort' => 'Int',
69
        ),
70
        'OperationManuals' => array(
71
            'Sort' => 'Int',
72
        ),
73
        'SpecSheets' => array(
74
            'Sort' => 'Int',
75
        ),
76
        'Warranties' => array(
77
            'Sort' => 'Int',
78
        ),
79
    );
80
81
    /**
82
     * @var string
83
     */
84
    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...
85
86
    /**
87
     * @var bool
88
     */
89
    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...
90
91
    /**
92
     * @var array
93
     */
94
    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...
95
        'ProductThumbnail' => 'Image',
96
        'Title' => 'Title',
97
        'Type' => 'Type',
98
        'CategoryList' => 'Categories',
99
    );
100
101
    /**
102
     * @var array
103
     */
104
    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...
105
        'Title' => array(
106
            'title' => 'Product Name',
107
        ),
108
        'Categories.ID' => array(
109
            'title' => 'Category',
110
        ),
111
    );
112
113
    /**
114
     * @var string
115
     */
116
    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...
117
118
    /**
119
     * @param bool $includerelations
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
        return $list;
149
    }
150
151
    /**
152
     * @return mixed
153
     */
154
    public function getImage()
155
    {
156
        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

156
        if ($this->/** @scrutinizer ignore-call */ Slides()->exists()) {
Loading history...
157
            $slide = $this->Slides()->first();
158
            if ($slide->ImageID > 0) {
159
                return $slide->Image();
160
            }
161
        }
162
        return false;
163
    }
164
165
    /**
166
     * @return mixed
167
     */
168
    public function Image()
169
    {
170
        return $this->getImage();
171
    }
172
173
    /**
174
     * @return mixed
175
     */
176
    public function ProductThumbnail()
177
    {
178
        if ($image = $this->getImage()) {
179
            if ($thumb = Image::get()->byID($image->ID)) {
180
                return $thumb->CMSThumbnail();
181
            }
182
        }
183
        return false;
184
    }
185
186
    /**
187
     * @return \SilverStripe\Forms\FieldList
188
     */
189
    public function getCMSFields()
190
    {
191
        $fields = parent::getCMSFields();
192
193
        $remove = [
194
            'Categories',
195
            'CareCleaningDocs',
196
            'OperationManuals',
197
            'SpecSheets',
198
            'Warranties',
199
            'DisabledBlocks',
200
            'Slides',
201
        ];
202
203
        if (!$this->ID) {
204
            $remove[] = 'Blocks';
205
        }
206
207
        $fields->removeByName($remove);
208
209
        $fields->insertBefore(
210
            $fields->dataFieldByName('SKU'),
211
            '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

211
            /** @scrutinizer ignore-type */ 'Content'
Loading history...
212
        );
213
214
        $fields->addFieldsToTab('Root.Info', array(
215
            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

215
            TextField::create(/** @scrutinizer ignore-type */ 'Dimensions'),
Loading history...
216
            HTMLEditorField::create('QuickFeatures')
217
                ->addExtraClass('stacked'),
218
        ));
219
220
        if ($this->ID) {
221
            // Categories
222
            $config = GridFieldConfig_RelationEditor::create();
223
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
224
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
225
            $config->addComponent(new GridFieldAddExistingSearchButton());
226
            $config->removeComponentsByType(GridFieldAddNewButton::class);
227
            $categories = $this->Categories()->sort('SortOrder');
228
            $categoryField = GridField::create('Categories', 'Categories', $categories, $config);
229
230
            $fields->addFieldsToTab('Root.Categories.Categories', array(
231
                $categoryField,
232
            ));
233
234
            // Care and Cleaning
235
            $config = GridFieldConfig_RecordEditor::create();
236
            $config->addComponent(new GridFieldOrderableRows('Sort'));
237
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
238
            $config->addComponent(new GridFieldAddExistingSearchButton());
239
            $operation = GridField::create('CareCleaningDocs', 'Care and Cleaning', $this->CareCleaningDocs()->sort('Sort'), $config);
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

239
            $operation = GridField::create('CareCleaningDocs', 'Care and Cleaning', $this->/** @scrutinizer ignore-call */ CareCleaningDocs()->sort('Sort'), $config);
Loading history...
240
            $fields->addFieldsToTab('Root.Files.Care', array(
241
                $operation,
242
            ));
243
244
            // Operation Manuals
245
            $config = GridFieldConfig_RecordEditor::create();
246
            $config->addComponent(new GridFieldOrderableRows('Sort'));
247
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
248
            $config->addComponent(new GridFieldAddExistingSearchButton());
249
            $operation = GridField::create('OperationManuals', 'Operation Manuals', $this->OperationManuals()->sort('Sort'), $config);
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

249
            $operation = GridField::create('OperationManuals', 'Operation Manuals', $this->/** @scrutinizer ignore-call */ OperationManuals()->sort('Sort'), $config);
Loading history...
250
            $fields->addFieldsToTab('Root.Files.Operation', array(
251
                $operation,
252
            ));
253
254
            // Spec Sheets
255
            $config = GridFieldConfig_RecordEditor::create();
256
            $config->addComponent(new GridFieldOrderableRows('Sort'));
257
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
258
            $config->addComponent(new GridFieldAddExistingSearchButton());
259
            $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

259
            $specsheets = GridField::create('SpecSheets', 'Spec Sheets', $this->/** @scrutinizer ignore-call */ SpecSheets()->sort('Sort'), $config);
Loading history...
260
            $fields->addFieldsToTab('Root.Files.SpecSheets', array(
261
                $specsheets,
262
            ));
263
264
            // Warranties
265
            $config = GridFieldConfig_RecordEditor::create();
266
            $config->addComponent(new GridFieldOrderableRows('Sort'));
267
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
268
            $config->addComponent(new GridFieldAddExistingSearchButton());
269
            $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

269
            $warranties = GridField::create('Warranties', 'Warranties', $this->/** @scrutinizer ignore-call */ Warranties()->sort('Sort'), $config);
Loading history...
270
            $fields->addFieldsToTab('Root.Files.Warranty', array(
271
                $warranties,
272
            ));
273
        }
274
275
        return $fields;
276
    }
277
278
    /**
279
     * @return CatalogCategory
280
     */
281
    public function getPrimaryCategory()
282
    {
283
        if ($this->Categories()->exists()) {
284
            $category = $this->Categories()->sort('SortOrder')->first();
285
        } else {
286
            $category = CatalogCategory::get()->first();
287
        }
288
        return $category;
289
    }
290
291
    /**
292
     * set ParentPage for ViewableDataobject
293
     *
294
     * @return string
295
     */
296
    public function getParentPage()
297
    {
298
        return $this->getPrimaryCategory();
299
    }
300
301
    /**
302
     * set ViewAction for ViewableDataobject
303
     *
304
     * @return string
305
     */
306
    public function getViewAction()
307
    {
308
        return 'view';
309
    }
310
311
    /**
312
     * @return \SilverStripe\ORM\DataList
313
     */
314
    public function getAncestors()
315
    {
316
        return CatalogCategory::get();
317
    }
318
319
    /**
320
     * @return array
321
     */
322
    public function providePermissions()
323
    {
324
        return array(
325
            'Product_EDIT' => 'Edit a Product',
326
            'Product_DELETE' => 'Delete a Product',
327
            'Product_CREATE' => 'Create a Product',
328
        );
329
    }
330
331
    /**
332
     * @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...
333
     *
334
     * @return bool|int
335
     */
336
    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

336
    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...
337
    {
338
        return Permission::check('Product_EDIT', 'any', $member);
339
    }
340
341
    /**
342
     * @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...
343
     *
344
     * @return bool|int
345
     */
346
    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

346
    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...
347
    {
348
        return Permission::check('Product_DELETE', 'any', $member);
349
    }
350
351
    /**
352
     * @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...
353
     *
354
     * @return bool|int
355
     */
356
    public function canCreate($member = null, $context = [])
357
    {
358
        return Permission::check('Product_CREATE', 'any', $member);
359
    }
360
361
    /**
362
     * @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...
363
     *
364
     * @return bool
365
     */
366
    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

366
    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...
367
    {
368
        return true;
369
    }
370
371
    /**
372
     * @return array
373
     */
374
    public function allowedChildren()
375
    {
376
        return [];
377
    }
378
}