1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/*
6 * Copyright (c) 2017-2020 Mark C. Prins <mprins@users.sf.net>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *
20 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
21 */
22/**
23 * adds a AGS layer to your map.
24 */
25class syntax_plugin_openlayersmap_agslayer extends SyntaxPlugin
26{
27    private $dflt = ['id'          => 'olmap', 'name'        => '', 'url'         => '', 'opacity'     => 0.8, 'attribution' => '', 'visible'     => false, 'layers'      => '', 'format'      => 'png', 'transparent' => 'true', 'baselayer'   => 'false'];
28
29    /**
30     * (non-PHPdoc)
31     *
32     * @see DokuWiki_Syntax_Plugin::getPType()
33     */
34    public function getPType(): string
35    {
36        return 'block';
37    }
38
39    /**
40     * (non-PHPdoc)
41     *
42     * @see DokuWiki_Syntax_Plugin::getType()
43     */
44    public function getType(): string
45    {
46        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
47        return 'baseonly';
48    }
49
50    /**
51     * (non-PHPdoc)
52     *
53     * @see Doku_Parser_Mode::getSort()
54     */
55    public function getSort(): int
56    {
57        return 904;
58    }
59
60    /**
61     * Connect to our special pattern.
62     *
63     * @see Doku_Parser_Mode::connectTo()
64     */
65    public function connectTo($mode): void
66    {
67        // look for: <olmap_agslayer id="olmap" name="cloud"
68        // url="http://geoservices2.wallonie.be/arcgis/rest/services/APP_KAYAK/KAYAK/MapServer/export"
69        // attribution="wallonie.be" visible="true" layers="show:0,1,2,3,4,7"></olmap_agslayer>
70        // sample:
71        // http://geoservices2.wallonie.be/arcgis/rest/services/APP_KAYAK/KAYAK/MapServer/export?LAYERS=show%3A0%2C1%2C2%2C3%2C4%2C7&TRANSPARENT=true&FORMAT=png&BBOX=643294.029959%2C6467184.088252%2C645740.014863%2C6469630.073157&SIZE=256%2C256&F=html&BBOXSR=3857&IMAGESR=3857
72        $this->Lexer->addSpecialPattern(
73            '<olmap_agslayer ?[^>\n]*>.*?</olmap_agslayer>',
74            $mode,
75            'plugin_openlayersmap_agslayer'
76        );
77    }
78
79    /**
80     * (non-PHPdoc)
81     *
82     * @see DokuWiki_Syntax_Plugin::handle()
83     */
84    public function handle($match, $state, $pos, Doku_Handler $handler): array
85    {
86        $param = [];
87        $data  = $this->dflt;
88
89        preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER);
90
91        foreach ($param as $kvpair) {
92            [$matched, $key, $val] = $kvpair;
93            if (isset($data [$key])) {
94                $key         = strtolower($key);
95                $data [$key] = $val;
96            }
97        }
98        return $data;
99    }
100
101    /**
102     * (non-PHPdoc)
103     *
104     * @see DokuWiki_Syntax_Plugin::render()
105     */
106    public function render($format, Doku_Renderer $renderer, $data): bool
107    {
108        if ($format !== 'xhtml') {
109            return false;
110        }
111
112        // incremented for each olmap_agslayer tag in the page source
113        static $overlaynumber = 0;
114
115        $renderer->doc .= DOKU_LF . '<script defer="defer" src="data:text/javascript;base64,';
116        $str           = '{';
117        foreach ($data as $key => $val) {
118            $str .= "'" . $key . "' : '" . $val . "',";
119        }
120        $str           .= "'type':'ags'}";
121        $renderer->doc .= base64_encode("olMapOverlays['ags" . $overlaynumber . "'] = " . $str . ";")
122            . '"></script>';
123        $overlaynumber++;
124        return true;
125    }
126}
127