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