1<?php
2/*
3 * Yurii's Gantt Plugin
4 *
5 * Copyright (C) 2020 Yurii K.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see http://www.gnu.org/licenses
19 */
20
21use dokuwiki\plugin\yuriigantt\src\Driver\Embedded;
22use dokuwiki\plugin\yuriigantt\src\Driver\Embedded\Handler;
23
24
25
26class syntax_plugin_yuriigantt extends DokuWiki_Syntax_Plugin // \dokuwiki\Extension\SyntaxPlugin
27{
28    const VIEW = 'dhtmlxgantt';
29
30    /**
31     * Connect lookup pattern to lexer.
32     *
33     * @param string $mode Parser mode
34     */
35    public function connectTo($mode)
36    {
37        if ($mode === 'base') {
38            Embedded::addLexerPattern($this->Lexer, $mode);
39        }
40    }
41
42
43    /**
44     * {@inheritdoc}
45     */
46    public function getPType()
47    {
48        return 'block';
49    }
50
51    /**
52     * {@inheritdoc}
53     */
54    public function getType()
55    {
56        return 'substition';
57    }
58
59
60    /**
61     * {@inheritdoc}
62     */
63    public function getSort()
64    {
65        return 1;
66    }
67
68
69    /**
70     * @param string $match
71     * @param int $state
72     * @param int $pos
73     * @param Doku_Handler|Handler $handler
74     * @return array
75     */
76    public function handle($match, $state, $pos, \Doku_Handler $handler)
77    {
78        global $ID;
79
80        $data = mb_substr($match, mb_strpos($match, "\n") + 1);
81        $data = mb_substr($data, 0, mb_strrpos($data, "\n"));
82        $database = json_decode($data);
83
84        if (empty($database) && !empty($ID)) {
85            $database = Embedded::initDatabase($ID);
86        }
87
88        if (!empty($ID) && $ID !== $database->pageId) {
89            $database->pageId = $ID;
90        }
91
92        //special case for embedded db
93        if ($handler instanceof Handler) {
94            $handler->setDatabase($database);
95        }
96
97        return $database;
98    }
99
100
101    /**
102     * Handles the actual output creation.
103     *
104     * The function must not assume any other of the classes methods have been run
105     * during the object's current life. The only reliable data it receives are its
106     * parameters.
107     *
108     * The function should always check for the given output format and return false
109     * when a format isn't supported.
110     *
111     * $renderer contains a reference to the renderer object which is
112     * currently handling the rendering. You need to use it for writing
113     * the output. How this is done depends on the renderer used (specified
114     * by $format
115     *
116     * The contents of the $data array depends on what the handler() function above
117     * created
118     *
119     * @param string $format output format being rendered
120     * @param Doku_Renderer|\dokuwiki\plugin\yuriigantt\src\Driver\Embedded\Renderer $renderer the current renderer object
121     * @param array $data data created by handler()
122     * @return  boolean                 rendered correctly? (however, returned value is not used at the moment)
123     */
124    public function render($format, \Doku_Renderer $renderer, $data)
125    {
126        if (strtolower($format) === 'xhtml') {
127            return $this->renderXHtml($renderer, $data);
128        } elseif (strtolower($format) === Embedded::MODE) {
129            $renderer->raw(Embedded::embed($data));
130            return true;
131        }
132
133        return false;
134    }
135
136
137    protected function renderXHtml(Doku_Renderer $renderer, $data)
138    {
139        global $conf;
140
141        if ($data->dsn !== Embedded::DSN) {
142            return false; // NOTE: add new drivers here
143        }
144
145        $html = $this->viewRender(self::VIEW, [
146            'database' => $data,
147            'pluginName' => $this->getPluginName(),
148            'baseUrl' => DOKU_URL,
149            'lang' => $conf['lang'],
150        ]);
151        $renderer->html($html);
152
153        return true;
154    }
155
156
157    protected function viewRender($view, array $params = [])
158    {
159        ob_start();
160        extract($params);
161        require __DIR__ . "/src/Views/{$view}.php";
162        return ob_get_clean();
163    }
164
165}
166