1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SRF\Filtered\View; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Parsers\GeoCoordinateParser; |
6
|
|
|
use Exception; |
7
|
|
|
use SMWPropertyValue; |
8
|
|
|
use SRF\Filtered\ResultItem; |
9
|
|
|
|
10
|
|
|
class MapView extends View { |
11
|
|
|
|
12
|
|
|
private static $viewParams = null; |
13
|
|
|
|
14
|
|
|
private $mapProvider = null; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param null $mapProvider |
19
|
|
|
*/ |
20
|
|
|
public function setMapProvider( $mapProvider ) { |
21
|
|
|
$this->mapProvider = $mapProvider; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return null |
26
|
|
|
*/ |
27
|
|
|
public function getMapProvider() { |
|
|
|
|
28
|
|
|
if ( $this->mapProvider === null ) { |
29
|
|
|
$this->setMapProvider( isset( $GLOBALS[ 'srfgMapProvider' ] ) ? $GLOBALS[ 'srfgMapProvider' ] : '' ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->mapProvider; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ResultItem $row |
37
|
|
|
* @return array|null |
38
|
|
|
*/ |
39
|
|
|
public function getJsDataForRow( ResultItem $row ) { |
40
|
|
|
|
41
|
|
|
$markerPositionPropertyName = $this->getActualParameters()[ 'map view marker position property' ]; |
42
|
|
|
|
43
|
|
|
foreach ( $row->getValue() as $field ) { |
44
|
|
|
|
45
|
|
|
$printRequest = $field->getPrintRequest(); |
46
|
|
|
$field->reset(); |
47
|
|
|
|
48
|
|
|
$value = $field->getNextDataItem(); |
49
|
|
|
if ( $printRequest->getData() instanceof SMWPropertyValue && |
50
|
|
|
$printRequest->getData()->getInceptiveProperty()->getKey() === $markerPositionPropertyName && |
51
|
|
|
( $value instanceof \SMWDIGeoCoord || $value instanceof \SMWDIBlob ) |
52
|
|
|
) { |
53
|
|
|
$values = []; // contains plain text |
54
|
|
|
|
55
|
|
|
if ( $value instanceof \SMWDIGeoCoord ) { |
56
|
|
|
|
57
|
|
|
while ( $value instanceof \SMWDIGeoCoord ) { |
58
|
|
|
$values[] = [ 'lat' => $value->getLatitude(), 'lng' => $value->getLongitude() ]; |
59
|
|
|
$value = $field->getNextDataItem(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} else { |
63
|
|
|
|
64
|
|
|
$coordParser = new GeoCoordinateParser(); |
65
|
|
|
while ( $value instanceof \SMWDataItem ) { |
66
|
|
|
try { |
67
|
|
|
$latlng = $coordParser->parse( $value->getSerialization() ); |
68
|
|
|
$values[] = [ 'lat' => $latlng->getLatitude(), 'lng' => $latlng->getLongitude() ]; |
69
|
|
|
$value = $field->getNextDataItem(); |
70
|
|
|
} catch ( Exception $exception ) { |
71
|
|
|
$this->getQueryPrinter()->addError( "Error on '$value': " . $exception->getMessage() ); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return [ 'positions' => $values, ]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns an array of config data for this view to be stored in the JS |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function getJsConfig() { |
89
|
|
|
$config = parent::getJsConfig(); |
90
|
|
|
|
91
|
|
|
foreach ( [ 'height', 'zoom', 'min zoom', 'max zoom' ] as $key ) { |
92
|
|
|
$this->addToConfig( $config, $key ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$config[ 'map provider' ] = $this->getMapProvider(); |
|
|
|
|
96
|
|
|
|
97
|
|
|
return $config; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* A function to describe the allowed parameters of a query for this view. |
102
|
|
|
* |
103
|
|
|
* @return array of Parameter |
104
|
|
|
*/ |
105
|
|
|
public static function getParameters() { |
106
|
|
|
|
107
|
|
|
if ( self::$viewParams === null ) { |
108
|
|
|
|
109
|
|
|
$params = parent::getParameters(); |
110
|
|
|
|
111
|
|
|
$params[ 'marker position property' ] = [ |
112
|
|
|
// 'type' => 'string', |
113
|
|
|
'name' => 'map view marker position property', |
114
|
|
|
'message' => 'srf-paramdesc-filtered-map-position', |
115
|
|
|
'default' => '', |
116
|
|
|
// 'islist' => false, |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
$params[ 'height' ] = [ |
120
|
|
|
'type' => 'dimension', |
121
|
|
|
'name' => 'map view height', |
122
|
|
|
'message' => 'srf-paramdesc-filtered-map-height', |
123
|
|
|
'default' => 'auto', |
124
|
|
|
// 'islist' => false, |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
$params[ 'zoom' ] = [ |
128
|
|
|
'type' => 'integer', |
129
|
|
|
'name' => 'map view zoom', |
130
|
|
|
'message' => 'srf-paramdesc-filtered-map-zoom', |
131
|
|
|
'default' => 14, |
132
|
|
|
// 'islist' => false, |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$params[ 'min zoom' ] = [ |
136
|
|
|
'type' => 'integer', |
137
|
|
|
'name' => 'map view min zoom', |
138
|
|
|
'message' => 'srf-paramdesc-filtered-map-zoom', |
139
|
|
|
'default' => -1, |
140
|
|
|
// 'islist' => false, |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
$params[ 'max zoom' ] = [ |
144
|
|
|
'type' => 'integer', |
145
|
|
|
'name' => 'map view max zoom', |
146
|
|
|
'message' => 'srf-paramdesc-filtered-map-zoom', |
147
|
|
|
'default' => -1, |
148
|
|
|
// 'islist' => false, |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
self::$viewParams = $params; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return self::$viewParams; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the name of the resource module to load. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function getResourceModules() { |
163
|
|
|
return 'ext.srf.filtered.map-view'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param array $config |
168
|
|
|
* @param string $key |
169
|
|
|
*/ |
170
|
|
|
private function addToConfig( &$config, $key ) { |
171
|
|
|
|
172
|
|
|
$paramDefinition = self::getParameters()[ $key ]; |
173
|
|
|
|
174
|
|
|
$param = $this->getActualParameters()[ $paramDefinition[ 'name' ] ]; |
175
|
|
|
|
176
|
|
|
if ( $param !== $paramDefinition[ 'default' ] ) { |
177
|
|
|
$config[ $key ] = $param; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function getInitError() { |
186
|
|
|
return $this->getMapProvider() === ''? 'srf-filtered-map-provider-missing-error' : null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
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: