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'; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private static $plural_name = 'Locations'; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private static $db = [ |
|
|
|
|
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 = [ |
|
|
|
|
59
|
|
|
'Categories' => LocationCategory::class, |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private static $table_name = 'LocationPage'; |
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private static $casting = [ |
|
|
|
|
71
|
|
|
'distance' => 'Decimal(9,3)', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
private static $default_sort = 'Title'; |
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* api access via Restful Server module |
81
|
|
|
* |
82
|
|
|
* @var bool |
83
|
|
|
*/ |
84
|
|
|
private static $api_access = true; |
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* search fields for Model Admin |
88
|
|
|
* |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
private static $searchable_fields = [ |
|
|
|
|
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 = [ |
|
|
|
|
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'; |
|
|
|
|
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) { |
|
|
|
|
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 |
|
|
|
|
229
|
|
|
* @param array $context |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function canView($member = null, $context = []) |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
return true; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param null $member |
|
|
|
|
239
|
|
|
* @param array $context |
240
|
|
|
* @return bool|int |
241
|
|
|
*/ |
242
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
243
|
|
|
{ |
244
|
|
|
return Permission::check('Location_EDIT', 'any', $member); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param null $member |
|
|
|
|
249
|
|
|
* @param array $context |
250
|
|
|
* @return bool|int |
251
|
|
|
*/ |
252
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
253
|
|
|
{ |
254
|
|
|
return Permission::check('Location_DELETE', 'any', $member); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param null $member |
|
|
|
|
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
|
|
|
|