1<?php 2/* 3 * Copyright (c) 2017-2020 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 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps 18 */ 19 20/** 21 * adds a AGS layer to your map. 22 */ 23class syntax_plugin_openlayersmapoverlays_agslayer extends DokuWiki_Syntax_Plugin { 24 private $dflt = array( 25 'id' => 'olmap', 26 'name' => '', 27 'url' => '', 28 'opacity' => 0.8, 29 'attribution' => '', 30 'visible' => false, 31 'layers' => '', 32 'format' => 'png', 33 'transparent' => 'true' 34 ); 35 36 /** 37 * (non-PHPdoc) 38 * 39 * @see DokuWiki_Syntax_Plugin::getPType() 40 */ 41 public function getPType(): string { 42 return 'block'; 43 } 44 45 /** 46 * (non-PHPdoc) 47 * 48 * @see DokuWiki_Syntax_Plugin::getType() 49 */ 50 public function getType(): string { 51 // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs'; 52 return 'baseonly'; 53 } 54 55 /** 56 * (non-PHPdoc) 57 * 58 * @see Doku_Parser_Mode::getSort() 59 */ 60 public function getSort(): int { 61 return 904; 62 } 63 64 /** 65 * Connect to our special pattern. 66 * 67 * @see Doku_Parser_Mode::connectTo() 68 */ 69 public function connectTo($mode): void { 70 // look for: <olmap_agslayer id="olmap" name="cloud" 71 // url="http://geoservices2.wallonie.be/arcgis/rest/services/APP_KAYAK/KAYAK/MapServer/export" 72 // attribution="wallonie.be" visible="true" layers="show:0,1,2,3,4,7"></olmap_agslayer> 73 // sample: 74 // http://geoservices2.wallonie.be/arcgis/rest/services/APP_KAYAK/KAYAK/MapServer/export?LAYERS=show%3A0%2C1%2C2%2C3%2C4%2C7&TRANSPARENT=true&FORMAT=png&BBOX=643294.029959%2C6467184.088252%2C645740.014863%2C6469630.073157&SIZE=256%2C256&F=html&BBOXSR=3857&IMAGESR=3857 75 $this->Lexer->addSpecialPattern( 76 '<olmap_agslayer ?[^>\n]*>.*?</olmap_agslayer>', 77 $mode, 'plugin_openlayersmapoverlays_agslayer' 78 ); 79 } 80 81 /** 82 * (non-PHPdoc) 83 * 84 * @see DokuWiki_Syntax_Plugin::handle() 85 */ 86 public function handle($match, $state, $pos, Doku_Handler $handler): array { 87 $param = array(); 88 $data = $this->dflt; 89 90 preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER); 91 92 foreach($param as $kvpair) { 93 list ($matched, $key, $val) = $kvpair; 94 if(isset ($data [$key])) { 95 $key = strtolower($key); 96 $data [$key] = $val; 97 } 98 } 99 return $data; 100 } 101 102 /** 103 * (non-PHPdoc) 104 * 105 * @see DokuWiki_Syntax_Plugin::render() 106 */ 107 public function render($format, Doku_Renderer $renderer, $data): bool { 108 if($format !== 'xhtml') { 109 return false; 110 } 111 112 static $loadedOLlib = false; 113 if(!$loadedOLlib) { 114 $renderer->doc .= DOKU_LF . '<script defer="defer" src="' . DOKU_BASE . 115 'lib/plugins/openlayersmapoverlays/lib/layers.js' . '"></script>'; 116 $loadedOLlib = true; 117 } 118 // incremented for each olmap_agslayer tag in the page source 119 static $overlaynumber = 0; 120 121 list ($id, $url, $name, $visible) = $data; 122 $renderer->doc .= DOKU_LF . '<script defer="defer" src="data:text/javascript;base64,'; 123 $str = '{'; 124 foreach($data as $key => $val) { 125 $str .= "'" . $key . "' : '" . $val . "',"; 126 } 127 $str .= "'type':'ags'}"; 128 $renderer->doc .= base64_encode("olMapOverlays['ags" . $overlaynumber . "'] = " . $str . ";") 129 . '"></script>'; 130 $overlaynumber++; 131 return true; 132 } 133} 134