xref: /plugin/struct/action/entry.php (revision b8a9b73c022f1b595c0bebf5f628eb2abb3bfe8e)
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;
1517560ecbSAndreas Gohruse plugin\struct\types\AbstractBaseType;
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
353c2e6844SAndreas Gohr    /** @var  array these schemas 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        // add the struct editor to the edit form;
46c2fd0bf0SMichael Große        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
473c2e6844SAndreas Gohr        // validate data on preview and save;
483c2e6844SAndreas Gohr        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_validation');
493c2e6844SAndreas Gohr        // ensure a page revision is created when struct data changes:
503c2e6844SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before');
513c2e6844SAndreas Gohr        // save struct data after page has been saved:
523c2e6844SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_pagesave_after');
53549a0837SAndreas Gohr    }
54549a0837SAndreas Gohr
55549a0837SAndreas Gohr    /**
563c2e6844SAndreas Gohr     * Enhance the editing form with structural data editing
5704641d56SMichael Große     *
5804641d56SMichael Große     * @param Doku_Event $event event object by reference
5904641d56SMichael Große     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
6004641d56SMichael Große     *                           handler was registered]
6104641d56SMichael Große     * @return bool
6204641d56SMichael Große     */
633c2e6844SAndreas Gohr    public function handle_editform(Doku_Event $event, $param) {
643c2e6844SAndreas Gohr        global $ID;
653c2e6844SAndreas Gohr
663c2e6844SAndreas Gohr        $assignments = new Assignments();
673c2e6844SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
683c2e6844SAndreas Gohr
693c2e6844SAndreas Gohr        $html = '';
703c2e6844SAndreas Gohr        foreach($tables as $table) {
713c2e6844SAndreas Gohr            $html .= $this->createForm($table);
723c2e6844SAndreas Gohr        }
733c2e6844SAndreas Gohr
743c2e6844SAndreas Gohr        /** @var Doku_Form $form */
753c2e6844SAndreas Gohr        $form = $event->data;
763c2e6844SAndreas Gohr        $html = "<div class=\"struct\">$html</div>";
773c2e6844SAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
783c2e6844SAndreas Gohr        $form->insertElement($pos, $html);
793c2e6844SAndreas Gohr
803c2e6844SAndreas Gohr        return true;
813c2e6844SAndreas Gohr    }
823c2e6844SAndreas Gohr
833c2e6844SAndreas Gohr    /**
843c2e6844SAndreas Gohr     * Clean up and validate the input data
853c2e6844SAndreas Gohr     *
863c2e6844SAndreas Gohr     * @param Doku_Event $event event object by reference
873c2e6844SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
883c2e6844SAndreas Gohr     *                           handler was registered]
893c2e6844SAndreas Gohr     * @return bool
903c2e6844SAndreas Gohr     */
913c2e6844SAndreas Gohr    public function handle_validation(Doku_Event $event, $param) {
923ece9074SMichael Große        global $ID, $INPUT;
9317560ecbSAndreas Gohr        $act = act_clean($event->data);
9417560ecbSAndreas Gohr        if(!in_array($act, array('save', 'preview'))) return false;
9504641d56SMichael Große
96a8b9fb5bSAndreas Gohr        $assignments = new Assignments();
97a8b9fb5bSAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
9817560ecbSAndreas Gohr        $structData = $INPUT->arr(self::$VAR);
993c2e6844SAndreas Gohr        $timestamp = time();
10004641d56SMichael Große
1013c2e6844SAndreas Gohr        $this->tosave = array();
1023c2e6844SAndreas Gohr        $this->validated = true;
10304641d56SMichael Große        foreach($tables as $table) {
1043c2e6844SAndreas Gohr            $schemaData = new SchemaData($table, $ID, $timestamp);
1053c2e6844SAndreas Gohr            if(!$schemaData->getId()) {
106bd92cd68SAndreas Gohr                // this schema is not available for some reason. skip it
107bd92cd68SAndreas Gohr                continue;
108bd92cd68SAndreas Gohr            }
109bd92cd68SAndreas Gohr
1103c2e6844SAndreas Gohr            $newData = $structData[$table];
1113c2e6844SAndreas Gohr            foreach($schemaData->getColumns() as $col) {
11217560ecbSAndreas Gohr                // fix multi value types
113afbd4e60SMichael Große                $type = $col->getType();
114afbd4e60SMichael Große                $label = $type->getLabel();
11517560ecbSAndreas Gohr                $trans = $type->getTranslatedLabel();
1163c2e6844SAndreas Gohr                if($type->isMulti() && !is_array($newData[$label])) {
1173c2e6844SAndreas Gohr                    $newData[$label] = $type->splitValues($newData[$label]);
11804641d56SMichael Große                }
11917560ecbSAndreas Gohr
12017560ecbSAndreas Gohr                // validate data
121*b8a9b73cSMichael Große                $this->validated = $this->validated && $this->validate($type, $trans, $newData[$label]);
12204641d56SMichael Große            }
12317560ecbSAndreas Gohr
1243c2e6844SAndreas Gohr            // has the data changed? mark it for saving.
1253c2e6844SAndreas Gohr            $olddata = $schemaData->getDataArray();
1263c2e6844SAndreas Gohr            if($olddata != $newData) {
1273c2e6844SAndreas Gohr                $this->tosave[] = $table;
12804641d56SMichael Große            }
12917560ecbSAndreas Gohr
1303c2e6844SAndreas Gohr            // write back cleaned up data
1313c2e6844SAndreas Gohr            $structData[$table] = $newData;
13217560ecbSAndreas Gohr        }
13317560ecbSAndreas Gohr        // write back cleaned up structData
1343c2e6844SAndreas Gohr        $INPUT->post->set(self::$VAR, $structData);
13517560ecbSAndreas Gohr
13617560ecbSAndreas Gohr        // did validation go through? otherwise abort saving
1373c2e6844SAndreas Gohr        if(!$this->validated && $act == 'save') {
13817560ecbSAndreas Gohr            $event->data = 'edit';
13917560ecbSAndreas Gohr        }
14017560ecbSAndreas Gohr
14104641d56SMichael Große        return false;
14204641d56SMichael Große    }
14304641d56SMichael Große
14417560ecbSAndreas Gohr    /**
1453c2e6844SAndreas Gohr     * Check if the page has to be changed
1463c2e6844SAndreas Gohr     *
1473c2e6844SAndreas Gohr     * @param Doku_Event $event event object by reference
1483c2e6844SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
1493c2e6844SAndreas Gohr     *                           handler was registered]
1503c2e6844SAndreas Gohr     * @return bool
1513c2e6844SAndreas Gohr     */
1523c2e6844SAndreas Gohr    public function handle_pagesave_before(Doku_Event $event, $param) {
1533c2e6844SAndreas Gohr        global $lang;
1543c2e6844SAndreas Gohr
1553c2e6844SAndreas Gohr        if($event->data['contentChanged']) return; // will be saved for page changes
1563c2e6844SAndreas Gohr        if(count($this->tosave)) {
1573c2e6844SAndreas Gohr            if(trim($event->data['newContent']) === '') {
1583c2e6844SAndreas Gohr                // this happens when a new page is tried to be created with only struct data
1593c2e6844SAndreas Gohr                msg($this->getLang('emptypage'), -1);
1603c2e6844SAndreas Gohr            } else {
1613c2e6844SAndreas Gohr                $event->data['contentChanged'] = true; // save for data changes
1623c2e6844SAndreas Gohr            }
1633c2e6844SAndreas Gohr        }
1643c2e6844SAndreas Gohr    }
1653c2e6844SAndreas Gohr
1663c2e6844SAndreas Gohr    /**
1673c2e6844SAndreas Gohr     * Save the data
1683c2e6844SAndreas Gohr     *
1693c2e6844SAndreas Gohr     * @param Doku_Event $event event object by reference
1703c2e6844SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
1713c2e6844SAndreas Gohr     *                           handler was registered]
1723c2e6844SAndreas Gohr     * @return bool
1733c2e6844SAndreas Gohr     */
1743c2e6844SAndreas Gohr    public function handle_pagesave_after(Doku_Event $event, $param) {
1753c2e6844SAndreas Gohr        global $INPUT;
1763c2e6844SAndreas Gohr
1773c2e6844SAndreas Gohr        // this should never happen, because the validation is checked in handle_validation already
1783c2e6844SAndreas Gohr        if(!$this->validated) return;
1793c2e6844SAndreas Gohr
1803c2e6844SAndreas Gohr        if($event->data['changeType'] == DOKU_CHANGE_TYPE_DELETE) {
1813c2e6844SAndreas Gohr            // FIXME we should probably clean out all data on delete!?
1823c2e6844SAndreas Gohr            return;
1833c2e6844SAndreas Gohr        }
1843c2e6844SAndreas Gohr
1853c2e6844SAndreas Gohr        $structData = $INPUT->arr(self::$VAR);
1863c2e6844SAndreas Gohr
1873c2e6844SAndreas Gohr        foreach($this->tosave as $table) {
1883c2e6844SAndreas Gohr            $schemaData = new SchemaData($table, $event->data['id'], $event->data['newRevision']);
1893c2e6844SAndreas Gohr            $schemaData->saveData($structData[$table]);
1903c2e6844SAndreas Gohr        }
1913c2e6844SAndreas Gohr    }
1923c2e6844SAndreas Gohr
1933c2e6844SAndreas Gohr    /**
19417560ecbSAndreas Gohr     * Validate the given data
19517560ecbSAndreas Gohr     *
19617560ecbSAndreas Gohr     * Catches the Validation exceptions and transforms them into proper messages.
19717560ecbSAndreas Gohr     *
19817560ecbSAndreas Gohr     * Blank values are not validated and always pass
19917560ecbSAndreas Gohr     *
20017560ecbSAndreas Gohr     * @param AbstractBaseType $type
20117560ecbSAndreas Gohr     * @param string $label
20217560ecbSAndreas Gohr     * @param array|string|int $data
20317560ecbSAndreas Gohr     * @return bool true if the data validates, otherwise false
20417560ecbSAndreas Gohr     */
20517560ecbSAndreas Gohr    protected function validate(AbstractBaseType $type, $label, $data) {
20617560ecbSAndreas Gohr        $prefix = sprintf($this->getLang('validation_prefix'), $label);
20717560ecbSAndreas Gohr
20817560ecbSAndreas Gohr        $ok = true;
20917560ecbSAndreas Gohr        if(is_array($data)) {
21017560ecbSAndreas Gohr            foreach($data as $value) {
21117560ecbSAndreas Gohr                if(!blank($value)) {
21217560ecbSAndreas Gohr                    try {
21317560ecbSAndreas Gohr                        $type->validate($value);
21417560ecbSAndreas Gohr                    } catch(ValidationException $e) {
21517560ecbSAndreas Gohr                        msg($prefix . $e->getMessage(), -1);
21617560ecbSAndreas Gohr                        $ok = false;
21717560ecbSAndreas Gohr                    }
21817560ecbSAndreas Gohr                }
21917560ecbSAndreas Gohr            }
22017560ecbSAndreas Gohr        } else {
22117560ecbSAndreas Gohr            if(!blank($data)) {
22217560ecbSAndreas Gohr                try {
22317560ecbSAndreas Gohr                    $type->validate($data);
22417560ecbSAndreas Gohr                } catch(ValidationException $e) {
22517560ecbSAndreas Gohr                    msg($prefix . $e->getMessage(), -1);
22617560ecbSAndreas Gohr                    $ok = false;
22717560ecbSAndreas Gohr                }
22817560ecbSAndreas Gohr            }
22917560ecbSAndreas Gohr        }
23017560ecbSAndreas Gohr
23117560ecbSAndreas Gohr        return $ok;
23217560ecbSAndreas Gohr    }
23317560ecbSAndreas Gohr
234c2fd0bf0SMichael Große    /**
23565598e4aSAndreas Gohr     * Create the form to edit schemadata
236f36fda9dSAndreas Gohr     *
237c2fd0bf0SMichael Große     * @param string $tablename
23865598e4aSAndreas Gohr     * @return string The HTML for this schema's form
239c2fd0bf0SMichael Große     */
24065598e4aSAndreas Gohr    protected function createForm($tablename) {
241c2fd0bf0SMichael Große        global $ID;
24283beda18SAndreas Gohr        global $REV;
24317560ecbSAndreas Gohr        global $INPUT;
24483beda18SAndreas Gohr        $schema = new SchemaData($tablename, $ID, $REV);
245c2fd0bf0SMichael Große        $schemadata = $schema->getData();
246c2fd0bf0SMichael Große
24717560ecbSAndreas Gohr        $structdata = $INPUT->arr(self::$VAR);
24817560ecbSAndreas Gohr        if(isset($structdata[$tablename])) {
24917560ecbSAndreas Gohr            $postdata = $structdata[$tablename];
25017560ecbSAndreas Gohr        } else {
25117560ecbSAndreas Gohr            $postdata = array();
25217560ecbSAndreas Gohr        }
25317560ecbSAndreas Gohr
25465598e4aSAndreas Gohr        $html = "<h3>$tablename</h3>";
255053212b1SAndreas Gohr        foreach($schemadata as $field) {
256053212b1SAndreas Gohr            $label = $field->getColumn()->getLabel();
25717560ecbSAndreas Gohr            if(isset($postdata[$label])) {
25817560ecbSAndreas Gohr                // posted data trumps stored data
25917560ecbSAndreas Gohr                $field->setValue($postdata[$label]);
26017560ecbSAndreas Gohr            }
2619e9bee91SAndreas Gohr            $trans = hsc($field->getColumn()->getTranslatedLabel());
26217560ecbSAndreas Gohr            $name = self::$VAR . "[$tablename][$label]";
263053212b1SAndreas Gohr            $input = $field->getValueEditor($name);
2649e9bee91SAndreas Gohr            $element = "<label>$trans $input</label><br />";
26565598e4aSAndreas Gohr            $html .= $element;
266c2fd0bf0SMichael Große        }
26765598e4aSAndreas Gohr
26865598e4aSAndreas Gohr        return $html;
269549a0837SAndreas Gohr    }
270549a0837SAndreas Gohr
271549a0837SAndreas Gohr}
272549a0837SAndreas Gohr
273549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
274