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
|
|
|
$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) { |
|
|
|
|
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 |
|
|
|
|
227
|
|
|
* @param array $context |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
|
|
public function canView($member = null, $context = []) |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
return true; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param null $member |
|
|
|
|
237
|
|
|
* @param array $context |
238
|
|
|
* @return bool|int |
239
|
|
|
*/ |
240
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
241
|
|
|
{ |
242
|
|
|
return Permission::check('Location_EDIT', 'any', $member); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param null $member |
|
|
|
|
247
|
|
|
* @param array $context |
248
|
|
|
* @return bool|int |
249
|
|
|
*/ |
250
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
return Permission::check('Location_DELETE', 'any', $member); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param null $member |
|
|
|
|
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
|
|
|
|