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 10if(!defined('DOKU_INC')) die(); 11 12use dokuwiki\plugin\struct\meta\AccessTable; 13use dokuwiki\plugin\struct\meta\Assignments; 14use dokuwiki\plugin\struct\meta\AccessTableData; 15use dokuwiki\plugin\struct\meta\Validator; 16use dokuwiki\plugin\struct\meta\Value; 17 18/** 19 * Class action_plugin_struct_entry 20 * 21 * Handles the whole struct data entry process 22 */ 23class action_plugin_struct_entry extends DokuWiki_Action_Plugin { 24 25 /** 26 * @var string The form name we use to transfer schema data 27 */ 28 protected static $VAR = 'struct_schema_data'; 29 30 /** @var helper_plugin_sqlite */ 31 protected $sqlite; 32 33 /** @var bool has the data been validated correctly? */ 34 protected $validated; 35 36 /** @var array these schemas have changed data and need to be saved */ 37 protected $tosave; 38 39 /** 40 * Registers a callback function for a given event 41 * 42 * @param Doku_Event_Handler $controller DokuWiki's event controller object 43 * @return void 44 */ 45 public function register(Doku_Event_Handler $controller) { 46 // add the struct editor to the edit form; 47 $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform'); 48 // validate data on preview and save; 49 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_validation'); 50 // ensure a page revision is created when struct data changes: 51 $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before'); 52 // save struct data after page has been saved: 53 $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_pagesave_after'); 54 } 55 56 /** 57 * Enhance the editing form with structural data editing 58 * 59 * @param Doku_Event $event event object by reference 60 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 61 * handler was registered] 62 * @return bool 63 */ 64 public function handle_editform(Doku_Event $event, $param) { 65 global $ID; 66 67 $assignments = new Assignments(); 68 $tables = $assignments->getPageAssignments($ID); 69 70 $html = ''; 71 foreach($tables as $table) { 72 $html .= $this->createForm($table); 73 } 74 75 /** @var Doku_Form $form */ 76 $form = $event->data; 77 $html = "<div class=\"struct_entry_form\">$html</div>"; 78 $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons 79 $form->insertElement($pos, $html); 80 81 return true; 82 } 83 84 /** 85 * Clean up and validate the input data 86 * 87 * @param Doku_Event $event event object by reference 88 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 89 * handler was registered] 90 * @return bool 91 */ 92 public function handle_validation(Doku_Event $event, $param) { 93 global $ID, $INPUT; 94 $act = act_clean($event->data); 95 if(!in_array($act, array('save', 'preview'))) return false; 96 97 // execute the validator 98 $validator = new Validator(); 99 $this->validated = $validator->validate($INPUT->arr(self::$VAR), $ID); 100 $this->tosave = $validator->getChangedSchemas(); 101 $INPUT->post->set(self::$VAR, $validator->getCleanedData()); 102 103 if(!$this->validated) foreach($validator->getErrors() as $error) { 104 msg(hsc($error), -1); 105 } 106 107 // did validation go through? otherwise abort saving 108 if(!$this->validated && $act == 'save') { 109 $event->data = 'edit'; 110 } 111 112 return false; 113 } 114 115 /** 116 * Check if the page has to be changed 117 * 118 * @param Doku_Event $event event object by reference 119 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 120 * handler was registered] 121 * @return bool 122 */ 123 public function handle_pagesave_before(Doku_Event $event, $param) { 124 if($event->data['contentChanged']) return; // will be saved for page changes 125 global $ACT; 126 global $REV; 127 128 if(count($this->tosave) || isset($GLOBALS['struct_plugin_force_page_save'])) { 129 if(trim($event->data['newContent']) === '') { 130 // this happens when a new page is tried to be created with only struct data 131 msg($this->getLang('emptypage'), -1); 132 } else { 133 $event->data['contentChanged'] = true; // save for data changes 134 135 // add a summary 136 if(empty($event->data['summary'])) { 137 $event->data['summary'] = $this->getLang('summary'); 138 } 139 } 140 } else if($ACT == 'revert' && $REV) { 141 // revert actions are not validated, so we need to force changes extra 142 $assignments = new Assignments(); 143 $tosave = $assignments->getPageAssignments($event->data['id']); 144 if(count($tosave)) { 145 $event->data['contentChanged'] = true; // save for data changes 146 } 147 } 148 } 149 150 /** 151 * Save the data 152 * 153 * When this is called, INPUT data has been validated already. On a restore action, the data is 154 * loaded from the database and not validated again. 155 * 156 * @param Doku_Event $event event object by reference 157 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 158 * handler was registered] 159 * @return bool 160 */ 161 public function handle_pagesave_after(Doku_Event $event, $param) { 162 global $INPUT; 163 global $ACT; 164 global $REV; 165 166 $assignments = new Assignments(); 167 168 if($ACT == 'revert' && $REV) { 169 // reversion is a special case, we load the data to restore from DB: 170 $structData = array(); 171 $this->tosave = $assignments->getPageAssignments($event->data['id']); 172 foreach($this->tosave as $table) { 173 $oldData = AccessTable::byTableName($table, $event->data['id'], $REV); 174 $structData[$table] = $oldData->getDataArray(); 175 } 176 } else { 177 // data comes from the edit form 178 $structData = $INPUT->arr(self::$VAR); 179 } 180 181 if($event->data['changeType'] == DOKU_CHANGE_TYPE_DELETE && empty($GLOBALS['PLUGIN_MOVE_WORKING'])) { 182 // clear all data on delete unless it's a move operation 183 $tables = $assignments->getPageAssignments($event->data['id']); 184 foreach($tables as $table) { 185 $schemaData = AccessTable::byTableName($table, $event->data['id'], time()); 186 $schemaData->clearData(); 187 } 188 } else { 189 // save the provided data 190 if($this->tosave) foreach($this->tosave as $table) { 191 $schemaData = AccessTable::byTableName($table, $event->data['id'], $event->data['newRevision']); 192 $schemaData->saveData($structData[$table]); 193 194 // make sure this schema is assigned 195 $assignments->assignPageSchema($event->data['id'], $table); 196 } 197 } 198 } 199 200 /** 201 * Create the form to edit schemadata 202 * 203 * @param string $tablename 204 * @return string The HTML for this schema's form 205 */ 206 protected function createForm($tablename) { 207 global $ID; 208 global $REV; 209 global $INPUT; 210 if (auth_quickaclcheck($ID) == AUTH_READ) return ''; 211 if (checklock($ID)) return ''; 212 $schema = AccessTable::byTableName($tablename, $ID, $REV); 213 $schemadata = $schema->getData(); 214 215 $structdata = $INPUT->arr(self::$VAR); 216 if(isset($structdata[$tablename])) { 217 $postdata = $structdata[$tablename]; 218 } else { 219 $postdata = array(); 220 } 221 222 // we need a short, unique identifier to use in the cookie. this should be good enough 223 $schemaid = 'SRCT'.substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5); 224 $html = '<fieldset data-schema="' . $schemaid . '">'; 225 $html .= '<legend>' . hsc($tablename) . '</legend>'; 226 foreach($schemadata as $field) { 227 $label = $field->getColumn()->getLabel(); 228 if(isset($postdata[$label])) { 229 // posted data trumps stored data 230 $field->setValue($postdata[$label]); 231 } 232 $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]"); 233 } 234 $html .= '</fieldset>'; 235 236 return $html; 237 } 238 239 /** 240 * Create the input field 241 * 242 * @param Value $field 243 * @param String $name field's name 244 * @return string 245 */ 246 public function makeField(Value $field, $name) { 247 $trans = hsc($field->getColumn()->getTranslatedLabel()); 248 $hint = hsc($field->getColumn()->getTranslatedHint()); 249 $class = $hint ? 'hashint' : ''; 250 $colname = $field->getColumn()->getFullQualifiedLabel(); 251 252 $input = $field->getValueEditor($name); 253 254 // we keep all the custom form stuff the field might produce, but hide it 255 if(!$field->getColumn()->isVisibleInEditor()) { 256 $hide = 'style="display:none"'; 257 } else { 258 $hide = ''; 259 } 260 261 $html = ''; 262 $html .= "<label $hide data-column=\"$colname\">"; 263 $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>"; 264 $html .= "<span class=\"input\">$input</span>"; 265 $html .= '</label>'; 266 267 return $html; 268 } 269} 270 271// vim:ts=4:sw=4:et: 272