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