1<?php 2/** 3 * DokuWiki Plugin struct (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7 */ 8 9// must be run within Dokuwiki 10use dokuwiki\plugin\struct\meta\Column; 11use dokuwiki\plugin\struct\meta\SchemaData; 12use dokuwiki\plugin\struct\meta\StructException; 13use dokuwiki\plugin\struct\meta\Title; 14use dokuwiki\plugin\struct\meta\Validator; 15 16if(!defined('DOKU_INC')) die(); 17 18/** 19 * Class action_plugin_struct_inline 20 * 21 * Handle inline editing 22 */ 23class action_plugin_struct_inline extends DokuWiki_Action_Plugin { 24 25 /** @var SchemaData */ 26 protected $schemadata = null; 27 28 /** @var Column */ 29 protected $column = null; 30 31 /** @var String */ 32 protected $pid = ''; 33 34 /** 35 * Registers a callback function for a given event 36 * 37 * @param Doku_Event_Handler $controller DokuWiki's event controller object 38 * @return void 39 */ 40 public function register(Doku_Event_Handler $controller) { 41 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax'); 42 } 43 44 /** 45 * @param Doku_Event $event 46 * @param $param 47 */ 48 public function handle_ajax(Doku_Event $event, $param) { 49 $len = strlen('plugin_struct_inline_'); 50 if(substr($event->data, 0, $len) != 'plugin_struct_inline_') return; 51 $event->preventDefault(); 52 $event->stopPropagation(); 53 54 if(substr($event->data,$len) == 'editor') { 55 $this->inline_editor(); 56 } 57 58 if(substr($event->data,$len) == 'save') { 59 try { 60 $this->inline_save(); 61 } catch(StructException $e) { 62 http_status(500); 63 header('Content-Type: text/plain; charset=utf-8'); 64 echo $e->getMessage(); 65 } 66 } 67 68 if(substr($event->data,$len) == 'cancel') { 69 $this->inline_cancel(); 70 } 71 } 72 73 /** 74 * Creates the inline editor 75 */ 76 protected function inline_editor() { 77 // silently fail when editing not possible 78 if(!$this->initFromInput()) return; 79 if(auth_quickaclcheck($this->pid) < AUTH_EDIT) return; 80 if(checklock($this->pid)) return; 81 82 // lock page 83 lock($this->pid); 84 85 // output the editor 86 $value = $this->schemadata->getDataColumn($this->column); 87 echo '<div>'; 88 echo $value->getValueEditor('entry'); 89 echo '</div>'; 90 $hint = $this->column->getType()->getTranslatedHint(); 91 if($hint) { 92 echo '<div class="hint">'; 93 echo hsc($hint); 94 echo '</div>'; 95 } 96 97 // csrf protection 98 formSecurityToken(); 99 } 100 101 /** 102 * Save the data posted by the inline editor 103 */ 104 protected function inline_save() { 105 global $INPUT; 106 107 if ( 108 !$this->initFromInput() || // initialize 109 getSecurityToken() != $INPUT->str('sectoc') || // csrf check 110 auth_quickaclcheck($this->pid) < AUTH_EDIT || // edit permissions 111 checklock($this->pid) // page is locked 112 ) { 113 throw new StructException('inline save error'); 114 } 115 116 // validate 117 $value = $INPUT->param('entry'); 118 $validator = new Validator(); 119 if(!$validator->validateValue($this->column, $value)) { 120 throw new StructException(join("\n", $validator->getErrors())); 121 } 122 123 // current data 124 $tosave = $this->schemadata->getDataArray(); 125 $tosave[$this->column->getLabel()] = $value; 126 $tosave = array($this->schemadata->getTable() => $tosave); 127 128 // save 129 /** @var helper_plugin_struct $helper */ 130 $helper = plugin_load('helper', 'struct'); 131 $helper->saveData($this->pid, $tosave, 'inline edit'); 132 133 // unlock 134 unlock($this->pid); 135 136 // reinit then render 137 $this->initFromInput(); 138 $value = $this->schemadata->getDataColumn($this->column); 139 $R = new Doku_Renderer_xhtml(); 140 $value->render($R, 'xhtml'); // FIXME use configured default renderer 141 echo $R->doc; 142 } 143 144 /** 145 * Unlock a page (on cancel action) 146 */ 147 protected function inline_cancel() { 148 global $INPUT; 149 $pid = $INPUT->str('pid'); 150 unlock($pid); 151 } 152 153 /** 154 * Initialize internal state based on input variables 155 * 156 * @return bool if initialization was successfull 157 */ 158 protected function initFromInput() { 159 global $INPUT; 160 161 $this->schemadata = null; 162 $this->column = null; 163 164 $pid = $INPUT->str('pid'); 165 list($table, $field) = explode('.', $INPUT->str('field')); 166 if(blank($pid)) return false; 167 if(blank($table)) return false; 168 if(blank($field)) return false; 169 170 $this->pid = $pid; 171 172 $this->schemadata = new SchemaData($table, $pid, 0); 173 if(!$this->schemadata->getId()) { 174 $this->schemadata = null; 175 return false; 176 } 177 178 179 $this->column = $this->schemadata->findColumn($field); 180 if(!$this->column || !$this->column->isVisibleInEditor()) { 181 $this->schemadata = null; 182 $this->column = null; 183 return false; 184 } 185 186 return true; 187 } 188 189} 190