1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class StockistCountryPage extends StockistSearchPage |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @inherited |
18
|
|
|
*/ |
19
|
|
|
private static $icon = 'mysite/images/treeicons/StockistCountryPage'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inherited |
23
|
|
|
*/ |
24
|
|
|
private static $db = array( |
|
|
|
|
25
|
|
|
'CountryCode' => 'Varchar(3)' |
26
|
|
|
); |
27
|
|
|
/** |
28
|
|
|
* @inherited |
29
|
|
|
*/ |
30
|
|
|
private static $indexes = array( |
|
|
|
|
31
|
|
|
'CountryCode' => true |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inherited |
36
|
|
|
*/ |
37
|
|
|
private static $many_many = array( |
|
|
|
|
38
|
|
|
'AdditionalCountries' => 'EcommerceCountry' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* extended by lumberjack |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private static $extensions = array( |
|
|
|
|
46
|
|
|
'Lumberjack', |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inherited |
51
|
|
|
*/ |
52
|
|
|
//private static $indexes = array( |
|
|
|
|
53
|
|
|
// 'Country' => array ( 'type' => 'unique', 'value' => 'Country' ) |
|
|
|
|
54
|
|
|
//); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inherited |
58
|
|
|
*/ |
59
|
|
|
private static $default_child = 'StockistPage'; |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inherited |
63
|
|
|
*/ |
64
|
|
|
private static $allowed_children = array('StockistPage', 'StockistCountryPage'); |
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inherited |
68
|
|
|
*/ |
69
|
|
|
private static $can_be_root = false; |
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Standard SS variable. |
73
|
|
|
*/ |
74
|
|
|
private static $singular_name = "Stockist Country Page"; |
|
|
|
|
75
|
|
|
public function i18n_singular_name() |
76
|
|
|
{ |
77
|
|
|
return "Stockist Country Page"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Standard SS variable. |
82
|
|
|
*/ |
83
|
|
|
private static $plural_name = "Stockist Country Pages"; |
|
|
|
|
84
|
|
|
public function i18n_plural_name() |
85
|
|
|
{ |
86
|
|
|
return "Stockist Country Pages"; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inherited |
91
|
|
|
*/ |
92
|
|
|
private static $description = 'Stockist Country Page'; |
|
|
|
|
93
|
|
|
|
94
|
|
|
public function getCMSFields() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$fields = parent::getCMSFields(); |
97
|
|
|
$fields->removeByName("Map"); |
98
|
|
|
$fields->removeFieldFromTab('Root.Main', 'Content'); |
99
|
|
|
$countryArrayWithCodes = EcommerceCountry::get()->map("Code", "Name")->toArray(); |
100
|
|
|
$countryArrayWithIDs = EcommerceCountry::get()->map("ID", "Name")->toArray(); |
101
|
|
|
$title = singleton("EcommerceCountry")->i18n_singular_name(); |
102
|
|
|
$countryField = new DropdownField('CountryCode', $title, array("" => " -- please select -- ") + $countryArrayWithCodes); |
103
|
|
|
$fields->addFieldsToTab('Root.Countries', $countryField); |
104
|
|
|
$fields->addFieldsToTab('Root.Countries', new CheckboxSetField('AdditionalCountries', 'Also for ', $countryArrayWithIDs)); |
105
|
|
|
$gridField = new GridField( |
106
|
|
|
'StockistPage', |
107
|
|
|
'Stockists', |
108
|
|
|
$this->Children(), |
109
|
|
|
new GridFieldConfig_Lumberjack() |
110
|
|
|
); |
111
|
|
|
$fields->addFieldsToTab('Root.Stockists', $gridField); |
112
|
|
|
return $fields; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
public function validate() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
if ($this->CountryCode) { |
119
|
|
|
$items = StockistCountryPage::get() |
120
|
|
|
->filter(array("CountryCode" => $this->CountryCode)) |
121
|
|
|
->exclude(array("ID" => $this->ID)); |
122
|
|
|
if ($items->count()) { |
123
|
|
|
$otherCountries = implode(", ", $items->map("ID", "Title")->toArray()); |
124
|
|
|
return new ValidationResult(false, "Another country with the same country code already exists: ".$this->CountryCode." namely: ".$otherCountries.". Please change the country."); |
125
|
|
|
} |
126
|
|
|
} elseif (!StockistSearchPage::get()->byID($this->ParentID)) { |
127
|
|
|
return new ValidationResult(false, "You need to add a country to any Stockist Country Page that is not a continent! Continents are defined as pages that are children of the main stockist search page."); |
128
|
|
|
} |
129
|
|
|
return parent::validate(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function onBeforeWrite() |
133
|
|
|
{ |
134
|
|
|
parent::onBeforeWrite(); |
135
|
|
|
$this->CountryCode = strtoupper($this->CountryCode); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* returns an array of stockist types that can be shown in a map. |
140
|
|
|
* @return Array |
141
|
|
|
*/ |
142
|
|
|
public function getMapTypes() |
143
|
|
|
{ |
144
|
|
|
$allTypes = singleton("StockistPage")->dbObject('Type')->enumValues(); |
145
|
|
|
foreach ($allTypes as $key => $type) { |
146
|
|
|
if ($key == "Online Store") { |
147
|
|
|
unset($allTypes[$key]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
return $allTypes; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* |
155
|
|
|
* create as many as you like |
156
|
|
|
* @inherited |
157
|
|
|
* @return Boolean |
158
|
|
|
*/ |
159
|
|
|
public function canCreate($member = null) |
160
|
|
|
{ |
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* a list of IDs of all child StockistCountryPages (recursive) and the page itself... |
166
|
|
|
* @return Array |
167
|
|
|
*/ |
168
|
|
|
public function AllChildrenIDs() |
169
|
|
|
{ |
170
|
|
|
$array[$this->ID] = $this->ID; |
|
|
|
|
171
|
|
|
$countries = $this->ChildCountries(); |
172
|
|
|
foreach ($countries as $country) { |
173
|
|
|
$children = $country->AllChildrenIDs(); |
174
|
|
|
foreach($children as $childIDKey => $childIDValue) { |
175
|
|
|
$array[$childIDKey] = $childIDValue; |
176
|
|
|
} |
177
|
|
|
$array[$country->ID] = $country->ID; |
178
|
|
|
} |
179
|
|
|
return $array; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* returns a data list of all child (recursive) GoogleMapLocationsObjects |
185
|
|
|
* @return DataList (GoogleMapLocationsObject) |
186
|
|
|
*/ |
187
|
|
|
public function AllChildLocations() |
188
|
|
|
{ |
189
|
|
|
$ids = $this->AllChildrenIDs(); |
190
|
|
|
$stockists = StockistPage::get()->filter(array('ParentID' => $ids)); |
191
|
|
|
return GoogleMapLocationsObject::get() |
192
|
|
|
->filter(array('ParentID' => $stockists->column('ID'))); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return DataList | Null |
197
|
|
|
*/ |
198
|
|
|
public function ChildCountries() |
199
|
|
|
{ |
200
|
|
|
return StockistCountryPage::get()->filter(array("ParentID" => $this->ID))->sort("Title"); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return DataList | Null |
205
|
|
|
*/ |
206
|
|
|
public function AllPhysicalStockists() |
207
|
|
|
{ |
208
|
|
|
return StockistPage::get()->filter(array("HasPhysicalStore" => true, "ParentID" => $this->AllChildrenIDs())); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return DataList | Null |
213
|
|
|
*/ |
214
|
|
|
public function AllOnlineStockists() |
215
|
|
|
{ |
216
|
|
|
return StockistPage::get()->filter(array("HasWebStore" => 1, "ParentID" => $this->AllChildrenIDs())); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* alias for getStockistPointField |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
public function StockistPointValueList($fieldName = 'LocalityName') |
224
|
|
|
{ |
225
|
|
|
return $this->getStockistPointValueList($fieldName); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* get a list of all values for one field for all stockists in this country |
231
|
|
|
* for example, all cities for Zimbabwe... (as defined by |
232
|
|
|
* its stockist pages AND the values saved in the stockists' geo locations. |
233
|
|
|
* |
234
|
|
|
* NB values are cached |
235
|
|
|
* |
236
|
|
|
* @param string $fieldName |
|
|
|
|
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
|
public function getStockistPointValueList($fieldNames = 'LocalityName') |
240
|
|
|
{ |
241
|
|
|
if (!is_array($fieldNames)) { |
242
|
|
|
$fieldNames = array($fieldNames); |
243
|
|
|
} |
244
|
|
|
$cachekey = "getStockistPointField".'_'.$this->ID.'_'.implode('_', $fieldNames); |
245
|
|
|
$cache = SS_Cache::factory($cachekey); |
246
|
|
|
if (!($result = $cache->load($cachekey))) { |
247
|
|
|
$stockists = StockistPage::get()->filter(array("ParentID" => $this->AllChildrenIDs())); |
248
|
|
|
$array = array(); |
249
|
|
|
foreach ($stockists as $stockist) { |
250
|
|
|
$array = array_merge($array, $stockist->getPointValues($fieldNames)); |
251
|
|
|
} |
252
|
|
|
sort($array); |
253
|
|
|
$cache->save(serialize($array), $cachekey); |
254
|
|
|
return $array; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return unserialize($result); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return ArrayList |
262
|
|
|
*/ |
263
|
|
|
public function Cities() |
264
|
|
|
{ |
265
|
|
|
$headingsCreated = array(); |
266
|
|
|
$al = ArrayList::create(); |
267
|
|
|
$array = $this->getStockistPointValueList(array('AdministrativeAreaName', 'LocalityName')); |
268
|
|
|
foreach ($array as $fieldInfo) { |
269
|
|
|
$childrenArray = explode(',', $fieldInfo); |
270
|
|
|
$URLSegmentFromFieldInfo = implode(',', array_map('urlencode', $childrenArray)); |
271
|
|
|
if (count($childrenArray) > 1) { |
272
|
|
|
foreach ($childrenArray as $key => $child) { |
273
|
|
|
if ($key == 0) { |
274
|
|
|
$primaryChild = $child; |
275
|
|
|
if (! isset($headingsCreated[$primaryChild])) { |
276
|
|
|
$headingsCreated[$primaryChild] = ArrayList::create(); |
277
|
|
|
$arrayData = array( |
278
|
|
|
"ID" => preg_replace("/[^A-Za-z0-9 ]/", '-', $child), |
279
|
|
|
"Title" => $child, |
280
|
|
|
"Link" => "", |
281
|
|
|
"HasChildren" => true, |
282
|
|
|
"Children" => $headingsCreated[$primaryChild] |
283
|
|
|
); |
284
|
|
|
$al->push( |
285
|
|
|
ArrayData::create($arrayData) |
286
|
|
|
); |
287
|
|
|
} |
288
|
|
View Code Duplication |
} else { |
|
|
|
|
289
|
|
|
$arrayData = array( |
290
|
|
|
"ID" => preg_replace("/[^A-Za-z0-9 ]/", '-', $child), |
291
|
|
|
"Title" => $child, |
292
|
|
|
"Link" => $this->Link("filter/LocalityName,AdministrativeAreaName/".$URLSegmentFromFieldInfo."/"), |
293
|
|
|
"HasChildren" => false, |
294
|
|
|
"Children" => null |
295
|
|
|
); |
296
|
|
|
$headingsCreated[$primaryChild]->push($arrayData); |
|
|
|
|
297
|
|
|
} |
298
|
|
|
} |
299
|
|
View Code Duplication |
} else { |
|
|
|
|
300
|
|
|
$arrayData = array( |
301
|
|
|
"ID" => preg_replace("/[^A-Za-z0-9 ]/", '-', $fieldInfo), |
302
|
|
|
"Title" => $fieldInfo, |
303
|
|
|
"Link" => $this->Link("filter/LocalityName,AdministrativeAreaName/".$URLSegmentFromFieldInfo."/"), |
304
|
|
|
"HasChildren" => false, |
305
|
|
|
"Children" => null |
306
|
|
|
); |
307
|
|
|
$al->push( |
308
|
|
|
ArrayData::create($arrayData) |
309
|
|
|
); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
$al->sort("City"); |
313
|
|
|
return $al; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
class StockistCountryPage_Controller extends StockistSearchPage_Controller |
|
|
|
|
318
|
|
|
{ |
319
|
|
|
private static $allowed_actions = array( |
|
|
|
|
320
|
|
|
'filter' |
321
|
|
|
); |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
public function init() |
325
|
|
|
{ |
326
|
|
|
parent::init(); |
327
|
|
|
if ($this->CountryCode) { |
328
|
|
|
$this->myCurrentCountryCode = $this->CountryCode; |
329
|
|
|
} |
330
|
|
|
//Requirements::customScript("jQuery(document).ready(function(){jQuery('#MapSidebar').show();});"); |
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function index() |
334
|
|
|
{ |
335
|
|
|
$this->addMap("showchildpointsmapxml"); |
336
|
|
|
return array(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* for template |
341
|
|
|
* @return Boolean |
342
|
|
|
*/ |
343
|
|
|
public function IsSearchPage() |
344
|
|
|
{ |
345
|
|
|
return false; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function filter($request) |
349
|
|
|
{ |
350
|
|
|
$points = $this->locationsForCurrentCountry($request)->column("ID"); |
351
|
|
|
$this->Title .= ' - '. urldecode($this->request->param("OtherID")); |
352
|
|
|
$this->addMap( |
353
|
|
|
$action = "showpointbyid", |
354
|
|
|
$title = $this->Title, |
355
|
|
|
$lng = 0, |
356
|
|
|
$lat = 0, |
357
|
|
|
implode(',', $points) |
358
|
|
|
); |
359
|
|
|
|
360
|
|
|
return array(); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function locationsForCurrentCountry() |
|
|
|
|
364
|
|
|
{ |
365
|
|
|
$fields = explode(",", $this->request->param("ID")); |
366
|
|
|
$values = explode(",", $this->request->param("OtherID")); |
367
|
|
|
if (count($fields) && count($values)) { |
368
|
|
|
$whereArrayOuter = array(); |
369
|
|
|
$whereArrayOuterOuter = array(); |
370
|
|
|
|
371
|
|
|
foreach ($values as $value) { |
372
|
|
|
if ($value) { |
373
|
|
|
$whereArrayInner = array(); |
374
|
|
|
foreach ($fields as $field) { |
375
|
|
|
$whereArrayInner[] = Convert::raw2sql(trim($field))." = '".Convert::raw2sql(trim($value))."'"; |
376
|
|
|
} |
377
|
|
|
$whereArrayOuter[] = '('.implode(' OR ', $whereArrayInner).')'; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
if (count($whereArrayOuter)) { |
381
|
|
|
$whereArrayOuterOuter[] = '('.implode(' AND ', $whereArrayOuter).')'; |
382
|
|
|
} |
383
|
|
|
if (count($this->myCurrentCountryCode)) { |
384
|
|
|
$whereArrayOuterOuter[] = '("CountryNameCode" = \''.$this->myCurrentCountryCode.'\')'; |
385
|
|
|
} |
386
|
|
|
$points = GoogleMapLocationsObject::get(); |
387
|
|
|
if (count($whereArrayOuterOuter)) { |
388
|
|
|
$points = $points->where('('.implode(' ) AND (', $whereArrayOuterOuter).')'); |
389
|
|
|
} |
390
|
|
|
} else { |
391
|
|
|
$points = GoogleMapLocationsObject::get()->filter(array("CountryNameCode" => $this->myCurrentCountryCode)); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
return $points; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.