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