Completed
Pull Request — master (#180)
by Peter
18:55 queued 08:54
created

MapsGoogleMaps3::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Class holding information and functionality specific to Google Maps v3.
5
 * This information and features can be used by any mapping feature.
6
 *
7
 * @since 0.7
8
 *
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 * @author Peter Grassberger < [email protected] >
12
 */
13
14
class MapsGoogleMaps3 extends MapsMappingService {
15
16
	/**
17
	 * List of map types (keys) and their internal values (values).
18
	 *
19
	 * @since 0.7
20
	 *
21
	 * @var array
22
	 */
23
	public static $mapTypes = [
24
		'normal' => 'ROADMAP',
25
		'roadmap' => 'ROADMAP',
26
		'satellite' => 'SATELLITE',
27
		'hybrid' => 'HYBRID',
28
		'terrain' => 'TERRAIN',
29
		'physical' => 'TERRAIN',
30
		'earth' => 'earth'
31
	];
32
33
	/**
34
	 * List of supported map layers.
35
	 *
36
	 * @since 1.0
37
	 *
38
	 * @var array
39
	 */
40
	protected static $mapLayers = [
41
		'traffic',
42
		'bicycling'
43
	];
44
45
	public static $typeControlStyles = [
46
		'default' => 'DEFAULT',
47
		'horizontal' => 'HORIZONTAL_BAR',
48
		'dropdown' => 'DROPDOWN_MENU'
49
	];
50
51
	/**
52
	 * List of supported control names.
53
	 *
54
	 * @since 1.0
55
	 *
56
	 * @var array
57
	 */
58
	protected static $controlNames = [
59
		'pan',
60
		'zoom',
61
		'type',
62
		'scale',
63
		'streetview',
64
		'rotate'
65
	];
66
67
	/**
68
	 * Constructor.
69
	 *
70
	 * @since 0.6.6
71
	 */
72
	public function __construct( $serviceName ) {
73
		parent::__construct(
74
			$serviceName,
75
			[ 'googlemaps', 'google' ]
76
		);
77
	}
78
79
	/**
80
	 * @see MapsMappingService::addParameterInfo
81
	 *
82
	 * @since 0.7
83
	 */
84
	public function addParameterInfo( array &$params ) {
85
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
86
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
87
		global $egMapsResizableByDefault, $egMapsGMaps3DefaultTilt;
88
89
		$params['zoom'] = [
90
			'type' => 'integer',
91
			'range' => [ 0, 20 ],
92
			'default' => self::getDefaultZoom(),
93
			'message' => 'maps-par-zoom',
94
		];
95
96
		$params['type'] = [
97
			'default' => $egMapsGMaps3Type,
98
			'values' => self::getTypeNames(),
99
			'message' => 'maps-googlemaps3-par-type',
100
			'post-format' => function( $value ) {
101
				return MapsGoogleMaps3::$mapTypes[strtolower( $value )];
102
			},
103
		];
104
105
		$params['types'] = [
106
			'dependencies' => 'type',
107
			'default' => $egMapsGMaps3Types,
108
			'values' => self::getTypeNames(),
109
			'message' => 'maps-googlemaps3-par-types',
110
			'islist' => true,
111
			'post-format' => function( array $value ) {
112
				foreach ( $value as &$part ) {
113
					$part = MapsGoogleMaps3::$mapTypes[strtolower( $part )];
114
				}
115
116
				return $value;
117
			},
118
		];
119
120
		$params['layers'] = [
121
			'default' => $egMapsGMaps3Layers,
122
			'values' => self::getLayerNames(),
123
			'message' => 'maps-googlemaps3-par-layers',
124
			'islist' => true,
125
		];
126
127
		$params['controls'] = [
128
			'default' => $egMapsGMaps3Controls,
129
			'values' => self::$controlNames,
130
			'message' => 'maps-googlemaps3-par-controls',
131
			'islist' => true,
132
			'post-format' => function( $value ) {
133
				return array_map( 'strtolower', $value );
134
			},
135
		];
136
137
		$params['zoomstyle'] = [
138
			'default' => $egMapsGMaps3DefZoomStyle,
139
			'values' => [ 'default', 'small', 'large' ],
140
			'message' => 'maps-googlemaps3-par-zoomstyle',
141
			'post-format' => 'strtoupper',
142
		];
143
144
		$params['typestyle'] = [
145
			'default' => $egMapsGMaps3DefTypeStyle,
146
			'values' => array_keys( self::$typeControlStyles ),
147
			'message' => 'maps-googlemaps3-par-typestyle',
148
			'post-format' => function( $value ) {
149
				return MapsGoogleMaps3::$typeControlStyles[strtolower( $value )];
150
			},
151
		];
152
153
		$params['autoinfowindows'] = [
154
			'type' => 'boolean',
155
			'default' => $egMapsGMaps3AutoInfoWindows,
156
			'message' => 'maps-googlemaps3-par-autoinfowindows',
157
		];
158
159
		$params['resizable'] = [
160
			'type' => 'boolean',
161
			'default' => $egMapsResizableByDefault,
162
			'message' => 'maps-par-resizable',
163
		];
164
165
		$params['kmlrezoom'] = [
166
			'type' => 'boolean',
167
			'default' => $GLOBALS['egMapsRezoomForKML'],
168
			'message' => 'maps-googlemaps3-par-kmlrezoom',
169
		];
170
171
		$params['poi'] = [
172
			'type' => 'boolean',
173
			'default' => $GLOBALS['egMapsShowPOI'],
174
			'message' => 'maps-googlemaps3-par-poi',
175
		];
176
177
		$params['markercluster'] = [
178
			'type' => 'boolean',
179
			'default' => false,
180
			'message' => 'maps-googlemaps3-par-markercluster',
181
		];
182
183
		$params['clustergridsize'] = [
184
				'type' => 'integer',
185
				'default' => 60,
186
				'message' => 'maps-googlemaps3-par-clustergridsize',
187
		];
188
189
		$params['clustermaxzoom'] = [
190
				'type' => 'integer',
191
				'default' => 20,
192
				'message' => 'maps-googlemaps3-par-clustermaxzoom',
193
		];
194
195
		$params['clusterzoomonclick'] = [
196
				'type' => 'boolean',
197
				'default' => true,
198
				'message' => 'maps-googlemaps3-par-clusterzoomonclick',
199
		];
200
201
		$params['clusteraveragecenter'] = [
202
				'type' => 'boolean',
203
				'default' => true,
204
				'message' => 'maps-googlemaps3-par-clusteraveragecenter',
205
		];
206
207
		$params['clusterminsize'] = [
208
				'type' => 'integer',
209
				'default' => 2,
210
				'message' => 'maps-googlemaps3-par-clusterminsize',
211
		];
212
213
		$params['tilt'] = [
214
			'type' => 'integer',
215
			'default' => $egMapsGMaps3DefaultTilt,
216
			'message' => 'maps-googlemaps3-par-tilt',
217
		];
218
219
		$params['imageoverlays'] = [
220
			'type' => 'mapsimageoverlay',
221
			'default' => [],
222
			'delimiter' => ';',
223
			'islist' => true,
224
			'message' => 'maps-googlemaps3-par-imageoverlays',
225
		];
226
227
		$params['kml'] = [
228
			'default' => [],
229
			'message' => 'maps-googlemaps3-par-kml',
230
			'islist' => true,
231
			// new MapsParamFile() FIXME
232
		];
233
234
		$params['gkml'] = [
235
			'default' => [],
236
			'message' => 'maps-googlemaps3-par-gkml',
237
			'islist' => true,
238
		];
239
240
		$params['fusiontables'] = [
241
			'default' => [],
242
			'message' => 'maps-googlemaps3-par-fusiontables',
243
			'islist' => true,
244
		];
245
246
		$params['searchmarkers'] = [
247
			'default' => '',
248
			'message' => 'maps-par-searchmarkers',
249
			// new CriterionSearchMarkers() FIXME
250
		];
251
252
		$params['enablefullscreen'] = [
253
			'type' => 'boolean',
254
			'default' => false,
255
			'message' => 'maps-googlemaps3-par-enable-fullscreen',
256
		];
257
	}
258
259
	/**
260
	 * @see iMappingService::getDefaultZoom
261
	 *
262
	 * @since 0.6.5
263
	 */
264
	public function getDefaultZoom() {
265
		global $egMapsGMaps3Zoom;
266
		return $egMapsGMaps3Zoom;
267
	}
268
269
	/**
270
	 * @see MapsMappingService::getMapId
271
	 *
272
	 * @since 0.6.5
273
	 */
274
	public function getMapId( $increment = true ) {
275
		static $mapsOnThisPage = 0;
276
277
		if ( $increment ) {
278
			$mapsOnThisPage++;
279
		}
280
281
		return 'map_google3_' . $mapsOnThisPage;
282
	}
283
284
	/**
285
	 * Returns the names of all supported map types.
286
	 *
287
	 * @return array
288
	 */
289
	public static function getTypeNames() {
290
		return array_keys( self::$mapTypes );
291
	}
292
293
	/**
294
	 * Returns the names of all supported map layers.
295
	 *
296
	 * @since 1.0
297
	 *
298
	 * @return array
299
	 */
300
	public static function getLayerNames() {
301
		return self::$mapLayers;
302
	}
303
304
	/**
305
	 * @see MapsMappingService::getDependencies
306
	 *
307
	 * @return array
308
	 */
309
	protected function getDependencies() {
310
		return [
311
			self::getApiScript(
312
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
313
				$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
314
			 )
315
		];
316
	}
317
318
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
319
		$urlArgs = array_merge(
320
			[
321
				'language' => self::getMappedLanguageCode( $langCode ),
322
				'sensor' => 'false'
323
			],
324
			$urlArgs
325
		);
326
		if ( !empty( $GLOBALS['egMapsGMaps3ApiKey'] ) ) {
327
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey']
328
		}
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...
329
330
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
331
	}
332
333
	/**
334
	 * Maps language codes to Google Maps API v3 compatible values.
335
	 *
336
	 * @param string $code
337
	 *
338
	 * @return string The mapped code
339
	 */
340
	protected static function getMappedLanguageCode( $code ) {
341
		$mappings = [
342
	         'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
343
	         'he' => 'iw',      // iw is googlish for hebrew
344
	         'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
345
		];
346
347
		if ( array_key_exists( $code, $mappings ) ) {
348
			$code = $mappings[$code];
349
		}
350
351
		return $code;
352
	}
353
354
	/**
355
	 * @see MapsMappingService::getResourceModules
356
	 *
357
	 * @since 1.0
358
	 *
359
	 * @return array of string
360
	 */
361
	public function getResourceModules() {
362
		return array_merge(
363
			parent::getResourceModules(),
364
			[ 'ext.maps.googlemaps3' ]
365
		);
366
	}
367
}
368