xref: /plugin/struct/action/entry.php (revision 748e747f37aa44250ee32847b5fc3ff1e47f0835)
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
10f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
1187dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
1293ca6f4fSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessDataValidator;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
14c2fd0bf0SMichael Große
153c2e6844SAndreas Gohr/**
163c2e6844SAndreas Gohr * Class action_plugin_struct_entry
173c2e6844SAndreas Gohr *
1869f7ec8fSAnna Dabrowska * Handles the entry process of struct data with type "page"
193c2e6844SAndreas Gohr */
20d6d97f60SAnna Dabrowskaclass action_plugin_struct_entry extends DokuWiki_Action_Plugin
21d6d97f60SAnna Dabrowska{
22549a0837SAndreas Gohr
2317560ecbSAndreas Gohr    /**
2417560ecbSAndreas Gohr     * @var string The form name we use to transfer schema data
2517560ecbSAndreas Gohr     */
2617560ecbSAndreas Gohr    protected static $VAR = 'struct_schema_data';
27c2fd0bf0SMichael Große
28c2fd0bf0SMichael Große    /** @var helper_plugin_sqlite */
29c2fd0bf0SMichael Große    protected $sqlite;
30c2fd0bf0SMichael Große
313c2e6844SAndreas Gohr    /** @var  bool has the data been validated correctly? */
323c2e6844SAndreas Gohr    protected $validated;
333c2e6844SAndreas Gohr
3493ca6f4fSAndreas Gohr    /** @var  AccessDataValidator[] these schemas are validated and have changed data and need to be saved */
353c2e6844SAndreas Gohr    protected $tosave;
363c2e6844SAndreas Gohr
37549a0837SAndreas Gohr    /**
38549a0837SAndreas Gohr     * Registers a callback function for a given event
39549a0837SAndreas Gohr     *
40549a0837SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
41549a0837SAndreas Gohr     * @return void
42549a0837SAndreas Gohr     */
43d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
44d6d97f60SAnna Dabrowska    {
453c2e6844SAndreas Gohr        // validate data on preview and save;
46*748e747fSAnna Dabrowska        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleValidation');
473c2e6844SAndreas Gohr        // ensure a page revision is created when struct data changes:
48*748e747fSAnna Dabrowska        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handlePagesaveBefore');
493c2e6844SAndreas Gohr        // save struct data after page has been saved:
50*748e747fSAnna Dabrowska        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handlePagesaveAfter');
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     */
61*748e747fSAnna Dabrowska    public function handleValidation(Doku_Event $event, $param)
62d6d97f60SAnna Dabrowska    {
633ece9074SMichael Große        global $ID, $INPUT;
6417560ecbSAndreas Gohr        $act = act_clean($event->data);
6517560ecbSAndreas Gohr        if (!in_array($act, array('save', 'preview'))) return false;
6667036dabSAndreas Gohr        $this->tosave = array();
6704641d56SMichael Große
6887dc1344SAndreas Gohr        // run the validation for each assignded schema
6993ca6f4fSAndreas Gohr        $valid = AccessDataValidator::validateDataForPage($INPUT->arr(self::$VAR), $ID, $errors);
7093ca6f4fSAndreas Gohr        if ($valid === false) {
7187dc1344SAndreas Gohr            $this->validated = false;
7293ca6f4fSAndreas Gohr            foreach ($errors as $error) {
73c8a548a8SAndreas Gohr                msg(hsc($error), -1);
74bd92cd68SAndreas Gohr            }
7587dc1344SAndreas Gohr        } else {
7693ca6f4fSAndreas Gohr            $this->validated = true;
7793ca6f4fSAndreas Gohr            $this->tosave = $valid;
7887dc1344SAndreas Gohr        }
7987dc1344SAndreas Gohr
8087dc1344SAndreas Gohr        // FIXME we used to set the cleaned data as new input data. this caused #140
8187dc1344SAndreas Gohr        // could we just not do that, and keep the cleaning to saving only? and fix that bug this way?
82bd92cd68SAndreas Gohr
8317560ecbSAndreas Gohr        // did validation go through? otherwise abort saving
843c2e6844SAndreas Gohr        if (!$this->validated && $act == 'save') {
8517560ecbSAndreas Gohr            $event->data = 'edit';
8617560ecbSAndreas Gohr        }
8717560ecbSAndreas Gohr
8887dc1344SAndreas Gohr        return true;
8904641d56SMichael Große    }
9004641d56SMichael Große
9117560ecbSAndreas Gohr    /**
923c2e6844SAndreas Gohr     * Check if the page has to be changed
933c2e6844SAndreas Gohr     *
943c2e6844SAndreas Gohr     * @param Doku_Event $event event object by reference
953c2e6844SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
963c2e6844SAndreas Gohr     *                           handler was registered]
973c2e6844SAndreas Gohr     * @return bool
983c2e6844SAndreas Gohr     */
99*748e747fSAnna Dabrowska    public function handlePagesaveBefore(Doku_Event $event, $param)
100d6d97f60SAnna Dabrowska    {
10187dc1344SAndreas Gohr        if ($event->data['contentChanged']) return false; // will be saved for page changes
102d683a527SAndreas Gohr        global $ACT;
10387dc1344SAndreas Gohr        if ($ACT == 'revert') return false; // this is handled in revert.php
1047dac04ffSAndreas Gohr
105df8d9fffSMichael Große        if ((is_array($this->tosave) && count($this->tosave)) || isset($GLOBALS['struct_plugin_force_page_save'])) {
1063c2e6844SAndreas Gohr            if (trim($event->data['newContent']) === '') {
1073c2e6844SAndreas Gohr                // this happens when a new page is tried to be created with only struct data
1083c2e6844SAndreas Gohr                msg($this->getLang('emptypage'), -1);
1093c2e6844SAndreas Gohr            } else {
1103c2e6844SAndreas Gohr                $event->data['contentChanged'] = true; // save for data changes
11148010be8SAndreas Gohr
11248010be8SAndreas Gohr                // add a summary
11348010be8SAndreas Gohr                if (empty($event->data['summary'])) {
11448010be8SAndreas Gohr                    $event->data['summary'] = $this->getLang('summary');
11548010be8SAndreas Gohr                }
1163c2e6844SAndreas Gohr            }
117d683a527SAndreas Gohr        }
11887dc1344SAndreas Gohr
11987dc1344SAndreas Gohr        return true;
1203c2e6844SAndreas Gohr    }
1213c2e6844SAndreas Gohr
1223c2e6844SAndreas Gohr    /**
1233c2e6844SAndreas Gohr     * Save the data
1243c2e6844SAndreas Gohr     *
12587dc1344SAndreas Gohr     * When this is called, INPUT data has been validated already.
12656672c36SAndreas Gohr     *
1273c2e6844SAndreas Gohr     * @param Doku_Event $event event object by reference
1283c2e6844SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
1293c2e6844SAndreas Gohr     *                           handler was registered]
1303c2e6844SAndreas Gohr     * @return bool
1313c2e6844SAndreas Gohr     */
132*748e747fSAnna Dabrowska    public function handlePagesaveAfter(Doku_Event $event, $param)
133d6d97f60SAnna Dabrowska    {
13456672c36SAndreas Gohr        global $ACT;
13587dc1344SAndreas Gohr        if ($ACT == 'revert') return false; // handled in revert
1363c2e6844SAndreas Gohr
137025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
13858cb1498SAndreas Gohr        if ($event->data['changeType'] == DOKU_CHANGE_TYPE_DELETE && empty($GLOBALS['PLUGIN_MOVE_WORKING'])) {
13958cb1498SAndreas Gohr            // clear all data on delete unless it's a move operation
140eeb8d29fSAndreas Gohr            $tables = $assignments->getPageAssignments($event->data['id']);
141eeb8d29fSAndreas Gohr            foreach ($tables as $table) {
142f392071eSAndreas Gohr                $schemaData = AccessTable::byTableName($table, $event->data['id'], time());
1436ebbbb8eSAndreas Gohr                if ($schemaData->getSchema()->isEditable()) {
144eeb8d29fSAndreas Gohr                    $schemaData->clearData();
1453c2e6844SAndreas Gohr                }
1466ebbbb8eSAndreas Gohr            }
147eeb8d29fSAndreas Gohr        } else {
148eeb8d29fSAndreas Gohr            // save the provided data
14987dc1344SAndreas Gohr            if ($this->tosave) foreach ($this->tosave as $validation) {
1506ebbbb8eSAndreas Gohr                if ($validation->getAccessTable()->getSchema()->isEditable()) {
15187dc1344SAndreas Gohr                    $validation->saveData($event->data['newRevision']);
152956fa7d4SAndreas Gohr
153956fa7d4SAndreas Gohr                    // make sure this schema is assigned
15487dc1344SAndreas Gohr                    $assignments->assignPageSchema(
15587dc1344SAndreas Gohr                        $event->data['id'],
15687dc1344SAndreas Gohr                        $validation->getAccessTable()->getSchema()->getTable()
15787dc1344SAndreas Gohr                    );
1583c2e6844SAndreas Gohr                }
1593c2e6844SAndreas Gohr            }
1606ebbbb8eSAndreas Gohr        }
16187dc1344SAndreas Gohr        return true;
162eeb8d29fSAndreas Gohr    }
163549a0837SAndreas Gohr}
164549a0837SAndreas Gohr
165549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
166