1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class StockistSearchPage extends Page |
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @inherited |
8
|
|
|
*/ |
9
|
|
|
private static $icon = 'mysite/images/treeicons/StockistSearchPage'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @inherited |
13
|
|
|
*/ |
14
|
|
|
private static $db = array( |
15
|
|
|
"DefaultZoom" => "Int" |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @inherited |
20
|
|
|
*/ |
21
|
|
|
private static $default_child = 'StockistCountryPage'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inherited |
25
|
|
|
*/ |
26
|
|
|
private static $allowed_children = array('StockistCountryPage'); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inherited |
30
|
|
|
*/ |
31
|
|
|
private static $defaults = array('DefaultZoom' => 0); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inherited |
35
|
|
|
*/ |
36
|
|
|
private static $can_be_root = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inherited |
40
|
|
|
*/ |
41
|
|
|
private static $description = 'Stockist Search Page - Main page for Stockists'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Standard SS variable. |
45
|
|
|
*/ |
46
|
|
|
private static $singular_name = "Stockist Search Page"; |
47
|
|
|
public function i18n_singular_name() |
48
|
|
|
{ |
49
|
|
|
return "Stockist Search Page"; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Standard SS variable. |
54
|
|
|
*/ |
55
|
|
|
private static $plural_name = "Stockist Search Pages"; |
56
|
|
|
public function i18n_plural_name() |
57
|
|
|
{ |
58
|
|
|
return "Stockist Search Pages"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inherited |
63
|
|
|
*/ |
64
|
|
|
public function getCMSFields() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$fields = parent::getCMSFields(); |
67
|
|
|
$fields->removeByName("Map"); |
68
|
|
|
$fields->addFieldToTab( |
69
|
|
|
"Root.SearchHistory", |
70
|
|
|
new LiteralField("SearchHistoryLink", "<a href=\"".$this->Link("showsearches")."\">What did people search for?</a>") |
71
|
|
|
); |
72
|
|
|
$fields->addFieldToTab('Root.Map', $defaultZoomField = new NumericField('DefaultZoom')); |
73
|
|
|
$defaultZoomField->setRightTitle('Set between 1 and 20. One is the whole world and twenty is highest zoom level for map. Leave at zero for auto-zoom.'); |
74
|
|
|
|
75
|
|
|
$countries = StockistCountryPage::get()->filter(['ParentID' => $this->ID]); |
76
|
|
|
if($countries->count()) { |
77
|
|
|
$countrySummaryHTMLArray = []; |
78
|
|
|
foreach($countries as $country) { |
79
|
|
|
$count = StockistPage::get()->filter(['ParentID' => $country->ID, 'ShowInSearch' => 1])->count(); |
80
|
|
|
$countrySummaryHTMLArray[] = $country->Title.': '.$count; |
81
|
|
|
} |
82
|
|
|
$fields->addFieldToTab( |
83
|
|
|
'Root.Check', |
84
|
|
|
LiteralField::create( |
85
|
|
|
'StockistCount', |
86
|
|
|
' |
87
|
|
|
<h2>Count per Country</h2> |
88
|
|
|
<ul> |
89
|
|
|
<li>'.implode('</li><li>', $countrySummaryHTMLArray).' |
90
|
|
|
</ul> |
91
|
|
|
' |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $fields; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* |
101
|
|
|
* can only create one |
102
|
|
|
*/ |
103
|
|
|
public function canCreate($member = null) |
104
|
|
|
{ |
105
|
|
|
return StockistSearchPage::get() |
106
|
|
|
->filter(array("ClassName" => "StockistSearchPage")) |
107
|
|
|
->count() ? false : true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* returns a list of continents |
113
|
|
|
* @return DataList |
114
|
|
|
*/ |
115
|
|
|
public function AllContintents() |
116
|
|
|
{ |
117
|
|
|
if ($root = $this->StockistSearchPage()) { |
118
|
|
|
return StockistCountryPage::get()->filter(array("ParentID" => $root->ID))->sort(array("Title" => "ASC")); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* this is only provided on a country level |
124
|
|
|
* @return DataList | Null |
|
|
|
|
125
|
|
|
*/ |
126
|
|
|
public function AllPhysicalStockists() |
127
|
|
|
{ |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* this is only provided on a country level |
133
|
|
|
* @return DataList | Null |
|
|
|
|
134
|
|
|
*/ |
135
|
|
|
public function AllOnlineStockists() |
136
|
|
|
{ |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return Boolean |
142
|
|
|
*/ |
143
|
|
|
public function HasPhysicalStockistsANDOnlineStockists() |
144
|
|
|
{ |
145
|
|
|
$a = $this->AllPhysicalStockists(); |
146
|
|
|
$b = $this->AllOnlineStockists(); |
147
|
|
|
if ($a && $b) { |
148
|
|
|
if ($a->count() && $b->count()) { |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
class StockistSearchPage_Controller extends Page_Controller |
157
|
|
|
{ |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inherited |
161
|
|
|
*/ |
162
|
|
|
private static $allowed_actions = array( |
163
|
|
|
"showsearches" => "ADMIN", |
164
|
|
|
"updatemap" => "ADMIN", |
165
|
|
|
"showtype" => true, |
166
|
|
|
"showcountries" => true, |
167
|
|
|
"listofstockists" => true, |
168
|
|
|
"listofstockistscsv" => true, |
169
|
|
|
'addresssearch' => true, |
170
|
|
|
'addresssearchmap' => true, |
171
|
|
|
'SearchByAddressForm' => true, |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
public function init() |
175
|
|
|
{ |
176
|
|
|
parent::init(); |
177
|
|
|
$this->myCurrentCountryCode = EcommerceCountry::get_country(); |
178
|
|
|
$this->HasGeoInfo = true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function index() |
182
|
|
|
{ |
183
|
|
|
$this->addMap( |
184
|
|
|
$action = "showpointbyid", |
185
|
|
|
$title = $this->Title.' - '.$this->MyStockistCountryTitle(), |
186
|
|
|
$lng = 0, |
187
|
|
|
$lat = 0, |
188
|
|
|
implode(',', $this->locationsForCurrentCountry()->column("ID")) |
189
|
|
|
); |
190
|
|
|
return array(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function MyAddAddressFinderForm() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
return $this->AddressFinderForm(array("StockistPage")); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* returns all the locations (GoogleMapLocationsObject) relevant |
200
|
|
|
* to the current user's country for display on the map. |
201
|
|
|
* We get the country from: |
202
|
|
|
* ```php |
203
|
|
|
* $countryCode = EcommerceCountry::get_country(); |
204
|
|
|
* ``` |
205
|
|
|
* |
206
|
|
|
* @return DataList of GoogleMapLocationsObject |
207
|
|
|
*/ |
208
|
|
|
protected function locationsForCurrentCountry() |
209
|
|
|
{ |
210
|
|
|
$objects = null; |
|
|
|
|
211
|
|
|
if ($this->myCurrentCountryCode) { |
212
|
|
|
$objects = GoogleMapLocationsObject::get()->filter(array("CountryNameCode" => $this->myCurrentCountryCode)); |
213
|
|
|
if ($objects->count()) { |
214
|
|
|
return $objects; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
$stockistCountryPage = StockistCountryPage::get() |
218
|
|
|
->filter(array('CountryCode' => $this->myCurrentCountryCode)) |
219
|
|
|
->first(); |
220
|
|
|
if (! $stockistCountryPage) { |
221
|
|
|
//use the AdditionalCountries to work out other options ... |
222
|
|
|
} |
223
|
|
|
if ($stockistCountryPage) { |
224
|
|
|
$list = $stockistCountryPage->AllChildLocations(); |
225
|
|
|
if ($list->count()) { |
226
|
|
|
return $list; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
//final backup |
231
|
|
|
return GoogleMapLocationsObject::get(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function AlphaList() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
if ($objects = $this->locationsForCurrentCountry()) { |
237
|
|
|
$parents = $objects->column('ParentID'); |
238
|
|
|
if (count($parents)) { |
239
|
|
|
$pages = StockistPage::get()->filter( |
240
|
|
|
array( |
241
|
|
|
"ID" => $parents |
242
|
|
|
) |
243
|
|
|
); |
244
|
|
|
if ($pages->count() < 25) { |
245
|
|
|
return $pages; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return DataList |
253
|
|
|
*/ |
254
|
|
|
public function Countries() |
255
|
|
|
{ |
256
|
|
|
$objects = StockistCountryPage::get()->filter(array("ParentID" => $this->ID))->sort(array("Title" => "ASC")); |
257
|
|
|
if ($objects->count() == 0) { |
258
|
|
|
return StockistCountryPage::get()->filter(array("ID" => $this->ID)); |
259
|
|
|
} |
260
|
|
|
return $objects; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* for template |
265
|
|
|
* @return Boolean |
266
|
|
|
*/ |
267
|
|
|
public function IsSearchPage() |
268
|
|
|
{ |
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function IsStockistPage() |
273
|
|
|
{ |
274
|
|
|
return true; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function showsearches() |
278
|
|
|
{ |
279
|
|
|
if (Permission::check('ADMIN')) { |
280
|
|
|
$sql = " |
281
|
|
|
SELECT SearchedFor |
282
|
|
|
FROM \"GoogleMapSearchRecord\" |
283
|
|
|
ORDER BY Created DESC |
284
|
|
|
LIMIT 10000 |
285
|
|
|
"; |
286
|
|
|
$rows = DB::query($sql); |
287
|
|
|
if ($rows) { |
288
|
|
|
$this->Content .= "<h2>previous searches</h2><ul>"; |
289
|
|
|
foreach ($rows as $row) { |
290
|
|
|
$this->Content .= "<li>".$row["SearchedFor"]."</li>"; |
291
|
|
|
} |
292
|
|
|
$this->Content .= "</ul>"; |
293
|
|
|
} |
294
|
|
|
return array(); |
295
|
|
|
} |
296
|
|
|
Security::permissionFailure($this, "You need to be logged in as administrator to see this map"); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* |
301
|
|
|
* @param HTTPRequest |
302
|
|
|
*/ |
303
|
|
|
public function showtype($request) |
|
|
|
|
304
|
|
|
{ |
305
|
|
|
$type = $request->param("ID"); |
306
|
|
|
return $this->redirect($this->Link()."#".$this->Link("showtype/$type/")); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* we use this specfically so that we can change the country for |
311
|
|
|
* a page. |
312
|
|
|
* @return String |
313
|
|
|
*/ |
314
|
|
|
public function MyStockistCountryTitle() |
315
|
|
|
{ |
316
|
|
|
if ($this->myCurrentCountryCode) { |
317
|
|
|
return EcommerceCountry::find_title($this->myCurrentCountryCode); |
318
|
|
|
} else { |
319
|
|
|
return $this->MyCountryTitle(); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
#################################### |
325
|
|
|
# CSV |
326
|
|
|
#################################### |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* |
330
|
|
|
* download CSV action |
331
|
|
|
*/ |
332
|
|
|
public function listofstockistscsv() |
333
|
|
|
{ |
334
|
|
|
$html = $this->createListOfStockists(); |
335
|
|
|
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"); |
336
|
|
|
|
337
|
|
|
/*** a new dom object ***/ |
338
|
|
|
$dom = new domDocument; |
339
|
|
|
|
340
|
|
|
/*** load the html into the object ***/ |
341
|
|
|
$dom->loadHTML($html); |
342
|
|
|
|
343
|
|
|
/*** discard white space ***/ |
344
|
|
|
$dom->preserveWhiteSpace = false; |
345
|
|
|
|
346
|
|
|
/*** the table by its tag name ***/ |
347
|
|
|
$htmlTable = $dom->getElementsByTagName('table'); |
348
|
|
|
$htmlRows = $htmlTable->item(0)->getElementsByTagName('tr'); |
349
|
|
|
$rowArray = array(); |
350
|
|
|
foreach ($htmlRows as $htmlRow) { |
351
|
|
|
$headerCells = array(); |
352
|
|
|
$htmlHeaders = $htmlRow->getElementsByTagName('th'); |
353
|
|
|
foreach ($htmlHeaders as $htmlHeader) { |
354
|
|
|
$headerCells [] = $htmlHeader->nodeValue; |
355
|
|
|
} |
356
|
|
|
$htmlCells = $htmlRow->getElementsByTagName('td'); |
357
|
|
|
$rowCells = array(); |
358
|
|
|
foreach ($htmlCells as $htmlCell) { |
359
|
|
|
$rowCells [] = $htmlCell->nodeValue; |
360
|
|
|
} |
361
|
|
|
$rowArray[] = '"'.implode('", "', array_merge($headerCells, $rowCells)).'"'; |
362
|
|
|
} |
363
|
|
|
$csv = implode("\r\n", $rowArray); |
364
|
|
|
$filename_prefix = 'stockists'; |
365
|
|
|
$filename = $filename_prefix."_".date("Y-m-d_H-i", time()); |
366
|
|
|
|
367
|
|
|
//Generate the CSV file header |
368
|
|
|
header("Content-type: application/vnd.ms-excel"); |
369
|
|
|
header("Content-Encoding: UTF-8"); |
370
|
|
|
header("Content-type: text/csv; charset=UTF-8"); |
371
|
|
|
header("Content-disposition: csv" . date("Y-m-d") . ".csv"); |
372
|
|
|
header("Content-disposition: filename=".$filename.".csv"); |
373
|
|
|
echo "\xEF\xBB\xBF"; // UTF-8 BOM |
374
|
|
|
//Print the contents of out to the generated file. |
375
|
|
|
print $csv; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* |
381
|
|
|
* create HTML row for table of stockists |
382
|
|
|
*/ |
383
|
|
|
private function getChildrenAsHTMLRows($parent) |
384
|
|
|
{ |
385
|
|
|
$childGroups = DataObject::get("SiteTree", "ParentID = ".$parent->ID); |
386
|
|
|
$html = ""; |
387
|
|
|
if ($childGroups) { |
388
|
|
|
foreach ($childGroups as $childGroup) { |
389
|
|
|
$html .= $this->getChildrenAsHTMLRows($childGroup); |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
$childStockists = DataObject::get("StockistPage", "ParentID = ".$parent->ID); |
393
|
|
|
if ($childStockists) { |
394
|
|
|
foreach ($childStockists as $stockist) { |
395
|
|
|
$stockistParent = $stockist; |
396
|
|
|
$parentNames = array(); |
397
|
|
|
$stockistParent = DataObject::get_by_id("SiteTree", $stockistParent->ParentID); |
398
|
|
|
while ($stockistParent && ($stockistParent->ParentID)) { |
399
|
|
|
$parentNames[] = $stockistParent->Title; |
400
|
|
|
$stockistParent = DataObject::get_by_id("SiteTree", $stockistParent->ParentID); |
401
|
|
|
} |
402
|
|
|
$html .= "<tr>"; |
403
|
|
|
$html .= "<td>".Convert::raw2xml(implode(", ", ($parentNames)))."</td>"; |
404
|
|
|
$html .= "<td>".Convert::raw2xml($stockist->Type)."</td>"; |
405
|
|
|
$html .= "<td>".Convert::raw2xml($stockist->Title)."</td>"; |
406
|
|
|
$html .= "<td>".Convert::raw2xml($stockist->City)."</td>"; |
407
|
|
|
$html .= "<td>".Convert::raw2xml($stockist->WebAddress)."</td>"; |
408
|
|
|
$html .= "<td>".Convert::raw2xml($stockist->Email)."</td>"; |
409
|
|
|
$html .= "<td>".Convert::raw2xml($stockist->Phone)."</td>"; |
410
|
|
|
$html .= "</tr>"; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
return $html; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
|
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* |
420
|
|
|
* view list of stockists on blank screen with download link |
421
|
|
|
*/ |
422
|
|
|
public function listofstockists() |
423
|
|
|
{ |
424
|
|
|
$html = "<h4><a href=\"".$this->Link("listofstockistscsv")."\">download csv file for Excel</a></h4>"; |
425
|
|
|
$html .= $this->createListOfStockists(); |
426
|
|
|
return $html; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
private function createListOfStockists() |
431
|
|
|
{ |
432
|
|
|
$html = " |
433
|
|
|
<table border=\"1\"> |
434
|
|
|
<tr> |
435
|
|
|
<th>Region / Type</th> |
436
|
|
|
<th>Type</th> |
437
|
|
|
<th>Name</th> |
438
|
|
|
<th>City</th> |
439
|
|
|
<th>WebAddress</th> |
440
|
|
|
<th>Email</th> |
441
|
|
|
<th>Phone</th> |
442
|
|
|
</tr>"; |
443
|
|
|
$html .= $this->getChildrenAsHTMLRows($this->Parent()); |
444
|
|
|
$html .= "</table>"; |
445
|
|
|
return $html; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
|
449
|
|
|
public function updatemap() |
450
|
|
|
{ |
451
|
|
|
$pages = StockistPage::get() |
452
|
|
|
->filter(array("HasGeoInfo" => 0)); |
453
|
|
|
if ($pages && $pages->count()) { |
454
|
|
|
foreach ($pages as $page) { |
455
|
|
|
$page->write(); |
456
|
|
|
$page->publish('Stage', 'Live'); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
die("locations updated"); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.