Completed
Push — master ( 107e98...308e27 )
by Jeroen De
02:59
created

GoogleMapsService::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 {
0 ignored issues
show
Deprecated Code introduced by
The class Maps\MappingService has been deprecated.

This class, trait or interface has been deprecated.

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