1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\ORM\ManyManyList; |
7
|
|
|
use SilverStripe\Security\PermissionProvider; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\EmailField; |
10
|
|
|
use SilverStripe\Forms\DropdownField; |
11
|
|
|
use SilverStripe\Security\Permission; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Location |
15
|
|
|
* |
16
|
|
|
* @property string $Title |
17
|
|
|
* @property bool $Featured |
18
|
|
|
* @property string $Website |
19
|
|
|
* @property string $Phone |
20
|
|
|
* @property string $Email |
21
|
|
|
* @property string $EmailAddress |
22
|
|
|
* @property string $Fax |
23
|
|
|
* @property int $Import_ID |
24
|
|
|
* |
25
|
|
|
* @method ManyManyList Categories |
26
|
|
|
*/ |
27
|
|
|
class Location extends DataObject implements PermissionProvider |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private static $singular_name = 'Location'; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private static $plural_name = 'Locations'; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private static $versioned_gridfield_extensions = true; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private static $db = array( |
|
|
|
|
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
|
|
|
); |
56
|
|
|
|
57
|
|
|
private static $many_many = [ |
|
|
|
|
58
|
|
|
'Categories' => LocationCategory::class, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private static $table_name = 'Location'; |
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private static $casting = array( |
|
|
|
|
70
|
|
|
'distance' => 'Decimal(9,3)', |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
private static $default_sort = 'Title'; |
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* api access via Restful Server module |
80
|
|
|
* |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
private static $api_access = true; |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* search fields for Model Admin |
87
|
|
|
* |
88
|
|
|
* @var array |
89
|
|
|
*/ |
90
|
|
|
private static $searchable_fields = array( |
|
|
|
|
91
|
|
|
'Title', |
92
|
|
|
'Address', |
93
|
|
|
'City', |
94
|
|
|
'State', |
95
|
|
|
'PostalCode', |
96
|
|
|
'Country', |
97
|
|
|
'Website', |
98
|
|
|
'Phone', |
99
|
|
|
'Email', |
100
|
|
|
'Featured', |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* columns for grid field |
105
|
|
|
* |
106
|
|
|
* @var array |
107
|
|
|
*/ |
108
|
|
|
private static $summary_fields = array( |
|
|
|
|
109
|
|
|
'Title', |
110
|
|
|
'Address', |
111
|
|
|
'City', |
112
|
|
|
'State', |
113
|
|
|
'PostalCode', |
114
|
|
|
'Country', |
115
|
|
|
'Featured.NiceAsBoolean', |
116
|
|
|
'Coords', |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Coords status for $summary_fields |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getCoords() |
125
|
|
|
{ |
126
|
|
|
return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false'; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* custom labels for fields |
131
|
|
|
* |
132
|
|
|
* @param bool $includerelations |
133
|
|
|
* @return array|string |
134
|
|
|
*/ |
135
|
|
|
public function fieldLabels($includerelations = true) |
136
|
|
|
{ |
137
|
|
|
$labels = parent::fieldLabels($includerelations); |
138
|
|
|
$labels['Title'] = 'Name'; |
139
|
|
|
$labels['PostalCode'] = 'Postal Code'; |
140
|
|
|
$labels['Category.Name'] = 'Category'; |
141
|
|
|
$labels['Category.ID'] = 'Category'; |
142
|
|
|
$labels['Featured.NiceAsBoolean'] = 'Featured'; |
143
|
|
|
return $labels; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return FieldList |
148
|
|
|
*/ |
149
|
|
|
public function getCMSFields() |
150
|
|
|
{ |
151
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
152
|
|
|
$fields->removeByName(array( |
153
|
|
|
'Import_ID', |
154
|
|
|
'LinkTracking', |
155
|
|
|
'FileTracking', |
156
|
|
|
)); |
157
|
|
|
|
158
|
|
|
$fields->dataFieldByName('Website') |
159
|
|
|
->setAttribute('placeholder', 'http://'); |
160
|
|
|
|
161
|
|
|
$fields->replaceField('Email', EmailField::create('Email')); |
162
|
|
|
|
163
|
|
|
$featured = $fields->dataFieldByName('Featured') |
164
|
|
|
->setDescription('Location will display near the top of the results list'); |
165
|
|
|
$fields->insertAfter( |
166
|
|
|
$featured, |
167
|
|
|
'CategoryID' |
168
|
|
|
); |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
$fields = parent::getCMSFields(); |
172
|
|
|
|
173
|
|
|
// allow to be extended via DataExtension |
174
|
|
|
$this->extend('updateLocationFields', $fields); |
175
|
|
|
|
176
|
|
|
return $fields; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param null $member |
|
|
|
|
181
|
|
|
* @param array $context |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
public function canView($member = null, $context = []) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param null $member |
|
|
|
|
191
|
|
|
* @param array $context |
192
|
|
|
* @return bool|int |
193
|
|
|
*/ |
194
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
return Permission::check('Location_EDIT', 'any', $member); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param null $member |
|
|
|
|
201
|
|
|
* @param array $context |
202
|
|
|
* @return bool|int |
203
|
|
|
*/ |
204
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
return Permission::check('Location_DELETE', 'any', $member); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param null $member |
|
|
|
|
211
|
|
|
* @param array $context |
212
|
|
|
* @return bool|int |
213
|
|
|
*/ |
214
|
|
|
public function canCreate($member = null, $context = []) |
215
|
|
|
{ |
216
|
|
|
return Permission::check('Location_CREATE', 'any', $member); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
public function providePermissions() |
223
|
|
|
{ |
224
|
|
|
return array( |
225
|
|
|
'Location_EDIT' => 'Edit a Location', |
226
|
|
|
'Location_DELETE' => 'Delete a Location', |
227
|
|
|
'Location_CREATE' => 'Create a Location', |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|