Completed
Push — styling ( 65b228 )
by Jeroen De
04:06
created

GoogleMapsService::getMappedLanguageCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 4
cts 5
cp 0.8
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Maps;
4
5
use Html;
6
use ParamProcessor\ProcessedParam;
7
use ParamProcessor\ProcessingResult;
8
9
/**
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 * @author Peter Grassberger < [email protected] >
13
 */
14
class GoogleMapsService implements MappingService {
15
16
	/**
17
	 * Maps user input map types to the Google Maps names for the map types.
18
	 */
19
	private const MAP_TYPES = [
20
		'normal' => 'ROADMAP',
21
		'roadmap' => 'ROADMAP',
22
		'satellite' => 'SATELLITE',
23
		'hybrid' => 'HYBRID',
24
		'terrain' => 'TERRAIN',
25
		'physical' => 'TERRAIN',
26
		'earth' => 'earth'
27
	];
28
29
	private const TYPE_CONTROL_STYLES = [
30
		'default' => 'DEFAULT',
31
		'horizontal' => 'HORIZONTAL_BAR',
32
		'dropdown' => 'DROPDOWN_MENU'
33
	];
34
35
	private $addedDependencies = [];
36
37 23
	public function getName(): string {
38 23
		return 'googlemaps3';
39
	}
40
41 23
	public function getAliases(): array {
42 23
		return [ 'googlemaps', 'google' ];
43
	}
44
45
	public function hasAlias( string $alias ): bool {
46
		return in_array( $alias, [ 'googlemaps', 'google' ] );
47
	}
48
49 7
	public function getParameterInfo(): array {
50 7
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
51 7
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
52 7
		global $egMapsResizableByDefault;
53
54 7
		$params = MapsFunctions::getCommonParameters();
55
56 7
		$params['visitedicon'] = [
57
			'default' => '',
58
			'message' => 'maps-displaymap-par-visitedicon',
59
		];
60
61 7
		$params['wmsoverlay'] = [
62
			'type' => 'wmsoverlay',
63
			'default' => false,
64
			'delimiter' => ' ',
65
			'message' => 'maps-displaymap-par-wmsoverlay',
66
		];
67
68 7
		$params['zoom'] = [
69 7
			'type' => 'integer',
70
			'range' => [ 0, 20 ],
71 7
			'default' => $GLOBALS['egMapsGMaps3Zoom'],
72 7
			'message' => 'maps-par-zoom',
73
		];
74
75 7
		$params['type'] = [
76 7
			'default' => $egMapsGMaps3Type,
77 7
			'values' => self::getTypeNames(),
78 7
			'message' => 'maps-googlemaps3-par-type',
79
			'post-format' => function ( $value ) {
80 7
				return GoogleMapsService::MAP_TYPES[strtolower( $value )];
81 7
			},
82
		];
83
84 7
		$params['types'] = [
85 7
			'dependencies' => 'type',
86 7
			'default' => $egMapsGMaps3Types,
87 7
			'values' => self::getTypeNames(),
88 7
			'message' => 'maps-googlemaps3-par-types',
89
			'islist' => true,
90
			'post-format' => function ( array $value ) {
91 7
				foreach ( $value as &$part ) {
92 7
					$part = self::MAP_TYPES[strtolower( $part )];
93
				}
94
95 7
				return $value;
96 7
			},
97
		];
98
99 7
		$params['layers'] = [
100 7
			'default' => $egMapsGMaps3Layers,
101
			'values' => [
102
				'traffic',
103
				'bicycling',
104
				'transit'
105
			],
106 7
			'message' => 'maps-googlemaps3-par-layers',
107
			'islist' => true,
108
		];
109
110 7
		$params['controls'] = [
111 7
			'default' => $egMapsGMaps3Controls,
112
			'values' => [
113
				'pan',
114
				'zoom',
115
				'type',
116
				'scale',
117
				'streetview',
118
				'rotate'
119
			],
120 7
			'message' => 'maps-googlemaps3-par-controls',
121
			'islist' => true,
122
			'post-format' => function ( $value ) {
123 7
				return array_map( 'strtolower', $value );
124 7
			},
125
		];
126
127 7
		$params['zoomstyle'] = [
128 7
			'default' => $egMapsGMaps3DefZoomStyle,
129
			'values' => [ 'default', 'small', 'large' ],
130 7
			'message' => 'maps-googlemaps3-par-zoomstyle',
131 7
			'post-format' => 'strtoupper',
132
		];
133
134 7
		$params['typestyle'] = [
135 7
			'default' => $egMapsGMaps3DefTypeStyle,
136 7
			'values' => array_keys( self::TYPE_CONTROL_STYLES ),
137 7
			'message' => 'maps-googlemaps3-par-typestyle',
138
			'post-format' => function ( $value ) {
139 7
				return self::TYPE_CONTROL_STYLES[strtolower( $value )];
140 7
			},
141
		];
142
143 7
		$params['autoinfowindows'] = [
144 7
			'type' => 'boolean',
145 7
			'default' => $egMapsGMaps3AutoInfoWindows,
146 7
			'message' => 'maps-googlemaps3-par-autoinfowindows',
147
		];
148
149 7
		$params['resizable'] = [
150 7
			'type' => 'boolean',
151 7
			'default' => $egMapsResizableByDefault,
152 7
			'message' => 'maps-par-resizable',
153
		];
154
155 7
		$params['kmlrezoom'] = [
156 7
			'type' => 'boolean',
157 7
			'default' => $GLOBALS['egMapsRezoomForKML'],
158 7
			'message' => 'maps-googlemaps3-par-kmlrezoom',
159
		];
160
161 7
		$params['poi'] = [
162 7
			'type' => 'boolean',
163 7
			'default' => $GLOBALS['egMapsShowPOI'],
164 7
			'message' => 'maps-googlemaps3-par-poi',
165
		];
166
167 7
		$params['cluster'] = [
168
			'aliases' => [ 'markercluster' ],
169
			'type' => 'boolean',
170
			'default' => false,
171
			'message' => 'maps-par-markercluster',
172
		];
173
174 7
		$params['clustergridsize'] = [
175
			'type' => 'integer',
176
			'default' => 60,
177
			'message' => 'maps-googlemaps3-par-clustergridsize',
178
		];
179
180 7
		$params['clustermaxzoom'] = [
181
			'type' => 'integer',
182
			'default' => 20,
183
			'message' => 'maps-par-clustermaxzoom',
184
		];
185
186 7
		$params['clusterzoomonclick'] = [
187
			'type' => 'boolean',
188
			'default' => true,
189
			'message' => 'maps-par-clusterzoomonclick',
190
		];
191
192 7
		$params['clusteraveragecenter'] = [
193
			'type' => 'boolean',
194
			'default' => true,
195
			'message' => 'maps-googlemaps3-par-clusteraveragecenter',
196
		];
197
198 7
		$params['clusterminsize'] = [
199
			'type' => 'integer',
200
			'default' => 2,
201
			'message' => 'maps-googlemaps3-par-clusterminsize',
202
		];
203
204 7
		$params['imageoverlays'] = [
205
			'type' => 'mapsimageoverlay',
206
			'default' => [],
207
			'delimiter' => ';',
208
			'islist' => true,
209
			'message' => 'maps-googlemaps3-par-imageoverlays',
210
		];
211
212 7
		$params['kml'] = [
213 7
			'default' => [],
214 7
			'message' => 'maps-par-kml',
215
			'islist' => true,
216
			'post-format' => function( array $kmlFileNames ) {
217 7
				return array_map(
218
					function( string $fileName ) {
219
						return wfExpandUrl( MapsFunctions::getFileUrl( $fileName ) );
0 ignored issues
show
Deprecated Code introduced by
The method Maps\MapsFunctions::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
220 7
					},
221
					$kmlFileNames
222
				);
223 7
			}
224
		];
225
226 7
		$params['gkml'] = [
227
			'default' => [],
228
			'message' => 'maps-googlemaps3-par-gkml',
229
			'islist' => true,
230
		];
231
232 7
		$params['searchmarkers'] = [
233
			'default' => '',
234
			'message' => 'maps-par-searchmarkers',
235
			// new CriterionSearchMarkers() FIXME
236
		];
237
238 7
		$params['fullscreen'] = [
239
			'aliases' => [ 'enablefullscreen' ],
240
			'type' => 'boolean',
241
			'default' => false,
242
			'message' => 'maps-par-enable-fullscreen',
243
		];
244
245 7
		$params['scrollwheelzoom'] = [
246
			'aliases' => [ 'scrollzoom' ],
247
			'type' => 'boolean',
248
			'default' => false,
249
			'message' => 'maps-par-scrollwheelzoom',
250
		];
251
252 7
		return $params;
253
	}
254
255
	/**
256
	 * Returns the names of all supported map types.
257
	 */
258 7
	private function getTypeNames(): array {
259 7
		return array_keys( self::MAP_TYPES );
260
	}
261
262 7
	public function newMapId(): string {
263 7
		static $mapsOnThisPage = 0;
264
265 7
		$mapsOnThisPage++;
266
267 7
		return 'map_google3_' . $mapsOnThisPage;
268
	}
269
270 7
	public function getResourceModules(): array {
271 7
		return [ 'ext.maps.googlemaps3', 'ext.maps.googlemaps3ajax' ];
272
	}
273
274 7
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
275 7
		$urlArgs = array_merge(
276
			[
277 7
				'language' => self::getMappedLanguageCode( $langCode )
278
			],
279
			$urlArgs
280
		);
281 7
		if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) {
282
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'];
283
		}
284 7
		if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) {
285
			$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion'];
286
		}
287
288 7
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
289
	}
290
291
	/**
292
	 * Maps language codes to Google Maps API v3 compatible values.
293
	 */
294 7
	private static function getMappedLanguageCode( string $code ): string {
295
		$mappings = [
296 7
			'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
297
			'he' => 'iw',      // iw is googlish for hebrew
298
			'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
299
		];
300
301 7
		if ( array_key_exists( $code, $mappings ) ) {
302
			return $mappings[$code];
303
		}
304
305 7
		return $code;
306
	}
307
308 7
	public function getDependencyHtml( array $params ): string {
309 7
		$dependencies = [];
310
311
		// Only add dependencies that have not yet been added.
312 7
		foreach ( $this->getDependencies() as $dependency ) {
313 7
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
314 7
				$dependencies[] = $dependency;
315 7
				$this->addedDependencies[] = $dependency;
316
			}
317
		}
318
319
		// If there are dependencies, put them all together in a string, otherwise return false.
320 7
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
321
	}
322
323 7
	private function getDependencies(): array {
324
		return [
325 7
			self::getApiScript(
326 7
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
327 7
					$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
328
			)
329
		];
330
	}
331
332 7
	public function processingResultToMapParams( ProcessingResult $processingResult ): array {
333 7
		$parameters = $processingResult->getParameters();
334
335 7
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
336 6
				$parameters['coordinates']->getValue()
337 7
			) > 1 ) {
338 1
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
339
		}
340
341 7
		$mapParams = [];
342
343 7
		foreach ( $parameters as $parameter ) {
344 7
			$mapParams[$parameter->getName()] = $parameter->getValue();
345
		}
346
347 7
		return $mapParams;
348
	}
349
350 1
	private function getParameterWithValue( ProcessedParam $param, $value ) {
351 1
		return new ProcessedParam(
352 1
			$param->getName(),
353
			$value,
354 1
			$param->wasSetToDefault(),
355 1
			$param->getOriginalName(),
356 1
			$param->getOriginalValue()
357
		);
358
	}
359
360
}
361