|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Locator\React\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Extension; |
|
6
|
|
|
use SilverStripe\View\Requirements; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class LocatorControllerExtension |
|
10
|
|
|
* @package Dynamic\Locator\React\Extensions |
|
11
|
|
|
*/ |
|
12
|
|
|
class LocatorControllerExtension extends Extension |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
public function onBeforeInit() |
|
19
|
|
|
{ |
|
20
|
|
|
// stops script from loading |
|
21
|
|
|
Requirements::block('jquery-locator'); |
|
22
|
|
|
|
|
23
|
|
|
$radii = $this->owner->getShowRadius() ? $this->owner->getRadii() : []; |
|
24
|
|
|
$radiiString = json_encode($radii); |
|
25
|
|
|
|
|
26
|
|
|
$categories = $this->owner->getCategories(); |
|
27
|
|
|
$categoriesString = $this->owner->categoriesString($categories); |
|
28
|
|
|
|
|
29
|
|
|
$unit = $this->owner->Unit ?: 'm'; |
|
30
|
|
|
$limit = $this->owner->getLimit(); |
|
31
|
|
|
$defaultLat = $this->owner->DefaultLat; |
|
32
|
|
|
$defaultLng = $this->owner->DefaultLng; |
|
33
|
|
|
$clusters = $this->owner->Clusters; |
|
34
|
|
|
$infoWindowTemplate = $this->owner->getInfoWindowTemplate(); |
|
35
|
|
|
$listTemplate = $this->owner->getListTemplate(); |
|
36
|
|
|
|
|
37
|
|
|
Requirements::customScript(" |
|
38
|
|
|
window.dynamic_locator = { |
|
39
|
|
|
'radii': {$radiiString}, |
|
40
|
|
|
'categories': {$categoriesString}, |
|
41
|
|
|
'unit': '{$unit}', |
|
42
|
|
|
'limit': {$limit}, |
|
43
|
|
|
'clusters': {$clusters}, |
|
44
|
|
|
'infoWindowTemplatePath': '{$infoWindowTemplate}', |
|
45
|
|
|
'listTemplatePath': '{$listTemplate}', |
|
46
|
|
|
'defaultCenter': { |
|
47
|
|
|
'lat': {$defaultLat}, |
|
48
|
|
|
'lng': {$defaultLng} |
|
49
|
|
|
} |
|
50
|
|
|
}; |
|
51
|
|
|
", 'react-locator'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param $categories |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function categoriesString($categories) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$string = '['; |
|
62
|
1 |
|
for ($i = 0; $i < $categories->count(); $i++) { |
|
63
|
1 |
|
$cat = $categories[$i]; |
|
64
|
1 |
|
$ID = $cat->ID; |
|
65
|
1 |
|
$Name = $cat->Name; |
|
66
|
|
|
$string .= "{ |
|
67
|
1 |
|
'ID': {$ID}, |
|
68
|
1 |
|
'Name': '{$Name}' |
|
69
|
|
|
}"; |
|
70
|
|
|
|
|
71
|
1 |
|
if ($i !== $categories->count() - 1) { |
|
72
|
1 |
|
$string .= ','; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
1 |
|
$string .= ']'; |
|
76
|
1 |
|
return $string; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|