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 107234bfb1Ssplitbrainuse dokuwiki\Extension\ActionPlugin; 117234bfb1Ssplitbrainuse dokuwiki\Extension\EventHandler; 127234bfb1Ssplitbrainuse dokuwiki\Extension\Event; 130549dcc5SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessDataValidator; 14f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable; 1587dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments; 16c2fd0bf0SMichael Große 173c2e6844SAndreas Gohr/** 183c2e6844SAndreas Gohr * Class action_plugin_struct_entry 193c2e6844SAndreas Gohr * 2069f7ec8fSAnna Dabrowska * Handles the entry process of struct data with type "page" 213c2e6844SAndreas Gohr */ 227234bfb1Ssplitbrainclass action_plugin_struct_entry extends ActionPlugin 23d6d97f60SAnna Dabrowska{ 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 * 41*5e29103aSannda * @param EventHandler $controller DokuWiki's event controller object 42549a0837SAndreas Gohr * @return void 43549a0837SAndreas Gohr */ 447234bfb1Ssplitbrain public function register(EventHandler $controller) 45d6d97f60SAnna Dabrowska { 463c2e6844SAndreas Gohr // validate data on preview and save; 47748e747fSAnna Dabrowska $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleValidation'); 483c2e6844SAndreas Gohr // ensure a page revision is created when struct data changes: 49748e747fSAnna Dabrowska $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handlePagesaveBefore'); 503c2e6844SAndreas Gohr // save struct data after page has been saved: 51748e747fSAnna Dabrowska $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handlePagesaveAfter'); 52549a0837SAndreas Gohr } 53549a0837SAndreas Gohr 54549a0837SAndreas Gohr /** 553c2e6844SAndreas Gohr * Clean up and validate the input data 563c2e6844SAndreas Gohr * 57*5e29103aSannda * @param Event $event event object by reference 583c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 593c2e6844SAndreas Gohr * handler was registered] 603c2e6844SAndreas Gohr * @return bool 613c2e6844SAndreas Gohr */ 627234bfb1Ssplitbrain public function handleValidation(Event $event, $param) 63d6d97f60SAnna Dabrowska { 643ece9074SMichael Große global $ID, $INPUT; 6517560ecbSAndreas Gohr $act = act_clean($event->data); 667234bfb1Ssplitbrain if (!in_array($act, ['save', 'preview'])) return false; 677234bfb1Ssplitbrain $this->tosave = []; 6804641d56SMichael Große 6987dc1344SAndreas Gohr // run the validation for each assignded schema 7093ca6f4fSAndreas Gohr $valid = AccessDataValidator::validateDataForPage($INPUT->arr(self::$VAR), $ID, $errors); 7193ca6f4fSAndreas Gohr if ($valid === false) { 7287dc1344SAndreas Gohr $this->validated = false; 7393ca6f4fSAndreas Gohr foreach ($errors as $error) { 74c8a548a8SAndreas Gohr msg(hsc($error), -1); 75bd92cd68SAndreas Gohr } 7687dc1344SAndreas Gohr } else { 7793ca6f4fSAndreas Gohr $this->validated = true; 7893ca6f4fSAndreas Gohr $this->tosave = $valid; 7987dc1344SAndreas Gohr } 8087dc1344SAndreas Gohr 8187dc1344SAndreas Gohr // FIXME we used to set the cleaned data as new input data. this caused #140 8287dc1344SAndreas Gohr // could we just not do that, and keep the cleaning to saving only? and fix that bug this way? 83bd92cd68SAndreas Gohr 8417560ecbSAndreas Gohr // did validation go through? otherwise abort saving 853c2e6844SAndreas Gohr if (!$this->validated && $act == 'save') { 8617560ecbSAndreas Gohr $event->data = 'edit'; 8717560ecbSAndreas Gohr } 8817560ecbSAndreas Gohr 8987dc1344SAndreas Gohr return true; 9004641d56SMichael Große } 9104641d56SMichael Große 9217560ecbSAndreas Gohr /** 933c2e6844SAndreas Gohr * Check if the page has to be changed 943c2e6844SAndreas Gohr * 95*5e29103aSannda * @param Event $event event object by reference 963c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 973c2e6844SAndreas Gohr * handler was registered] 983c2e6844SAndreas Gohr * @return bool 993c2e6844SAndreas Gohr */ 1007234bfb1Ssplitbrain public function handlePagesaveBefore(Event $event, $param) 101d6d97f60SAnna Dabrowska { 10287dc1344SAndreas Gohr if ($event->data['contentChanged']) return false; // will be saved for page changes 103d683a527SAndreas Gohr global $ACT; 10487dc1344SAndreas Gohr if ($ACT == 'revert') return false; // this is handled in revert.php 1057dac04ffSAndreas Gohr 106df8d9fffSMichael Große if ((is_array($this->tosave) && count($this->tosave)) || isset($GLOBALS['struct_plugin_force_page_save'])) { 1073c2e6844SAndreas Gohr if (trim($event->data['newContent']) === '') { 1083c2e6844SAndreas Gohr // this happens when a new page is tried to be created with only struct data 1093c2e6844SAndreas Gohr msg($this->getLang('emptypage'), -1); 1103c2e6844SAndreas Gohr } else { 1113c2e6844SAndreas Gohr $event->data['contentChanged'] = true; // save for data changes 11248010be8SAndreas Gohr 11348010be8SAndreas Gohr // add a summary 11448010be8SAndreas Gohr if (empty($event->data['summary'])) { 11548010be8SAndreas Gohr $event->data['summary'] = $this->getLang('summary'); 11648010be8SAndreas Gohr } 1173c2e6844SAndreas Gohr } 118d683a527SAndreas Gohr } 11987dc1344SAndreas Gohr 12087dc1344SAndreas Gohr return true; 1213c2e6844SAndreas Gohr } 1223c2e6844SAndreas Gohr 1233c2e6844SAndreas Gohr /** 1243c2e6844SAndreas Gohr * Save the data 1253c2e6844SAndreas Gohr * 12687dc1344SAndreas Gohr * When this is called, INPUT data has been validated already. 12756672c36SAndreas Gohr * 128*5e29103aSannda * @param Event $event event object by reference 1293c2e6844SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 1303c2e6844SAndreas Gohr * handler was registered] 1313c2e6844SAndreas Gohr * @return bool 1323c2e6844SAndreas Gohr */ 1337234bfb1Ssplitbrain public function handlePagesaveAfter(Event $event, $param) 134d6d97f60SAnna Dabrowska { 13556672c36SAndreas Gohr global $ACT; 13687dc1344SAndreas Gohr if ($ACT == 'revert') return false; // handled in revert 1373c2e6844SAndreas Gohr 138025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 13958cb1498SAndreas Gohr if ($event->data['changeType'] == DOKU_CHANGE_TYPE_DELETE && empty($GLOBALS['PLUGIN_MOVE_WORKING'])) { 14058cb1498SAndreas Gohr // clear all data on delete unless it's a move operation 141eeb8d29fSAndreas Gohr $tables = $assignments->getPageAssignments($event->data['id']); 142eeb8d29fSAndreas Gohr foreach ($tables as $table) { 1434cd5cc28SAnna Dabrowska $schemaData = AccessTable::getPageAccess($table, $event->data['id']); 1446ebbbb8eSAndreas Gohr if ($schemaData->getSchema()->isEditable()) { 145eeb8d29fSAndreas Gohr $schemaData->clearData(); 1463c2e6844SAndreas Gohr } 1476ebbbb8eSAndreas Gohr } 1487234bfb1Ssplitbrain } elseif ($this->tosave) { 149eeb8d29fSAndreas Gohr // save the provided data 1507234bfb1Ssplitbrain foreach ($this->tosave as $validation) { 1516ebbbb8eSAndreas Gohr if ($validation->getAccessTable()->getSchema()->isEditable()) { 15287dc1344SAndreas Gohr $validation->saveData($event->data['newRevision']); 153956fa7d4SAndreas Gohr 154956fa7d4SAndreas Gohr // make sure this schema is assigned 15587dc1344SAndreas Gohr $assignments->assignPageSchema( 15687dc1344SAndreas Gohr $event->data['id'], 15787dc1344SAndreas Gohr $validation->getAccessTable()->getSchema()->getTable() 15887dc1344SAndreas Gohr ); 1593c2e6844SAndreas Gohr } 1603c2e6844SAndreas Gohr } 1616ebbbb8eSAndreas Gohr } 16287dc1344SAndreas Gohr return true; 163eeb8d29fSAndreas Gohr } 164549a0837SAndreas Gohr} 165549a0837SAndreas Gohr 166549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 167