Completed
Push — AUTOMATED_TESTING ( 0fc006...6591ac )
by Gordon
12:25
created

MapUtil::instance()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 34
rs 8.8571
cc 3
eloc 21
nc 4
nop 0
1
<?php
2
3
class MapUtil
4
{
5
6
	/**
7
	 * @var string The Google Maps API key
8
	 */
9
	protected static $api_key;
10
11
	/**
12
	 * @var int Number of active {@see GoogleMapsAPI} instances (for the HTML ID)
13
	 */
14
	protected static $instances = 0;
15
16
	/**
17
	 * @var int The default width of a Google Map
18
	 */
19
	public static $map_width = '100%';
20
21
	/**
22
	 * @var int The default height of a Google Map
23
	 */
24
	public static $map_height = '400px';
25
26
	/**
27
	 * @var int Prefix for the div ID of the map
28
	 */
29
	public static $div_id = "google_map";
30
31
	/**
32
	 * @var boolean Automatic center/zoom for the map
33
	 */
34
	public static $automatic_center = true;
35
36
	/**
37
	 * @var boolean Show the marker fields on the map
38
	 */
39
	public static $hide_marker = false;
40
41
42
43
44
	/**
45
	 * @var boolean Show the marker fields on the map
46
	 */
47
	public static $map_type = 'google.maps.MapTypeId.ROADMAP';
48
49
	/**
50
	 * @var string $center Center of map (adress)
51
	 */
52
	public static $center = 'Paris, France';
53
54
	/* Width of the map information window */
55
	public static $info_window_width = 250;
56
57
	/* Whether or not to allow full screen */
58
	private static $allow_full_screen = null;
59
60
61
	public static function reset() {
62
		self::$api_key = null;
63
		self::$instances = 0;
64
		self::$map_width = '100%';
0 ignored issues
show
Documentation Bug introduced by
The property $map_width was declared of type integer, but '100%' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
65
		self::$map_height = '400px';
0 ignored issues
show
Documentation Bug introduced by
The property $map_height was declared of type integer, but '400px' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
66
		self::$div_id = "google_map";
0 ignored issues
show
Documentation Bug introduced by
The property $div_id was declared of type integer, but 'google_map' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
67
		self::$automatic_center = true;
68
		self::$hide_marker = false;
69
		self::$map_type = 'google.maps.MapTypeId.ROADMAP';
0 ignored issues
show
Documentation Bug introduced by
The property $map_type was declared of type boolean, but 'google.maps.MapTypeId.ROADMAP' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
70
		self::$center = 'Paris, France';
71
		self::$info_window_width = 250;
72
		self::$allow_full_screen = null;
73
	}
74
75
	/**
76
	 * Set the API key for Google Maps
77
	 *
78
	 * @param string $key
79
	 */
80
	public static function set_api_key($key) {
81
		self::$api_key = $key;
82
	}
83
84
	/**
85
	 * Set the default size of the map
86
	 *
87
	 * @param int $width
88
	 * @param int $height
89
	 */
90
	public static function set_map_size($width, $height) {
91
		self:: $map_width = $width;
92
		self::$map_height = $height;
93
	}
94
95
	/**
96
	 * FIXME - NOT USED?
97
	 * Set the type of the gmap
98
	 *
99
	 * @param string $mapType (can be 'google.maps.MapTypeId.ROADMAP', 'G_SATELLITE_MAP',
100
	 * 'G_HYBRID_MAP', 'G_PHYSICAL_MAP')
101
	 *
102
	 * @return void
103
	 */
104
	public static function set_map_type($mapType) {
105
		self::$map_type = $mapType;
0 ignored issues
show
Documentation Bug introduced by
The property $map_type was declared of type boolean, but $mapType is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
106
	}
107
108
	/**
109
	 * FIXME - NOT USED?
110
	 * Set the with of the gmap infowindow (on marker clik)
111
	 *
112
	 * @param int $info_window_width GoogleMap info window width
113
	 *
114
	 * @return void
115
	 */
116
	public static function set_info_window_width($info_window_width)
117
	{
118
		self::$info_window_width = $info_window_width;
119
	}
120
121
	/**
122
	 * FIXME - NOT USED?
123
	 * Set the center of the gmap (an address)
124
	 *
125
	 * @param string $center GoogleMap  center (an address)
126
	 *
127
	 * @return void
128
	 */
129
	public static function set_center($center)
130
	{
131
		self::$center = $center;
132
	}
133
134
	/**
135
	 * Get a new GoogleMapAPI object and load it with the default settings
136
	 *
137
	 * @return MapAPI
138
	 */
139
	public static function instance()
140
	{
141
		self::$instances++;
142
143
		if (self::$allow_full_screen == null) {
144
			self::$allow_full_screen = Config::inst()->get('Mappable', 'allow_full_screen');
145
		}
146
147
		$url = Director::absoluteBaseURL();
148
149
		// remove http and https
150
		$url = str_replace('http://', '', $url);
151
		$url = str_replace('https://', '', $url);
152
		$parts = explode('/', $url);
153
		$host = $parts[0];
154
155
		$key = self::$api_key;
156
157
		// if an array, get the key by an array keyed by host
158
		if (is_array($key)) {
159
			$key = $key[$host];
160
		}
161
162
163
		$gmap = new MapAPI($key);
164
		$gmap->setDivId(self::$div_id."_".self::$instances);
165
		$gmap->setEnableAutomaticCenterZoom(self::$automatic_center);
0 ignored issues
show
Documentation introduced by
self::$automatic_center is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166
		$gmap->setSize(self::$map_width, self::$map_height);
167
		$gmap->setDefaultHideMarker(self::$hide_marker);
168
		$gmap->setMapType(self::$map_type);
0 ignored issues
show
Documentation introduced by
self::$map_type is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
		$gmap->setCenter(self::$center);
170
		$gmap->setAllowFullScreen(self::$allow_full_screen);
171
		return $gmap;
172
	}
173
174
175
	/**
176
	 * Sanitize a string of HTML content for safe inclusion in the JavaScript
177
	 * for a Google Map
178
	 *
179
	 * @return string
180
	 */
181
	public static function sanitize($content) {
182
		return addslashes(str_replace(array("\n", "\r", "\t"), '', $content));
183
	}
184
185
186
	/**
187
	 * Creates a new {@link GoogleMapsAPI} object loaded with the default settings
188
	 * and places all of the items in a {@link SS_List}
189
	 * e.g. {@link DataList} or {@link ArrayList} on the map
190
	 *
191
	 * @param SS_List list of objects to display on a map
192
	 * @param  array $infowindowtemplateparams Optional array of extra parameters to pass to the map info window
193
	 * @return MapAPI
194
	 */
195
	public static function get_map(SS_List $list, $infowindowtemplateparams) {
196
		$gmap = self::instance();
197
		if ($list) {
198
			foreach ($list as $mappable) {
199
				if (self::ChooseToAddDataobject($mappable))
200
					$gmap->addMarkerAsObject($mappable, $infowindowtemplateparams);
201
			}
202
		}
203
		return $gmap;
204
	}
205
206
	/**
207
	 * Determines if the current DataObject should be included to the map
208
	 * Checks if it has Mappable interface implemented
209
	 * If it has MapExtension included, the value of MapPinEdited is also checked
210
	 *
211
	 * @param DataObject $do
212
	 * @return bool
213
	 */
214
	private static function ChooseToAddDataobject(DataObject $do) {
215
		$isMappable = $do->is_a('Mappable');
216
217
		foreach ($do->getExtensionInstances() as $extension) {
218
			$isMappable = $isMappable || $extension instanceof Mappable;
219
		}
220
221
		$filterMapPinEdited = $do->hasExtension('MapExtension')
222
			? $do->MapPinEdited
223
			: true;
224
225
		return $isMappable && $filterMapPinEdited;
226
	}
227
}
228