xref: /plugin/openlayersmap/syntax/agslayer.php (revision bea711fe9bb2e1d14e26bfd0459f73e628995fe6)
1<?php
2/*
3 * Copyright (c) 2017-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 * adds a AGS layer to your map.
22 */
23class syntax_plugin_openlayersmap_agslayer 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        'layers'      => '',
33        'format'      => 'png',
34        'transparent' => 'true',
35        'baselayer'   => 'false',
36    );
37
38    /**
39     * (non-PHPdoc)
40     *
41     * @see DokuWiki_Syntax_Plugin::getPType()
42     */
43    public function getPType(): string
44    {
45        return 'block';
46    }
47
48    /**
49     * (non-PHPdoc)
50     *
51     * @see DokuWiki_Syntax_Plugin::getType()
52     */
53    public function getType(): string
54    {
55        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
56        return 'baseonly';
57    }
58
59    /**
60     * (non-PHPdoc)
61     *
62     * @see Doku_Parser_Mode::getSort()
63     */
64    public function getSort(): int
65    {
66        return 904;
67    }
68
69    /**
70     * Connect to our special pattern.
71     *
72     * @see Doku_Parser_Mode::connectTo()
73     */
74    public function connectTo($mode): void
75    {
76        // look for: <olmap_agslayer id="olmap" name="cloud"
77        // url="http://geoservices2.wallonie.be/arcgis/rest/services/APP_KAYAK/KAYAK/MapServer/export"
78        // attribution="wallonie.be" visible="true" layers="show:0,1,2,3,4,7"></olmap_agslayer>
79        // sample:
80        // 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
81        $this->Lexer->addSpecialPattern(
82            '<olmap_agslayer ?[^>\n]*>.*?</olmap_agslayer>',
83            $mode,
84            'plugin_openlayersmap_agslayer'
85        );
86    }
87
88    /**
89     * (non-PHPdoc)
90     *
91     * @see DokuWiki_Syntax_Plugin::handle()
92     */
93    public function handle($match, $state, $pos, Doku_Handler $handler): array
94    {
95        $param = array();
96        $data  = $this->dflt;
97
98        preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER);
99
100        foreach ($param as $kvpair) {
101            list ($matched, $key, $val) = $kvpair;
102            if (isset ($data [$key])) {
103                $key         = strtolower($key);
104                $data [$key] = $val;
105            }
106        }
107        return $data;
108    }
109
110    /**
111     * (non-PHPdoc)
112     *
113     * @see DokuWiki_Syntax_Plugin::render()
114     */
115    public function render($format, Doku_Renderer $renderer, $data): bool
116    {
117        if ($format !== 'xhtml') {
118            return false;
119        }
120
121        // incremented for each olmap_agslayer tag in the page source
122        static $overlaynumber = 0;
123
124        $renderer->doc .= DOKU_LF . '<script defer="defer" src="data:text/javascript;base64,';
125        $str           = '{';
126        foreach ($data as $key => $val) {
127            $str .= "'" . $key . "' : '" . $val . "',";
128        }
129        $str           .= "'type':'ags'}";
130        $renderer->doc .= base64_encode("olMapOverlays['ags" . $overlaynumber . "'] = " . $str . ";")
131            . '"></script>';
132        $overlaynumber++;
133        return true;
134    }
135}
136