Passed
Push — master ( affe64...6c9189 )
by Jason
02:51
created

Location::providePermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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