1<?php
2/**
3 * DokuWiki Plugin drawio (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Milos Kozak <milos.kozak@lejmr.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class syntax_plugin_drawio extends DokuWiki_Syntax_Plugin
15{
16    /**
17     * @return string Syntax mode type
18     */
19    public function getType()
20    {
21        return 'substition';
22    }
23
24    /**
25     * @return int Sort order - Low numbers go before high numbers
26     */
27    public function getSort()
28    {
29        return 303;
30    }
31
32    /**
33     * Connect lookup pattern to lexer.
34     *
35     * @param string $mode Parser mode
36     */
37    public function connectTo($mode)
38    {
39        $this->Lexer->addSpecialPattern("\{\{drawio>.+?\}\}",$mode,'plugin_drawio');
40    }
41
42    /**
43     * Handle matches of the drawio syntax
44     *
45     * @param string       $match   The match of the syntax
46     * @param int          $state   The state of the handler
47     * @param int          $pos     The position in the document
48     * @param Doku_Handler $handler The handler
49     *
50     * @return array Data for the renderer
51     */
52    public function handle($match, $state, $pos, Doku_Handler $handler)
53    {
54        return substr($match,9,-2);
55    }
56
57    /**
58     * Render xhtml output or metadata
59     *
60     * @param string        $mode     Renderer mode (supported modes: xhtml)
61     * @param Doku_Renderer $renderer The renderer
62     * @param array         $data     The data from the handler() function
63     *
64     * @return bool If rendering was successful.
65     */
66    public function render($mode, Doku_Renderer $renderer, $data)
67    {
68        if ($mode !== 'xhtml') {
69            return false;
70        }
71		$renderer->nocache();
72
73        // Validate that the image exists otherwise pring a default image
74        global $conf;
75        $media_id = $data;
76        // if no extention specified, use png
77        if(!in_array(pathinfo($media_id, PATHINFO_EXTENSION),array_map('trim',explode(",",$this->getConf('toolbar_possible_extension'))) )){
78            $media_id .= ".png";
79        }
80
81		$current_id = getID();
82		$current_ns = getNS($current_id);
83
84		resolve_mediaid($current_ns, $media_id, $exists);
85
86        if(!$exists){
87            $renderer->doc .= "<img class='mediacenter' id='".$media_id."'
88                        style='max-width:100%;cursor:pointer;' onclick='edit(this);'
89                        src='".DOKU_BASE."lib/plugins/drawio/blank-image.png'
90                        alt='".$media_id."' />";
91            return true;
92        }
93        $renderer->doc .= "<img class='mediacenter' id='".$media_id."'
94                        style='max-width:100%;cursor:pointer;' onclick='edit(this);'
95						src='".DOKU_BASE."lib/exe/fetch.php?media=".$media_id."'
96                        alt='".$media_id."' />";
97        return true;
98    }
99}
100