Passed
Pull Request — master (#213)
by Matthew
04:01
created

Location::getWebsite()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
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 mixed
148
     */
149
    public function getWebsite()
150
    {
151
        $website = $this->getField('Website');
152
        if ($website && strpos($website, '//') === false && strpos($website, '.') >= 0) {
153
            return '//' . $website;
154
        }
155
        return $website;
156
    }
157
158
    /**
159
     * @return bool|string
160
     */
161
    public function getCountryCode()
162
    {
163
        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...
164
            return strtoupper($this->Country);
165
        }
166
        return false;
167
    }
168
169
    /**
170
     * custom labels for fields
171
     *
172
     * @param bool $includerelations
173
     * @return array|string
174
     */
175
    public function fieldLabels($includerelations = true)
176
    {
177
        $labels = parent::fieldLabels($includerelations);
178
        $labels['Title'] = 'Name';
179
        $labels['Address2'] = 'Address 2';
180
        $labels['PostalCode'] = 'Postal Code';
181
        $labels['Categories.Name'] = 'Categories';
182
        $labels['Featured.NiceAsBoolean'] = 'Featured';
183
        return $labels;
184
    }
185
186
    /**
187
     * @return FieldList
188
     */
189
    public function getCMSFields()
190
    {
191
        $this->beforeUpdateCMSFields(function ($fields) {
192
            $fields->removeByName(array(
193
                'Import_ID',
194
                'LinkTracking',
195
                'FileTracking',
196
            ));
197
198
            $fields->dataFieldByName('Website')
199
                ->setAttribute('placeholder', 'http://');
200
201
            $fields->replaceField('Email', EmailField::create('Email'));
202
203
            $featured = $fields->dataFieldByName('Featured')
204
                ->setDescription('Location will display near the top of the results list');
205
            $fields->insertAfter(
206
                'Fax',
207
                $featured
208
            );
209
210
            if ($this->ID) {
211
                $categories = $fields->dataFieldByName('Categories');
212
                $config = $categories->getConfig();
213
                $config->removeComponentsByType([
214
                    GridFieldAddExistingAutocompleter::class
215
                ])
216
                    ->addComponents([
217
                        new GridFieldAddExistingSearchButton()
218
                    ]);
219
            }
220
        });
221
222
        $fields = parent::getCMSFields();
223
224
        // allow to be extended via DataExtension
225
        $this->extend('updateLocationFields', $fields);
226
227
        return $fields;
228
    }
229
230
    /**
231
     * @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...
232
     * @param array $context
233
     * @return bool
234
     */
235
    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

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

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

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