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 12f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable; 1387dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments; 1493ca6f4fSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessDataValidator; 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 3593ca6f4fSAndreas Gohr /** @var AccessDataValidator[] these schemas are validated and 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 // validate data on preview and save; 463c2e6844SAndreas Gohr $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_validation'); 473c2e6844SAndreas Gohr // ensure a page revision is created when struct data changes: 483c2e6844SAndreas Gohr $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before'); 493c2e6844SAndreas Gohr // save struct data after page has been saved: 503c2e6844SAndreas Gohr $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_pagesave_after'); 51549a0837SAndreas Gohr } 52549a0837SAndreas Gohr 53549a0837SAndreas Gohr /** 543c2e6844SAndreas Gohr * Clean up and validate the input data 553c2e6844SAndreas Gohr * 563c2e6844SAndreas Gohr * @param Doku_Event $event event object by reference 573c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 583c2e6844SAndreas Gohr * handler was registered] 593c2e6844SAndreas Gohr * @return bool 603c2e6844SAndreas Gohr */ 613c2e6844SAndreas Gohr public function handle_validation(Doku_Event $event, $param) { 623ece9074SMichael Große global $ID, $INPUT; 6317560ecbSAndreas Gohr $act = act_clean($event->data); 6417560ecbSAndreas Gohr if(!in_array($act, array('save', 'preview'))) return false; 6567036dabSAndreas Gohr $this->tosave = array(); 6604641d56SMichael Große 6787dc1344SAndreas Gohr // run the validation for each assignded schema 6893ca6f4fSAndreas Gohr $valid = AccessDataValidator::validateDataForPage($INPUT->arr(self::$VAR), $ID, $errors); 6993ca6f4fSAndreas Gohr if($valid === false) { 7087dc1344SAndreas Gohr $this->validated = false; 7193ca6f4fSAndreas Gohr foreach($errors as $error) { 72c8a548a8SAndreas Gohr msg(hsc($error), -1); 73bd92cd68SAndreas Gohr } 7487dc1344SAndreas Gohr } else { 7593ca6f4fSAndreas Gohr $this->validated = true; 7693ca6f4fSAndreas Gohr $this->tosave = $valid; 7787dc1344SAndreas Gohr } 7887dc1344SAndreas Gohr 7987dc1344SAndreas Gohr // FIXME we used to set the cleaned data as new input data. this caused #140 8087dc1344SAndreas Gohr // could we just not do that, and keep the cleaning to saving only? and fix that bug this way? 81bd92cd68SAndreas Gohr 8217560ecbSAndreas Gohr // did validation go through? otherwise abort saving 833c2e6844SAndreas Gohr if(!$this->validated && $act == 'save') { 8417560ecbSAndreas Gohr $event->data = 'edit'; 8517560ecbSAndreas Gohr } 8617560ecbSAndreas Gohr 8787dc1344SAndreas Gohr return true; 8804641d56SMichael Große } 8904641d56SMichael Große 9017560ecbSAndreas Gohr /** 913c2e6844SAndreas Gohr * Check if the page has to be changed 923c2e6844SAndreas Gohr * 933c2e6844SAndreas Gohr * @param Doku_Event $event event object by reference 943c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 953c2e6844SAndreas Gohr * handler was registered] 963c2e6844SAndreas Gohr * @return bool 973c2e6844SAndreas Gohr */ 983c2e6844SAndreas Gohr public function handle_pagesave_before(Doku_Event $event, $param) { 9987dc1344SAndreas Gohr if($event->data['contentChanged']) return false; // will be saved for page changes 100d683a527SAndreas Gohr global $ACT; 10187dc1344SAndreas Gohr if($ACT == 'revert') return false; // this is handled in revert.php 1027dac04ffSAndreas Gohr 103*df8d9fffSMichael Große if((is_array($this->tosave) && count($this->tosave)) || isset($GLOBALS['struct_plugin_force_page_save'])) { 1043c2e6844SAndreas Gohr if(trim($event->data['newContent']) === '') { 1053c2e6844SAndreas Gohr // this happens when a new page is tried to be created with only struct data 1063c2e6844SAndreas Gohr msg($this->getLang('emptypage'), -1); 1073c2e6844SAndreas Gohr } else { 1083c2e6844SAndreas Gohr $event->data['contentChanged'] = true; // save for data changes 10948010be8SAndreas Gohr 11048010be8SAndreas Gohr // add a summary 11148010be8SAndreas Gohr if(empty($event->data['summary'])) { 11248010be8SAndreas Gohr $event->data['summary'] = $this->getLang('summary'); 11348010be8SAndreas Gohr } 1143c2e6844SAndreas Gohr } 115d683a527SAndreas Gohr } 11687dc1344SAndreas Gohr 11787dc1344SAndreas Gohr return true; 1183c2e6844SAndreas Gohr } 1193c2e6844SAndreas Gohr 1203c2e6844SAndreas Gohr /** 1213c2e6844SAndreas Gohr * Save the data 1223c2e6844SAndreas Gohr * 12387dc1344SAndreas Gohr * When this is called, INPUT data has been validated already. 12456672c36SAndreas Gohr * 1253c2e6844SAndreas Gohr * @param Doku_Event $event event object by reference 1263c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 1273c2e6844SAndreas Gohr * handler was registered] 1283c2e6844SAndreas Gohr * @return bool 1293c2e6844SAndreas Gohr */ 1303c2e6844SAndreas Gohr public function handle_pagesave_after(Doku_Event $event, $param) { 13156672c36SAndreas Gohr global $ACT; 13287dc1344SAndreas Gohr if($ACT == 'revert') return false; // handled in revert 1333c2e6844SAndreas Gohr 134025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 13558cb1498SAndreas Gohr if($event->data['changeType'] == DOKU_CHANGE_TYPE_DELETE && empty($GLOBALS['PLUGIN_MOVE_WORKING'])) { 13658cb1498SAndreas Gohr // clear all data on delete unless it's a move operation 137eeb8d29fSAndreas Gohr $tables = $assignments->getPageAssignments($event->data['id']); 138eeb8d29fSAndreas Gohr foreach($tables as $table) { 139f392071eSAndreas Gohr $schemaData = AccessTable::byTableName($table, $event->data['id'], time()); 1406ebbbb8eSAndreas Gohr if($schemaData->getSchema()->isEditable()){ 141eeb8d29fSAndreas Gohr $schemaData->clearData(); 1423c2e6844SAndreas Gohr } 1436ebbbb8eSAndreas Gohr } 144eeb8d29fSAndreas Gohr } else { 145eeb8d29fSAndreas Gohr // save the provided data 14687dc1344SAndreas Gohr if($this->tosave) foreach($this->tosave as $validation) { 1476ebbbb8eSAndreas Gohr if($validation->getAccessTable()->getSchema()->isEditable()) { 14887dc1344SAndreas Gohr $validation->saveData($event->data['newRevision']); 149956fa7d4SAndreas Gohr 150956fa7d4SAndreas Gohr // make sure this schema is assigned 15187dc1344SAndreas Gohr $assignments->assignPageSchema( 15287dc1344SAndreas Gohr $event->data['id'], 15387dc1344SAndreas Gohr $validation->getAccessTable()->getSchema()->getTable() 15487dc1344SAndreas Gohr ); 1553c2e6844SAndreas Gohr } 1563c2e6844SAndreas Gohr } 1576ebbbb8eSAndreas Gohr } 15887dc1344SAndreas Gohr return true; 159eeb8d29fSAndreas Gohr } 160549a0837SAndreas Gohr} 161549a0837SAndreas Gohr 162549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 163