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