1549a0837SAndreas Gohr<?php 2d6d97f60SAnna Dabrowska 3549a0837SAndreas Gohr/** 4549a0837SAndreas Gohr * DokuWiki Plugin struct (Action Component) 5549a0837SAndreas Gohr * 6549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7549a0837SAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 8549a0837SAndreas Gohr */ 9549a0837SAndreas Gohr 10*0549dcc5SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessDataValidator; 11f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable; 1287dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments; 13c2fd0bf0SMichael Große 143c2e6844SAndreas Gohr/** 153c2e6844SAndreas Gohr * Class action_plugin_struct_entry 163c2e6844SAndreas Gohr * 1769f7ec8fSAnna Dabrowska * Handles the entry process of struct data with type "page" 183c2e6844SAndreas Gohr */ 19d6d97f60SAnna Dabrowskaclass action_plugin_struct_entry extends DokuWiki_Action_Plugin 20d6d97f60SAnna Dabrowska{ 2117560ecbSAndreas Gohr /** 2217560ecbSAndreas Gohr * @var string The form name we use to transfer schema data 2317560ecbSAndreas Gohr */ 2417560ecbSAndreas Gohr protected static $VAR = 'struct_schema_data'; 25c2fd0bf0SMichael Große 26c2fd0bf0SMichael Große /** @var helper_plugin_sqlite */ 27c2fd0bf0SMichael Große protected $sqlite; 28c2fd0bf0SMichael Große 293c2e6844SAndreas Gohr /** @var bool has the data been validated correctly? */ 303c2e6844SAndreas Gohr protected $validated; 313c2e6844SAndreas Gohr 3293ca6f4fSAndreas Gohr /** @var AccessDataValidator[] these schemas are validated and have changed data and need to be saved */ 333c2e6844SAndreas Gohr protected $tosave; 343c2e6844SAndreas Gohr 35549a0837SAndreas Gohr /** 36549a0837SAndreas Gohr * Registers a callback function for a given event 37549a0837SAndreas Gohr * 38549a0837SAndreas Gohr * @param Doku_Event_Handler $controller DokuWiki's event controller object 39549a0837SAndreas Gohr * @return void 40549a0837SAndreas Gohr */ 41d6d97f60SAnna Dabrowska public function register(Doku_Event_Handler $controller) 42d6d97f60SAnna Dabrowska { 433c2e6844SAndreas Gohr // validate data on preview and save; 44748e747fSAnna Dabrowska $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleValidation'); 453c2e6844SAndreas Gohr // ensure a page revision is created when struct data changes: 46748e747fSAnna Dabrowska $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handlePagesaveBefore'); 473c2e6844SAndreas Gohr // save struct data after page has been saved: 48748e747fSAnna Dabrowska $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handlePagesaveAfter'); 49549a0837SAndreas Gohr } 50549a0837SAndreas Gohr 51549a0837SAndreas Gohr /** 523c2e6844SAndreas Gohr * Clean up and validate the input data 533c2e6844SAndreas Gohr * 543c2e6844SAndreas Gohr * @param Doku_Event $event event object by reference 553c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 563c2e6844SAndreas Gohr * handler was registered] 573c2e6844SAndreas Gohr * @return bool 583c2e6844SAndreas Gohr */ 59748e747fSAnna Dabrowska public function handleValidation(Doku_Event $event, $param) 60d6d97f60SAnna Dabrowska { 613ece9074SMichael Große global $ID, $INPUT; 6217560ecbSAndreas Gohr $act = act_clean($event->data); 6317560ecbSAndreas Gohr if (!in_array($act, array('save', 'preview'))) return false; 6467036dabSAndreas Gohr $this->tosave = array(); 6504641d56SMichael Große 6687dc1344SAndreas Gohr // run the validation for each assignded schema 6793ca6f4fSAndreas Gohr $valid = AccessDataValidator::validateDataForPage($INPUT->arr(self::$VAR), $ID, $errors); 6893ca6f4fSAndreas Gohr if ($valid === false) { 6987dc1344SAndreas Gohr $this->validated = false; 7093ca6f4fSAndreas Gohr foreach ($errors as $error) { 71c8a548a8SAndreas Gohr msg(hsc($error), -1); 72bd92cd68SAndreas Gohr } 7387dc1344SAndreas Gohr } else { 7493ca6f4fSAndreas Gohr $this->validated = true; 7593ca6f4fSAndreas Gohr $this->tosave = $valid; 7687dc1344SAndreas Gohr } 7787dc1344SAndreas Gohr 7887dc1344SAndreas Gohr // FIXME we used to set the cleaned data as new input data. this caused #140 7987dc1344SAndreas Gohr // could we just not do that, and keep the cleaning to saving only? and fix that bug this way? 80bd92cd68SAndreas Gohr 8117560ecbSAndreas Gohr // did validation go through? otherwise abort saving 823c2e6844SAndreas Gohr if (!$this->validated && $act == 'save') { 8317560ecbSAndreas Gohr $event->data = 'edit'; 8417560ecbSAndreas Gohr } 8517560ecbSAndreas Gohr 8687dc1344SAndreas Gohr return true; 8704641d56SMichael Große } 8804641d56SMichael Große 8917560ecbSAndreas Gohr /** 903c2e6844SAndreas Gohr * Check if the page has to be changed 913c2e6844SAndreas Gohr * 923c2e6844SAndreas Gohr * @param Doku_Event $event event object by reference 933c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 943c2e6844SAndreas Gohr * handler was registered] 953c2e6844SAndreas Gohr * @return bool 963c2e6844SAndreas Gohr */ 97748e747fSAnna Dabrowska public function handlePagesaveBefore(Doku_Event $event, $param) 98d6d97f60SAnna Dabrowska { 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 103df8d9fffSMichael 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 */ 130748e747fSAnna Dabrowska public function handlePagesaveAfter(Doku_Event $event, $param) 131d6d97f60SAnna Dabrowska { 13256672c36SAndreas Gohr global $ACT; 13387dc1344SAndreas Gohr if ($ACT == 'revert') return false; // handled in revert 1343c2e6844SAndreas Gohr 135025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 13658cb1498SAndreas Gohr if ($event->data['changeType'] == DOKU_CHANGE_TYPE_DELETE && empty($GLOBALS['PLUGIN_MOVE_WORKING'])) { 13758cb1498SAndreas Gohr // clear all data on delete unless it's a move operation 138eeb8d29fSAndreas Gohr $tables = $assignments->getPageAssignments($event->data['id']); 139eeb8d29fSAndreas Gohr foreach ($tables as $table) { 1404cd5cc28SAnna Dabrowska $schemaData = AccessTable::getPageAccess($table, $event->data['id']); 1416ebbbb8eSAndreas Gohr if ($schemaData->getSchema()->isEditable()) { 142eeb8d29fSAndreas Gohr $schemaData->clearData(); 1433c2e6844SAndreas Gohr } 1446ebbbb8eSAndreas Gohr } 145eeb8d29fSAndreas Gohr } else { 146eeb8d29fSAndreas Gohr // save the provided data 14787dc1344SAndreas Gohr if ($this->tosave) foreach ($this->tosave as $validation) { 1486ebbbb8eSAndreas Gohr if ($validation->getAccessTable()->getSchema()->isEditable()) { 14987dc1344SAndreas Gohr $validation->saveData($event->data['newRevision']); 150956fa7d4SAndreas Gohr 151956fa7d4SAndreas Gohr // make sure this schema is assigned 15287dc1344SAndreas Gohr $assignments->assignPageSchema( 15387dc1344SAndreas Gohr $event->data['id'], 15487dc1344SAndreas Gohr $validation->getAccessTable()->getSchema()->getTable() 15587dc1344SAndreas Gohr ); 1563c2e6844SAndreas Gohr } 1573c2e6844SAndreas Gohr } 1586ebbbb8eSAndreas Gohr } 15987dc1344SAndreas Gohr return true; 160eeb8d29fSAndreas Gohr } 161549a0837SAndreas Gohr} 162549a0837SAndreas Gohr 163549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 164