LocationPage::getCMSFields()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 29
nc 1
nop 0
dl 0
loc 44
rs 9.456
c 1
b 0
f 0
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
        'Featured' => 'Boolean',
49
        'Website' => 'Varchar(255)',
50
        'Phone' => 'Varchar(40)',
51
        'Email' => 'Varchar(255)',
52
        'Fax' => 'Varchar(45)',
53
        'Import_ID' => 'Int',
54
        'LegacyObjectID' => '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 = 'LocationPage';
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 = [
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 int[]
75
     */
76
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
77
        'ShowInMenus' => 0,
78
    ];
79
80
    /**
81
     * @var string
82
     */
83
    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...
84
85
    /**
86
     * api access via Restful Server module
87
     *
88
     * @var bool
89
     */
90
    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...
91
92
    /**
93
     * search fields for Model Admin
94
     *
95
     * @var array
96
     */
97
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
98
        'Title',
99
        'Address',
100
        'City',
101
        'State',
102
        'PostalCode',
103
        'Country',
104
        'Website',
105
        'Phone',
106
        'Email',
107
        'Featured',
108
    ];
109
110
    /**
111
     * columns for grid field
112
     *
113
     * @var array
114
     */
115
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
116
        'Title',
117
        'Address',
118
        'Address2',
119
        'City',
120
        'State',
121
        'PostalCode',
122
        'CountryCode',
123
        'Phone' => 'Phone',
124
        'Fax' => 'Fax',
125
        'Email' => 'Email',
126
        'Website' => 'Website',
127
        'Featured',
128
        'CategoryList',
129
        'Lat',
130
        'Lng',
131
        'Import_ID',
132
    ];
133
134
    /**
135
     * @var bool
136
     */
137
    private static $show_in_sitetree = false;
0 ignored issues
show
introduced by
The private property $show_in_sitetree is not used, and could be removed.
Loading history...
138
139
    /**
140
     * @var array
141
     */
142
    private static $allowed_children = [];
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
143
144
    /**
145
     * Coords status for $summary_fields
146
     *
147
     * @return string
148
     */
149
    public function getCoords()
150
    {
151
        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 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
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...
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function getCategoryList()
158
    {
159
        if ($this->Categories()->count()) {
160
            return implode(', ', $this->Categories()->column('Name'));
161
        }
162
163
        return '';
164
    }
165
166
    /**
167
     * @return bool|string
168
     */
169
    public function getCountryCode()
170
    {
171
        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...
172
            return strtoupper($this->Country);
173
        }
174
175
        return false;
176
    }
177
178
    /**
179
     * custom labels for fields
180
     *
181
     * @param bool $includerelations
182
     * @return array|string
183
     */
184
    public function fieldLabels($includerelations = true)
185
    {
186
        $labels = parent::fieldLabels($includerelations);
187
188
        $labels['Title'] = 'Name';
189
        $labels['Address2'] = 'Address 2';
190
        $labels['PostalCode'] = 'Postal Code';
191
        $labels['Categories.Name'] = 'Categories';
192
        $labels['Featured.NiceAsBoolean'] = 'Featured';
193
194
        return $labels;
195
    }
196
197
    /**
198
     * @return FieldList
199
     */
200
    public function getCMSFields()
201
    {
202
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
203
            $fields->addFieldsToTab(
204
                'Root.Main',
205
                [
206
                    CheckboxField::create('Featured')
207
                        ->setTitle('Featured'),
208
                ],
209
                'Content'
210
            );
211
212
            if ($this->exists()) {
213
                $fields->addFieldToTab(
214
                    'Root.Categories',
215
                    GridField::create(
216
                        'Categories',
217
                        'Categories',
218
                        $this->Categories(),
219
                        $catConfig = GridFieldConfig_RelationEditor::create()
220
                    )
221
                );
222
223
                $catConfig->removeComponentsByType([
224
                    GridFieldAddExistingAutocompleter::class,
225
                ])->addComponents([
226
                    new GridFieldAddExistingSearchButton()
227
                ]);
228
            }
229
230
            $fields->addFieldsToTab('Root.Contact', [
231
                TextField::create('Website')
232
                    ->setTitle('Website')
233
                    ->setDescription('Include the http/https (example: https://google.com)'),
234
                TextField::create('Phone')
235
                    ->setTitle('Phone'),
236
                EmailField::create('Email')
237
                    ->setTitle('Email'),
238
                TextField::create('Fax')
239
                    ->setTitle('Fax'),
240
            ]);
241
        });
242
243
        return parent::getCMSFields();
244
    }
245
246
    /**
247
     * @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...
248
     * @param array $context
249
     * @return bool
250
     */
251
    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

251
    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...
252
    {
253
        return true;
254
    }
255
256
    /**
257
     * @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...
258
     * @param array $context
259
     * @return bool|int
260
     */
261
    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

261
    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...
262
    {
263
        return Permission::check('Location_EDIT', 'any', $member);
264
    }
265
266
    /**
267
     * @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...
268
     * @param array $context
269
     * @return bool|int
270
     */
271
    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

271
    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...
272
    {
273
        return Permission::check('Location_DELETE', 'any', $member);
274
    }
275
276
    /**
277
     * @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...
278
     * @param array $context
279
     * @return bool|int
280
     */
281
    public function canCreate($member = null, $context = [])
282
    {
283
        return Permission::check('Location_CREATE', 'any', $member);
284
    }
285
286
    /**
287
     * @return array
288
     */
289
    public function providePermissions()
290
    {
291
        return [
292
            'Location_EDIT' => 'Edit a Location',
293
            'Location_DELETE' => 'Delete a Location',
294
            'Location_CREATE' => 'Create a Location',
295
        ];
296
    }
297
298
    /**
299
     * @return string
300
     */
301
    public function getWebsiteURL()
302
    {
303
        $url = $this->Website;
304
305
        if ($url && !preg_match('/^(http|https):\/\//', $url)) {
306
            $url = 'http://' . $url;
307
        }
308
309
        $this->extend('updateWebsiteURL', $url);
310
311
        return $url;
312
    }
313
}
314