1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator; |
4
|
|
|
|
5
|
|
|
use Dynamic\SilverStripeGeocoder\GoogleGeocoder; |
6
|
|
|
use muskie9\DataToArrayList\ORM\DataToArrayListHelper; |
|
|
|
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\ORM\ArrayList; |
9
|
|
|
use SilverStripe\ORM\DataList; |
10
|
|
|
use SilverStripe\View\ArrayData; |
11
|
|
|
use SilverStripe\View\Requirements; |
12
|
|
|
use SilverStripe\Control\HTTPRequest; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class LocatorController |
16
|
|
|
*/ |
17
|
|
|
class LocatorController extends \PageController |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private static $base_filter = []; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $base_exclude = [ |
|
|
|
|
29
|
|
|
'Lat' => 0, |
30
|
|
|
'Lng' => 0, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private static $base_filter_any = []; |
|
|
|
|
37
|
|
|
|
38
|
|
|
private static $allowed_actions = array( |
39
|
|
|
'json', |
40
|
|
|
'settings' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var DataList|ArrayList |
45
|
|
|
*/ |
46
|
|
|
protected $locations; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set Requirements based on input from CMS |
50
|
|
|
*/ |
51
|
|
|
public function init() |
52
|
|
|
{ |
53
|
|
|
parent::init(); |
54
|
|
|
|
55
|
|
|
$key = Config::inst()->get(GoogleGeocoder::class, 'geocoder_api_key'); |
56
|
|
|
Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function json() |
60
|
|
|
{ |
61
|
|
|
$this->getResponse()->addHeader("Content-Type", "application/json"); |
62
|
|
|
|
63
|
|
|
$data = new ArrayData(array( |
64
|
|
|
"Locations" => $this->getLocations($this->getRequest()), |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
return $data->renderWith('Dynamic/Locator/locations'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function settings() |
71
|
|
|
{ |
72
|
|
|
$this->getResponse()->addHeader("Content-Type", "application/json"); |
73
|
|
|
|
74
|
|
|
$data = new ArrayData(array( |
75
|
|
|
// TODO |
76
|
|
|
"Radii" => $this->getRadii(), |
77
|
|
|
"Limit" => $this->getLimit(), |
78
|
|
|
"ShowRadius" => $this->getShowRadius(), |
79
|
|
|
"InfoWindowTemplate" => $this->getInfoWindowTemplate(), |
80
|
|
|
"ListTemplate" => $this->getListTemplate(), |
81
|
|
|
"Categories" => $this->getCategories(), |
82
|
|
|
"Unit" => $this->Unit, |
83
|
|
|
"Clusters" => $this->Clusters, |
84
|
|
|
)); |
85
|
|
|
|
86
|
|
|
$this->extend('updateSettings', $data); |
87
|
|
|
|
88
|
|
|
return $data->renderWith('Dynamic/Locator/settings'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param HTTPRequest|null $request |
93
|
|
|
* |
94
|
|
|
* @return ArrayList|DataList|static |
95
|
|
|
*/ |
96
|
|
|
public function getLocations(HTTPRequest $request = null) |
97
|
|
|
{ |
98
|
|
|
if ($request === null) { |
99
|
|
|
$request = $this->request; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$filter = $this->config()->get('base_filter'); |
103
|
|
|
|
104
|
|
|
if ($request->getVar('category')) { |
105
|
|
|
$filter['CategoryID'] = $request->getVar('category'); |
106
|
|
|
} else { |
107
|
|
|
if ($this->getCategories()->exists()) { |
108
|
|
|
foreach ($this->getCategories() as $category) { |
109
|
|
|
$filter['CategoryID'][] = $category->ID; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->extend('updateLocatorFilter', $filter, $request); |
115
|
|
|
|
116
|
|
|
$filterAny = $this->config()->get('base_filter_any'); |
117
|
|
|
$this->extend('updateLocatorFilterAny', $filterAny, $request); |
118
|
|
|
|
119
|
|
|
$exclude = $this->config()->get('base_exclude'); |
120
|
|
|
$this->extend('updateLocatorExclude', $exclude, $request); |
121
|
|
|
|
122
|
|
|
$locations = Locator::get_locations($filter, $filterAny, $exclude); |
123
|
|
|
|
124
|
|
|
$locations = DataToArrayListHelper::to_array_list($locations); |
125
|
|
|
|
126
|
|
|
//allow for adjusting list post possible distance calculation |
127
|
|
|
$this->extend('updateLocationList', $locations); |
128
|
|
|
if ($locations->canSortBy('distance')) { |
129
|
|
|
$locations = $locations->sort('distance'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($request->getVar('address') && $request->getVar('radius')) { |
133
|
|
|
$radius = $request->getVar('radius'); |
134
|
|
|
$locations = $locations->filterByCallback(function ($location) use (&$radius) { |
135
|
|
|
if ($radius === -1) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
return $location->Distance <= $radius; |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
//allow for returning list to be set as |
143
|
|
|
$this->extend('updateListType', $locations); |
144
|
|
|
$limit = Config::inst()->get(LocatorController::class, 'limit'); |
145
|
|
|
if ($limit > 0) { |
146
|
|
|
$locations = $locations->limit($limit); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $locations; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths