1<?php
2
3/**
4 * @license    See LICENSE file
5 * @author     Jaap de Haan <jaap.dehaan@color-of-code.de>
6 */
7
8// See help:
9// * https://www.dokuwiki.org/devel:section_editor
10// * https://www.dokuwiki.org/devel:releases:refactor2021
11
12use dokuwiki\Form\Form;
13use dokuwiki\Utf8;
14
15class action_plugin_bpmnio_editor extends DokuWiki_Action_Plugin
16{
17    public function register(Doku_Event_Handler $controller)
18    {
19        $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'secedit_button');
20
21        $controller->register_hook('EDIT_FORM_ADDTEXTAREA', 'BEFORE', $this, 'handle_form');
22        $controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, 'handle_form');
23
24        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_post');
25    }
26
27    function secedit_button(Doku_Event $event)
28    {
29        if ($this->_shall_ignore($event)) return;
30
31        $event->data['name'] = $this->getLang('edit_diagram');
32    }
33
34    function handle_form(Doku_Event $event)
35    {
36        if ($this->_shall_ignore($event)) return;
37
38        global $TEXT;
39        global $RANGE;
40        global $INPUT;
41
42        if (!$RANGE) {
43            // section editing failed, use default editor instead
44            $event->data['target'] = 'section';
45            return;
46        }
47
48        $event->stopPropagation();
49        $event->preventDefault();
50
51        $form = &$event->data['form'];
52        $data = base64_encode($TEXT);
53
54        $type = 'bpmn';
55        if ($event->data['target'] === 'plugin_bpmnio_dmn')
56            $type = 'dmn';
57
58        $this->_addHidden($form, 'plugin_bpmnio_data', $data);
59        $this->_addHTML($form, <<<HTML
60            <div class="plugin-bpmnio" id="plugin_bpmnio__{$type}_editor">
61                <div class="{$type}_js_data">{$data}</div>
62                <div class="{$type}_js_canvas">
63                    <div class="{$type}_js_container"></div>
64                </div>
65                </div>
66            </div>
67            HTML);
68
69        // used during previews
70        $this->_addHidden($form, 'target', "plugin_bpmnio_{$type}");
71        $this->_addHidden($form, 'range', $RANGE);
72    }
73
74    function handle_post(Doku_Event $event)
75    {
76        global $TEXT;
77        global $INPUT;
78
79        if (!$INPUT->post->has('plugin_bpmnio_data')) return;
80
81        $TEXT = base64_decode($INPUT->post->str('plugin_bpmnio_data'));
82    }
83
84    private function _addHidden($form, $field, $data)
85    {
86        if (is_a($form, Form::class)) { // $event->name is EDIT_FORM_ADDTEXTAREA
87            $form->setHiddenField($field, $data);
88        } else { // $event->name is HTML_EDIT_FORMSELECTION
89            $form->addHidden($field, $data);
90        }
91    }
92
93    private function _addHTML($form, $data)
94    {
95        $form->addHTML($data);
96    }
97
98    private function _shall_ignore(Doku_Event $event)
99    {
100        if ($event->data['target'] === 'plugin_bpmnio_bpmn')
101            return false;
102        if ($event->data['target'] === 'plugin_bpmnio_dmn')
103            return false;
104        return true;
105    }
106}
107