1549a0837SAndreas Gohr<?php 2549a0837SAndreas Gohr/** 3549a0837SAndreas Gohr * DokuWiki Plugin struct (Action Component) 4549a0837SAndreas Gohr * 5549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6549a0837SAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7549a0837SAndreas Gohr */ 8549a0837SAndreas Gohr 9549a0837SAndreas Gohr// must be run within Dokuwiki 10549a0837SAndreas Gohrif(!defined('DOKU_INC')) die(); 11549a0837SAndreas Gohr 12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments; 13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaData; 14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Validator; 15ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Value; 16c2fd0bf0SMichael Große 173c2e6844SAndreas Gohr/** 183c2e6844SAndreas Gohr * Class action_plugin_struct_entry 193c2e6844SAndreas Gohr * 203c2e6844SAndreas Gohr * Handles the whole struct data entry process 213c2e6844SAndreas Gohr */ 22549a0837SAndreas Gohrclass action_plugin_struct_entry extends DokuWiki_Action_Plugin { 23549a0837SAndreas Gohr 2417560ecbSAndreas Gohr /** 2517560ecbSAndreas Gohr * @var string The form name we use to transfer schema data 2617560ecbSAndreas Gohr */ 2717560ecbSAndreas Gohr protected static $VAR = 'struct_schema_data'; 28c2fd0bf0SMichael Große 29c2fd0bf0SMichael Große /** @var helper_plugin_sqlite */ 30c2fd0bf0SMichael Große protected $sqlite; 31c2fd0bf0SMichael Große 323c2e6844SAndreas Gohr /** @var bool has the data been validated correctly? */ 333c2e6844SAndreas Gohr protected $validated; 343c2e6844SAndreas Gohr 353c2e6844SAndreas Gohr /** @var array these schemas have changed data and need to be saved */ 363c2e6844SAndreas Gohr protected $tosave; 373c2e6844SAndreas Gohr 38549a0837SAndreas Gohr /** 39549a0837SAndreas Gohr * Registers a callback function for a given event 40549a0837SAndreas Gohr * 41549a0837SAndreas Gohr * @param Doku_Event_Handler $controller DokuWiki's event controller object 42549a0837SAndreas Gohr * @return void 43549a0837SAndreas Gohr */ 44549a0837SAndreas Gohr public function register(Doku_Event_Handler $controller) { 453c2e6844SAndreas Gohr // add the struct editor to the edit form; 46c2fd0bf0SMichael Große $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform'); 473c2e6844SAndreas Gohr // validate data on preview and save; 483c2e6844SAndreas Gohr $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_validation'); 493c2e6844SAndreas Gohr // ensure a page revision is created when struct data changes: 503c2e6844SAndreas Gohr $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before'); 513c2e6844SAndreas Gohr // save struct data after page has been saved: 523c2e6844SAndreas Gohr $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_pagesave_after'); 53549a0837SAndreas Gohr } 54549a0837SAndreas Gohr 55549a0837SAndreas Gohr /** 563c2e6844SAndreas Gohr * Enhance the editing form with structural data editing 5704641d56SMichael Große * 5804641d56SMichael Große * @param Doku_Event $event event object by reference 5904641d56SMichael Große * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 6004641d56SMichael Große * handler was registered] 6104641d56SMichael Große * @return bool 6204641d56SMichael Große */ 633c2e6844SAndreas Gohr public function handle_editform(Doku_Event $event, $param) { 643c2e6844SAndreas Gohr global $ID; 653c2e6844SAndreas Gohr 663c2e6844SAndreas Gohr $assignments = new Assignments(); 673c2e6844SAndreas Gohr $tables = $assignments->getPageAssignments($ID); 683c2e6844SAndreas Gohr 693c2e6844SAndreas Gohr $html = ''; 703c2e6844SAndreas Gohr foreach($tables as $table) { 713c2e6844SAndreas Gohr $html .= $this->createForm($table); 723c2e6844SAndreas Gohr } 733c2e6844SAndreas Gohr 743c2e6844SAndreas Gohr /** @var Doku_Form $form */ 753c2e6844SAndreas Gohr $form = $event->data; 763c2e6844SAndreas Gohr $html = "<div class=\"struct\">$html</div>"; 773c2e6844SAndreas Gohr $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons 783c2e6844SAndreas Gohr $form->insertElement($pos, $html); 793c2e6844SAndreas Gohr 803c2e6844SAndreas Gohr return true; 813c2e6844SAndreas Gohr } 823c2e6844SAndreas Gohr 833c2e6844SAndreas Gohr /** 843c2e6844SAndreas Gohr * Clean up and validate the input data 853c2e6844SAndreas Gohr * 863c2e6844SAndreas Gohr * @param Doku_Event $event event object by reference 873c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 883c2e6844SAndreas Gohr * handler was registered] 893c2e6844SAndreas Gohr * @return bool 903c2e6844SAndreas Gohr */ 913c2e6844SAndreas Gohr public function handle_validation(Doku_Event $event, $param) { 923ece9074SMichael Große global $ID, $INPUT; 9317560ecbSAndreas Gohr $act = act_clean($event->data); 9417560ecbSAndreas Gohr if(!in_array($act, array('save', 'preview'))) return false; 9504641d56SMichael Große 96c8a548a8SAndreas Gohr // execute the validator 97c8a548a8SAndreas Gohr $validator = new Validator(); 98c8a548a8SAndreas Gohr $this->validated = $validator->validate($INPUT->arr(self::$VAR), $ID); 99c8a548a8SAndreas Gohr $this->tosave = $validator->getChangedSchemas(); 100c8a548a8SAndreas Gohr $INPUT->post->set(self::$VAR, $validator->getCleanedData()); 10104641d56SMichael Große 102c8a548a8SAndreas Gohr if(!$this->validated) foreach($validator->getErrors() as $error) { 103c8a548a8SAndreas Gohr msg(hsc($error), -1); 104bd92cd68SAndreas Gohr } 105bd92cd68SAndreas Gohr 10617560ecbSAndreas Gohr // did validation go through? otherwise abort saving 1073c2e6844SAndreas Gohr if(!$this->validated && $act == 'save') { 10817560ecbSAndreas Gohr $event->data = 'edit'; 10917560ecbSAndreas Gohr } 11017560ecbSAndreas Gohr 11104641d56SMichael Große return false; 11204641d56SMichael Große } 11304641d56SMichael Große 11417560ecbSAndreas Gohr /** 1153c2e6844SAndreas Gohr * Check if the page has to be changed 1163c2e6844SAndreas Gohr * 1173c2e6844SAndreas Gohr * @param Doku_Event $event event object by reference 1183c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 1193c2e6844SAndreas Gohr * handler was registered] 1203c2e6844SAndreas Gohr * @return bool 1213c2e6844SAndreas Gohr */ 1223c2e6844SAndreas Gohr public function handle_pagesave_before(Doku_Event $event, $param) { 1233c2e6844SAndreas Gohr if($event->data['contentChanged']) return; // will be saved for page changes 124*d683a527SAndreas Gohr global $ACT; 125*d683a527SAndreas Gohr global $REV; 1267dac04ffSAndreas Gohr 1277dac04ffSAndreas Gohr if(count($this->tosave) || isset($GLOBALS['struct_plugin_force_page_save'])) { 1283c2e6844SAndreas Gohr if(trim($event->data['newContent']) === '') { 1293c2e6844SAndreas Gohr // this happens when a new page is tried to be created with only struct data 1303c2e6844SAndreas Gohr msg($this->getLang('emptypage'), -1); 1313c2e6844SAndreas Gohr } else { 1323c2e6844SAndreas Gohr $event->data['contentChanged'] = true; // save for data changes 13348010be8SAndreas Gohr 13448010be8SAndreas Gohr // add a summary 13548010be8SAndreas Gohr if(empty($event->data['summary'])) { 13648010be8SAndreas Gohr $event->data['summary'] = $this->getLang('summary'); 13748010be8SAndreas Gohr } 1383c2e6844SAndreas Gohr } 139*d683a527SAndreas Gohr } else if($ACT == 'revert' && $REV) { 140*d683a527SAndreas Gohr // revert actions are not validated, so we need to force changes extra 141*d683a527SAndreas Gohr $assignments = new Assignments(); 142*d683a527SAndreas Gohr $tosave = $assignments->getPageAssignments($event->data['id']); 143*d683a527SAndreas Gohr if(count($tosave)) { 144*d683a527SAndreas Gohr $event->data['contentChanged'] = true; // save for data changes 145*d683a527SAndreas Gohr } 1463c2e6844SAndreas Gohr } 1473c2e6844SAndreas Gohr } 1483c2e6844SAndreas Gohr 1493c2e6844SAndreas Gohr /** 1503c2e6844SAndreas Gohr * Save the data 1513c2e6844SAndreas Gohr * 15256672c36SAndreas Gohr * When this is called, INPUT data has been validated already. On a restore action, the data is 15356672c36SAndreas Gohr * loaded from the database and not validated again. 15456672c36SAndreas Gohr * 1553c2e6844SAndreas Gohr * @param Doku_Event $event event object by reference 1563c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 1573c2e6844SAndreas Gohr * handler was registered] 1583c2e6844SAndreas Gohr * @return bool 1593c2e6844SAndreas Gohr */ 1603c2e6844SAndreas Gohr public function handle_pagesave_after(Doku_Event $event, $param) { 1613c2e6844SAndreas Gohr global $INPUT; 16256672c36SAndreas Gohr global $ACT; 16356672c36SAndreas Gohr global $REV; 1643c2e6844SAndreas Gohr 165956fa7d4SAndreas Gohr $assignments = new Assignments(); 166956fa7d4SAndreas Gohr 16756672c36SAndreas Gohr if($ACT == 'revert' && $REV) { 16856672c36SAndreas Gohr // reversion is a special case, we load the data to restore from DB: 16956672c36SAndreas Gohr $structData = array(); 17056672c36SAndreas Gohr $this->tosave = $assignments->getPageAssignments($event->data['id']); 17156672c36SAndreas Gohr foreach($this->tosave as $table) { 17256672c36SAndreas Gohr $oldData = new SchemaData($table, $event->data['id'], $REV); 173bf4e3880SAndreas Gohr $oldData->optionRawValue(true); 17456672c36SAndreas Gohr $structData[$table] = $oldData->getDataArray(); 17556672c36SAndreas Gohr } 17656672c36SAndreas Gohr } else { 17756672c36SAndreas Gohr // data comes from the edit form 1783c2e6844SAndreas Gohr $structData = $INPUT->arr(self::$VAR); 17956672c36SAndreas Gohr } 1803c2e6844SAndreas Gohr 18158cb1498SAndreas Gohr if($event->data['changeType'] == DOKU_CHANGE_TYPE_DELETE && empty($GLOBALS['PLUGIN_MOVE_WORKING'])) { 18258cb1498SAndreas Gohr // clear all data on delete unless it's a move operation 183eeb8d29fSAndreas Gohr $tables = $assignments->getPageAssignments($event->data['id']); 184eeb8d29fSAndreas Gohr foreach($tables as $table) { 185eeb8d29fSAndreas Gohr $schemaData = new SchemaData($table, $event->data['id'], time()); 186eeb8d29fSAndreas Gohr $schemaData->clearData(); 1873c2e6844SAndreas Gohr } 188eeb8d29fSAndreas Gohr } else { 189eeb8d29fSAndreas Gohr // save the provided data 1907b9717bfSAndreas Gohr if($this->tosave) foreach($this->tosave as $table) { 1913c2e6844SAndreas Gohr $schemaData = new SchemaData($table, $event->data['id'], $event->data['newRevision']); 1923c2e6844SAndreas Gohr $schemaData->saveData($structData[$table]); 193956fa7d4SAndreas Gohr 194956fa7d4SAndreas Gohr // make sure this schema is assigned 195956fa7d4SAndreas Gohr $assignments->assignPageSchema($event->data['id'], $table); 1963c2e6844SAndreas Gohr } 1973c2e6844SAndreas Gohr } 198eeb8d29fSAndreas Gohr } 1993c2e6844SAndreas Gohr 2003c2e6844SAndreas Gohr /** 20165598e4aSAndreas Gohr * Create the form to edit schemadata 202f36fda9dSAndreas Gohr * 203c2fd0bf0SMichael Große * @param string $tablename 20465598e4aSAndreas Gohr * @return string The HTML for this schema's form 205c2fd0bf0SMichael Große */ 20665598e4aSAndreas Gohr protected function createForm($tablename) { 207c2fd0bf0SMichael Große global $ID; 20883beda18SAndreas Gohr global $REV; 20917560ecbSAndreas Gohr global $INPUT; 210fc13e8e7SMichael Große if (auth_quickaclcheck($ID) == AUTH_READ) return ''; 211cb249c51SMichael Große if (checklock($ID)) return ''; 21283beda18SAndreas Gohr $schema = new SchemaData($tablename, $ID, $REV); 213c2fd0bf0SMichael Große $schemadata = $schema->getData(); 214c2fd0bf0SMichael Große 21517560ecbSAndreas Gohr $structdata = $INPUT->arr(self::$VAR); 21617560ecbSAndreas Gohr if(isset($structdata[$tablename])) { 21717560ecbSAndreas Gohr $postdata = $structdata[$tablename]; 21817560ecbSAndreas Gohr } else { 21917560ecbSAndreas Gohr $postdata = array(); 22017560ecbSAndreas Gohr } 22117560ecbSAndreas Gohr 2225d5ff984SAndreas Gohr // we need a short, unique identifier to use in the cookie. this should be good enough 2235d5ff984SAndreas Gohr $schemaid = 'SRCT'.substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5); 2245d5ff984SAndreas Gohr $html = '<fieldset data-schema="' . $schemaid . '">'; 22534bdbea7SAndreas Gohr $html .= '<legend>' . hsc($tablename) . '</legend>'; 226053212b1SAndreas Gohr foreach($schemadata as $field) { 227053212b1SAndreas Gohr $label = $field->getColumn()->getLabel(); 22817560ecbSAndreas Gohr if(isset($postdata[$label])) { 22917560ecbSAndreas Gohr // posted data trumps stored data 23017560ecbSAndreas Gohr $field->setValue($postdata[$label]); 23117560ecbSAndreas Gohr } 23295838d50SAndreas Gohr $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]"); 233ed3de3d6SAndreas Gohr } 234ed3de3d6SAndreas Gohr $html .= '</fieldset>'; 235ed3de3d6SAndreas Gohr 236ed3de3d6SAndreas Gohr return $html; 237ed3de3d6SAndreas Gohr } 238ed3de3d6SAndreas Gohr 239ed3de3d6SAndreas Gohr /** 240ed3de3d6SAndreas Gohr * Create the input field 241ed3de3d6SAndreas Gohr * 242ed3de3d6SAndreas Gohr * @param Value $field 243ed3de3d6SAndreas Gohr * @param String $name field's name 244ed3de3d6SAndreas Gohr * @return string 245ed3de3d6SAndreas Gohr */ 24695838d50SAndreas Gohr protected function makeField(Value $field, $name) { 2479e9bee91SAndreas Gohr $trans = hsc($field->getColumn()->getTranslatedLabel()); 2483a717675SAndreas Gohr $hint = hsc($field->getColumn()->getTranslatedHint()); 2493a717675SAndreas Gohr $class = $hint ? 'hashint' : ''; 250ed3de3d6SAndreas Gohr $colname = $field->getColumn()->getFullQualifiedLabel(); 2513a717675SAndreas Gohr 252053212b1SAndreas Gohr $input = $field->getValueEditor($name); 2532350b48eSAndreas Gohr 2542350b48eSAndreas Gohr // we keep all the custom form stuff the field might produce, but hide it 2552350b48eSAndreas Gohr if(!$field->getColumn()->isVisibleInEditor()) { 2562350b48eSAndreas Gohr $hide = 'style="display:none"'; 2572350b48eSAndreas Gohr } else { 2582350b48eSAndreas Gohr $hide = ''; 2592350b48eSAndreas Gohr } 2602350b48eSAndreas Gohr 261ed3de3d6SAndreas Gohr $html = ''; 262ed3de3d6SAndreas Gohr $html .= "<label $hide data-column=\"$colname\">"; 2633a717675SAndreas Gohr $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>"; 2643a717675SAndreas Gohr $html .= "<span class=\"input\">$input</span>"; 2653a717675SAndreas Gohr $html .= '</label>'; 26665598e4aSAndreas Gohr 26765598e4aSAndreas Gohr return $html; 268549a0837SAndreas Gohr } 269549a0837SAndreas Gohr} 270549a0837SAndreas Gohr 271549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 272