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
|
|
|
/* Signals whether at least one map has already been rendered */ |
55
|
|
|
private static $map_already_rendered = false; |
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%'; |
|
|
|
|
65
|
|
|
self::$map_height = '400px'; |
|
|
|
|
66
|
|
|
self::$div_id = "google_map"; |
|
|
|
|
67
|
|
|
self::$automatic_center = true; |
68
|
|
|
self::$hide_marker = false; |
69
|
|
|
self::$map_type = 'google.maps.MapTypeId.ROADMAP'; |
|
|
|
|
70
|
|
|
self::$center = 'Paris, France'; |
71
|
|
|
self::$map_already_rendered = false; |
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
|
|
|
/** |
86
|
|
|
* @param boolean $new_map_already_rendered |
87
|
|
|
*/ |
88
|
|
|
public static function set_map_already_rendered($new_map_already_rendered) { |
89
|
|
|
self::$map_already_rendered = $new_map_already_rendered; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function get_map_already_rendered() { |
93
|
|
|
return self::$map_already_rendered; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the default size of the map |
98
|
|
|
* |
99
|
|
|
* @param int $width |
100
|
|
|
* @param int $height |
101
|
|
|
*/ |
102
|
|
|
public static function set_map_size($width, $height) { |
103
|
|
|
self:: $map_width = $width; |
104
|
|
|
self::$map_height = $height; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* FIXME - NOT USED? |
109
|
|
|
* Set the type of the gmap |
110
|
|
|
* |
111
|
|
|
* @param string $mapType (can be 'google.maps.MapTypeId.ROADMAP', 'G_SATELLITE_MAP', |
112
|
|
|
* 'G_HYBRID_MAP', 'G_PHYSICAL_MAP') |
113
|
|
|
* |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public static function set_map_type($mapType) { |
117
|
|
|
self::$map_type = $mapType; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the center of the gmap (an address, using text geocoder query) |
122
|
|
|
* |
123
|
|
|
* @param string $center GoogleMap center (an address) |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public static function set_center($center) |
128
|
|
|
{ |
129
|
|
|
self::$center = $center; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get a new GoogleMapAPI object and load it with the default settings |
134
|
|
|
* |
135
|
|
|
* @return MapAPI |
136
|
|
|
*/ |
137
|
|
|
public static function instance() |
138
|
|
|
{ |
139
|
|
|
self::$instances++; |
140
|
|
|
|
141
|
|
|
if (self::$allow_full_screen == null) { |
142
|
|
|
self::$allow_full_screen = Config::inst()->get('Mappable', 'allow_full_screen'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$url = Director::absoluteBaseURL(); |
146
|
|
|
|
147
|
|
|
// remove http and https |
148
|
|
|
$url = str_replace('http://', '', $url); |
149
|
|
|
$url = str_replace('https://', '', $url); |
150
|
|
|
$parts = explode('/', $url); |
151
|
|
|
$host = $parts[0]; |
152
|
|
|
|
153
|
|
|
$key = self::$api_key; |
154
|
|
|
|
155
|
|
|
// if an array, get the key by an array keyed by host |
156
|
|
|
if (is_array($key)) { |
157
|
|
|
$key = $key[$host]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$gmap = new MapAPI($key); |
161
|
|
|
$gmap->setDivId(self::$div_id."_".self::$instances); |
162
|
|
|
$gmap->setEnableAutomaticCenterZoom(self::$automatic_center); |
|
|
|
|
163
|
|
|
$gmap->setSize(self::$map_width, self::$map_height); |
164
|
|
|
$gmap->setDefaultHideMarker(self::$hide_marker); |
165
|
|
|
$gmap->setMapType(self::$map_type); |
|
|
|
|
166
|
|
|
$gmap->setCenter(self::$center); |
167
|
|
|
$gmap->setAllowFullScreen(self::$allow_full_screen); |
168
|
|
|
return $gmap; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Sanitize a string of HTML content for safe inclusion in the JavaScript |
174
|
|
|
* for a Google Map |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public static function sanitize($content) { |
179
|
|
|
return addslashes(str_replace(array("\n", "\r", "\t"), '', $content)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Creates a new {@link GoogleMapsAPI} object loaded with the default settings |
185
|
|
|
* and places all of the items in a {@link SS_List} |
186
|
|
|
* e.g. {@link DataList} or {@link ArrayList} on the map |
187
|
|
|
* |
188
|
|
|
* @param SS_List list of objects to display on a map |
189
|
|
|
* @param array $infowindowtemplateparams Optional array of extra parameters to pass to the map info window |
190
|
|
|
* @return MapAPI |
191
|
|
|
*/ |
192
|
|
|
public static function get_map(SS_List $list, $infowindowtemplateparams) { |
193
|
|
|
$gmap = self::instance(); |
194
|
|
|
if ($list) { |
195
|
|
|
foreach ($list as $mappable) { |
196
|
|
|
if (self::ChooseToAddDataobject($mappable)) |
197
|
|
|
$gmap->addMarkerAsObject($mappable, $infowindowtemplateparams); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
return $gmap; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Determines if the current DataObject should be included to the map |
205
|
|
|
* Checks if it has Mappable interface implemented |
206
|
|
|
* If it has MapExtension included, the value of MapPinEdited is also checked |
207
|
|
|
* |
208
|
|
|
* @param DataObject $do |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
private static function ChooseToAddDataobject(DataObject $do) { |
212
|
|
|
$isMappable = $do->is_a('Mappable'); |
213
|
|
|
|
214
|
|
|
foreach ($do->getExtensionInstances() as $extension) { |
215
|
|
|
$isMappable = $isMappable || $extension instanceof Mappable; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$filterMapPinEdited = $do->hasExtension('MapExtension') |
219
|
|
|
? $do->MapPinEdited |
220
|
|
|
: true; |
221
|
|
|
|
222
|
|
|
return $isMappable && $filterMapPinEdited; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
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.