xref: /plugin/openlayersmap/syntax/olmap.php (revision 45c50327270724a4b1bef398d9485bdb3d46debb)
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><td class="icon"><img src="/lib/plugins/openlayersmap/icons/'.$img.'" alt="icon"></td>
137							<td class="lat" title="latitude in decimal degrees">'.$lat.'</td>
138							<td class="lon" title="longitude in decimal degrees">'.$lon.'</td>
139							<td class="desc">'.$text.'</td>'."\n".'</tr>';
140				}
141				$poi = substr($poi, 2);
142			}
143			$js .= "createMap({" . $param . " },[$poi]);";
144
145			return array($mapid,$style,$js,$mainLat,$mainLon,$poitable,$gmap['summary']);
146		}
147
148		/**
149		 * render html tag/output. render the content.
150		 * @Override
151		 */
152		function render($mode, &$renderer, $data) {
153			static $initialised = false; // set to true after script initialisation
154			list ($mapid, $style, $param, $mainLat, $mainLon, $poitable, $poitabledesc) = $data;
155
156			if ($mode == 'xhtml') {
157				$olscript = '';
158				$olEnable = false;
159				$gscript = '';
160				$gEnable = false;
161				$vscript = '';
162				$vEnable = false;
163				$yscript = '';
164				$yEnable = false;
165
166				$scriptEnable = '';
167
168				if (!$initialised) {
169					$initialised = true;
170					// render necessary script tags
171					$gscript = $this->getConf('googleScriptUrl');
172					$gscript = $gscript ? '<script type="text/javascript" src="' . $gscript . '"></script>' : "";
173
174					$vscript = $this->getConf('veScriptUrl');
175					$vscript = $vscript ? '<script type="text/javascript" src="' . $vscript . '"></script>' : "";
176
177					$yscript = $this->getConf('yahooScriptUrl');
178					$yscript = $yscript ? '<script type="text/javascript" src="' . $yscript . '"></script>' : "";
179
180					$olscript = $this->getConf('olScriptUrl');
181					$olscript = $olscript ? '<script type="text/javascript" src="' . $olscript . '"></script>' : "";
182					$olscript = str_replace('DOKU_PLUGIN', DOKU_PLUGIN, $olscript);
183
184					$scriptEnable = '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n";
185					$scriptEnable .= $olscript ? 'olEnable = true;' : 'olEnable = false;';
186					$scriptEnable .= $yscript ? ' yEnable = true;' : ' yEnable = false;';
187					$scriptEnable .= $vscript ? ' veEnable = true;' : ' veEnable = false;';
188					$scriptEnable .= $gscript ? ' gEnable = true;' : ' gEnable = false;';
189					$scriptEnable .= "\n" . '//--><!]]>' . "\n" . '</script>';
190				}
191				$renderer->doc .= "
192				$olscript
193				$gscript
194				$vscript
195				$yscript
196				$scriptEnable
197			    <div id='olContainer' class='olContainer'>
198			        <div id='$mapid-olToolbar' class='olToolbar'></div>
199			        <div style='clear:both;'></div>
200			        <div id='$mapid' $style ></div>
201			        <div id='$mapid-olStatusBar' class='olStatusBarContainer'>
202			            <div id='$mapid-statusbar-scale' class='olStatusBar olStatusBarScale'>scale</div>
203			            <div id='$mapid-statusbar-link' class='olStatusBar olStatusBarPermalink'>
204			                <a href='' id='$mapid-statusbar-link-ref'>map link</a>
205			            </div>
206			            <div id='$mapid-statusbar-mouseposition' class='olStatusBar olStatusBarMouseposition'></div>
207			            <div id='$mapid-statusbar-projection' class='olStatusBar olStatusBarProjection'>proj</div>
208			            <div id='$mapid-statusbar-text' class='olStatusBar olStatusBarText'>txt</div>
209			        </div>
210			    </div>
211			    <p>&nbsp;</p>
212			    <script type='text/javascript'><!--//--><![CDATA[//><!--
213			    var $mapid = $param
214			   //--><!]]></script>";
215
216				// render a (hidden) table of the POI for the print and a11y presentation
217				$renderer->doc .= '
218 	<table class="olPOItable" id="'.$mapid.'-table" summary="'.$poitabledesc.'" title="'.$this->getLang('olmapPOItitle').'">
219		<caption class="caption">'.$this->getLang('olmapPOItitle').'</caption>
220		<thead class="olPOITblHeader">
221			<tr>
222				<th class="rowId" scope="col">id</th>
223				<th class="icon" scope="col">icon</th>
224				<th class="lat" scope="col">latitude</th>
225				<th class="lon" scope="col">longitude</th>
226				<th class="desc" scope="col">description</th>
227			</tr>
228		</thead>
229		<tbody class="olPOITblBody">
230			'.$poitable.'
231		</tbody>
232		<tfoot class="olPOITblFooter"><tr><td colspan="5">'.$poitabledesc.'</td></tr></tfoot>
233	</table>';
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}