1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator\React\Extensions; |
4
|
|
|
|
5
|
|
|
use Dynamic\SilverStripeGeocoder\GoogleGeocoder; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Core\Convert; |
9
|
|
|
use SilverStripe\Core\Extension; |
10
|
|
|
use SilverStripe\Core\Manifest\ModuleResourceLoader; |
11
|
|
|
use SilverStripe\Security\SecurityToken; |
12
|
|
|
use SilverStripe\View\Requirements; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class LocatorControllerExtension |
16
|
|
|
* @package Dynamic\Locator\React\Extensions |
17
|
|
|
*/ |
18
|
|
|
class LocatorControllerExtension extends Extension |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
1 |
|
public function onBeforeInit() |
25
|
|
|
{ |
26
|
|
|
// stops script from loading |
27
|
1 |
|
Requirements::block('jquery-locator'); |
28
|
|
|
|
29
|
|
|
// require i18n translation stuff |
30
|
1 |
|
Requirements::javascript('silverstripe/admin: client/dist/js/i18n.js'); |
31
|
1 |
|
Requirements::add_i18n_javascript('dynamic/silverstripe-locator-react: client/lang'); |
|
|
|
|
32
|
|
|
|
33
|
|
|
// because we need another library when using autocomplete |
34
|
1 |
|
if ($this->owner->Autocomplete) { |
35
|
|
|
// google maps api key |
36
|
1 |
|
$key = Config::inst()->get(GoogleGeocoder::class, 'geocoder_api_key'); |
37
|
1 |
|
Requirements::block("https://maps.google.com/maps/api/js?key={$key}"); |
38
|
1 |
|
Requirements::javascript("https://maps.google.com/maps/api/js?key={$key}&libraries=places"); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
Requirements::customScript(" |
42
|
|
|
window.ss = window.ss || {}; |
43
|
1 |
|
window.ss.config = " . $this->owner->getClientConfig() . "; |
44
|
|
|
"); |
45
|
|
|
|
46
|
1 |
|
$this->owner->customScript(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Generates the custom script for settings |
51
|
|
|
*/ |
52
|
2 |
|
public function customScript() |
53
|
|
|
{ |
54
|
2 |
|
$radii = $this->owner->getShowRadius() ? $this->owner->getRadii() : []; |
55
|
2 |
|
$radiiString = json_encode($radii); |
56
|
|
|
|
57
|
2 |
|
$categories = $this->owner->getUsedCategories(); |
58
|
2 |
|
$categoriesString = $this->owner->categoriesString($categories); |
59
|
|
|
|
60
|
2 |
|
$unit = $this->owner->Unit ? $this->owner->Unit : 'm'; |
61
|
|
|
// otherwise this is 0 or 1 |
62
|
2 |
|
$clusters = $this->owner->Clusters ? 'true' : 'false'; |
63
|
2 |
|
$autocomplete = $this->owner->Autocomplete ? 'true' : 'false'; |
64
|
|
|
|
65
|
2 |
|
$stylePath = ModuleResourceLoader::singleton()->resolveURL($this->owner->getMapStyle()); |
66
|
2 |
|
$markerIconPath = ModuleResourceLoader::singleton()->resolveURL($this->owner->getMarkerIcon()); |
67
|
|
|
|
68
|
|
|
// force to float |
69
|
2 |
|
$defaultLat = (float) $this->owner->DefaultLat; |
70
|
2 |
|
$defaultLng = (float) $this->owner->DefaultLng; |
71
|
|
|
|
72
|
2 |
|
Requirements::customScript(" |
73
|
|
|
window.dynamic_locator = { |
74
|
2 |
|
'radii': {$radiiString}, |
75
|
2 |
|
'categories': {$categoriesString}, |
76
|
2 |
|
'unit': '{$unit}', |
77
|
2 |
|
'limit': {$this->owner->getLimit()}, |
78
|
2 |
|
'clusters': {$clusters}, |
79
|
2 |
|
'mapStylePath': '{$stylePath}', |
80
|
2 |
|
'markerImagePath': '{$markerIconPath}', |
81
|
|
|
'defaultCenter': { |
82
|
2 |
|
'lat': {$defaultLat}, |
83
|
2 |
|
'lng': {$defaultLng} |
84
|
|
|
}, |
85
|
2 |
|
'autocomplete': {$autocomplete} |
86
|
|
|
}; |
87
|
2 |
|
", 'react-locator'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $categories |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
3 |
|
public function categoriesString($categories) |
96
|
|
|
{ |
97
|
3 |
|
$string = '['; |
98
|
3 |
|
for ($i = 0; $i < $categories->count(); $i++) { |
99
|
1 |
|
$cat = $categories[$i]; |
100
|
1 |
|
$ID = $cat->ID; |
101
|
1 |
|
$Name = $cat->Name; |
102
|
|
|
$string .= "{ |
103
|
1 |
|
'ID': {$ID}, |
104
|
1 |
|
'Name': '{$Name}' |
105
|
|
|
}"; |
106
|
|
|
|
107
|
1 |
|
if ($i !== $categories->count() - 1) { |
108
|
1 |
|
$string .= ','; |
109
|
|
|
} |
110
|
|
|
} |
111
|
3 |
|
$string .= ']'; |
112
|
|
|
|
113
|
3 |
|
return $string; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
1 |
|
public function getClientConfig() |
120
|
|
|
{ |
121
|
1 |
|
$token = SecurityToken::inst(); |
122
|
|
|
|
123
|
|
|
$clientConfig = [ |
124
|
1 |
|
'name' => static::class, |
125
|
1 |
|
'url' => trim($this->owner->Link(), '/'), |
126
|
1 |
|
'baseUrl' => Director::baseURL(), |
127
|
1 |
|
'absoluteBaseUrl' => Director::absoluteBaseURL(), |
128
|
1 |
|
$token->getName() => $token->getValue(), |
129
|
|
|
'sections' => [ |
130
|
|
|
[ |
131
|
|
|
'name'=> '', |
132
|
|
|
'url' => '', |
133
|
|
|
], |
134
|
|
|
], |
135
|
|
|
]; |
136
|
|
|
|
137
|
1 |
|
$this->owner->extend('updateClientConfig', $clientConfig); |
138
|
|
|
|
139
|
1 |
|
return Convert::raw2json($clientConfig); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|