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