Completed
Pull Request — master (#547)
by Stephan
45:47 queued 25:50
created

MapView::getMapProviderDark()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
namespace SRF\Filtered\View;
4
5
use DataValues\Geo\Parsers\LatLongParser;
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
	private $mapProviderDark = null;
16
17
	/**
18
	 * @param null $mapProvider
19 2
	 */
20 2
	public function setMapProvider( $mapProvider ) {
21 2
		$this->mapProvider = $mapProvider;
22
	}
23
24
	/**
25
	 * @return null
26 2
	 */
27 2
	public function getMapProvider() {
28 2
		if ( $this->mapProvider === null ) {
29
			$this->setMapProvider( isset( $GLOBALS['srfgMapProvider'] ) ? $GLOBALS['srfgMapProvider'] : '' );
30
		}
31 2
32
		return $this->mapProvider;
33
	}
34
35
	/**
36
	 * @param null $mapProviderDark
37
	 */
38
	public function setMapProviderDark( $mapProviderDark ) {
39 1
		$this->mapProviderDark = $mapProviderDark;
40
	}
41 1
42 1
	public function getMapProviderDark() {
43 1
		if ( $this->mapProviderDark === null ) {
44 1
			$this->setMapProviderDark( isset( $GLOBALS['srfgMapProviderDark'] ) ? $GLOBALS['srfgMapProviderDark'] : '' );
45
		}
46
47 1
		return $this->mapProviderDark;
48
	}
49 1
50 1
	/**
51
	 * @param ResultItem $row
52 1
	 *
53 1
	 * @return array|null
54 1
	 */
55 1
	public function getJsDataForRow( ResultItem $row ) {
56
57
		$markerPositionPropertyName = str_replace(
58
			' ',
59
			'_',
60
			$this->getActualParameters()['map view marker position property']
61
		);
62
63
		foreach ( $row->getValue() as $field ) {
64
65
			$printRequest = $field->getPrintRequest();
66
			$field->reset();
67
68
			$value = $field->getNextDataItem();
69
			if ( $printRequest->getData() instanceof SMWPropertyValue &&
0 ignored issues
show
Bug introduced by
The class SMWPropertyValue does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
70
				$printRequest->getData()->getInceptiveProperty()->getKey() === $markerPositionPropertyName &&
71
				( $value instanceof \SMWDIGeoCoord || $value instanceof \SMWDIBlob )
72
			) {
73
				$values = []; // contains plain text
74
75
				if ( $value instanceof \SMWDIGeoCoord ) {
76
77
					while ( $value instanceof \SMWDIGeoCoord ) {
78
						$values[] = [ 'lat' => $value->getLatitude(), 'lng' => $value->getLongitude() ];
79
						$value = $field->getNextDataItem();
80
					}
81
82
				} else {
83
84
					$coordParser = new LatLongParser();
85
					while ( $value instanceof \SMWDataItem ) {
86 1
						try {
87
							$latlng = $coordParser->parse( $value->getSerialization() );
88
							$values[] = [ 'lat' => $latlng->getLatitude(), 'lng' => $latlng->getLongitude() ];
89
							$value = $field->getNextDataItem();
90
						}
91
						catch ( Exception $exception ) {
92
							$this->getQueryPrinter()->addError( "Error on '$value': " . $exception->getMessage() );
93
						}
94 1
					}
95 1
96
				}
97
98 1
				return [ 'positions' => $values, ];
99
			}
100
		}
101
102
		return null;
103
	}
104
105
	/**
106
	 * Returns an array of config data for this view to be stored in the JS
107
	 *
108 1
	 * @return array
109 1
	 */
110
	public function getJsConfig() {
111
		$config = parent::getJsConfig();
112 1
113
		$jsConfigKeys = [
114 1
			'height',
115
			'zoom',
116 1
			'minZoom',
117
			'maxZoom',
118
			'marker cluster',
119
			'marker cluster max zoom',
120
			'maxClusterRadius',
121
			'zoomToBoundsOnClick',
122
		];
123
124 2
		foreach ( $jsConfigKeys as $key ) {
125
			$this->addToConfig( $config, $key );
126 2
		}
127
128 1
		$this->addMarkerIconSetupToConfig( $config );
129
130 1
		$config['map provider'] = $this->getMapProvider();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $config['map provider'] is correct as $this->getMapProvider() (which targets SRF\Filtered\View\MapView::getMapProvider()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
131
		$config['map provider dark'] = $this->getMapProviderDark();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $config['map provider dark'] is correct as $this->getMapProviderDark() (which targets SRF\Filtered\View\MapView::getMapProviderDark()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
132
133
		return $config;
134
	}
135
136
	/**
137
	 * A function to describe the allowed parameters of a query for this view.
138 1
	 *
139
	 * @return array of Parameter
140
	 */
141
	public static function getParameters() {
142
143
		if ( self::$viewParams === null ) {
144
145
			$params = parent::getParameters();
146 1
147
			$params['marker position property'] = [
148
				// 'type' => 'string',
149
				'name' => 'map view marker position property',
150
				'message' => 'srf-paramdesc-filtered-map-position',
151
				'default' => '',
152
				// 'islist' => false,
153
			];
154 1
155
			$params['marker icon property'] = [
156
				// 'type' => 'string',
157
				'name' => 'map view marker icon property',
158
				'message' => 'srf-paramdesc-filtered-map-icon',
159
				'default' => '',
160
				// 'islist' => false,
161 1
			];
162
163
			$params['marker icons'] = [
164
				// 'type' => 'string',
165
				'name' => 'map view marker icons',
166
				'message' => 'srf-paramdesc-filtered-map-icons',
167
				'default' => [],
168 1
				'islist' => true,
169
			];
170
171
			$params['height'] = [
172
				'type' => 'dimension',
173
				'name' => 'map view height',
174
				'message' => 'srf-paramdesc-filtered-map-height',
175 1
				'default' => 'auto',
176
			];
177
178
			$params['zoom'] = [
179
				'type' => 'integer',
180
				'name' => 'map view zoom',
181
				'message' => 'srf-paramdesc-filtered-map-zoom',
182
				'default' => '',
183 1
			];
184
185
			$params['minZoom'] = [
186
				'type' => 'integer',
187
				'name' => 'map view min zoom',
188
				'message' => 'srf-paramdesc-filtered-map-min-zoom',
189
				'default' => '',
190 1
			];
191
192
			$params['maxZoom'] = [
193
				'type' => 'integer',
194
				'name' => 'map view max zoom',
195
				'message' => 'srf-paramdesc-filtered-map-max-zoom',
196
				'default' => '',
197
			];
198 1
199
			//markercluster
200
			$params['marker cluster'] = [
201
				'type' => 'boolean',
202
				'name' => 'map view marker cluster',
203
				'message' => 'srf-paramdesc-filtered-map-marker-cluster',
204
				'default' => true,
205
			];
206 1
207
			$params['marker cluster max zoom'] = [
208
				'type' => 'integer',
209
				'name' => 'map view marker cluster max zoom',
210
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-max-zoom',
211
				'default' => '',
212
			];
213 1
214
			//clustermaxradius - maxClusterRadius: The maximum radius that a cluster will cover from the central marker (in pixels). Default 80.
215
			$params['maxClusterRadius'] = [
216 2
				'type' => 'integer',
217
				'name' => 'map view marker cluster radius',
218
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-max-radius',
219
				'default' => '',
220
			];
221
222
			//clusterzoomonclick - zoomToBoundsOnClick: When you click a cluster we zoom to its bounds.
223
			$params['zoomToBoundsOnClick'] = [
224 1
				'type' => 'boolean',
225 1
				'name' => 'map view marker cluster zoom on click',
226
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-zoom-on-click',
227
				'default' => true,
228
			];
229
230
			self::$viewParams = $params;
231
		}
232 1
233
		return self::$viewParams;
234 1
	}
235
236 1
	/**
237
	 * Returns the name of the resource module to load.
238 1
	 *
239
	 * @return string
240
	 */
241
	public function getResourceModules() {
242 1
		return 'ext.srf.filtered.map-view';
243
	}
244
245
	/**
246
	 * @param array $config
247 1
	 * @param string $key
248
	 */
249 1
	private function addToConfig( &$config, $key ) {
250
251 1
		$paramDefinition = self::getParameters()[$key];
252
253
		$param = $this->getActualParameters()[$paramDefinition['name']];
254
255 1
		if ( $param !== $paramDefinition['default'] ) {
256 1
			$config[$key] = $param;
257
		}
258
259
	}
260
261
	/**
262
	 * @param $config
263
	 */
264
	protected function addMarkerIconSetupToConfig( &$config ) {
265
266
		$param = $this->getActualParameters()['map view marker icon property'];
267
268
		if ( $param !== '' ) {
269
			$config['marker icon property'] = $this->getPropertyId( $param );
270
		}
271
272
		$config['marker icons'] = $this->getMarkerIcons();
273
	}
274
275
	/**
276
	 * @param $prop
277
	 *
278
	 * @return array
279
	 */
280 1
	protected function getPropertyId( $prop ) {
281
282 1
		$prop = strtr( $prop, ' ', '_' );
283
284 1
		$printrequests = $this->getQueryPrinter()->getPrintrequests();
285
		$cur = reset( $printrequests );
286 1
287
		while ( $cur !== false && ( !array_key_exists( 'property', $cur ) || $cur['property'] !== $prop ) ) {
288
			$cur = next( $printrequests );
289
		}
290
291
		return key( $printrequests );
292
	}
293
294
	/**
295
	 * @return array
296
	 */
297
	private function getMarkerIcons() {
298
299
		$ret = [];
300
301
		$actualParameters = self::getActualParameters()['map view marker icons'];
302
303
		foreach ( $actualParameters as $relation ) {
0 ignored issues
show
Bug introduced by
The expression $actualParameters of type string is not traversable.
Loading history...
304
305
			$relation = explode( '=', $relation, 2 );
306
307 1
			if ( count( $relation ) === 1 ) {
308
				$key = 'default';
309
				$icon = $relation[0];
310
			} else {
311
				$key = $relation[0];
312
				$icon = $relation[1];
313 2
			}
314 2
315
			$file = \WikiPage::factory( \Title::newFromText( $icon, NS_FILE ) )->getFile();
316
317
			if ( $file->exists() ) {
318
				$ret[$key] = $file->getUrl();
319
			} else {
320
				// TODO: $this->getQueryPrinter()->addError( NO_SUCH_FILE );
321
			}
322
		}
323
324
		return $ret;
325
	}
326
327
	/**
328
	 * @return bool
329
	 */
330
	public function getInitError() {
331
		return $this->getMapProvider() === '' ? 'srf-filtered-map-provider-missing-error' : null;
332
	}
333
334
}
335