Passed
Pull Request — master (#210)
by Jason
02:49
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
        $list = '';
140
        $i = 1;
141
142
        foreach ($this->Categories() as $category) {
143
            $list .= $category->Name;
144
            if ($i < $this->Categories()->count()) {
145
                $list .= ', ';
146
            }
147
            $i++;
148
        }
149
150
        return $list;
151
    }
152
153
    /**
154
     * @return bool|string
155
     */
156
    public function getCountryCode()
157
    {
158
        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...
159
            return strtoupper($this->Country);
160
        }
161
        return false;
162
    }
163
164
    /**
165
     * custom labels for fields
166
     *
167
     * @param bool $includerelations
168
     * @return array|string
169
     */
170
    public function fieldLabels($includerelations = true)
171
    {
172
        $labels = parent::fieldLabels($includerelations);
173
        $labels['Title'] = 'Name';
174
        $labels['Address2'] = 'Address 2';
175
        $labels['PostalCode'] = 'Postal Code';
176
        $labels['Categories.Name'] = 'Categories';
177
        $labels['Featured.NiceAsBoolean'] = 'Featured';
178
        return $labels;
179
    }
180
181
    /**
182
     * @return FieldList
183
     */
184
    public function getCMSFields()
185
    {
186
        $this->beforeUpdateCMSFields(function ($fields) {
187
            $fields->removeByName(array(
188
                'Import_ID',
189
                'LinkTracking',
190
                'FileTracking',
191
            ));
192
193
            $fields->dataFieldByName('Website')
194
                ->setAttribute('placeholder', 'http://');
195
196
            $fields->replaceField('Email', EmailField::create('Email'));
197
198
            $featured = $fields->dataFieldByName('Featured')
199
                ->setDescription('Location will display near the top of the results list');
200
            $fields->insertAfter(
201
                'Fax',
202
                $featured
203
            );
204
205
            if ($this->ID) {
206
                $categories = $fields->dataFieldByName('Categories');
207
                $config = $categories->getConfig();
208
                $config->removeComponentsByType([
209
                    GridFieldAddExistingAutocompleter::class
210
                ])
211
                    ->addComponents([
212
                        new GridFieldAddExistingSearchButton()
213
                    ]);
214
            }
215
        });
216
217
        $fields = parent::getCMSFields();
218
219
        // allow to be extended via DataExtension
220
        $this->extend('updateLocationFields', $fields);
221
222
        return $fields;
223
    }
224
225
    /**
226
     * @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...
227
     * @param array $context
228
     * @return bool
229
     */
230
    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

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

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

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