Completed
Pull Request — master (#24)
by Jason
13:57
created

CatalogProduct::canDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
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';
0 ignored issues
show
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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()) {
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
127
            $i = 0;
128
            foreach ($this->Categories()->sort('SortOrder') as $category) {
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
129
                $list .= $category->Title;
130
                if(++$i !== $this->Categories()->Count()) {
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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()) {
0 ignored issues
show
Documentation Bug introduced by
The method Slides does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
144
            $slide = $this->Slides()->first();
0 ignored issues
show
Documentation Bug introduced by
The method Slides does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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'),
0 ignored issues
show
Documentation introduced by
$fields->dataFieldByName('SKU') is of type object<SilverStripe\Forms\FormField>|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
197 1
            'Content'
0 ignored issues
show
Documentation introduced by
'Content' is of type string, but the function expects a object<SilverStripe\Forms\FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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');
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
The method CareCleaningDocs does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
The method OperationManuals does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
The method SpecSheets does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
The method Warranties does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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()) {
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
269
            $category = $this->Categories()->sort('SortOrder')->first();
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<Dynamic\ProductCatalog\ORM\CatalogProduct>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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 = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

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

Loading history...
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 = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

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

Loading history...
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 = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

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

Loading history...
352
    {
353
        return true;
354
    }
355
356
    public function allowedChildren()
357
    {
358
        return [];
359
    }
360
}