|
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
|
1 |
|
public function onBeforeInit() |
|
21
|
|
|
{ |
|
22
|
|
|
// stops script from loading |
|
23
|
1 |
|
Requirements::block('jquery-locator'); |
|
24
|
|
|
|
|
25
|
|
|
// require i18n translation stuff |
|
26
|
1 |
|
Requirements::javascript('silverstripe/admin: client/dist/js/i18n.js'); |
|
27
|
1 |
|
Requirements::add_i18n_javascript('dynamic/silverstripe-locator-react: client/lang'); |
|
28
|
|
|
|
|
29
|
|
|
// because we need another library when using autocomplete |
|
30
|
1 |
|
if ($this->owner->Autocomplete) { |
|
31
|
|
|
// google maps api key |
|
32
|
1 |
|
$key = Config::inst()->get(GoogleGeocoder::class, 'geocoder_api_key'); |
|
33
|
1 |
|
Requirements::block("https://maps.google.com/maps/api/js?key={$key}"); |
|
34
|
1 |
|
Requirements::javascript("https://maps.google.com/maps/api/js?key={$key}&libraries=places"); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
$this->owner->customScript(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Generates the custom script for settings |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function customScript() |
|
44
|
|
|
{ |
|
45
|
2 |
|
$radii = $this->owner->getShowRadius() ? $this->owner->getRadii() : []; |
|
46
|
2 |
|
$radiiString = json_encode($radii); |
|
47
|
|
|
|
|
48
|
2 |
|
$categories = $this->owner->getCategories(); |
|
49
|
2 |
|
$categoriesString = $this->owner->categoriesString($categories); |
|
50
|
|
|
|
|
51
|
2 |
|
$unit = $this->owner->Unit ?: 'm'; |
|
52
|
2 |
|
$limit = $this->owner->getLimit(); |
|
53
|
2 |
|
$defaultLat = $this->owner->DefaultLat; |
|
54
|
2 |
|
$defaultLng = $this->owner->DefaultLng; |
|
55
|
|
|
// otherwise this is 0 or 1 |
|
56
|
2 |
|
$clusters = $this->owner->Clusters ? 'true' : 'false'; |
|
57
|
2 |
|
$autocomplete = $this->owner->Autocomplete ? 'true' : 'false'; |
|
58
|
2 |
|
$infoWindowTemplate = $this->owner->getInfoWindowTemplate(); |
|
59
|
2 |
|
$listTemplate = $this->owner->getListTemplate(); |
|
60
|
|
|
|
|
61
|
2 |
|
Requirements::customScript(" |
|
62
|
|
|
window.dynamic_locator = { |
|
63
|
2 |
|
'radii': {$radiiString}, |
|
64
|
2 |
|
'categories': {$categoriesString}, |
|
65
|
2 |
|
'unit': '{$unit}', |
|
66
|
2 |
|
'limit': {$limit}, |
|
67
|
2 |
|
'clusters': {$clusters}, |
|
68
|
2 |
|
'infoWindowTemplatePath': '{$infoWindowTemplate}', |
|
69
|
2 |
|
'listTemplatePath': '{$listTemplate}', |
|
70
|
|
|
'defaultCenter': { |
|
71
|
2 |
|
'lat': {$defaultLat}, |
|
72
|
2 |
|
'lng': {$defaultLng} |
|
73
|
|
|
}, |
|
74
|
2 |
|
'autocomplete': {$autocomplete} |
|
75
|
|
|
}; |
|
76
|
2 |
|
", 'react-locator'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $categories |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
3 |
|
public function categoriesString($categories) |
|
85
|
|
|
{ |
|
86
|
3 |
|
$string = '['; |
|
87
|
3 |
|
for ($i = 0; $i < $categories->count(); $i++) { |
|
88
|
1 |
|
$cat = $categories[$i]; |
|
89
|
1 |
|
$ID = $cat->ID; |
|
90
|
1 |
|
$Name = $cat->Name; |
|
91
|
|
|
$string .= "{ |
|
92
|
1 |
|
'ID': {$ID}, |
|
93
|
1 |
|
'Name': '{$Name}' |
|
94
|
|
|
}"; |
|
95
|
|
|
|
|
96
|
1 |
|
if ($i !== $categories->count() - 1) { |
|
97
|
1 |
|
$string .= ','; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
3 |
|
$string .= ']'; |
|
101
|
|
|
|
|
102
|
3 |
|
return $string; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|