1<?php 2/* 3 * Copyright (c) 2011-2013 Mark C. Prins <mprins@users.sf.net> 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 19if (!defined('DOKU_INC')) die(); 20 21if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 22 23require_once(DOKU_PLUGIN.'syntax.php'); 24/** 25 * DokuWiki Plugin geotag (Syntax Component). 26 * Handles the rendering part of the geotag plugin. 27 * 28 * @license BSD license 29 * @author Mark C. Prins <mprins@users.sf.net> 30 */ 31class syntax_plugin_geotag_geotag extends DokuWiki_Syntax_Plugin { 32 /** 33 * (non-PHPdoc) 34 * @see DokuWiki_Syntax_Plugin::getType() 35 */ 36 public function getType() { return 'substition'; } 37 38 /** 39 * (non-PHPdoc) 40 * @see DokuWiki_Syntax_Plugin::getPType() 41 */ 42 public function getPType() { return 'block'; } 43 44 /** 45 * (non-PHPdoc) 46 * @see Doku_Parser_Mode::getSort() 47 */ 48 public function getSort() { return 305; } 49 50 /** 51 * (non-PHPdoc) 52 * @see Doku_Parser_Mode::connectTo() 53 */ 54 public function connectTo($mode) { 55 $this->Lexer->addSpecialPattern('\{\{geotag>.*?\}\}',$mode,'plugin_geotag_geotag'); 56 } 57 58 /** 59 * (non-PHPdoc) 60 * @see DokuWiki_Syntax_Plugin::handle() 61 */ 62 public function handle($match, $state, $pos, &$handler){ 63 $tags = trim(substr($match, 9, -2)); 64 // parse geotag content 65 preg_match("(lat[:|=]\d*\.\d*)",$tags,$lat); 66 preg_match("(lon[:|=]\d*\.\d*)",$tags,$lon); 67 preg_match("(region[:|=][a-zA-Z\s\w'-]*)",$tags,$region); 68 preg_match("(placename[:|=][a-zA-Z\s\w'-]*)",$tags,$placename); 69 preg_match("(country[:|=][a-zA-Z\s\w'-]*)",$tags,$country); 70 preg_match("(hide|unhide)",$tags,$hide); 71 72 $showlocation=$this->getConf('geotag_location_prefix'); 73 if ($this->getConf('geotag_showlocation')) { 74 $showlocation=trim(substr($placename[0],10)); 75 if (strlen($showlocation)>0) { 76 $showlocation .=': '; 77 } 78 } 79 // read config for system setting 80 $style=''; 81 if ($this->getConf('geotag_hide')) { 82 $style=' style="display: none;"'; 83 } 84 // override config for the current tag 85 if (trim($hide[0])=='hide'){ 86 $style=' style="display: none;"'; 87 } elseif(trim($hide[0])=='unhide'){ 88 $style=''; 89 } 90 91 $data = array( 92 trim(substr($lat[0],4)), 93 trim(substr($lon[0],4)), 94 $this->_geohash(substr($lat[0],4), substr($lon[0],4)), 95 trim(substr($region[0],7)), 96 trim(substr($placename[0],10)), 97 trim(substr($country[0],8)), 98 $showlocation, 99 $style, 100 ); 101 return $data; 102 } 103 104 /** 105 * (non-PHPdoc) 106 * @see DokuWiki_Syntax_Plugin::render() 107 */ 108 public function render($mode, &$renderer, $data) { 109 if ($data === false) return false; 110 list ($lat, $lon, $geohash, $region, $placename, $country, $showlocation, $style) = $data; 111 if ($mode == 'xhtml') { 112 if ($this->getConf('geotag_prevent_microformat_render')) { 113 // config says no microformat rendering 114 return true; 115 } 116 // render geotag microformat 117 $renderer->doc .= '<span class="geotagPrint">'. 118 $this->getLang('geotag_desc').'</span><div class="geo"'. 119 $style.' title="'.$this->getLang('geotag_desc').$placename.'">'. 120 $showlocation.'<span class="latitude">'. 121 $lat.'</span>;<span class="longitude">'. 122 $lon.'</span></div>'.DOKU_LF; 123 return true; 124 } elseif ($mode == 'metadata') { 125 // render metadata (action plugin will put it in the page head) 126 $renderer->meta['geo']['lat'] = $lat; 127 $renderer->meta['geo']['lon'] = $lon; 128 $renderer->meta['geo']['placename'] = $placename; 129 $renderer->meta['geo']['region'] = $region; 130 $renderer->meta['geo']['country'] = $country; 131 $renderer->meta['geo']['geohash'] = $geohash; 132 return true; 133 } elseif ($mode=='odt'){ 134 $renderer->p_open(); 135 $renderer->_odtAddImage(DOKU_PLUGIN.'geotag/images/geotag.png',null, null, 'left',''); 136 $renderer->doc .= '<text:span>'.$this->getLang('geotag_desc').' '.$placename.': </text:span>'; 137 $renderer->monospace_open(); 138 $renderer->doc .= $lat.';'.$lon; 139 $renderer->monospace_close(); 140 $renderer->p_close(); 141 return true; 142 } 143 return false; 144 } 145 146 /** 147 * Calculate the geohash for this lat/lon pair. 148 * 149 * @param float $lat 150 * @param float $lon 151 */ 152 private function _geohash($lat, $lon){ 153 if (!$geophp = &plugin_load('helper', 'geophp')){ 154 dbglog($geophp,'syntax_plugin_geotag_geotag::_geohash: geophp plugin is not available.'); 155 return ""; 156 } 157 $_lat = floatval($lat); 158 $_lon = floatval($lon); 159 $geometry = new Point($_lon,$_lat); 160 //dbglog($geometry, 'geometry to calculate geohash from..'); 161 return $geometry->out('geohash'); 162 } 163} 164