1<?php 2/* 3 * Copyright (c) 2008-2011 Mark C. Prins <mc.prins@gmail.com> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18/** 19 * Plugin OL Maps: Allow Display of a OpenLayers Map in a wiki page. 20 * 21 * @author Mark Prins 22 */ 23 24if (!defined('DOKU_INC')) 25define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/'); 26if (!defined('DOKU_PLUGIN')) 27define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 28require_once (DOKU_PLUGIN . 'syntax.php'); 29 30/** 31 * All DokuWiki plugins to extend the parser/rendering mechanism 32 * need to inherit from this class 33 */ 34class syntax_plugin_openlayersmap_olmap extends DokuWiki_Syntax_Plugin { 35 /** defaults of the known attributes of the olmap tag. */ 36 private $dflt = array ( 37 'id' => 'olmap', 38 'width' => '550px', 39 'height' => '450px', 40 'lat' => 50.0, 41 'lon' => 5.1, 42 'zoom' => 12, 43 'toolbar' => true, 44 'statusbar' => true, 45 'controls' => true, 46 'poihoverstyle' => false, 47 'baselyr'=>'OpenStreetMap', 48 'gpxfile' => '', 49 'kmlfile' => '', 50 'summary'=>'' 51 ); 52 53 /** 54 * Return the type of syntax this plugin defines. 55 * @Override 56 */ 57 function getType() {return 'substition';} 58 59 /** 60 * Defines how this syntax is handled regarding paragraphs. 61 * @Override 62 */ 63 function getPType() {return 'block';} 64 65 /** 66 * Returns a number used to determine in which order modes are added. 67 * @Override 68 */ 69 function getSort() {return 901;} 70 71 /** 72 * This function is inherited from Doku_Parser_Mode. 73 * Here is the place to register the regular expressions needed 74 * to match your syntax. 75 * @Override 76 */ 77 function connectTo($mode) { 78 $this->Lexer->addSpecialPattern('<olmap ?[^>\n]*>.*?</olmap>', $mode, 'plugin_openlayersmap_olmap'); 79 } 80 81 /** 82 * handle each olmap tag. prepare the matched syntax for use in the renderer. 83 * @Override 84 */ 85 function handle($match, $state, $pos, &$handler) { 86 // break matched cdata into its components 87 list ($str_params, $str_points) = explode('>', substr($match, 7, -9), 2); 88 // get the lat/lon for adding them to the metadata (used by geotag) 89 preg_match('(lat[:|=]\"\d*\.\d*\")',$match,$mainLat); 90 preg_match('(lon[:|=]\"\d*\.\d*\")',$match,$mainLon); 91 $mainLat=substr($mainLat[0],5,-1); 92 $mainLon=substr($mainLon[0],5,-1); 93 94 $gmap = $this->_extract_params($str_params); 95 $overlay = $this->_extract_points($str_points); 96 97 $imgUrl = "{{"; 98 // use mapquest open for now 99 $imgUrl .= $this->_getMapQuest($gmap,$overlay); 100 // dw specific params 101 $imgUrl .="&.png?".$gmap['width']."x".$gmap['height']; 102 $imgUrl .= "&nolink"; 103 $imgUrl .= "|".$gmap['summary']." }}"; 104 105 // remove 'px' 106 $imgUrl = str_replace("px", "",$imgUrl); 107 108 $imgUrl=p_render("xhtml", p_get_instructions($imgUrl), $info); 109 110 $mapid = $gmap['id']; 111 112 // determine width and height (inline styles) for the map image 113 if ($gmap['width'] || $gmap['height']) { 114 $style = $gmap['width'] ? 'width: ' . $gmap['width'] . ";" : ""; 115 $style .= $gmap['height'] ? 'height: ' . $gmap['height'] . ";" : ""; 116 $style = "style='$style'"; 117 } else { 118 $style = ''; 119 } 120 121 // unset gmap values for width and height - they don't go into javascript 122 unset ($gmap['width'], $gmap['height']); 123 124 // create a javascript parameter string for the map 125 $param = ''; 126 foreach ($gmap as $key => $val) { 127 $param .= is_numeric($val) ? "$key: $val, " : "$key: '" . hsc($val) . "', "; 128 } 129 if (!empty ($param)) { 130 $param = substr($param, 0, -2); 131 } 132 unset ($gmap['id']); 133 134 // create a javascript serialisation of the point data 135 $poi = ''; 136 $poitable=''; 137 $rowId=0; 138 if (!empty ($overlay)) { 139 foreach ($overlay as $data) { 140 list ($lat, $lon, $text, $angle, $opacity, $img) = $data; 141 $rowId++; 142 $poi .= ", {lat: $lat, lon: $lon, txt: '$text', angle: $angle, opacity: $opacity, img: '$img', rowId: $rowId}"; 143 $poitable .='<tr>'."\n".'<td class="rowId">'.$rowId.'</td> 144 <td class="icon"><img src="/lib/plugins/openlayersmap/icons/'.$img.'" alt="icon"></td> 145 <td class="lat" title="'.$this->getLang('olmapPOIlatTitle').'">'.$lat.'</td> 146 <td class="lon" title="'.$this->getLang('olmapPOIlonTitle').'">'.$lon.'</td> 147 <td class="txt">'.$text.'</td>'."\n".'</tr>'; 148 } 149 $poi = substr($poi, 2); 150 } 151 $js .= "createMap({" . $param . " },[$poi]);"; 152 153 return array($mapid,$style,$js,$mainLat,$mainLon,$poitable,$gmap['summary'],$imgUrl); 154 } 155 156 /** 157 * render html tag/output. render the content. 158 * @Override 159 */ 160 function render($mode, &$renderer, $data) { 161 static $initialised = false; // set to true after script initialisation 162 list ($mapid, $style, $param, $mainLat, $mainLon, $poitable, $poitabledesc, $staticImgUrl) = $data; 163 164 if ($mode == 'xhtml') { 165 $olscript = ''; 166 $olEnable = false; 167 $gscript = ''; 168 $gEnable = false; 169 $vscript = ''; 170 $vEnable = false; 171 $yscript = ''; 172 $yEnable = false; 173 174 $scriptEnable = ''; 175 176 if (!$initialised) { 177 $initialised = true; 178 // render necessary script tags 179 $gscript = $this->getConf('googleScriptUrl'); 180 $gscript = $gscript ? '<script type="text/javascript" src="' . $gscript . '"></script>' : ""; 181 182 $vscript = $this->getConf('veScriptUrl'); 183 $vscript = $vscript ? '<script type="text/javascript" src="' . $vscript . '"></script>' : ""; 184 185 $yscript = $this->getConf('yahooScriptUrl'); 186 $yscript = $yscript ? '<script type="text/javascript" src="' . $yscript . '"></script>' : ""; 187 188 $olscript = $this->getConf('olScriptUrl'); 189 $olscript = $olscript ? '<script type="text/javascript" src="' . $olscript . '"></script>' : ""; 190 $olscript = str_replace('DOKU_PLUGIN', DOKU_PLUGIN, $olscript); 191 192 $scriptEnable = '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n"; 193 $scriptEnable .= $olscript ? 'olEnable = true;' : 'olEnable = false;'; 194 $scriptEnable .= $yscript ? ' yEnable = true;' : ' yEnable = false;'; 195 $scriptEnable .= $vscript ? ' veEnable = true;' : ' veEnable = false;'; 196 $scriptEnable .= $gscript ? ' gEnable = true;' : ' gEnable = false;'; 197 $scriptEnable .= "\n" . '//--><!]]>' . "\n" . '</script>'; 198 } 199 $renderer->doc .= " 200 $olscript 201 $gscript 202 $vscript 203 $yscript 204 $scriptEnable 205 <div id='olContainer' class='olContainer'> 206 <span id='$mapid-static' class='olStaticMap'>$staticImgUrl</span> 207 <div id='$mapid-olToolbar' class='olToolbar'></div> 208 <div style='clear:both;'></div> 209 <div id='$mapid' $style ></div> 210 <div id='$mapid-olStatusBar' class='olStatusBarContainer'> 211 <div id='$mapid-statusbar-scale' class='olStatusBar olStatusBarScale'>scale</div> 212 <div id='$mapid-statusbar-link' class='olStatusBar olStatusBarPermalink'> 213 <a href='' id='$mapid-statusbar-link-ref'>map link</a> 214 </div> 215 <div id='$mapid-statusbar-mouseposition' class='olStatusBar olStatusBarMouseposition'></div> 216 <div id='$mapid-statusbar-projection' class='olStatusBar olStatusBarProjection'>proj</div> 217 <div id='$mapid-statusbar-text' class='olStatusBar olStatusBarText'>txt</div> 218 </div> 219 </div> 220 <p> </p> 221 <script type='text/javascript'><!--//--><![CDATA[//><!-- 222 var $mapid = $param 223 //--><!]]></script>"; 224 225 // render a (hidden) table of the POI for the print and a11y presentation 226 $renderer->doc .= ' 227 <table class="olPOItable inline" id="'.$mapid.'-table" summary="'.$poitabledesc.'" title="'.$this->getLang('olmapPOItitle').'"> 228 <caption class="olPOITblCaption">'.$this->getLang('olmapPOItitle').'</caption> 229 <thead class="olPOITblHeader"> 230 <tr> 231 <th class="rowId" scope="col">id</th> 232 <th class="icon" scope="col">'.$this->getLang('olmapPOIicon').'</th> 233 <th class="lat" scope="col" title="'.$this->getLang('olmapPOIlatTitle').'">'.$this->getLang('olmapPOIlat').'</th> 234 <th class="lon" scope="col" title="'.$this->getLang('olmapPOIlonTitle').'">'.$this->getLang('olmapPOIlon').'</th> 235 <th class="txt" scope="col">'.$this->getLang('olmapPOItxt').'</th> 236 </tr> 237 </thead> 238 <tbody class="olPOITblBody">'.$poitable.'</tbody> 239 <tfoot class="olPOITblFooter"><tr><td colspan="5">'.$poitabledesc.'</td></tr></tfoot> 240 </table>'; 241 //TODO no tfoot when $poitabledesc is empty 242 243 } elseif ($mode == 'metadata') { 244 // render metadata if available 245 if (!(($this->dflt['lat']==$mainLat)||($thisdflt['lon']==$mainLon))){ 246 // unless they are the default 247 $renderer->meta['geo']['lat'] = $mainLat; 248 $renderer->meta['geo']['lon'] = $mainLon; 249 } 250 return true; 251 } 252 return false; 253 } 254 255 /** 256 * extract parameters for the map from the parameter string 257 * 258 * @param string $str_params string of key="value" pairs 259 * @return array associative array of parameters key=>value 260 */ 261 private function _extract_params($str_params) { 262 $param = array (); 263 preg_match_all('/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER); 264 // parse match for instructions, break into key value pairs 265 $gmap = $this->dflt; 266 foreach ($param as $kvpair) { 267 list ($match, $key, $val) = $kvpair; 268 $key = strtolower($key); 269 if (isset ($gmap[$key])){ 270 if ($key == 'summary'){ 271 // preserve case for summary field 272 $gmap[$key] = $val; 273 }else { 274 $gmap[$key] = strtolower($val); 275 } 276 } 277 } 278 return $gmap; 279 } 280 281 /** 282 * extract overlay points for the map from the wiki syntax data 283 * 284 * @param string $str_points multi-line string of lat,lon,text triplets 285 * @return array multi-dimensional array of lat,lon,text triplets 286 */ 287 private function _extract_points($str_points) { 288 $point = array (); 289 //preg_match_all('/^([+-]?[0-9].*?),\s*([+-]?[0-9].*?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER); 290 /* 291 group 1: ([+-]?[0-9]+(?:\.[0-9]*)?) 292 group 2: ([+-]?[0-9]+(?:\.[0-9]*)?) 293 group 3: (.*?) 294 group 4: (.*?) 295 group 5: (.*?) 296 group 6: (.*) 297 */ 298 preg_match_all('/^([+-]?[0-9]+(?:\.[0-9]*)?),\s*([+-]?[0-9]+(?:\.[0-9]*)?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER); 299 // create poi array 300 $overlay = array (); 301 foreach ($point as $pt) { 302 list ($match, $lat, $lon, $angle, $opacity, $img, $text) = $pt; 303 $lat = is_numeric($lat) ? $lat : 0; 304 $lon = is_numeric($lon) ? $lon : 0; 305 $angle = is_numeric($angle) ? $angle : 0; 306 $opacity = is_numeric($opacity) ? $opacity : 0.8; 307 $img = trim($img); 308 // TODO validate & set up default img? 309 $text = addslashes(str_replace("\n", "", p_render("xhtml", p_get_instructions($text), $info))); 310 $overlay[] = array($lat, $lon, $text, $angle, $opacity, $img); 311 } 312 return $overlay; 313 } 314 315 /** 316 * Create a MapQuest static map API image url. 317 * @param array $gmap 318 * @param array $overlay 319 */ 320 private function _getMapQuest($gmap,$overlay) { 321 $imgUrl = "http://open.mapquestapi.com/staticmap/v3/getmap?"; 322 $imgUrl .= "size=".$gmap['width'].",".$gmap['height']; 323 $imgUrl .= "¢er=".$gmap['lat'].",".$gmap['lon']; 324 325 if ($gmap['zoom']>16) { 326 $imgUrl .= "&zoom=16"; 327 } else { 328 $imgUrl .= "&zoom=".$gmap['zoom']; 329 } 330 // TODO mapquest allows using one image url with a multiplier $NUMBER eg: 331 // $NUMBER = 2 332 // $imgUrl .= DOKU_URL."/".DOKU_PLUGIN."/".getPluginName()."/icons/".$img.",$NUMBER,C,".$lat1.",".$lon1.",0,0,0,0,C,".$lat2.",".$lon2.",0,0,0,0"; 333 if (!empty ($overlay)) { 334 $imgUrl .= "&xis="; 335 foreach ($overlay as $data) { 336 list ($lat, $lon, $text, $angle, $opacity, $img) = $data; 337 $imgUrl .= DOKU_URL."lib/plugins/openlayersmap/icons/".$img.",1,C,".$lat.",".$lon.",0,0,0,0,"; 338 } 339 $imgUrl = substr($imgUrl,0,-1); 340 $imgUrl .= "&imageType=png&type=map"; 341 } 342 return $imgUrl; 343 } 344}