1<?php
2/*
3 * Copyright (c) 2014-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 a Mapillary layer to your map.
22 */
23class syntax_plugin_openlayersmapoverlays_mapillarylayer extends DokuWiki_Syntax_Plugin {
24    private $dflt = array(
25        'id'      => 'olmap',
26        'visible' => false,
27        'skey'    => ''
28    );
29
30    /**
31     *
32     *
33     * @see DokuWiki_Syntax_Plugin::getPType()
34     */
35    public function getPType(): string {
36        return 'block';
37    }
38
39    /**
40     *
41     *
42     * @see DokuWiki_Syntax_Plugin::getType()
43     */
44    public function getType(): string {
45        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
46        return 'baseonly';
47    }
48
49    /**
50     *
51     *
52     * @see Doku_Parser_Mode::getSort()
53     */
54    public function getSort(): int {
55        return 903;
56    }
57
58    /**
59     * Connect to our special pattern.
60     *
61     * @see Doku_Parser_Mode::connectTo()
62     */
63    public function connectTo($mode): void {
64        // look for: <olmap_mapillarylayer id="olmap" visible="false"></olmap_mapillarylayer>
65        $this->Lexer->addSpecialPattern(
66            '<olmap_mapillarylayer ?[^>\n]*>.*?</olmap_mapillarylayer>', $mode,
67            'plugin_openlayersmapoverlays_mapillarylayer'
68        );
69    }
70
71    /**
72     * (non-PHPdoc)
73     *
74     * @see DokuWiki_Syntax_Plugin::handle()
75     */
76    public function handle($match, $state, $pos, Doku_Handler $handler): array {
77        $param = array();
78        $data  = $this->dflt;
79
80        preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER);
81
82        foreach($param as $kvpair) {
83            list ($matched, $key, $val) = $kvpair;
84            $key = strtolower($key);
85            if(isset ($data [$key])) {
86                $data [$key] = hsc($val);
87            }
88        }
89        return $data;
90    }
91
92    /**
93     * (non-PHPdoc)
94     *
95     * @see DokuWiki_Syntax_Plugin::render()
96     */
97    public function render($format, Doku_Renderer $renderer, $data): bool {
98        if($format !== 'xhtml') {
99            return false;
100        }
101
102        static $loadedOLlib = false;
103        if(!$loadedOLlib) {
104            $renderer->doc .= DOKU_LF . '<script defer="defer" src="' . DOKU_BASE
105                . 'lib/plugins/openlayersmapoverlays/lib/layers.js' . '"></script>';
106            $loadedOLlib   = true;
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':'mapillary'}";
118        $renderer->doc .= base64_encode("olMapOverlays['mapillary" . $overlaynumber . "'] = " . $str . ";")
119            . '"></script>';
120        $overlaynumber++;
121        return true;
122    }
123}
124