Passed
Pull Request — master (#173)
by Matthew
13:34
created

LocationCategory::getCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 23
rs 9.0856
1
<?php
2
3
namespace Dynamic\Locator;
4
5
use SilverStripe\Forms\GridField\GridField;
6
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
7
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\Permission;
10
11
/**
12
 * Class LocationCategory
13
 *
14
 * @property string $Name
15
 * @method Locations|HasManyList $Locations
16
 * @method Locators|ManyManyList $Locators
17
 */
18
class LocationCategory extends DataObject
19
{
20
    /**
21
     * @var string
22
     */
23
    private static $singular_name = 'Category';
24
25
    /**
26
     * @var string
27
     */
28
    private static $plural_name = 'Categories';
29
30
    /**
31
     * @var array
32
     */
33
    private static $db = array(
34
        'Name' => 'Varchar(100)',
35
    );
36
37
    /**
38
     * @var array
39
     */
40
    private static $has_many = array(
41
        'Locations' => Location::class,
42
    );
43
44
    /**
45
     * @var array
46
     */
47
    private static $belogs_many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $belogs_many_many 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...
48
        'Locators' => Locator::class,
49
    );
50
51
    /**
52
     * @var string
53
     */
54
    private static $table_name = 'LocationCategory';
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...
55
56
    /**
57
     * @var string
58
     */
59
    private static $default_sort = 'Name';
0 ignored issues
show
Unused Code introduced by
The property $default_sort 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...
60
61
    /**
62
     * @return \SilverStripe\Forms\FieldList
63
     */
64
    public function getCMSFields()
65
    {
66
        $fields = parent::getCMSFields();
67
68
        $fields->removeByName([
69
            'Locations',
70
        ]);
71
72
        if ($this->ID) {
73
            // Locations
74
            $config = GridFieldConfig_RelationEditor::create();
75
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
76
            $config->addComponent(new GridFieldAddExistingAutocompleter());
77
            $config->removeComponentsByType('GridFieldAddNewButton');
78
            $locations = $this->Locations();
0 ignored issues
show
Bug introduced by
The method Locations() does not exist on Dynamic\Locator\LocationCategory. 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

78
            /** @scrutinizer ignore-call */ 
79
            $locations = $this->Locations();
Loading history...
79
            $locationField = GridField::create('Locations', 'Locations', $locations, $config);
0 ignored issues
show
Bug introduced by
'Locations' 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

79
            $locationField = GridField::create(/** @scrutinizer ignore-type */ 'Locations', 'Locations', $locations, $config);
Loading history...
80
81
            $fields->addFieldsToTab('Root.Locations', array(
82
                $locationField,
83
            ));
84
        }
85
86
        return $fields;
87
    }
88
89
    /**
90
     * @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...
91
     * @param array $context
92
     * @return bool
93
     */
94
    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

94
    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...
95
    {
96
        return true;
97
    }
98
99
    /**
100
     * @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...
101
     * @param array $context
102
     * @return bool|int
103
     */
104
    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

104
    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...
105
    {
106
        return Permission::check('Location_EDIT', 'any', $member);
107
    }
108
109
    /**
110
     * @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...
111
     * @param array $context
112
     * @return bool|int
113
     */
114
    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

114
    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...
115
    {
116
        return Permission::check('Location_DELETE', 'any', $member);
117
    }
118
119
    /**
120
     * @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...
121
     * @param array $context
122
     * @return bool|int
123
     */
124
    public function canCreate($member = null, $context = [])
125
    {
126
        return Permission::check('Location_CREATE', 'any', $member);
127
    }
128
}
129