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'; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private static $plural_name = 'Locations'; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private static $db = array( |
|
|
|
|
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 = [ |
|
|
|
|
55
|
|
|
'Categories' => LocationCategory::class, |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private static $table_name = 'Location'; |
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private static $casting = array( |
|
|
|
|
67
|
|
|
'distance' => 'Decimal(9,3)', |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private static $default_sort = 'Title'; |
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* api access via Restful Server module |
77
|
|
|
* |
78
|
|
|
* @var bool |
79
|
|
|
*/ |
80
|
|
|
private static $api_access = true; |
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* search fields for Model Admin |
84
|
|
|
* |
85
|
|
|
* @var array |
86
|
|
|
*/ |
87
|
|
|
private static $searchable_fields = array( |
|
|
|
|
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( |
|
|
|
|
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'; |
|
|
|
|
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) { |
|
|
|
|
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 |
|
|
|
|
220
|
|
|
* @param array $context |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function canView($member = null, $context = []) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
return true; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param null $member |
|
|
|
|
230
|
|
|
* @param array $context |
231
|
|
|
* @return bool|int |
232
|
|
|
*/ |
233
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
return Permission::check('Location_EDIT', 'any', $member); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param null $member |
|
|
|
|
240
|
|
|
* @param array $context |
241
|
|
|
* @return bool|int |
242
|
|
|
*/ |
243
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
244
|
|
|
{ |
245
|
|
|
return Permission::check('Location_DELETE', 'any', $member); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param null $member |
|
|
|
|
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
|
|
|
|