Completed
Push — master ( ae77a5...829315 )
by Jeroen De
06:25
created

MapView::getPropertyId()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 20
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
	 */
20 2
	public function setMapProvider( $mapProvider ) {
21 2
		$this->mapProvider = $mapProvider;
22 2
	}
23
24
	/**
25
	 * @return null
26
	 */
27 2
	public function getMapProvider() {
28 2
		if ( $this->mapProvider === null ) {
29 2
			$this->setMapProvider( isset( $GLOBALS['srfgMapProvider'] ) ? $GLOBALS['srfgMapProvider'] : '' );
30
		}
31
32 2
		return $this->mapProvider;
33
	}
34
35
	/**
36
	 * @param null $mapProviderDark
37
	 */
38 1
	public function setMapProviderDark( $mapProviderDark ) {
39 1
		$this->mapProviderDark = $mapProviderDark;
40 1
	}
41
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
50
	/**
51
	 * @param ResultItem $row
52
	 *
53
	 * @return array|null
54
	 */
55 1
	public function getJsDataForRow( ResultItem $row ) {
56
57 1
		$markerPositionPropertyName = str_replace(
58 1
			' ',
59 1
			'_',
60 1
			$this->getActualParameters()['map view marker position property']
61
		);
62
63 1
		foreach ( $row->getValue() as $field ) {
64
65 1
			$printRequest = $field->getPrintRequest();
66 1
			$field->reset();
67
68 1
			$value = $field->getNextDataItem();
69 1
			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 1
				$printRequest->getData()->getInceptiveProperty()->getKey() === $markerPositionPropertyName &&
71 1
				( $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
						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
					}
95
96
				}
97
98
				return [ 'positions' => $values, ];
99
			}
100
		}
101
102 1
		return null;
103
	}
104
105
	/**
106
	 * Returns an array of config data for this view to be stored in the JS
107
	 *
108
	 * @return array
109
	 */
110 1
	public function getJsConfig() {
111 1
		$config = parent::getJsConfig();
112
113
		$jsConfigKeys = [
114 1
			'height',
115
			'zoom',
116
			'minZoom',
117
			'maxZoom',
118
			'marker cluster',
119
			'marker cluster max zoom',
120
			'maxClusterRadius',
121
			'zoomToBoundsOnClick',
122
		];
123
124 1
		foreach ( $jsConfigKeys as $key ) {
125 1
			$this->addToConfig( $config, $key );
126
		}
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 1
		$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 1
		return $config;
134
	}
135
136
	/**
137
	 * A function to describe the allowed parameters of a query for this view.
138
	 *
139
	 * @return array of Parameter
140
	 */
141 2
	public static function getParameters() {
142
143 2
		if ( self::$viewParams === null ) {
144
145 1
			$params = parent::getParameters();
146
147 1
			$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
155 1
			$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
			];
162
163 1
			$params['marker icons'] = [
164
				// 'type' => 'string',
165
				'name' => 'map view marker icons',
166
				'message' => 'srf-paramdesc-filtered-map-icons',
167
				'default' => [],
168
				'islist' => true,
169
			];
170
171 1
			$params['height'] = [
172
				'type' => 'dimension',
173
				'name' => 'map view height',
174
				'message' => 'srf-paramdesc-filtered-map-height',
175
				'default' => 'auto',
176
			];
177
178 1
			$params['zoom'] = [
179
				'type' => 'integer',
180
				'name' => 'map view zoom',
181
				'message' => 'srf-paramdesc-filtered-map-zoom',
182
				'default' => '',
183
			];
184
185 1
			$params['minZoom'] = [
186
				'type' => 'integer',
187
				'name' => 'map view min zoom',
188
				'message' => 'srf-paramdesc-filtered-map-min-zoom',
189
				'default' => '',
190
			];
191
192 1
			$params['maxZoom'] = [
193
				'type' => 'integer',
194
				'name' => 'map view max zoom',
195
				'message' => 'srf-paramdesc-filtered-map-max-zoom',
196
				'default' => '',
197
			];
198
199
			//markercluster
200 1
			$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
207 1
			$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
214
			//clustermaxradius - maxClusterRadius: The maximum radius that a cluster will cover from the central marker (in pixels). Default 80.
215 1
			$params['maxClusterRadius'] = [
216
				'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 1
			$params['zoomToBoundsOnClick'] = [
224
				'type' => 'boolean',
225
				'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 1
			self::$viewParams = $params;
231
		}
232
233 2
		return self::$viewParams;
234
	}
235
236
	/**
237
	 * Returns the name of the resource module to load.
238
	 *
239
	 * @return string
240
	 */
241 1
	public function getResourceModules() {
242 1
		return 'ext.srf.filtered.map-view';
243
	}
244
245
	/**
246
	 * @param array $config
247
	 * @param string $key
248
	 */
249 1
	private function addToConfig( &$config, $key ) {
250
251 1
		$paramDefinition = self::getParameters()[$key];
252
253 1
		$param = $this->getActualParameters()[$paramDefinition['name']];
254
255 1
		if ( $param !== $paramDefinition['default'] ) {
256
			$config[$key] = $param;
257
		}
258
259 1
	}
260
261
	/**
262
	 * @param $config
263
	 */
264 1
	protected function addMarkerIconSetupToConfig( &$config ) {
265
266 1
		$param = $this->getActualParameters()['map view marker icon property'];
267
268 1
		if ( $param !== '' ) {
269
			$config['marker icon property'] = $this->getPropertyId( $param );
270
		}
271
272 1
		$config['marker icons'] = $this->getMarkerIcons();
273 1
	}
274
275
	/**
276
	 * @param $prop
277
	 *
278
	 * @return array
279
	 */
280
	protected function getPropertyId( $prop ) {
281
282
		$prop = strtr( $prop, ' ', '_' );
283
284
		$printrequests = $this->getQueryPrinter()->getPrintrequests();
285
		$cur = reset( $printrequests );
286
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 1
	private function getMarkerIcons() {
298
299 1
		$ret = [];
300
301 1
		$actualParameters = self::getActualParameters()['map view marker icons'];
302
303 1
		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
			if ( count( $relation ) === 1 ) {
308
				$key = 'default';
309
				$icon = $relation[0];
310
			} else {
311
				$key = $relation[0];
312
				$icon = $relation[1];
313
			}
314
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 1
		return $ret;
325
	}
326
327
	/**
328
	 * @return bool
329
	 */
330 2
	public function getInitError() {
331 2
		return $this->getMapProvider() === '' ? 'srf-filtered-map-provider-missing-error' : null;
332
	}
333
334
}
335