1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SRF\Filtered\View; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Parsers\GeoCoordinateParser; |
6
|
|
|
use Exception; |
7
|
|
|
use Message; |
8
|
|
|
use SMWPropertyValue; |
9
|
|
|
use SRF\Filtered\ResultItem; |
10
|
|
|
|
11
|
|
|
class MapView extends View { |
12
|
|
|
|
13
|
|
|
private static $viewParams = null; |
14
|
|
|
|
15
|
|
|
private $mapProvider = null; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param null $mapProvider |
20
|
2 |
|
*/ |
21
|
2 |
|
public function setMapProvider( $mapProvider ) { |
22
|
2 |
|
$this->mapProvider = $mapProvider; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return null |
27
|
2 |
|
*/ |
28
|
2 |
|
public function getMapProvider() { |
|
|
|
|
29
|
2 |
|
if ( $this->mapProvider === null ) { |
30
|
|
|
$this->setMapProvider( isset( $GLOBALS[ 'srfgMapProvider' ] ) ? $GLOBALS[ 'srfgMapProvider' ] : '' ); |
31
|
|
|
} |
32
|
2 |
|
|
33
|
|
|
return $this->mapProvider; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ResultItem $row |
38
|
|
|
* @return array|null |
39
|
1 |
|
*/ |
40
|
|
|
public function getJsDataForRow( ResultItem $row ) { |
41
|
1 |
|
|
42
|
|
|
$markerPositionPropertyName = str_replace( ' ', '_', $this->getActualParameters()[ 'map view marker position property' ] ); |
43
|
1 |
|
|
44
|
|
|
foreach ( $row->getValue() as $field ) { |
45
|
1 |
|
|
46
|
1 |
|
$printRequest = $field->getPrintRequest(); |
47
|
|
|
$field->reset(); |
48
|
1 |
|
|
49
|
1 |
|
$value = $field->getNextDataItem(); |
50
|
1 |
|
if ( $printRequest->getData() instanceof SMWPropertyValue && |
51
|
1 |
|
$printRequest->getData()->getInceptiveProperty()->getKey() === $markerPositionPropertyName && |
52
|
|
|
( $value instanceof \SMWDIGeoCoord || $value instanceof \SMWDIBlob ) |
53
|
|
|
) { |
54
|
|
|
$values = []; // contains plain text |
55
|
|
|
|
56
|
|
|
if ( $value instanceof \SMWDIGeoCoord ) { |
57
|
|
|
|
58
|
|
|
while ( $value instanceof \SMWDIGeoCoord ) { |
59
|
|
|
$values[] = [ 'lat' => $value->getLatitude(), 'lng' => $value->getLongitude() ]; |
60
|
|
|
$value = $field->getNextDataItem(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} elseif ( class_exists( 'DataValues\Geo\Parsers\GeoCoordinateParser' ) ) { |
64
|
|
|
|
65
|
|
|
$coordParser = new GeoCoordinateParser(); |
66
|
|
|
while ( $value instanceof \SMWDataItem ) { |
67
|
|
|
try { |
68
|
|
|
$latlng = $coordParser->parse( $value->getSerialization() ); |
69
|
|
|
$values[] = [ 'lat' => $latlng->getLatitude(), 'lng' => $latlng->getLongitude() ]; |
70
|
|
|
$value = $field->getNextDataItem(); |
71
|
|
|
} catch ( Exception $exception ) { |
72
|
|
|
$this->getQueryPrinter()->addError( "Error on '$value': " . $exception->getMessage() ); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} else { |
77
|
1 |
|
$this->getQueryPrinter()->addError( Message::newFromKey( 'srf-filtered-map-geocoordinateparser-missing-error' )->inContentLanguage()->text() ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return [ 'positions' => $values, ]; |
81
|
1 |
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
1 |
|
* Returns an array of config data for this view to be stored in the JS |
89
|
1 |
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getJsConfig() { |
92
|
1 |
|
$config = parent::getJsConfig(); |
93
|
|
|
|
94
|
|
|
$jsConfigKeys = [ |
95
|
|
|
'height', |
96
|
|
|
'zoom', |
97
|
|
|
'minZoom', |
98
|
|
|
'maxZoom', |
99
|
|
|
'marker cluster', |
100
|
|
|
'marker cluster max zoom', |
101
|
|
|
'maxClusterRadius', |
102
|
1 |
|
'zoomToBoundsOnClick', |
103
|
1 |
|
]; |
104
|
|
|
|
105
|
|
|
foreach ( $jsConfigKeys as $key ) { |
106
|
1 |
|
$this->addToConfig( $config, $key ); |
107
|
|
|
} |
108
|
1 |
|
|
109
|
|
|
$config[ 'map provider' ] = $this->getMapProvider(); |
|
|
|
|
110
|
|
|
|
111
|
|
|
return $config; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* A function to describe the allowed parameters of a query for this view. |
116
|
2 |
|
* |
117
|
|
|
* @return array of Parameter |
118
|
2 |
|
*/ |
119
|
|
|
public static function getParameters() { |
120
|
1 |
|
|
121
|
|
|
if ( self::$viewParams === null ) { |
122
|
1 |
|
|
123
|
|
|
$params = parent::getParameters(); |
124
|
|
|
|
125
|
|
|
$params[ 'marker position property' ] = [ |
126
|
|
|
// 'type' => 'string', |
127
|
|
|
'name' => 'map view marker position property', |
128
|
|
|
'message' => 'srf-paramdesc-filtered-map-position', |
129
|
|
|
'default' => '', |
130
|
1 |
|
// 'islist' => false, |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$params[ 'height' ] = [ |
134
|
|
|
'type' => 'dimension', |
135
|
|
|
'name' => 'map view height', |
136
|
|
|
'message' => 'srf-paramdesc-filtered-map-height', |
137
|
|
|
'default' => 'auto', |
138
|
1 |
|
// 'islist' => false, |
139
|
|
|
]; |
140
|
|
|
|
141
|
|
|
$params[ 'zoom' ] = [ |
142
|
|
|
'type' => 'integer', |
143
|
|
|
'name' => 'map view zoom', |
144
|
|
|
'message' => 'srf-paramdesc-filtered-map-zoom', |
145
|
|
|
'default' => '', |
146
|
1 |
|
// 'islist' => false, |
147
|
|
|
]; |
148
|
|
|
|
149
|
|
|
$params[ 'minZoom' ] = [ |
150
|
|
|
'type' => 'integer', |
151
|
|
|
'name' => 'map view min zoom', |
152
|
|
|
'message' => 'srf-paramdesc-filtered-map-min-zoom', |
153
|
|
|
'default' => '', |
154
|
1 |
|
// 'islist' => false, |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
$params[ 'maxZoom' ] = [ |
158
|
|
|
'type' => 'integer', |
159
|
|
|
'name' => 'map view max zoom', |
160
|
|
|
'message' => 'srf-paramdesc-filtered-map-max-zoom', |
161
|
|
|
'default' => '', |
162
|
|
|
// 'islist' => false, |
163
|
1 |
|
]; |
164
|
|
|
|
165
|
|
|
//markercluster |
166
|
|
|
$params[ 'marker cluster' ] = [ |
167
|
|
|
'type' => 'boolean', |
168
|
|
|
'name' => 'map view marker cluster', |
169
|
|
|
'message' => 'srf-paramdesc-filtered-map-marker-cluster', |
170
|
|
|
'default' => true, |
171
|
1 |
|
// 'islist' => false, |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
$params[ 'marker cluster max zoom' ] = [ |
175
|
|
|
'type' => 'integer', |
176
|
|
|
'name' => 'map view marker cluster max zoom', |
177
|
|
|
'message' => 'srf-paramdesc-filtered-map-marker-cluster-max-zoom', |
178
|
|
|
'default' => '', |
179
|
|
|
// 'islist' => false, |
180
|
1 |
|
]; |
181
|
|
|
|
182
|
|
|
//clustermaxradius - maxClusterRadius: The maximum radius that a cluster will cover from the central marker (in pixels). Default 80. |
183
|
|
|
$params[ 'maxClusterRadius' ] = [ |
184
|
|
|
'type' => 'integer', |
185
|
|
|
'name' => 'map view marker cluster radius', |
186
|
|
|
'message' => 'srf-paramdesc-filtered-map-marker-cluster-max-radius', |
187
|
|
|
'default' => '', |
188
|
|
|
// 'islist' => false, |
189
|
1 |
|
]; |
190
|
|
|
|
191
|
|
|
//clusterzoomonclick - zoomToBoundsOnClick: When you click a cluster we zoom to its bounds. |
192
|
|
|
$params[ 'zoomToBoundsOnClick' ] = [ |
193
|
|
|
'type' => 'boolean', |
194
|
|
|
'name' => 'map view marker cluster zoom on click', |
195
|
|
|
'message' => 'srf-paramdesc-filtered-map-marker-cluster-zoom-on-click', |
196
|
|
|
'default' => true, |
197
|
1 |
|
// 'islist' => false, |
198
|
|
|
]; |
199
|
|
|
|
200
|
2 |
|
self::$viewParams = $params; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return self::$viewParams; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns the name of the resource module to load. |
208
|
1 |
|
* |
209
|
1 |
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function getResourceModules() { |
212
|
|
|
return 'ext.srf.filtered.map-view'; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
1 |
|
* @param array $config |
217
|
|
|
* @param string $key |
218
|
1 |
|
*/ |
219
|
|
|
private function addToConfig( &$config, $key ) { |
220
|
1 |
|
|
221
|
|
|
$paramDefinition = self::getParameters()[ $key ]; |
222
|
1 |
|
|
223
|
|
|
$param = $this->getActualParameters()[ $paramDefinition[ 'name' ] ]; |
224
|
|
|
|
225
|
|
|
if ( $param !== $paramDefinition[ 'default' ] ) { |
226
|
1 |
|
$config[ $key ] = $param; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
2 |
|
/** |
232
|
2 |
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function getInitError() { |
235
|
|
|
return $this->getMapProvider() === ''? 'srf-filtered-map-provider-missing-error' : null; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: