xref: /plugin/openlayersmap/syntax/wmslayer.php (revision bea711fe9bb2e1d14e26bfd0459f73e628995fe6)
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        'baselayer'   => 'false',
37    );
38
39    /**
40     * (non-PHPdoc)
41     *
42     * @see DokuWiki_Syntax_Plugin::getPType()
43     */
44    public function getPType(): string
45    {
46        return 'block';
47    }
48
49    /**
50     * (non-PHPdoc)
51     *
52     * @see DokuWiki_Syntax_Plugin::getType()
53     */
54    public function getType(): string
55    {
56        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
57        return 'baseonly';
58    }
59
60    /**
61     * (non-PHPdoc)
62     *
63     * @see Doku_Parser_Mode::getSort()
64     */
65    public function getSort(): int
66    {
67        return 902;
68    }
69
70    /**
71     * Connect to our special pattern.
72     *
73     * @see Doku_Parser_Mode::connectTo()
74     */
75    public function connectTo($mode): void
76    {
77        // look for: <olmap_wmslayer id="olmap" name="cloud" url="http://openweathermap.org/t/tile.cgi?SERVICE=WMS"
78        // attribution="OpenWeatherMap" visible="true" layers="GLBETA_PR"></olmap_wmslayer>
79        $this->Lexer->addSpecialPattern(
80            '<olmap_wmslayer ?[^>\n]*>.*?</olmap_wmslayer>',
81            $mode,
82            'plugin_openlayersmap_wmslayer'
83        );
84    }
85
86    /**
87     * (non-PHPdoc)
88     *
89     * @see DokuWiki_Syntax_Plugin::handle()
90     */
91    public function handle($match, $state, $pos, Doku_Handler $handler): array
92    {
93        $param = array();
94        $data  = $this->dflt;
95
96        preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER);
97
98        foreach ($param as $kvpair) {
99            list ($matched, $key, $val) = $kvpair;
100            if (isset ($data [$key])) {
101                $key         = strtolower($key);
102                $data [$key] = $val;
103            }
104        }
105        // dbglog($data,'syntax_plugin_overlayer::handle: parsed data is:');
106        return $data;
107    }
108
109    /**
110     * (non-PHPdoc)
111     *
112     * @see DokuWiki_Syntax_Plugin::render()
113     */
114    public function render($format, Doku_Renderer $renderer, $data): bool
115    {
116        if ($format !== 'xhtml') {
117            return false;
118        }
119
120        // incremented for each olmap_wmslayer tag in the page source
121        static $overlaynumber = 0;
122
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}