1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator\React\Extensions; |
4
|
|
|
|
5
|
|
|
use Dynamic\SilverStripeGeocoder\GoogleGeocoder; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Core\Extension; |
8
|
|
|
use SilverStripe\View\Requirements; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class LocatorControllerExtension |
12
|
|
|
* @package Dynamic\Locator\React\Extensions |
13
|
|
|
*/ |
14
|
|
|
class LocatorControllerExtension extends Extension |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
public function onBeforeInit() |
21
|
|
|
{ |
22
|
|
|
// stops script from loading |
23
|
|
|
Requirements::block('jquery-locator'); |
24
|
|
|
|
25
|
|
|
// because we need another library when using autocomplete |
26
|
|
|
if ($this->owner->Autocomplete) { |
27
|
|
|
// google maps api key |
28
|
|
|
$key = Config::inst()->get(GoogleGeocoder::class, 'geocoder_api_key'); |
29
|
|
|
Requirements::block("https://maps.google.com/maps/api/js?key={$key}"); |
30
|
|
|
Requirements::javascript("https://maps.google.com/maps/api/js?key={$key}&libraries=places"); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->customScript(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Generates the custom script for settings |
38
|
|
|
*/ |
39
|
|
|
private function customScript() |
40
|
|
|
{ |
41
|
|
|
$radii = $this->owner->getShowRadius() ? $this->owner->getRadii() : []; |
42
|
|
|
$radiiString = json_encode($radii); |
43
|
|
|
|
44
|
|
|
$categories = $this->owner->getCategories(); |
45
|
|
|
$categoriesString = $this->owner->categoriesString($categories); |
46
|
|
|
|
47
|
|
|
$unit = $this->owner->Unit ?: 'm'; |
48
|
|
|
$limit = $this->owner->getLimit(); |
49
|
|
|
$defaultLat = $this->owner->DefaultLat; |
50
|
|
|
$defaultLng = $this->owner->DefaultLng; |
51
|
|
|
// otherwise this is 0 or 1 |
52
|
|
|
$clusters = $this->owner->Clusters ? 'true' : 'false'; |
53
|
|
|
$autocomplete = $this->owner->Autocomplete ? 'true' : 'false'; |
54
|
|
|
$infoWindowTemplate = $this->owner->getInfoWindowTemplate(); |
55
|
|
|
$listTemplate = $this->owner->getListTemplate(); |
56
|
|
|
|
57
|
|
|
Requirements::customScript(" |
58
|
|
|
window.dynamic_locator = { |
59
|
|
|
'radii': {$radiiString}, |
60
|
|
|
'categories': {$categoriesString}, |
61
|
|
|
'unit': '{$unit}', |
62
|
|
|
'limit': {$limit}, |
63
|
|
|
'clusters': {$clusters}, |
64
|
|
|
'infoWindowTemplatePath': '{$infoWindowTemplate}', |
65
|
|
|
'listTemplatePath': '{$listTemplate}', |
66
|
|
|
'defaultCenter': { |
67
|
|
|
'lat': {$defaultLat}, |
68
|
|
|
'lng': {$defaultLng} |
69
|
|
|
}, |
70
|
|
|
'autocomplete': {$autocomplete} |
71
|
|
|
}; |
72
|
|
|
", 'react-locator'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $categories |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
1 |
|
public function categoriesString($categories) |
81
|
|
|
{ |
82
|
1 |
|
$string = '['; |
83
|
1 |
|
for ($i = 0; $i < $categories->count(); $i++) { |
84
|
1 |
|
$cat = $categories[$i]; |
85
|
1 |
|
$ID = $cat->ID; |
86
|
1 |
|
$Name = $cat->Name; |
87
|
|
|
$string .= "{ |
88
|
1 |
|
'ID': {$ID}, |
89
|
1 |
|
'Name': '{$Name}' |
90
|
|
|
}"; |
91
|
|
|
|
92
|
1 |
|
if ($i !== $categories->count() - 1) { |
93
|
1 |
|
$string .= ','; |
94
|
|
|
} |
95
|
|
|
} |
96
|
1 |
|
$string .= ']'; |
97
|
|
|
|
98
|
1 |
|
return $string; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|