Passed
Pull Request — master (#223)
by Nic
03:06
created

LocationPage::canEdit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Dynamic\Locator\Page;
4
5
use Dynamic\Locator\Model\LocationCategory;
6
use SilverStripe\Forms\CheckboxField;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
9
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\ORM\ManyManyList;
12
use SilverStripe\Security\PermissionProvider;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\Forms\EmailField;
15
use SilverStripe\Security\Permission;
16
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
17
18
/**
19
 * Class Location
20
 *
21
 * @property string $Title
22
 * @property bool $Featured
23
 * @property string $Website
24
 * @property string $Phone
25
 * @property string $Email
26
 * @property string $EmailAddress
27
 * @property string $Fax
28
 * @property int $Import_ID
29
 *
30
 * @method ManyManyList Categories
31
 */
32
class LocationPage extends \Page implements PermissionProvider
33
{
34
    /**
35
     * @var string
36
     */
37
    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...
38
39
    /**
40
     * @var string
41
     */
42
    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...
43
44
    /**
45
     * @var array
46
     */
47
    private static $db = [
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
        'LegacyObjectID' => 'Int',
56
    ];
57
58
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
59
        'Categories' => LocationCategory::class,
60
    ];
61
62
    /**
63
     * @var string
64
     */
65
    private static $table_name = 'LocationPage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
66
67
    /**
68
     * @var array
69
     */
70
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
71
        'distance' => 'Decimal(9,3)',
72
    ];
73
74
    /**
75
     * @var string
76
     */
77
    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...
78
79
    /**
80
     * api access via Restful Server module
81
     *
82
     * @var bool
83
     */
84
    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...
85
86
    /**
87
     * search fields for Model Admin
88
     *
89
     * @var array
90
     */
91
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
92
        'Title',
93
        'Address',
94
        'City',
95
        'State',
96
        'PostalCode',
97
        'Country',
98
        'Website',
99
        'Phone',
100
        'Email',
101
        'Featured',
102
    ];
103
104
    /**
105
     * columns for grid field
106
     *
107
     * @var array
108
     */
109
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
110
        'Title',
111
        'Address',
112
        'Address2',
113
        'City',
114
        'State',
115
        'PostalCode',
116
        'CountryCode',
117
        'Phone' => 'Phone',
118
        'Fax' => 'Fax',
119
        'Email' => 'Email',
120
        'Website' => 'Website',
121
        'Featured',
122
        'CategoryList',
123
        'Lat',
124
        'Lng',
125
        'Import_ID',
126
    ];
127
128
    /**
129
     * Coords status for $summary_fields
130
     *
131
     * @return string
132
     */
133
    public function getCoords()
134
    {
135
        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\Page\LocationPage. Since you implemented __get, consider adding a @property annotation.
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\Page\LocationPage. 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...
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    public function getCategoryList()
142
    {
143
        if ($this->Categories()->count()) {
144
            return implode(', ', $this->Categories()->column('Name'));
145
        }
146
147
        return '';
148
    }
149
150
    /**
151
     * @return bool|string
152
     */
153
    public function getCountryCode()
154
    {
155
        if ($this->Country) {
0 ignored issues
show
Bug Best Practice introduced by
The property Country does not exist on Dynamic\Locator\Page\LocationPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
156
            return strtoupper($this->Country);
157
        }
158
159
        return false;
160
    }
161
162
    /**
163
     * custom labels for fields
164
     *
165
     * @param bool $includerelations
166
     * @return array|string
167
     */
168
    public function fieldLabels($includerelations = true)
169
    {
170
        $labels = parent::fieldLabels($includerelations);
171
172
        $labels['Title'] = 'Name';
173
        $labels['Address2'] = 'Address 2';
174
        $labels['PostalCode'] = 'Postal Code';
175
        $labels['Categories.Name'] = 'Categories';
176
        $labels['Featured.NiceAsBoolean'] = 'Featured';
177
178
        return $labels;
179
    }
180
181
    /**
182
     * @return FieldList
183
     */
184
    public function getCMSFields()
185
    {
186
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
187
            $fields->addFieldsToTab(
188
                'Root.Main',
189
                [
190
                    CheckboxField::create('Featured')
191
                        ->setTitle('Featured'),
192
                    TextField::create('Website')
193
                        ->setTitle('Website')
194
                        ->setDescription('Include the http/https (example: https://google.com)'),
195
                    TextField::create('Phone')
196
                        ->setTitle('Phone'),
197
                    EmailField::create('Email')
198
                        ->setTitle('Email'),
199
                    TextField::create('Fax')
200
                        ->setTitle('Fax'),
201
                ],
202
                'Content'
203
            );
204
205
            if ($this->exists()) {
206
                $fields->addFieldToTab(
207
                    'Root.Categories',
208
                    GridField::create(
209
                        'Categories',
210
                        'Categories',
211
                        $this->Categories(),
212
                        $catConfig = GridFieldConfig_RelationEditor::create()
213
                    )
214
                );
215
216
                $catConfig->removeComponentsByType([
217
                    GridFieldAddExistingAutocompleter::class,
218
                ])->addComponents([
219
                    new GridFieldAddExistingSearchButton()
220
                ]);
221
            }
222
        });
223
224
        return parent::getCMSFields();
225
    }
226
227
    /**
228
     * @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...
229
     * @param array $context
230
     * @return bool
231
     */
232
    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

232
    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...
233
    {
234
        return true;
235
    }
236
237
    /**
238
     * @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...
239
     * @param array $context
240
     * @return bool|int
241
     */
242
    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

242
    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...
243
    {
244
        return Permission::check('Location_EDIT', 'any', $member);
245
    }
246
247
    /**
248
     * @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...
249
     * @param array $context
250
     * @return bool|int
251
     */
252
    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

252
    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...
253
    {
254
        return Permission::check('Location_DELETE', 'any', $member);
255
    }
256
257
    /**
258
     * @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...
259
     * @param array $context
260
     * @return bool|int
261
     */
262
    public function canCreate($member = null, $context = [])
263
    {
264
        return Permission::check('Location_CREATE', 'any', $member);
265
    }
266
267
    /**
268
     * @return array
269
     */
270
    public function providePermissions()
271
    {
272
        return [
273
            'Location_EDIT' => 'Edit a Location',
274
            'Location_DELETE' => 'Delete a Location',
275
            'Location_CREATE' => 'Create a Location',
276
        ];
277
    }
278
279
    /**
280
     * @return string
281
     */
282
    public function getWebsiteURL()
283
    {
284
        $url = $this->Website;
285
286
        $this->extend('updateWebsiteURL', $url);
287
288
        return $url;
289
    }
290
}
291