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

Location::canEdit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Dynamic\Locator;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Security\PermissionProvider;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\EmailField;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Security\Permission;
11
12
/**
13
 * Class Location
14
 *
15
 * @property string $Title
16
 * @property bool $Featured
17
 * @property string $Website
18
 * @property string $Phone
19
 * @property string $Email
20
 * @property string $EmailAddress
21
 * @property int $Import_ID
22
 * @property int $CategoryID
23
 * @method LocationCategory $Category
24
 */
25
class Location extends DataObject implements PermissionProvider
26
{
27
    /**
28
     * @var string
29
     */
30
    private static $singular_name = 'Location';
31
32
    /**
33
     * @var string
34
     */
35
    private static $plural_name = 'Locations';
36
37
    /**
38
     * @var array
39
     */
40
    private static $db = array(
41
        'Title' => 'Varchar(255)',
42
        'Featured' => 'Boolean',
43
        'Website' => 'Varchar(255)',
44
        'Phone' => 'Varchar(40)',
45
        'Email' => 'Varchar(255)',
46
        'Import_ID' => 'Int',
47
    );
48
49
    /**
50
     * @var array
51
     */
52
    private static $has_one = array(
53
        'Category' => LocationCategory::class,
54
    );
55
56
    /**
57
     * @var string
58
     */
59
    private static $table_name = 'Location';
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...
60
61
    /**
62
     * @var array
63
     */
64
    private static $casting = array(
65
        'distance' => 'Decimal(9,3)',
66
    );
67
68
    /**
69
     * @var string
70
     */
71
    private static $default_sort = 'Title';
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...
72
73
    /**
74
     * api access via Restful Server module
75
     *
76
     * @var bool
77
     */
78
    private static $api_access = true;
0 ignored issues
show
Unused Code introduced by
The property $api_access 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...
79
80
    /**
81
     * search fields for Model Admin
82
     *
83
     * @var array
84
     */
85
    private static $searchable_fields = array(
86
        'Title',
87
        'Address',
88
        'City',
89
        'State',
90
        'PostalCode',
91
        'Country',
92
        'Website',
93
        'Phone',
94
        'Email',
95
        'Category.ID',
96
        'Featured',
97
    );
98
99
    /**
100
     * columns for grid field
101
     *
102
     * @var array
103
     */
104
    private static $summary_fields = array(
105
        'Title',
106
        'Address',
107
        'City',
108
        'State',
109
        'PostalCode',
110
        'Country',
111
        'Category.Name',
112
        'Featured.NiceAsBoolean',
113
        'Coords',
114
    );
115
116
    /**
117
     * Coords status for $summary_fields
118
     *
119
     * @return string
120
     */
121
    public function getCoords()
122
    {
123
        return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false';
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->Lat of type null|mixed to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $this->Lng of type null|mixed to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
Bug Best Practice introduced by
The property Lng does not exist on Dynamic\Locator\Location. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property Lat does not exist on Dynamic\Locator\Location. Since you implemented __get, consider adding a @property annotation.
Loading history...
124
    }
125
126
    /**
127
     * custom labels for fields
128
     *
129
     * @param bool $includerelations
130
     *
131
     * @return array|string
132
     */
133
    public function fieldLabels($includerelations = true)
134
    {
135
        $labels = parent::fieldLabels($includerelations);
136
        $labels['Title'] = 'Name';
137
        $labels['PostalCode'] = 'Postal Code';
138
        $labels['Category.Name'] = 'Category';
139
        $labels['Category.ID'] = 'Category';
140
        $labels['Featured.NiceAsBoolean'] = 'Featured';
141
142
        return $labels;
143
    }
144
145
    /**
146
     * @return FieldList
147
     */
148
    public function getCMSFields()
149
    {
150
        // so it can easily be extended - concept taken from the blog module
151
        $this->beforeUpdateCMSFields(function ($fields) {
152
            $fields->removeByName(array(
153
                'Import_ID',
154
            ));
155
156
            $fields->dataFieldByName('Website')
157
                   ->setAttribute('placeholder', 'http://');
158
159
            $fields->replaceField('Email', EmailField::create('Email'));
0 ignored issues
show
Bug introduced by
'Email' 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

159
            $fields->replaceField('Email', EmailField::create(/** @scrutinizer ignore-type */ 'Email'));
Loading history...
160
161
            $fields->replaceField(
162
                'CategoryID',
163
                DropdownField::create('CategoryID', 'Category', LocationCategory::get()->map())->setEmptyString('')
0 ignored issues
show
Bug introduced by
Dynamic\Locator\LocationCategory::get()->map() of type SilverStripe\ORM\Map 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

163
                DropdownField::create('CategoryID', 'Category', /** @scrutinizer ignore-type */ LocationCategory::get()->map())->setEmptyString('')
Loading history...
164
            );
165
166
            $featured = $fields->dataFieldByName('Featured')
167
                               ->setDescription('Location will display near the top of the results list');
168
            $fields->insertAfter(
169
                $featured,
170
                'CategoryID'
171
            );
172
        });
173
174
        return parent::getCMSFields();
175
    }
176
177
    /**
178
     * @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...
179
     * @param array $context
180
     *
181
     * @return bool
182
     */
183
    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

183
    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...
184
    {
185
        return true;
186
    }
187
188
    /**
189
     * @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...
190
     * @param array $context
191
     *
192
     * @return bool|int
193
     */
194
    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

194
    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...
195
    {
196
        return Permission::check('Location_EDIT', 'any', $member);
197
    }
198
199
    /**
200
     * @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...
201
     * @param array $context
202
     *
203
     * @return bool|int
204
     */
205
    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

205
    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...
206
    {
207
        return Permission::check('Location_DELETE', 'any', $member);
208
    }
209
210
    /**
211
     * @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...
212
     * @param array $context
213
     *
214
     * @return bool|int
215
     */
216
    public function canCreate($member = null, $context = [])
217
    {
218
        return Permission::check('Location_CREATE', 'any', $member);
219
    }
220
221
    /**
222
     * @return array
223
     */
224
    public function providePermissions()
225
    {
226
        return array(
227
            'Location_EDIT' => 'Edit a Location',
228
            'Location_DELETE' => 'Delete a Location',
229
            'Location_CREATE' => 'Create a Location',
230
        );
231
    }
232
}
233