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