xref: /plugin/struct/action/entry.php (revision c8a548a86ff277414b4aa64fb1d00e574940a79d)
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
12fb31ca9fSAndreas Gohruse plugin\struct\meta\Assignments;
13c2fd0bf0SMichael Großeuse plugin\struct\meta\SchemaData;
1417560ecbSAndreas Gohruse plugin\struct\meta\ValidationException;
15*c8a548a8SAndreas Gohruse plugin\struct\meta\Validator;
1617560ecbSAndreas Gohruse plugin\struct\types\AbstractBaseType;
17c2fd0bf0SMichael Große
183c2e6844SAndreas Gohr/**
193c2e6844SAndreas Gohr * Class action_plugin_struct_entry
203c2e6844SAndreas Gohr *
213c2e6844SAndreas Gohr * Handles the whole struct data entry process
223c2e6844SAndreas Gohr */
23549a0837SAndreas Gohrclass action_plugin_struct_entry extends DokuWiki_Action_Plugin {
24549a0837SAndreas Gohr
2517560ecbSAndreas Gohr    /**
2617560ecbSAndreas Gohr     * @var string The form name we use to transfer schema data
2717560ecbSAndreas Gohr     */
2817560ecbSAndreas Gohr    protected static $VAR = 'struct_schema_data';
29c2fd0bf0SMichael Große
30c2fd0bf0SMichael Große    /** @var helper_plugin_sqlite */
31c2fd0bf0SMichael Große    protected $sqlite;
32c2fd0bf0SMichael Große
333c2e6844SAndreas Gohr    /** @var  bool has the data been validated correctly? */
343c2e6844SAndreas Gohr    protected $validated;
353c2e6844SAndreas Gohr
363c2e6844SAndreas Gohr    /** @var  array these schemas have changed data and need to be saved */
373c2e6844SAndreas Gohr    protected $tosave;
383c2e6844SAndreas Gohr
39549a0837SAndreas Gohr    /**
40549a0837SAndreas Gohr     * Registers a callback function for a given event
41549a0837SAndreas Gohr     *
42549a0837SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
43549a0837SAndreas Gohr     * @return void
44549a0837SAndreas Gohr     */
45549a0837SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
463c2e6844SAndreas Gohr        // add the struct editor to the edit form;
47c2fd0bf0SMichael Große        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
483c2e6844SAndreas Gohr        // validate data on preview and save;
493c2e6844SAndreas Gohr        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_validation');
503c2e6844SAndreas Gohr        // ensure a page revision is created when struct data changes:
513c2e6844SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before');
523c2e6844SAndreas Gohr        // save struct data after page has been saved:
533c2e6844SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_pagesave_after');
54549a0837SAndreas Gohr    }
55549a0837SAndreas Gohr
56549a0837SAndreas Gohr    /**
573c2e6844SAndreas Gohr     * Enhance the editing form with structural data editing
5804641d56SMichael Große     *
5904641d56SMichael Große     * @param Doku_Event $event event object by reference
6004641d56SMichael Große     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
6104641d56SMichael Große     *                           handler was registered]
6204641d56SMichael Große     * @return bool
6304641d56SMichael Große     */
643c2e6844SAndreas Gohr    public function handle_editform(Doku_Event $event, $param) {
653c2e6844SAndreas Gohr        global $ID;
663c2e6844SAndreas Gohr
673c2e6844SAndreas Gohr        $assignments = new Assignments();
683c2e6844SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
693c2e6844SAndreas Gohr
703c2e6844SAndreas Gohr        $html = '';
713c2e6844SAndreas Gohr        foreach($tables as $table) {
723c2e6844SAndreas Gohr            $html .= $this->createForm($table);
733c2e6844SAndreas Gohr        }
743c2e6844SAndreas Gohr
753c2e6844SAndreas Gohr        /** @var Doku_Form $form */
763c2e6844SAndreas Gohr        $form = $event->data;
773c2e6844SAndreas Gohr        $html = "<div class=\"struct\">$html</div>";
783c2e6844SAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
793c2e6844SAndreas Gohr        $form->insertElement($pos, $html);
803c2e6844SAndreas Gohr
813c2e6844SAndreas Gohr        return true;
823c2e6844SAndreas Gohr    }
833c2e6844SAndreas Gohr
843c2e6844SAndreas Gohr    /**
853c2e6844SAndreas Gohr     * Clean up and validate the input data
863c2e6844SAndreas Gohr     *
873c2e6844SAndreas Gohr     * @param Doku_Event $event event object by reference
883c2e6844SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
893c2e6844SAndreas Gohr     *                           handler was registered]
903c2e6844SAndreas Gohr     * @return bool
913c2e6844SAndreas Gohr     */
923c2e6844SAndreas Gohr    public function handle_validation(Doku_Event $event, $param) {
933ece9074SMichael Große        global $ID, $INPUT;
9417560ecbSAndreas Gohr        $act = act_clean($event->data);
9517560ecbSAndreas Gohr        if(!in_array($act, array('save', 'preview'))) return false;
9604641d56SMichael Große
97*c8a548a8SAndreas Gohr        // execute the validator
98*c8a548a8SAndreas Gohr        $validator = new Validator();
99*c8a548a8SAndreas Gohr        $this->validated = $validator->validate($INPUT->arr(self::$VAR), $ID);
100*c8a548a8SAndreas Gohr        $this->tosave = $validator->getChangedSchemas();
101*c8a548a8SAndreas Gohr        $INPUT->post->set(self::$VAR, $validator->getCleanedData());
10204641d56SMichael Große
103*c8a548a8SAndreas Gohr        if(!$this->validated) foreach($validator->getErrors() as $error) {
104*c8a548a8SAndreas Gohr            msg(hsc($error), -1);
105bd92cd68SAndreas Gohr        }
106bd92cd68SAndreas Gohr
10717560ecbSAndreas Gohr        // did validation go through? otherwise abort saving
1083c2e6844SAndreas Gohr        if(!$this->validated && $act == 'save') {
10917560ecbSAndreas Gohr            $event->data = 'edit';
11017560ecbSAndreas Gohr        }
11117560ecbSAndreas Gohr
11204641d56SMichael Große        return false;
11304641d56SMichael Große    }
11404641d56SMichael Große
11517560ecbSAndreas Gohr    /**
1163c2e6844SAndreas Gohr     * Check if the page has to be changed
1173c2e6844SAndreas Gohr     *
1183c2e6844SAndreas Gohr     * @param Doku_Event $event event object by reference
1193c2e6844SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
1203c2e6844SAndreas Gohr     *                           handler was registered]
1213c2e6844SAndreas Gohr     * @return bool
1223c2e6844SAndreas Gohr     */
1233c2e6844SAndreas Gohr    public function handle_pagesave_before(Doku_Event $event, $param) {
1243c2e6844SAndreas Gohr        if($event->data['contentChanged']) return; // will be saved for page changes
1253c2e6844SAndreas Gohr        if(count($this->tosave)) {
1263c2e6844SAndreas Gohr            if(trim($event->data['newContent']) === '') {
1273c2e6844SAndreas Gohr                // this happens when a new page is tried to be created with only struct data
1283c2e6844SAndreas Gohr                msg($this->getLang('emptypage'), -1);
1293c2e6844SAndreas Gohr            } else {
1303c2e6844SAndreas Gohr                $event->data['contentChanged'] = true; // save for data changes
1313c2e6844SAndreas Gohr            }
1323c2e6844SAndreas Gohr        }
1333c2e6844SAndreas Gohr    }
1343c2e6844SAndreas Gohr
1353c2e6844SAndreas Gohr    /**
1363c2e6844SAndreas Gohr     * Save the data
1373c2e6844SAndreas Gohr     *
13856672c36SAndreas Gohr     * When this is called, INPUT data has been validated already. On a restore action, the data is
13956672c36SAndreas Gohr     * loaded from the database and not validated again.
14056672c36SAndreas Gohr     *
1413c2e6844SAndreas Gohr     * @param Doku_Event $event event object by reference
1423c2e6844SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
1433c2e6844SAndreas Gohr     *                           handler was registered]
1443c2e6844SAndreas Gohr     * @return bool
1453c2e6844SAndreas Gohr     */
1463c2e6844SAndreas Gohr    public function handle_pagesave_after(Doku_Event $event, $param) {
1473c2e6844SAndreas Gohr        global $INPUT;
14856672c36SAndreas Gohr        global $ACT;
14956672c36SAndreas Gohr        global $REV;
1503c2e6844SAndreas Gohr
151956fa7d4SAndreas Gohr        $assignments = new Assignments();
152956fa7d4SAndreas Gohr
15356672c36SAndreas Gohr        if($ACT == 'revert' && $REV) {
15456672c36SAndreas Gohr            // reversion is a special case, we load the data to restore from DB:
15556672c36SAndreas Gohr            $structData = array();
15656672c36SAndreas Gohr            $this->tosave = $assignments->getPageAssignments($event->data['id']);
15756672c36SAndreas Gohr            foreach($this->tosave as $table) {
15856672c36SAndreas Gohr                $oldData = new SchemaData($table, $event->data['id'], $REV);
15956672c36SAndreas Gohr                $structData[$table] = $oldData->getDataArray();
16056672c36SAndreas Gohr            }
16156672c36SAndreas Gohr        } else {
16256672c36SAndreas Gohr            // data comes from the edit form
1633c2e6844SAndreas Gohr            $structData = $INPUT->arr(self::$VAR);
16456672c36SAndreas Gohr        }
1653c2e6844SAndreas Gohr
1663c2e6844SAndreas Gohr        if($event->data['changeType'] == DOKU_CHANGE_TYPE_DELETE) {
167eeb8d29fSAndreas Gohr            // clear all data
168eeb8d29fSAndreas Gohr            $tables = $assignments->getPageAssignments($event->data['id']);
169eeb8d29fSAndreas Gohr            foreach($tables as $table) {
170eeb8d29fSAndreas Gohr                $schemaData = new SchemaData($table, $event->data['id'], time());
171eeb8d29fSAndreas Gohr                $schemaData->clearData();
1723c2e6844SAndreas Gohr            }
173eeb8d29fSAndreas Gohr        } else {
174eeb8d29fSAndreas Gohr            // save the provided data
1753c2e6844SAndreas Gohr            foreach($this->tosave as $table) {
1763c2e6844SAndreas Gohr                $schemaData = new SchemaData($table, $event->data['id'], $event->data['newRevision']);
1773c2e6844SAndreas Gohr                $schemaData->saveData($structData[$table]);
178956fa7d4SAndreas Gohr
179956fa7d4SAndreas Gohr                // make sure this schema is assigned
180956fa7d4SAndreas Gohr                $assignments->assignPageSchema($event->data['id'], $table);
1813c2e6844SAndreas Gohr            }
1823c2e6844SAndreas Gohr        }
183eeb8d29fSAndreas Gohr    }
1843c2e6844SAndreas Gohr
1853c2e6844SAndreas Gohr    /**
18665598e4aSAndreas Gohr     * Create the form to edit schemadata
187f36fda9dSAndreas Gohr     *
188c2fd0bf0SMichael Große     * @param string $tablename
18965598e4aSAndreas Gohr     * @return string The HTML for this schema's form
190c2fd0bf0SMichael Große     */
19165598e4aSAndreas Gohr    protected function createForm($tablename) {
192c2fd0bf0SMichael Große        global $ID;
19383beda18SAndreas Gohr        global $REV;
19417560ecbSAndreas Gohr        global $INPUT;
195fc13e8e7SMichael Große        if (auth_quickaclcheck($ID) == AUTH_READ) return '';
196cb249c51SMichael Große        if (checklock($ID)) return '';
19783beda18SAndreas Gohr        $schema = new SchemaData($tablename, $ID, $REV);
198c2fd0bf0SMichael Große        $schemadata = $schema->getData();
199c2fd0bf0SMichael Große
20017560ecbSAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
20117560ecbSAndreas Gohr        if(isset($structdata[$tablename])) {
20217560ecbSAndreas Gohr            $postdata = $structdata[$tablename];
20317560ecbSAndreas Gohr        } else {
20417560ecbSAndreas Gohr            $postdata = array();
20517560ecbSAndreas Gohr        }
20617560ecbSAndreas Gohr
2075d5ff984SAndreas Gohr        // we need a short, unique identifier to use in the cookie. this should be good enough
2085d5ff984SAndreas Gohr        $schemaid = 'SRCT'.substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
2095d5ff984SAndreas Gohr        $html = '<fieldset data-schema="' . $schemaid . '">';
21034bdbea7SAndreas Gohr        $html .= '<legend>' . hsc($tablename) . '</legend>';
211053212b1SAndreas Gohr        foreach($schemadata as $field) {
212053212b1SAndreas Gohr            $label = $field->getColumn()->getLabel();
21317560ecbSAndreas Gohr            if(isset($postdata[$label])) {
21417560ecbSAndreas Gohr                // posted data trumps stored data
21517560ecbSAndreas Gohr                $field->setValue($postdata[$label]);
21617560ecbSAndreas Gohr            }
2179e9bee91SAndreas Gohr            $trans = hsc($field->getColumn()->getTranslatedLabel());
2183a717675SAndreas Gohr            $hint  = hsc($field->getColumn()->getTranslatedHint());
2193a717675SAndreas Gohr            $class = $hint ? 'hashint' : '';
2203a717675SAndreas Gohr
22117560ecbSAndreas Gohr            $name = self::$VAR . "[$tablename][$label]";
222053212b1SAndreas Gohr            $input = $field->getValueEditor($name);
2233a717675SAndreas Gohr            $html .= '<label>';
2243a717675SAndreas Gohr            $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
2253a717675SAndreas Gohr            $html .= "<span class=\"input\">$input</span>";
2263a717675SAndreas Gohr            $html .= '</label>';
227c2fd0bf0SMichael Große        }
22834bdbea7SAndreas Gohr        $html .= '</fieldset>';
22965598e4aSAndreas Gohr
23065598e4aSAndreas Gohr        return $html;
231549a0837SAndreas Gohr    }
232549a0837SAndreas Gohr
233*c8a548a8SAndreas Gohr
234549a0837SAndreas Gohr}
235549a0837SAndreas Gohr
236549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
237