Passed
Push — master ( 4e9ab4...e35aa4 )
by Jason
03:05
created

Location::getCountryCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

223
    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...
224
    {
225
        return true;
226
    }
227
228
    /**
229
     * @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...
230
     * @param array $context
231
     * @return bool|int
232
     */
233
    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

233
    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...
234
    {
235
        return Permission::check('Location_EDIT', 'any', $member);
236
    }
237
238
    /**
239
     * @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...
240
     * @param array $context
241
     * @return bool|int
242
     */
243
    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

243
    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...
244
    {
245
        return Permission::check('Location_DELETE', 'any', $member);
246
    }
247
248
    /**
249
     * @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...
250
     * @param array $context
251
     * @return bool|int
252
     */
253
    public function canCreate($member = null, $context = [])
254
    {
255
        return Permission::check('Location_CREATE', 'any', $member);
256
    }
257
258
    /**
259
     * @return array
260
     */
261
    public function providePermissions()
262
    {
263
        return array(
264
            'Location_EDIT' => 'Edit a Location',
265
            'Location_DELETE' => 'Delete a Location',
266
            'Location_CREATE' => 'Create a Location',
267
        );
268
    }
269
}
270