Completed
Push — master ( 7034c7...e9a56b )
by Jeroen De
03:10
created

GoogleMapsService::getDependencyHtml()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4
1
<?php
2
3
namespace Maps;
4
5
use Html;
6
7
/**
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 * @author Peter Grassberger < [email protected] >
11
 */
12
class GoogleMapsService extends MappingService {
13
14
	/**
15
	 * Maps user input map types to the Google Maps names for the map types.
16
	 */
17
	private const MAP_TYPES = [
18
		'normal' => 'ROADMAP',
19
		'roadmap' => 'ROADMAP',
20
		'satellite' => 'SATELLITE',
21
		'hybrid' => 'HYBRID',
22
		'terrain' => 'TERRAIN',
23
		'physical' => 'TERRAIN',
24
		'earth' => 'earth'
25
	];
26
27
	private const TYPE_CONTROL_STYLES = [
28
		'default' => 'DEFAULT',
29
		'horizontal' => 'HORIZONTAL_BAR',
30
		'dropdown' => 'DROPDOWN_MENU'
31
	];
32
33
	private $addedDependencies = [];
34
35 7
	public function getName(): string {
36 7
		return 'googlemaps3';
37
	}
38
39 20
	public function getAliases(): array {
40 20
		return [ 'googlemaps', 'google' ];
41
	}
42
43 8
	public function hasAlias( string $alias ): bool {
44 8
		return in_array( $alias, [ 'googlemaps', 'google' ] );
45
	}
46
47 7
	public function getParameterInfo(): array {
48 7
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
49 7
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
50 7
		global $egMapsResizableByDefault;
51
52 7
		$params = [];
53
54 7
		$params['zoom'] = [
55 7
			'type' => 'integer',
56
			'range' => [ 0, 20 ],
57 7
			'default' => $GLOBALS['egMapsGMaps3Zoom'],
58 7
			'message' => 'maps-par-zoom',
59
		];
60
61 7
		$params['type'] = [
62 7
			'default' => $egMapsGMaps3Type,
63 7
			'values' => self::getTypeNames(),
64 7
			'message' => 'maps-googlemaps3-par-type',
65 7
			'post-format' => function ( $value ) {
66 7
				return GoogleMapsService::MAP_TYPES[strtolower( $value )];
67 7
			},
68
		];
69
70 7
		$params['types'] = [
71 7
			'dependencies' => 'type',
72 7
			'default' => $egMapsGMaps3Types,
73 7
			'values' => self::getTypeNames(),
74 7
			'message' => 'maps-googlemaps3-par-types',
75
			'islist' => true,
76 7
			'post-format' => function ( array $value ) {
77 7
				foreach ( $value as &$part ) {
78 7
					$part = self::MAP_TYPES[strtolower( $part )];
79
				}
80
81 7
				return $value;
82 7
			},
83
		];
84
85 7
		$params['layers'] = [
86 7
			'default' => $egMapsGMaps3Layers,
87
			'values' => [
88
				'traffic',
89
				'bicycling',
90
				'transit'
91
			],
92 7
			'message' => 'maps-googlemaps3-par-layers',
93
			'islist' => true,
94
		];
95
96 7
		$params['controls'] = [
97 7
			'default' => $egMapsGMaps3Controls,
98
			'values' => [
99
				'pan',
100
				'zoom',
101
				'type',
102
				'scale',
103
				'streetview',
104
				'rotate'
105
			],
106 7
			'message' => 'maps-googlemaps3-par-controls',
107
			'islist' => true,
108 7
			'post-format' => function ( $value ) {
109 7
				return array_map( 'strtolower', $value );
110 7
			},
111
		];
112
113 7
		$params['zoomstyle'] = [
114 7
			'default' => $egMapsGMaps3DefZoomStyle,
115
			'values' => [ 'default', 'small', 'large' ],
116 7
			'message' => 'maps-googlemaps3-par-zoomstyle',
117 7
			'post-format' => 'strtoupper',
118
		];
119
120 7
		$params['typestyle'] = [
121 7
			'default' => $egMapsGMaps3DefTypeStyle,
122 7
			'values' => array_keys( self::TYPE_CONTROL_STYLES ),
123 7
			'message' => 'maps-googlemaps3-par-typestyle',
124 7
			'post-format' => function ( $value ) {
125 7
				return self::TYPE_CONTROL_STYLES[strtolower( $value )];
126 7
			},
127
		];
128
129 7
		$params['autoinfowindows'] = [
130 7
			'type' => 'boolean',
131 7
			'default' => $egMapsGMaps3AutoInfoWindows,
132 7
			'message' => 'maps-googlemaps3-par-autoinfowindows',
133
		];
134
135 7
		$params['resizable'] = [
136 7
			'type' => 'boolean',
137 7
			'default' => $egMapsResizableByDefault,
138 7
			'message' => 'maps-par-resizable',
139
		];
140
141 7
		$params['kmlrezoom'] = [
142 7
			'type' => 'boolean',
143 7
			'default' => $GLOBALS['egMapsRezoomForKML'],
144 7
			'message' => 'maps-googlemaps3-par-kmlrezoom',
145
		];
146
147 7
		$params['poi'] = [
148 7
			'type' => 'boolean',
149 7
			'default' => $GLOBALS['egMapsShowPOI'],
150 7
			'message' => 'maps-googlemaps3-par-poi',
151
		];
152
153 7
		$params['markercluster'] = [
154
			'type' => 'boolean',
155
			'default' => false,
156
			'message' => 'maps-par-markercluster',
157
		];
158
159 7
		$params['clustergridsize'] = [
160
			'type' => 'integer',
161
			'default' => 60,
162
			'message' => 'maps-googlemaps3-par-clustergridsize',
163
		];
164
165 7
		$params['clustermaxzoom'] = [
166
			'type' => 'integer',
167
			'default' => 20,
168
			'message' => 'maps-par-clustermaxzoom',
169
		];
170
171 7
		$params['clusterzoomonclick'] = [
172
			'type' => 'boolean',
173
			'default' => true,
174
			'message' => 'maps-par-clusterzoomonclick',
175
		];
176
177 7
		$params['clusteraveragecenter'] = [
178
			'type' => 'boolean',
179
			'default' => true,
180
			'message' => 'maps-googlemaps3-par-clusteraveragecenter',
181
		];
182
183 7
		$params['clusterminsize'] = [
184
			'type' => 'integer',
185
			'default' => 2,
186
			'message' => 'maps-googlemaps3-par-clusterminsize',
187
		];
188
189 7
		$params['imageoverlays'] = [
190
			'type' => 'mapsimageoverlay',
191
			'default' => [],
192
			'delimiter' => ';',
193
			'islist' => true,
194
			'message' => 'maps-googlemaps3-par-imageoverlays',
195
		];
196
197 7
		$params['kml'] = [
198 7
			'default' => [],
199 7
			'message' => 'maps-par-kml',
200
			'islist' => true,
201 7
			'post-format' => function( array $kmlFileNames ) {
202 7
				return array_map(
203 7
					function( string $fileName ) {
204
						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...
205 7
					},
206 7
					$kmlFileNames
207
				);
208 7
			}
209
		];
210
211 7
		$params['gkml'] = [
212
			'default' => [],
213
			'message' => 'maps-googlemaps3-par-gkml',
214
			'islist' => true,
215
		];
216
217 7
		$params['searchmarkers'] = [
218
			'default' => '',
219
			'message' => 'maps-par-searchmarkers',
220
			// new CriterionSearchMarkers() FIXME
221
		];
222
223 7
		$params['enablefullscreen'] = [
224
			'type' => 'boolean',
225
			'default' => false,
226
			'message' => 'maps-par-enable-fullscreen',
227
		];
228
229 7
		$params['scrollwheelzoom'] = [
230
			'type' => 'boolean',
231
			'default' => false,
232
			'message' => 'maps-par-scrollwheelzoom',
233
		];
234
235 7
		return $params;
236
	}
237
238
	/**
239
	 * Returns the names of all supported map types.
240
	 */
241 7
	private function getTypeNames(): array {
242 7
		return array_keys( self::MAP_TYPES );
243
	}
244
245 7
	public function getMapId(): string {
246 7
		static $mapsOnThisPage = 0;
247
248 7
		$mapsOnThisPage++;
249
250 7
		return 'map_google3_' . $mapsOnThisPage;
251
	}
252
253 7
	public function getResourceModules(): array {
254 7
		return [ 'ext.maps.googlemaps3', 'ext.sm.googlemaps3ajax' ];
255
	}
256
257 7
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
258 7
		$urlArgs = array_merge(
259
			[
260 7
				'language' => self::getMappedLanguageCode( $langCode )
261
			],
262 7
			$urlArgs
263
		);
264 7
		if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) {
265
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'];
266
		}
267 7
		if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) {
268
			$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion'];
269
		}
270
271 7
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
272
	}
273
274
	/**
275
	 * Maps language codes to Google Maps API v3 compatible values.
276
	 */
277 7
	private static function getMappedLanguageCode( string $code ): string {
278
		$mappings = [
279 7
			'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
280
			'he' => 'iw',      // iw is googlish for hebrew
281
			'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
282
		];
283
284 7
		if ( array_key_exists( $code, $mappings ) ) {
285
			return $mappings[$code];
286
		}
287
288 7
		return $code;
289
	}
290
291 7
	public function getDependencyHtml( array $params ): string {
292 7
		$dependencies = [];
293
294
		// Only add dependencies that have not yet been added.
295 7
		foreach ( $this->getDependencies() as $dependency ) {
296 7
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
297 1
				$dependencies[] = $dependency;
298 1
				$this->addedDependencies[] = $dependency;
299
			}
300
		}
301
302
		// If there are dependencies, put them all together in a string, otherwise return false.
303 7
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
304
	}
305
306 7
	private function getDependencies(): array {
307
		return [
308 7
			self::getApiScript(
309 7
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
310 7
					$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
311
			)
312
		];
313
	}
314
}
315