xref: /plugin/openlayersmap/syntax/olmap.php (revision a57aaf07a34c563b41d065a48840d44bf9e56b1e)
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 recognized 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() {
58			return 'substition';
59		}
60
61		/**
62		 * Defines how this syntax is handled regarding paragraphs.
63		 * @Override
64		 */
65		function getPType() {
66			return 'block';
67		}
68
69		/**
70		 * Returns a number used to determine in which order modes are added.
71		 * @Override
72		 */
73		function getSort() {
74			return 901;
75		}
76
77		/**
78		 * This function is inherited from Doku_Parser_Mode.
79		 * Here is the place to register the regular expressions needed
80		 * to match your syntax.
81		 * @Override
82		 */
83		function connectTo($mode) {
84			$this->Lexer->addSpecialPattern('<olmap ?[^>\n]*>.*?</olmap>', $mode, 'plugin_openlayersmap_olmap');
85		}
86
87		/**
88		 * handle each olmap tag. prepare the matched syntax for use in the renderer.
89		 * @Override
90		 */
91		function handle($match, $state, $pos, &$handler) {
92			// break matched cdata into its components
93			list ($str_params, $str_points) = explode('>', substr($match, 7, -9), 2);
94			// get the lat/lon for adding them to the metadata (used by geotag)
95			preg_match('(lat[:|=]\"\d*\.\d*\")',$match,$mainLat);
96			preg_match('(lon[:|=]\"\d*\.\d*\")',$match,$mainLon);
97			$mainLat=substr($mainLat[0],5,-1);
98			$mainLon=substr($mainLon[0],5,-1);
99
100			$gmap = $this->_extract_params($str_params);
101			$overlay = $this->_extract_points($str_points);
102
103			$mapid = $gmap['id'];
104
105			// determine width and height (inline styles) for the map image
106			if ($gmap['width'] || $gmap['height']) {
107				$style = $gmap['width'] ? 'width: ' . $gmap['width'] . ";" : "";
108				$style .= $gmap['height'] ? 'height: ' . $gmap['height'] . ";" : "";
109				$style = "style='$style'";
110			} else {
111				$style = '';
112			}
113
114			// unset gmap values for width and height - they don't go into javascript
115			unset ($gmap['width'], $gmap['height']);
116
117			// create a javascript parameter string for the map
118			$param = '';
119			foreach ($gmap as $key => $val) {
120				$param .= is_numeric($val) ? "$key: $val, " : "$key: '" . hsc($val) . "', ";
121			}
122			if (!empty ($param)) {
123				$param = substr($param, 0, -2);
124			}
125			unset ($gmap['id']);
126
127			// create a javascript serialisation of the point data
128			$poi = '';
129			$poitable='';
130			$rowId=0;
131			if (!empty ($overlay)) {
132				foreach ($overlay as $data) {
133					list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
134					$rowId++;
135					$poi .= ", {lat: $lat, lon: $lon, txt: '$text', angle: $angle, opacity: $opacity, img: '$img', rowId: $rowId}";
136					$poitable .='<tr>'."\n".'<td class="rowId">'.$rowId.'</td>
137							<td class="icon"><img src="/lib/plugins/openlayersmap/icons/'.$img.'" alt="icon"></td>
138							<td class="lat" title="'.$this->getLang('olmapPOIlatTitle').'">'.$lat.'</td>
139							<td class="lon" title="'.$this->getLang('olmapPOIlonTitle').'">'.$lon.'</td>
140							<td class="txt">'.$text.'</td>'."\n".'</tr>';
141				}
142				$poi = substr($poi, 2);
143			}
144			$js .= "createMap({" . $param . " },[$poi]);";
145
146			return array($mapid,$style,$js,$mainLat,$mainLon,$poitable,$gmap['summary']);
147		}
148
149		/**
150		 * render html tag/output. render the content.
151		 * @Override
152		 */
153		function render($mode, &$renderer, $data) {
154			static $initialised = false; // set to true after script initialisation
155			list ($mapid, $style, $param, $mainLat, $mainLon, $poitable, $poitabledesc) = $data;
156
157			if ($mode == 'xhtml') {
158				$olscript = '';
159				$olEnable = false;
160				$gscript = '';
161				$gEnable = false;
162				$vscript = '';
163				$vEnable = false;
164				$yscript = '';
165				$yEnable = false;
166
167				$scriptEnable = '';
168
169				if (!$initialised) {
170					$initialised = true;
171					// render necessary script tags
172					$gscript = $this->getConf('googleScriptUrl');
173					$gscript = $gscript ? '<script type="text/javascript" src="' . $gscript . '"></script>' : "";
174
175					$vscript = $this->getConf('veScriptUrl');
176					$vscript = $vscript ? '<script type="text/javascript" src="' . $vscript . '"></script>' : "";
177
178					$yscript = $this->getConf('yahooScriptUrl');
179					$yscript = $yscript ? '<script type="text/javascript" src="' . $yscript . '"></script>' : "";
180
181					$olscript = $this->getConf('olScriptUrl');
182					$olscript = $olscript ? '<script type="text/javascript" src="' . $olscript . '"></script>' : "";
183					$olscript = str_replace('DOKU_PLUGIN', DOKU_PLUGIN, $olscript);
184
185					$scriptEnable = '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n";
186					$scriptEnable .= $olscript ? 'olEnable = true;' : 'olEnable = false;';
187					$scriptEnable .= $yscript ? ' yEnable = true;' : ' yEnable = false;';
188					$scriptEnable .= $vscript ? ' veEnable = true;' : ' veEnable = false;';
189					$scriptEnable .= $gscript ? ' gEnable = true;' : ' gEnable = false;';
190					$scriptEnable .= "\n" . '//--><!]]>' . "\n" . '</script>';
191				}
192				$renderer->doc .= "
193				$olscript
194				$gscript
195				$vscript
196				$yscript
197				$scriptEnable
198			    <div id='olContainer' class='olContainer'>
199			        <div id='$mapid-olToolbar' class='olToolbar'></div>
200			        <div style='clear:both;'></div>
201			        <div id='$mapid' $style ></div>
202			        <div id='$mapid-olStatusBar' class='olStatusBarContainer'>
203			            <div id='$mapid-statusbar-scale' class='olStatusBar olStatusBarScale'>scale</div>
204			            <div id='$mapid-statusbar-link' class='olStatusBar olStatusBarPermalink'>
205			                <a href='' id='$mapid-statusbar-link-ref'>map link</a>
206			            </div>
207			            <div id='$mapid-statusbar-mouseposition' class='olStatusBar olStatusBarMouseposition'></div>
208			            <div id='$mapid-statusbar-projection' class='olStatusBar olStatusBarProjection'>proj</div>
209			            <div id='$mapid-statusbar-text' class='olStatusBar olStatusBarText'>txt</div>
210			        </div>
211			    </div>
212			    <p>&nbsp;</p>
213			    <script type='text/javascript'><!--//--><![CDATA[//><!--
214			    var $mapid = $param
215			   //--><!]]></script>";
216
217				// render a (hidden) table of the POI for the print and a11y presentation
218				$renderer->doc .= '
219 	<table class="olPOItable inline" id="'.$mapid.'-table" summary="'.$poitabledesc.'" title="'.$this->getLang('olmapPOItitle').'">
220		<caption class="olPOITblCaption">'.$this->getLang('olmapPOItitle').'</caption>
221		<thead class="olPOITblHeader">
222			<tr>
223				<th class="rowId" scope="col">id</th>
224				<th class="icon" scope="col">'.$this->getLang('olmapPOIicon').'</th>
225				<th class="lat" scope="col" title="'.$this->getLang('olmapPOIlatTitle').'">'.$this->getLang('olmapPOIlat').'</th>
226				<th class="lon" scope="col" title="'.$this->getLang('olmapPOIlonTitle').'">'.$this->getLang('olmapPOIlon').'</th>
227				<th class="txt" scope="col">'.$this->getLang('olmapPOItxt').'</th>
228			</tr>
229		</thead>
230		<tbody class="olPOITblBody">'.$poitable.'</tbody>
231		<tfoot class="olPOITblFooter"><tr><td colspan="5">'.$poitabledesc.'</td></tr></tfoot>
232	</table>';
233				//TODO no tfoot when $poitabledesc is empty
234
235			} elseif ($mode == 'metadata') {
236				// render metadata if available
237				if (!(($this->dflt['lat']==$mainLat)||($thisdflt['lon']==$mainLon))){
238					// unless they are the default
239					$renderer->meta['geo']['lat'] = $mainLat;
240					$renderer->meta['geo']['lon'] = $mainLon;
241				}
242				return true;
243			}
244			return false;
245		}
246
247		/**
248		 * extract parameters for the map from the parameter string
249		 *
250		 * @param   string    $str_params   string of key="value" pairs
251		 * @return  array                   associative array of parameters key=>value
252		 */
253		function _extract_params($str_params) {
254			$param = array ();
255			preg_match_all('/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER);
256			// parse match for instructions, break into key value pairs
257			$gmap = $this->dflt;
258			foreach ($param as $kvpair) {
259				list ($match, $key, $val) = $kvpair;
260				$key = strtolower($key);
261				if (isset ($gmap[$key])){
262					if ($key == 'summary'){
263						// preserve case for summary field
264						$gmap[$key] = $val;
265					}else {
266						$gmap[$key] = strtolower($val);
267					}
268				}
269			}
270			return $gmap;
271		}
272
273		/**
274		 * extract overlay points for the map from the wiki syntax data
275		 *
276		 * @param   string    $str_points   multi-line string of lat,lon,text triplets
277		 * @return  array                   multi-dimensional array of lat,lon,text triplets
278		 */
279		function _extract_points($str_points) {
280			$point = array ();
281			//preg_match_all('/^([+-]?[0-9].*?),\s*([+-]?[0-9].*?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER);
282			/*
283			group 1: ([+-]?[0-9]+(?:\.[0-9]*)?)
284			group 2: ([+-]?[0-9]+(?:\.[0-9]*)?)
285			group 3: (.*?)
286			group 4: (.*?)
287			group 5: (.*?)
288			group 6: (.*)
289			*/
290			preg_match_all('/^([+-]?[0-9]+(?:\.[0-9]*)?),\s*([+-]?[0-9]+(?:\.[0-9]*)?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER);
291			// create poi array
292			$overlay = array ();
293			foreach ($point as $pt) {
294				list ($match, $lat, $lon, $angle, $opacity, $img, $text) = $pt;
295				$lat = is_numeric($lat) ? $lat : 0;
296				$lon = is_numeric($lon) ? $lon : 0;
297				$angle = is_numeric($angle) ? $angle : 0;
298				$opacity = is_numeric($opacity) ? $opacity : 0.8;
299				$img = trim($img);
300				// TODO validate & set up default img?
301				$text = addslashes(str_replace("\n", "", p_render("xhtml", p_get_instructions($text), $info)));
302				$overlay[] = array($lat, $lon, $text, $angle, $opacity, $img);
303			}
304			return $overlay;
305		}
306}