xref: /plugin/struct/action/entry.php (revision 93ca6f4f4326051070b84b0a5019072b884cd2c5)
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;
14*93ca6f4fSAndreas 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
35*93ca6f4fSAndreas 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
68*93ca6f4fSAndreas Gohr        $valid = AccessDataValidator::validateDataForPage($INPUT->arr(self::$VAR), $ID, $errors);
69*93ca6f4fSAndreas Gohr        if($valid === false) {
7087dc1344SAndreas Gohr            $this->validated = false;
71*93ca6f4fSAndreas Gohr            foreach($errors as $error) {
72c8a548a8SAndreas Gohr                msg(hsc($error), -1);
73bd92cd68SAndreas Gohr            }
7487dc1344SAndreas Gohr        } else {
75*93ca6f4fSAndreas Gohr            $this->validated = true;
76*93ca6f4fSAndreas 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
1037dac04ffSAndreas Gohr        if(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
134956fa7d4SAndreas Gohr        $assignments = new Assignments();
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());
140eeb8d29fSAndreas Gohr                $schemaData->clearData();
1413c2e6844SAndreas Gohr            }
142eeb8d29fSAndreas Gohr        } else {
143eeb8d29fSAndreas Gohr            // save the provided data
14487dc1344SAndreas Gohr            if($this->tosave) foreach($this->tosave as $validation) {
14587dc1344SAndreas Gohr                $validation->saveData($event->data['newRevision']);
146956fa7d4SAndreas Gohr
147956fa7d4SAndreas Gohr                // make sure this schema is assigned
14887dc1344SAndreas Gohr                $assignments->assignPageSchema(
14987dc1344SAndreas Gohr                    $event->data['id'],
15087dc1344SAndreas Gohr                    $validation->getAccessTable()->getSchema()->getTable()
15187dc1344SAndreas Gohr                );
1523c2e6844SAndreas Gohr            }
1533c2e6844SAndreas Gohr        }
15487dc1344SAndreas Gohr        return true;
155eeb8d29fSAndreas Gohr    }
1563c2e6844SAndreas Gohr
1573c2e6844SAndreas Gohr    /**
15865598e4aSAndreas Gohr     * Create the form to edit schemadata
159f36fda9dSAndreas Gohr     *
160c2fd0bf0SMichael Große     * @param string $tablename
16165598e4aSAndreas Gohr     * @return string The HTML for this schema's form
162c2fd0bf0SMichael Große     */
16365598e4aSAndreas Gohr    protected function createForm($tablename) {
164c2fd0bf0SMichael Große        global $ID;
16583beda18SAndreas Gohr        global $REV;
16617560ecbSAndreas Gohr        global $INPUT;
167fc13e8e7SMichael Große        if(auth_quickaclcheck($ID) == AUTH_READ) return '';
168cb249c51SMichael Große        if(checklock($ID)) return '';
169f411d872SAndreas Gohr        $schema = AccessTable::byTableName($tablename, $ID, $REV);
170c2fd0bf0SMichael Große        $schemadata = $schema->getData();
171c2fd0bf0SMichael Große
17217560ecbSAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
17317560ecbSAndreas Gohr        if(isset($structdata[$tablename])) {
17417560ecbSAndreas Gohr            $postdata = $structdata[$tablename];
17517560ecbSAndreas Gohr        } else {
17617560ecbSAndreas Gohr            $postdata = array();
17717560ecbSAndreas Gohr        }
17817560ecbSAndreas Gohr
1795d5ff984SAndreas Gohr        // we need a short, unique identifier to use in the cookie. this should be good enough
1805d5ff984SAndreas Gohr        $schemaid = 'SRCT' . substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
1815d5ff984SAndreas Gohr        $html = '<fieldset data-schema="' . $schemaid . '">';
18234bdbea7SAndreas Gohr        $html .= '<legend>' . hsc($tablename) . '</legend>';
183053212b1SAndreas Gohr        foreach($schemadata as $field) {
184053212b1SAndreas Gohr            $label = $field->getColumn()->getLabel();
18517560ecbSAndreas Gohr            if(isset($postdata[$label])) {
18617560ecbSAndreas Gohr                // posted data trumps stored data
18717560ecbSAndreas Gohr                $field->setValue($postdata[$label]);
18817560ecbSAndreas Gohr            }
18995838d50SAndreas Gohr            $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]");
190ed3de3d6SAndreas Gohr        }
191ed3de3d6SAndreas Gohr        $html .= '</fieldset>';
192ed3de3d6SAndreas Gohr
193ed3de3d6SAndreas Gohr        return $html;
194ed3de3d6SAndreas Gohr    }
195ed3de3d6SAndreas Gohr
196ed3de3d6SAndreas Gohr    /**
197ed3de3d6SAndreas Gohr     * Create the input field
198ed3de3d6SAndreas Gohr     *
199ed3de3d6SAndreas Gohr     * @param Value $field
200ed3de3d6SAndreas Gohr     * @param String $name field's name
201ed3de3d6SAndreas Gohr     * @return string
202ed3de3d6SAndreas Gohr     */
2036b5e52fdSAndreas Gohr    public function makeField(Value $field, $name) {
2049e9bee91SAndreas Gohr        $trans = hsc($field->getColumn()->getTranslatedLabel());
2053a717675SAndreas Gohr        $hint = hsc($field->getColumn()->getTranslatedHint());
2063a717675SAndreas Gohr        $class = $hint ? 'hashint' : '';
207ed3de3d6SAndreas Gohr        $colname = $field->getColumn()->getFullQualifiedLabel();
2083a717675SAndreas Gohr
209053212b1SAndreas Gohr        $input = $field->getValueEditor($name);
2102350b48eSAndreas Gohr
2112350b48eSAndreas Gohr        // we keep all the custom form stuff the field might produce, but hide it
2122350b48eSAndreas Gohr        if(!$field->getColumn()->isVisibleInEditor()) {
2132350b48eSAndreas Gohr            $hide = 'style="display:none"';
2142350b48eSAndreas Gohr        } else {
2152350b48eSAndreas Gohr            $hide = '';
2162350b48eSAndreas Gohr        }
2172350b48eSAndreas Gohr
218ed3de3d6SAndreas Gohr        $html = '';
219ed3de3d6SAndreas Gohr        $html .= "<label $hide data-column=\"$colname\">";
2203a717675SAndreas Gohr        $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
2213a717675SAndreas Gohr        $html .= "<span class=\"input\">$input</span>";
2223a717675SAndreas Gohr        $html .= '</label>';
22365598e4aSAndreas Gohr
22465598e4aSAndreas Gohr        return $html;
225549a0837SAndreas Gohr    }
226549a0837SAndreas Gohr}
227549a0837SAndreas Gohr
228549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
229