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
|
|
|
/** @var int Icon width of the gmarker **/ |
27
|
|
|
public static $iconWidth = 24; |
28
|
|
|
|
29
|
|
|
/** @var int Icon height of the gmarker **/ |
30
|
|
|
public static $iconHeight = 24; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int Prefix for the div ID of the map |
34
|
|
|
*/ |
35
|
|
|
public static $div_id = "google_map"; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var boolean Automatic center/zoom for the map |
39
|
|
|
*/ |
40
|
|
|
public static $automatic_center = true; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var boolean Show directions fields on the map |
44
|
|
|
*/ |
45
|
|
|
public static $direction_fields = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var boolean Show the marker fields on the map |
49
|
|
|
*/ |
50
|
|
|
public static $hide_marker = false; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var boolean Show the marker fields on the map |
57
|
|
|
*/ |
58
|
|
|
public static $map_type = 'google.maps.MapTypeId.ROADMAP'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string $center Center of map (adress) |
62
|
|
|
*/ |
63
|
|
|
public static $center = 'Paris, France'; |
64
|
|
|
|
65
|
|
|
/* Width of the map information window */ |
66
|
|
|
public static $info_window_width = 250; |
67
|
|
|
|
68
|
|
|
/* Signals whether at least one map has already been rendered */ |
69
|
|
|
private static $map_already_rendered = false; |
70
|
|
|
|
71
|
|
|
/* Whether or not to allow full screen */ |
72
|
|
|
private static $allow_full_screen = null; |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public static function reset() { |
76
|
|
|
self::$api_key = null; |
77
|
|
|
self::$instances = 0; |
78
|
|
|
self::$map_width = '100%'; |
|
|
|
|
79
|
|
|
self::$map_height = '400px'; |
|
|
|
|
80
|
|
|
self::$iconWidth = 24; |
81
|
|
|
self::$iconHeight = 24; |
82
|
|
|
self::$div_id = "google_map"; |
|
|
|
|
83
|
|
|
self::$automatic_center = true; |
84
|
|
|
self::$direction_fields = false; |
85
|
|
|
self::$hide_marker = false; |
86
|
|
|
self::$map_type = 'google.maps.MapTypeId.ROADMAP'; |
|
|
|
|
87
|
|
|
self::$center = 'Paris, France'; |
88
|
|
|
self::$info_window_width = 250; |
89
|
|
|
self::$map_already_rendered = false; |
90
|
|
|
self::$allow_full_screen = null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the API key for Google Maps |
95
|
|
|
* |
96
|
|
|
* @param string $key |
97
|
|
|
*/ |
98
|
|
|
public static function set_api_key($key) { |
99
|
|
|
self::$api_key = $key; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param boolean $new_map_already_rendered |
105
|
|
|
*/ |
106
|
|
|
public static function set_map_already_rendered($new_map_already_rendered) { |
107
|
|
|
self::$map_already_rendered = $new_map_already_rendered; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public static function get_map_already_rendered() { |
111
|
|
|
return self::$map_already_rendered; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the default size of the map |
116
|
|
|
* |
117
|
|
|
* @param int $width |
118
|
|
|
* @param int $height |
119
|
|
|
*/ |
120
|
|
|
public static function set_map_size($width, $height) { |
121
|
|
|
self:: $map_width = $width; |
122
|
|
|
self::$map_height = $height; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* FIXME - NOT USED? |
127
|
|
|
* Set the type of the gmap |
128
|
|
|
* |
129
|
|
|
* @param string $mapType (can be 'google.maps.MapTypeId.ROADMAP', 'G_SATELLITE_MAP', |
130
|
|
|
* 'G_HYBRID_MAP', 'G_PHYSICAL_MAP') |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public static function set_map_type($mapType) { |
135
|
|
|
self::$map_type = $mapType; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* FIXME - NOT USED? |
140
|
|
|
* Set the with of the gmap infowindow (on marker clik) |
141
|
|
|
* |
142
|
|
|
* @param int $info_window_width GoogleMap info window width |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public static function set_info_window_width($info_window_width) |
147
|
|
|
{ |
148
|
|
|
self::$info_window_width = $info_window_width; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* FIXME - NOT USED? |
153
|
|
|
* Set the center of the gmap (an address) |
154
|
|
|
* |
155
|
|
|
* @param string $center GoogleMap center (an address) |
156
|
|
|
* |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public static function set_center($center) |
160
|
|
|
{ |
161
|
|
|
self::$center = $center; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* FIXME Is this used? |
166
|
|
|
* |
167
|
|
|
* Set the size of the icon markers |
168
|
|
|
* |
169
|
|
|
* @param int $iconWidth GoogleMap marker icon width |
170
|
|
|
* @param int $iconHeight GoogleMap marker icon height |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
|
175
|
|
|
public static function set_icon_size($iconWidth, $iconHeight) |
176
|
|
|
{ |
177
|
|
|
self::$iconWidth = $iconWidth; |
178
|
|
|
self::$iconHeight = $iconHeight; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get a new GoogleMapAPI object and load it with the default settings |
183
|
|
|
* |
184
|
|
|
* @return MapAPI |
185
|
|
|
*/ |
186
|
|
|
public static function instance() |
187
|
|
|
{ |
188
|
|
|
self::$instances++; |
189
|
|
|
|
190
|
|
|
if (self::$allow_full_screen == null) { |
191
|
|
|
self::$allow_full_screen = Config::inst()->get('Mappable', 'allow_full_screen'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$url = Director::absoluteBaseURL(); |
195
|
|
|
|
196
|
|
|
// remove http and https |
197
|
|
|
$url = str_replace('http://', '', $url); |
198
|
|
|
$url = str_replace('https://', '', $url); |
199
|
|
|
$parts = explode('/', $url); |
200
|
|
|
$host = $parts[0]; |
201
|
|
|
|
202
|
|
|
$key = self::$api_key; |
203
|
|
|
|
204
|
|
|
// if an array, get the key by an array keyed by host |
205
|
|
|
if (is_array($key)) { |
206
|
|
|
$key = $key[$host]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
$gmap = new MapAPI($key); |
211
|
|
|
$gmap->setDivId(self::$div_id."_".self::$instances); |
212
|
|
|
$gmap->setEnableAutomaticCenterZoom(self::$automatic_center); |
|
|
|
|
213
|
|
|
$gmap->setDisplayDirectionFields(self::$direction_fields); |
214
|
|
|
$gmap->setSize(self::$map_width, self::$map_height); |
215
|
|
|
$gmap->setDefaultHideMarker(self::$hide_marker); |
216
|
|
|
$gmap->setMapType(self::$map_type); |
|
|
|
|
217
|
|
|
$gmap->setCenter(self::$center); |
218
|
|
|
$gmap->setIconSize(self::$iconWidth, self::$iconHeight); |
219
|
|
|
$gmap->setIncludeDownloadJavascript(self::$map_already_rendered); |
220
|
|
|
$gmap->setAllowFullScreen(self::$allow_full_screen); |
221
|
|
|
return $gmap; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Sanitize a string of HTML content for safe inclusion in the JavaScript |
227
|
|
|
* for a Google Map |
228
|
|
|
* |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
public static function sanitize($content) { |
232
|
|
|
return addslashes(str_replace(array("\n", "\r", "\t"), '', $content)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Creates a new {@link GoogleMapsAPI} object loaded with the default settings |
238
|
|
|
* and places all of the items in a {@link SS_List} |
239
|
|
|
* e.g. {@link DataList} or {@link ArrayList} on the map |
240
|
|
|
* |
241
|
|
|
* @param SS_List list of objects to display on a map |
242
|
|
|
* @param array $infowindowtemplateparams Optional array of extra parameters to pass to the map info window |
243
|
|
|
* @return MapAPI |
244
|
|
|
*/ |
245
|
|
|
public static function get_map(SS_List $list, $infowindowtemplateparams) { |
246
|
|
|
$gmap = self::instance(); |
247
|
|
|
if ($list) { |
248
|
|
|
foreach ($list as $mappable) { |
249
|
|
|
if (self::ChooseToAddDataobject($mappable)) |
250
|
|
|
$gmap->addMarkerAsObject($mappable, $infowindowtemplateparams); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
return $gmap; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Determines if the current DataObject should be included to the map |
258
|
|
|
* Checks if it has Mappable interface implemented |
259
|
|
|
* If it has MapExtension included, the value of MapPinEdited is also checked |
260
|
|
|
* |
261
|
|
|
* @param DataObject $do |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
|
|
private static function ChooseToAddDataobject(DataObject $do) { |
265
|
|
|
$isMappable = $do->is_a('Mappable'); |
266
|
|
|
|
267
|
|
|
foreach ($do->getExtensionInstances() as $extension) { |
268
|
|
|
$isMappable = $isMappable || $extension instanceof Mappable; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$filterMapPinEdited = $do->hasExtension('MapExtension') |
272
|
|
|
? $do->MapPinEdited |
273
|
|
|
: true; |
274
|
|
|
|
275
|
|
|
return $isMappable && $filterMapPinEdited; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
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.