Newer
Older
Brandon Bergren
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
// $Id$
/**
* @file
* GMap macro parsing routines.
*/
/**
* Parse a macro style definition.
* Example: #111111/1/100/#111111/1
* @internal
*/
function _gmap_parse_style($style) {
if (strpos($style, '/') === FALSE) {
// Style ref.
return $style;
}
$styles = explode('/', $style);
// @@@ Todo: Fix up old xmaps stuff. Possibly detect by looking for array length 7?
// Strip off # signs, they get added by code.
if (isset($styles[0]) && substr($styles[0], 0, 1) == '#') {
$styles[0] = substr($styles[0], 1);
}
if (isset($styles[3]) && substr($styles[3], 0, 1) == '#') {
$styles[3] = substr($styles[3], 1);
}
// Assume anything > 0 and < 1.1 was an old representation.
if ($styles[2] > 0 && $styles[2] < 1.1) {
$styles[2] = $styles[2] * 100;
}
if (isset($styles[4])) {
if ($styles[4] > 0 && $styles[4] < 1.1) {
$styles[4] = $styles[4] * 100;
}
}
return $styles;
}
/**
* Parse "x.xxxxx , y.yyyyyy (+ x.xxxxx, y.yyyyy ...)" into an array of points.
* @internal
*/
function _gmap_str2coord($str) {
// Explode along + axis
$arr = explode('+', $str);
// Explode along , axis
$points = array();
foreach ($arr as $pt) {
list($lat, $lon) = explode(',', $pt);
$points[] = array((float)trim($lat), (float)trim($lon));
}
return $points;
}
function _gmap_parse_macro($instring, $ver = 2) {
// Get a list of keys that are "multiple."
$m = array();
$multiple = module_invoke_all('gmap', 'macro_multiple', $m);
// Remove leading and trailing tags
if (substr(trim($instring), -1)==']') {
$instring = substr(trim($instring), 0, -1);
}
if (substr($instring, 0, 5)=='[gmap') {
$instring = substr($instring, 6);
}
// Chop the macro into an array
$temp = explode('|', $instring);
$m = array();
foreach ($temp as $row) {
$offset = strpos($row, '=');
if ($offset !== FALSE) {
$k = trim(substr($row, 0, $offset));
$r = trim(substr($row, $offset+1));
if (in_array($k, $multiple)) {
// Things that can appear multiple times
if (!isset($m[$k])) {
$m[$k] = array();
}
$m[$k][] = $r;
}
else {
$m[$k] = $r;
}
}
}
// Synonyms
if (isset($m['type'])) {
$m['maptype'] = $m['type'];
unset($m['type']);
}
if (isset($m['control'])) {
$m['controltype'] = $m['control'];
unset($m['control']);
}
if (isset($m['feed']) && is_array($m['feed'])) {
foreach ($m['feed'] as $k => $v) {
$temp = explode('::', $v);
// Normalize url
if (substr($temp[1], 0, 1) == '/') {
$temp[1] = substr($temp[1], 1);
}
$temp[1] = url($temp[1]);
$m['feed'][$k] = array(
'markername' => $temp[0],
'url' => $temp[1],
);
}
}
// Add custom styles.
if (isset($m['style']) && is_array($m['style'])) {
foreach ($m['style'] as $k => $v) {
$temp = explode(':', $v);
$m['styles'][$temp[0]] = _gmap_parse_style($temp[1]);
}
unset($m['style']);
}
// Merge points and markers
if (!isset($m['points']) || !is_array($m['points'])) $m['points'] = array();
if (!isset($m['markers']) || !is_array($m['markers'])) $m['markers'] = array();
$m['markers-temp'] = array_merge($m['points'], $m['markers']);
unset($m['points']);
unset($m['markers']);
// all shapes in 1 array
if (isset($m['circle']) && is_array($m['circle'])) {
foreach ($m['circle'] as $shape) {
$s = array('type' => 'circle');
$cp = strpos($shape, ':');
if ($cp !== FALSE) {
$stylestr = substr($shape, 0, $cp);
$s['style'] = _gmap_parse_style($stylestr);
$shape = substr($shape, $cp+1);
}
$tmp = explode('+', $shape);
$s['radius'] = $tmp[1] ? $tmp[1] : 100;
if ($tmp[2]) $s['numpoints'] = trim($tmp[2]);
$tmp = _gmap_str2coord($tmp[0]);
$s['center'] = $tmp[0];
$m['shapes'][] = $s;
}
unset($m['circle']);
}
// Fixup legacy lines.
if (isset($m['line1'])) {
$def = gmap_defaults();
if (!isset($m['line'])) $m['line'] = array();
$m['line'][] = $def['line_colors'][0] .':'. $m['line1'];
unset($m['line1']);
}
if (isset($m['line2'])) {
$def = gmap_defaults();
if (!isset($m['line'])) $m['line'] = array();
$m['line'][] = $def['line_colors'][1] .':'. $m['line3'];
unset($m['line2']);
}
if (isset($m['line3'])) {
$def = gmap_defaults();
if (!isset($m['line'])) $m['line'] = array();
$m['line'][] = $def['line_colors'][2] .':'. $m['line3'];
unset($m['line3']);
}
if (isset($m['line']) && is_array($m['line'])) {
foreach ($m['line'] as $shape) {
$s = array('type' => 'line');
$cp = strpos($shape, ':');
if ($cp != FALSE) {
$stylestr = substr($shape, 0, $cp);
$s['style'] = _gmap_parse_style($stylestr);
$shape = substr($shape, $cp+1);
}
$s['points'] = _gmap_str2coord($shape);
$m['shapes'][] = $s;
}
unset($m['line']);
}
if (isset($m['rpolygon']) && is_array($m['rpolygon'])) {
foreach ($m['rpolygon'] as $shape) {
$s = array('type' => 'rpolygon');
$cp = strpos($shape, ':');
if ($cp !== FALSE) {
$stylestr = substr($shape, 0, $cp);
$s['style'] = _gmap_parse_style($stylestr);
$shape = substr($shape, $cp+1);
}
$tmp = explode('+', $shape);
if ($tmp[2]) {
$s['numpoints'] = (int)trim($tmp[2]);
$tmp = array_slice($tmp, 0, 2);
}
$shape = implode('+', $tmp);
$tmp = _gmap_str2coord($shape);
$s['center'] = $tmp[0];
$s['point2'] = $tmp[1];
$m['shapes'][] = $s;
}
unset($m['rpolygon']);
}
if (isset($m['polygon']) && is_array($m['polygon'])) {
foreach ($m['polygon'] as $shape) {
$s = array('type' => 'polygon');
$cp = strpos($shape, ':');
if ($cp !== FALSE) {
$stylestr = substr($shape, 0, $cp);
$s['style'] = _gmap_parse_style($stylestr);
$shape = substr($shape, $cp+1);
}
$s['points'] = _gmap_str2coord($shape);
$m['shapes'][] = $s;
}
unset($m['polygon']);
}
// Version 1 -> 2 conversion
if ($ver == 1) {
// Zoom is flipped
if (isset($m['zoom'])) {
$m['zoom'] = 18 - $m['zoom'];
if ($m['zoom'] < 1) {
$m['zoom'] = 1;
}
}
}
// Center -> latitude and longitude
if (isset($m['center']) && $m['center']) {
list($m['latitude'], $m['longitude']) = explode(',', $m['center']);
unset($m['center']);
}
// Behavior
if (isset($m['behaviour'])) {
$m['behavior'] = $m['behaviour'];
unset($m['behaviour']);
}
if (isset($m['behavior'])) {
$sep = ' ';
if (strpos($m['behavior'], ',') !== FALSE) {
// In some places, commas were used to seperate behaviors.
// This was originally an accident, but it's easy enough to support.
$sep = ',';
}
$m['behavior-temp'] = explode($sep, $m['behavior']);
$m['behavior'] = array();
foreach ($m['behavior-temp'] as $v) {
$m['behavior'][substr($v, 1)] = (substr($v, 0, 1) == '+') ? TRUE : FALSE;
}
unset($m['behavior-temp']);
}
// tcontrol now is mtc.
if (isset($m['tcontrol'])) {
if (strtolower(trim($m['tcontrol'])) == 'on') {
$m['mtc'] = 'standard';
}
else {
$m['mtc'] = 'none';
}
unset($m['tcontrol']);
}
// notype also controls mtc.
if (isset($m['behavior']['notype'])) {
if ($m['behavior']['notype']) {
$m['mtc']= 'none';
}
unset($m['behavior']['notype']);
}
// Stuff that was converted to behavior flags
// Scale control.
if (isset($m['scontrol'])) {
if (strtolower(trim($m['scontrol'])) == 'on') {
$m['behavior']['scale'] = TRUE;
}
else {
$m['behavior']['scale'] = FALSE;
}
unset($m['scontrol']);
}
// Draggability.
if (isset($m['drag'])) {
if (strtolower(trim($m['drag'])) == 'yes') {
$m['behavior']['nodrag'] = FALSE;
}
else {
$m['behavior']['nodrag'] = TRUE;
}
unset($m['drag']);
}
// Markers fixup
foreach ($m['markers-temp'] as $t) {
unset($markername);
// Named?
if (strpos($t, '::')) { // Single : gets handled below.
list($markername, $t) = explode('::', $t, 2);
}
// Break down into points
$points = explode('+', $t);
$offset = -1;
foreach ($points as $point) {
$marker = array();
$offset++;
$marker['options'] = array();
// Labelled?
// @@@ Gmap allows both a tooltip and a popup, how to represent?
if (strpos($point, ':')) {
list($point, $marker['text']) = explode(':', $point, 2);
$marker['text'] = theme('gmap_marker_popup', $marker['text']);
}
if (strpos($point, '%')) {
list($point, $addons) = explode('%', $point, 2);
$motemp = explode('%', $addons);
foreach ($motemp as $option) {
$marker['options'][trim($option)] = TRUE;
}
}
list($marker['latitude'], $marker['longitude']) = explode(',', $point, 2);
// Named markers get an offset too.
if (isset($markername)) {
$marker['markername'] = $markername;
$marker['offset'] = $offset;
}
$m['markers'][] = $marker;
}
}
unset($m['markers-temp']);
// Assign an id if one wasn't specified.
if (!isset($m['id'])) {
$m['id'] = gmap_get_auto_mapid();
}
// The macro can now be manipulated by reference.
foreach (module_implements('gmap') as $module) {
$additions = call_user_func_array($module .'_gmap', array('parse_macro', &$m));
if (!empty($additions)) {
foreach ($additions as $k => $v) {
$m[$k] = $v;
}
}
}
return $m;
}