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