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