summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/formats/filtered/src/View/MapView.php
blob: ceade356e719ebc01c129bb5229341c7e5a4f7fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php

namespace SRF\Filtered\View;

use DataValues\Geo\Parsers\LatLongParser;
use Exception;
use SMWPropertyValue;
use SRF\Filtered\ResultItem;

class MapView extends View {

	private static $viewParams = null;

	private $mapProvider = null;

	/**
	 * @param null $mapProvider
	 */
	public function setMapProvider( $mapProvider ) {
		$this->mapProvider = $mapProvider;
	}

	/**
	 * @return null
	 */
	public function getMapProvider() {
		if ( $this->mapProvider === null ) {
			$this->setMapProvider( isset( $GLOBALS['srfgMapProvider'] ) ? $GLOBALS['srfgMapProvider'] : '' );
		}

		return $this->mapProvider;
	}

	/**
	 * @param ResultItem $row
	 *
	 * @return array|null
	 */
	public function getJsDataForRow( ResultItem $row ) {

		$markerPositionPropertyName = str_replace(
			' ',
			'_',
			$this->getActualParameters()['map view marker position property']
		);

		foreach ( $row->getValue() as $field ) {

			$printRequest = $field->getPrintRequest();
			$field->reset();

			$value = $field->getNextDataItem();
			if ( $printRequest->getData() instanceof SMWPropertyValue &&
				$printRequest->getData()->getInceptiveProperty()->getKey() === $markerPositionPropertyName &&
				( $value instanceof \SMWDIGeoCoord || $value instanceof \SMWDIBlob )
			) {
				$values = []; // contains plain text

				if ( $value instanceof \SMWDIGeoCoord ) {

					while ( $value instanceof \SMWDIGeoCoord ) {
						$values[] = [ 'lat' => $value->getLatitude(), 'lng' => $value->getLongitude() ];
						$value = $field->getNextDataItem();
					}

				} else {

					$coordParser = new LatLongParser();
					while ( $value instanceof \SMWDataItem ) {
						try {
							$latlng = $coordParser->parse( $value->getSerialization() );
							$values[] = [ 'lat' => $latlng->getLatitude(), 'lng' => $latlng->getLongitude() ];
							$value = $field->getNextDataItem();
						}
						catch ( Exception $exception ) {
							$this->getQueryPrinter()->addError( "Error on '$value': " . $exception->getMessage() );
						}
					}

				}

				return [ 'positions' => $values, ];
			}
		}

		return null;
	}

	/**
	 * Returns an array of config data for this view to be stored in the JS
	 *
	 * @return array
	 */
	public function getJsConfig() {
		$config = parent::getJsConfig();

		$jsConfigKeys = [
			'height',
			'zoom',
			'minZoom',
			'maxZoom',
			'marker cluster',
			'marker cluster max zoom',
			'maxClusterRadius',
			'zoomToBoundsOnClick',
		];

		foreach ( $jsConfigKeys as $key ) {
			$this->addToConfig( $config, $key );
		}

		$this->addMarkerIconSetupToConfig( $config );

		$config['map provider'] = $this->getMapProvider();

		return $config;
	}

	/**
	 * A function to describe the allowed parameters of a query for this view.
	 *
	 * @return array of Parameter
	 */
	public static function getParameters() {

		if ( self::$viewParams === null ) {

			$params = parent::getParameters();

			$params['marker position property'] = [
				// 'type' => 'string',
				'name' => 'map view marker position property',
				'message' => 'srf-paramdesc-filtered-map-position',
				'default' => '',
				// 'islist' => false,
			];

			$params['marker icon property'] = [
				// 'type' => 'string',
				'name' => 'map view marker icon property',
				'message' => 'srf-paramdesc-filtered-map-icon',
				'default' => '',
				// 'islist' => false,
			];

			$params['marker icons'] = [
				// 'type' => 'string',
				'name' => 'map view marker icons',
				'message' => 'srf-paramdesc-filtered-map-icons',
				'default' => [],
				'islist' => true,
			];

			$params['height'] = [
				'type' => 'dimension',
				'name' => 'map view height',
				'message' => 'srf-paramdesc-filtered-map-height',
				'default' => 'auto',
			];

			$params['zoom'] = [
				'type' => 'integer',
				'name' => 'map view zoom',
				'message' => 'srf-paramdesc-filtered-map-zoom',
				'default' => '',
			];

			$params['minZoom'] = [
				'type' => 'integer',
				'name' => 'map view min zoom',
				'message' => 'srf-paramdesc-filtered-map-min-zoom',
				'default' => '',
			];

			$params['maxZoom'] = [
				'type' => 'integer',
				'name' => 'map view max zoom',
				'message' => 'srf-paramdesc-filtered-map-max-zoom',
				'default' => '',
			];

			//markercluster
			$params['marker cluster'] = [
				'type' => 'boolean',
				'name' => 'map view marker cluster',
				'message' => 'srf-paramdesc-filtered-map-marker-cluster',
				'default' => true,
			];

			$params['marker cluster max zoom'] = [
				'type' => 'integer',
				'name' => 'map view marker cluster max zoom',
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-max-zoom',
				'default' => '',
			];

			//clustermaxradius - maxClusterRadius: The maximum radius that a cluster will cover from the central marker (in pixels). Default 80.
			$params['maxClusterRadius'] = [
				'type' => 'integer',
				'name' => 'map view marker cluster radius',
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-max-radius',
				'default' => '',
			];

			//clusterzoomonclick - zoomToBoundsOnClick: When you click a cluster we zoom to its bounds.
			$params['zoomToBoundsOnClick'] = [
				'type' => 'boolean',
				'name' => 'map view marker cluster zoom on click',
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-zoom-on-click',
				'default' => true,
			];

			self::$viewParams = $params;
		}

		return self::$viewParams;
	}

	/**
	 * Returns the name of the resource module to load.
	 *
	 * @return string
	 */
	public function getResourceModules() {
		return 'ext.srf.filtered.map-view';
	}

	/**
	 * @param array $config
	 * @param string $key
	 */
	private function addToConfig( &$config, $key ) {

		$paramDefinition = self::getParameters()[$key];

		$param = $this->getActualParameters()[$paramDefinition['name']];

		if ( $param !== $paramDefinition['default'] ) {
			$config[$key] = $param;
		}

	}

	/**
	 * @param $config
	 */
	protected function addMarkerIconSetupToConfig( &$config ) {

		$param = $this->getActualParameters()['map view marker icon property'];

		if ( $param !== '' ) {
			$config['marker icon property'] = $this->getPropertyId( $param );
		}

		$config['marker icons'] = $this->getMarkerIcons();
	}

	/**
	 * @param $prop
	 *
	 * @return array
	 */
	protected function getPropertyId( $prop ) {

		$prop = strtr( $prop, ' ', '_' );

		$printrequests = $this->getQueryPrinter()->getPrintrequests();
		$cur = reset( $printrequests );

		while ( $cur !== false && ( !array_key_exists( 'property', $cur ) || $cur['property'] !== $prop ) ) {
			$cur = next( $printrequests );
		}

		return key( $printrequests );
	}

	/**
	 * @return array
	 */
	private function getMarkerIcons() {

		$ret = [];

		$actualParameters = self::getActualParameters()['map view marker icons'];

		foreach ( $actualParameters as $relation ) {

			$relation = explode( '=', $relation, 2 );

			if ( count( $relation ) === 1 ) {
				$key = 'default';
				$icon = $relation[0];
			} else {
				$key = $relation[0];
				$icon = $relation[1];
			}

			$file = \WikiPage::factory( \Title::newFromText( $icon, NS_FILE ) )->getFile();

			if ( $file->exists() ) {
				$ret[$key] = $file->getUrl();
			} else {
				// TODO: $this->getQueryPrinter()->addError( NO_SUCH_FILE );
			}
		}

		return $ret;
	}

	/**
	 * @return bool
	 */
	public function getInitError() {
		return $this->getMapProvider() === '' ? 'srf-filtered-map-provider-missing-error' : null;
	}

}