| 1 |
<?php
|
| 2 |
// $Id: gmap_markerinfo.inc,v 1.2 2008/07/15 16:30:29 bdragon Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* GMap marker information routines.
|
| 7 |
*
|
| 8 |
* This file is pulled in whenever we need to refresh the marker information
|
| 9 |
* and marker name caches.
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Get marker icon data for constructing json object.
|
| 14 |
*/
|
| 15 |
function _gmap_get_icondata() {
|
| 16 |
$icons = array();
|
| 17 |
|
| 18 |
$imagetypes = array(
|
| 19 |
'shadow',
|
| 20 |
'printImage',
|
| 21 |
'mozPrintImage',
|
| 22 |
'printShadow',
|
| 23 |
'transparent',
|
| 24 |
);
|
| 25 |
|
| 26 |
$markerdir = variable_get('gmap_markerfiles', drupal_get_path('module', 'gmap') .'/markers');
|
| 27 |
|
| 28 |
// The following routines are designed to be easy to comprehend, not fast.
|
| 29 |
// This whole process gets cached.
|
| 30 |
|
| 31 |
// Get the ini files.
|
| 32 |
$inifiles = file_scan_directory($markerdir, '.*\.ini$');
|
| 33 |
// Parse the ini files and store by path
|
| 34 |
$inis = array();
|
| 35 |
foreach ($inifiles as $file) {
|
| 36 |
$path = substr($file->filename, strlen($markerdir), -strlen($file->basename));
|
| 37 |
if (!isset($inis[$path])) {
|
| 38 |
$inis[$path] = array();
|
| 39 |
}
|
| 40 |
$inis[$path][] = parse_ini_file($file->filename, TRUE);
|
| 41 |
}
|
| 42 |
unset($inifiles);
|
| 43 |
|
| 44 |
// Per directory..
|
| 45 |
foreach ($inis as $path => $path_inis) {
|
| 46 |
$icons[$path] = array(
|
| 47 |
'tempf' => array(),
|
| 48 |
'f' => array(),
|
| 49 |
'w' => array(),
|
| 50 |
'h' => array(),
|
| 51 |
'i' => array(), // Sets of sets
|
| 52 |
);
|
| 53 |
|
| 54 |
// Part 1: Collect image names
|
| 55 |
$filenames = array();
|
| 56 |
foreach ($path_inis as $ini) {
|
| 57 |
foreach ($ini as $k => $v) {
|
| 58 |
// Is this definition for an icon? (anything with a dot is a file)
|
| 59 |
if (strpos($k, '.') !== FALSE) {
|
| 60 |
// Add the icon name.
|
| 61 |
$filenames[$k] = TRUE;
|
| 62 |
}
|
| 63 |
else {
|
| 64 |
// Shadow / alternate search
|
| 65 |
foreach ($imagetypes as $check) {
|
| 66 |
if (isset($v[$check])) {
|
| 67 |
$filenames[$v[$check]] = TRUE;
|
| 68 |
}
|
| 69 |
}
|
| 70 |
// A sequence is a list of image names.
|
| 71 |
if (isset($v['sequence'])) {
|
| 72 |
foreach (explode(',', $v['sequence']) as $f) {
|
| 73 |
$filenames[trim($f)] = TRUE;
|
| 74 |
}
|
| 75 |
}
|
| 76 |
}
|
| 77 |
}
|
| 78 |
}
|
| 79 |
$icons[$path]['tempf'] = $filenames;
|
| 80 |
}
|
| 81 |
unset($filenames);
|
| 82 |
|
| 83 |
// Part 2: Assign ids, get width and height
|
| 84 |
foreach ($icons as $path => $v) {
|
| 85 |
$counter = 0;
|
| 86 |
foreach ($icons[$path]['tempf'] as $filename => $fv) {
|
| 87 |
// Skip empty filenames to avoid warnings.
|
| 88 |
if (empty($filename)) {
|
| 89 |
continue;
|
| 90 |
}
|
| 91 |
$size = getimagesize($markerdir . $path . $filename);
|
| 92 |
$icons[$path]['f'][$counter] = $filename;
|
| 93 |
$icons[$path]['w'][$counter] = $size[0];
|
| 94 |
$icons[$path]['h'][$counter] = $size[1];
|
| 95 |
// Store an index under tempf for the next part...
|
| 96 |
$icons[$path]['tempf'][$filename] = $counter;
|
| 97 |
$counter++;
|
| 98 |
}
|
| 99 |
_gmap_compress_array($icons[$path]['w']);
|
| 100 |
_gmap_compress_array($icons[$path]['h']);
|
| 101 |
}
|
| 102 |
|
| 103 |
// Part 3: Main ini parsing
|
| 104 |
// Per directory...
|
| 105 |
foreach ($inis as $path => $path_inis) {
|
| 106 |
// Per file...
|
| 107 |
foreach ($path_inis as $ini) {
|
| 108 |
// Compression.
|
| 109 |
foreach ($ini as $k => $v) {
|
| 110 |
// Compress sequence filenames.
|
| 111 |
if (isset($ini[$k]['sequence'])) {
|
| 112 |
$temp = array();
|
| 113 |
foreach (explode(',', $ini[$k]['sequence']) as $file) {
|
| 114 |
$temp[] = $icons[$path]['tempf'][$file];
|
| 115 |
}
|
| 116 |
$ini[$k]['sequence'] = $temp;
|
| 117 |
}
|
| 118 |
// Compress other image field filenames.
|
| 119 |
foreach ($imagetypes as $t) {
|
| 120 |
if (isset($ini[$k][$t])) {
|
| 121 |
$ini[$k][$t] = $icons[$path]['tempf'][$ini[$k][$t]];
|
| 122 |
}
|
| 123 |
}
|
| 124 |
// Setup key for compression
|
| 125 |
$ini[$k]['key'] = $k;
|
| 126 |
}
|
| 127 |
|
| 128 |
$mv = array();
|
| 129 |
$iv = array();
|
| 130 |
if (isset($ini['defaults'])) {
|
| 131 |
$mv[0] = $ini['defaults'];
|
| 132 |
unset($ini['defaults']);
|
| 133 |
}
|
| 134 |
else {
|
| 135 |
$mv[0] = array();
|
| 136 |
}
|
| 137 |
foreach ($ini as $k => $v) {
|
| 138 |
if (strpos($k, '.') === FALSE) {
|
| 139 |
$mv[] = $ini[$k];
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
$iv[] = $ini[$k];
|
| 143 |
}
|
| 144 |
}
|
| 145 |
|
| 146 |
$icons[$path]['i'][] = array(
|
| 147 |
_gmap_compress_icon_def($mv),
|
| 148 |
_gmap_compress_icon_def($iv),
|
| 149 |
);
|
| 150 |
|
| 151 |
}
|
| 152 |
}
|
| 153 |
foreach ($icons as $path => $v) {
|
| 154 |
unset($icons[$path]['tempf']);
|
| 155 |
}
|
| 156 |
return $icons;
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
| 160 |
* Make a compressed definition.
|
| 161 |
* A series of arrays with trailing holes allowed.
|
| 162 |
* Any missing values at the end of subarrays
|
| 163 |
* are equal to the last defined value.
|
| 164 |
*/
|
| 165 |
function _gmap_compress_icon_def($iconset) {
|
| 166 |
$order = array(
|
| 167 |
'key', 'name', 'sequence', 'anchorX', 'anchorY',
|
| 168 |
'infoX', 'infoY', 'shadow', 'printImage', 'mozPrintImage',
|
| 169 |
'printShadow', 'transparent',
|
| 170 |
);
|
| 171 |
$ints = array(3 => TRUE, 4 => TRUE, 5 => TRUE, 6 => TRUE);
|
| 172 |
$nulls = array('', '', array(), 0, 0, 0, 0, '', '', '', '', '', );
|
| 173 |
|
| 174 |
$a = array();
|
| 175 |
for ($c0 = 0; $c0<count($order); $c0++) {
|
| 176 |
$a[$c0] = array();
|
| 177 |
for ($c1=0; $c1<count($iconset); $c1++) {
|
| 178 |
$temp = isset($iconset[$c1][$order[$c0]]) ? $iconset[$c1][$order[$c0]] : $nulls[$c0];
|
| 179 |
// Ensure that numeric quantities are encoded as ints, not strings.
|
| 180 |
if (isset($ints[$c0])) {
|
| 181 |
$temp = (int)$temp;
|
| 182 |
}
|
| 183 |
$a[$c0][$c1] = $temp;
|
| 184 |
}
|
| 185 |
_gmap_compress_array($a[$c0]);
|
| 186 |
}
|
| 187 |
for ($c0--;$c0>=0;$c0--) {
|
| 188 |
if ($a[$c0] === array($nulls[$c0])) {
|
| 189 |
unset($a[$c0]);
|
| 190 |
}
|
| 191 |
else {
|
| 192 |
break;
|
| 193 |
}
|
| 194 |
}
|
| 195 |
return $a;
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* Remove trailing duplicates from an array.
|
| 200 |
*/
|
| 201 |
function _gmap_compress_array(&$arr) {
|
| 202 |
if (empty($arr)) {
|
| 203 |
return;
|
| 204 |
}
|
| 205 |
$c = count($arr) - 1;
|
| 206 |
// Walk backwards and unset duplicates...
|
| 207 |
for ($cval = $arr[$c]; $c>0; $c--) {
|
| 208 |
if ($arr[$c-1]==$cval) {
|
| 209 |
unset($arr[$c]);
|
| 210 |
}
|
| 211 |
else {
|
| 212 |
// .. until we hit a different number.
|
| 213 |
break;
|
| 214 |
}
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
function _gmap_get_marker_titles() {
|
| 219 |
$markerdir = variable_get('gmap_markerfiles', drupal_get_path('module', 'gmap') .'/markers');
|
| 220 |
|
| 221 |
// The following routines are designed to be easy to comprehend, not fast.
|
| 222 |
// This whole process gets cached.
|
| 223 |
|
| 224 |
// Get the ini files.
|
| 225 |
$inifiles = file_scan_directory($markerdir, '.*\.ini$');
|
| 226 |
// Parse the ini files and store by path
|
| 227 |
$inis = array();
|
| 228 |
foreach ($inifiles as $file) {
|
| 229 |
$data = parse_ini_file($file->filename, TRUE);
|
| 230 |
if (isset($data['defaults'])) {
|
| 231 |
// Ignore defaults
|
| 232 |
unset($data['defaults']);
|
| 233 |
}
|
| 234 |
foreach ($data as $k => $v) {
|
| 235 |
if (strpos($k, '.') !== FALSE) {
|
| 236 |
// Ignore files
|
| 237 |
unset($data[$k]);
|
| 238 |
}
|
| 239 |
}
|
| 240 |
$inis[] = $data;
|
| 241 |
}
|
| 242 |
unset($inifiles);
|
| 243 |
|
| 244 |
$titles = array();
|
| 245 |
foreach ($inis as $ini => $inidata) {
|
| 246 |
foreach ($inidata as $k => $v) {
|
| 247 |
$titles[$k] = $inis[$ini][$k]['name'];
|
| 248 |
}
|
| 249 |
}
|
| 250 |
return $titles;
|
| 251 |
}
|