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