1<?php
2/*
3 * Copyright (c) 2012-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 WMS 1.1.1 layer to your map.
22 */
23class syntax_plugin_openlayersmapoverlays_wmslayer 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        'version'     => '1.1.1',
33        'format'      => 'image/png',
34        'transparent' => 'true'
35    );
36
37    /**
38     * (non-PHPdoc)
39     *
40     * @see DokuWiki_Syntax_Plugin::getPType()
41     */
42    public function getPType(): string {
43        return 'block';
44    }
45
46    /**
47     * (non-PHPdoc)
48     *
49     * @see DokuWiki_Syntax_Plugin::getType()
50     */
51    public function getType(): string {
52        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
53        return 'baseonly';
54    }
55
56    /**
57     * (non-PHPdoc)
58     *
59     * @see Doku_Parser_Mode::getSort()
60     */
61    public function getSort(): int {
62        return 902;
63    }
64
65    /**
66     * Connect to our special pattern.
67     *
68     * @see Doku_Parser_Mode::connectTo()
69     */
70    public function connectTo($mode): void {
71        // look for: <olmap_wmslayer id="olmap" name="cloud" url="http://openweathermap.org/t/tile.cgi?SERVICE=WMS"
72        // attribution="OpenWeatherMap" visible="true" layers="GLBETA_PR"></olmap_wmslayer>
73        $this->Lexer->addSpecialPattern(
74            '<olmap_wmslayer ?[^>\n]*>.*?</olmap_wmslayer>',
75            $mode, 'plugin_openlayersmapoverlays_wmslayer'
76        );
77    }
78
79    /**
80     * (non-PHPdoc)
81     *
82     * @see DokuWiki_Syntax_Plugin::handle()
83     */
84    public function handle($match, $state, $pos, Doku_Handler $handler): array {
85        $param = array();
86        $data  = $this->dflt;
87
88        preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER);
89
90        foreach($param as $kvpair) {
91            list ($matched, $key, $val) = $kvpair;
92            if(isset ($data [$key])) {
93                $key         = strtolower($key);
94                $data [$key] = $val;
95            }
96        }
97        // dbglog($data,'syntax_plugin_overlayer::handle: parsed data is:');
98        return $data;
99    }
100
101    /**
102     * (non-PHPdoc)
103     *
104     * @see DokuWiki_Syntax_Plugin::render()
105     */
106    public function render($format, Doku_Renderer $renderer, $data): bool {
107        if($format !== 'xhtml') {
108            return false;
109        }
110
111        static $loadedOLlib = false;
112        if(!$loadedOLlib) {
113            $renderer->doc .= DOKU_LF . '<script defer="defer" src="' . DOKU_BASE
114                . 'lib/plugins/openlayersmapoverlays/lib/layers.js' . '"></script>';
115            $loadedOLlib   = true;
116        }
117        // incremented for each olmap_wmslayer tag in the page source
118        static $overlaynumber = 0;
119
120        list ($id, $url, $name, $visible) = $data;
121        $renderer->doc .= DOKU_LF . '<script defer="defer" src="data:text/javascript;base64,';
122        $str           = '{';
123        foreach($data as $key => $val) {
124            $str .= "'" . $key . "' : '" . $val . "',";
125        }
126        $str           .= "'type':'wms'}";
127        $renderer->doc .= base64_encode("olMapOverlays['wms" . $overlaynumber . "'] = " . $str . ";")
128            . '"></script>';
129        $overlaynumber++;
130        return true;
131    }
132}