xref: /plugin/struct/action/entry.php (revision a8b9fb5be080ac101c3f428022e0c7f423299291)
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;
14c2fd0bf0SMichael Große
15549a0837SAndreas Gohrclass action_plugin_struct_entry extends DokuWiki_Action_Plugin {
16549a0837SAndreas Gohr
17c2fd0bf0SMichael Große
18c2fd0bf0SMichael Große    /** @var helper_plugin_sqlite */
19c2fd0bf0SMichael Große    protected $sqlite;
20c2fd0bf0SMichael Große
21549a0837SAndreas Gohr    /**
22549a0837SAndreas Gohr     * Registers a callback function for a given event
23549a0837SAndreas Gohr     *
24549a0837SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
25549a0837SAndreas Gohr     * @return void
26549a0837SAndreas Gohr     */
27549a0837SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
28549a0837SAndreas Gohr
29c2fd0bf0SMichael Große        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
3004641d56SMichael Große        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_pagesave');
31549a0837SAndreas Gohr
32549a0837SAndreas Gohr    }
33549a0837SAndreas Gohr
34549a0837SAndreas Gohr    /**
3504641d56SMichael Große     * Save values of Schemas but do not interfere with saving the page.
3604641d56SMichael Große     *
3704641d56SMichael Große     * @param Doku_Event $event  event object by reference
3804641d56SMichael Große     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
3904641d56SMichael Große     *                           handler was registered]
4004641d56SMichael Große     * @return bool
4104641d56SMichael Große     */
4204641d56SMichael Große    public function handle_pagesave(Doku_Event &$event, $param) {
433ece9074SMichael Große        global $ID, $INPUT;
4404641d56SMichael Große        if (act_clean($event->data) !== "save") return false;
4504641d56SMichael Große
46*a8b9fb5bSAndreas Gohr        $assignments = new Assignments();
47*a8b9fb5bSAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
4804641d56SMichael Große        $structData = $INPUT->arr('Schema');
49bd92cd68SAndreas Gohr        $timestamp = time(); //FIXME we should use the time stamp used to save the page data
5004641d56SMichael Große
5104641d56SMichael Große        foreach ($tables as $table) {
5204641d56SMichael Große            $schema = new SchemaData($table, $ID, $timestamp);
53bd92cd68SAndreas Gohr            if(!$schema->getId()) {
54bd92cd68SAndreas Gohr                // this schema is not available for some reason. skip it
55bd92cd68SAndreas Gohr                continue;
56bd92cd68SAndreas Gohr            }
57bd92cd68SAndreas Gohr
5804641d56SMichael Große            $schemaData = $structData[$table];
5904641d56SMichael Große            foreach ($schema->getColumns() as $col) {
60afbd4e60SMichael Große                $type = $col->getType();
61afbd4e60SMichael Große                $label = $type->getLabel();
62afbd4e60SMichael Große                if ($type->isMulti() && !is_array($schemaData[$label])) {
63afbd4e60SMichael Große                    $schemaData[$label] = $type->splitValues($schemaData[$label]);
6404641d56SMichael Große                }
6504641d56SMichael Große            }
6604641d56SMichael Große            $schema->saveData($schemaData);
6704641d56SMichael Große        }
6804641d56SMichael Große        return false;
6904641d56SMichael Große    }
7004641d56SMichael Große
71016bf382SMichael Große    /*
72f36fda9dSAndreas Gohr     * Enhance the editing form with structural data editing
73549a0837SAndreas Gohr     *
74549a0837SAndreas Gohr     * @param Doku_Event $event  event object by reference
75549a0837SAndreas Gohr     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
76549a0837SAndreas Gohr     *                           handler was registered]
77c2fd0bf0SMichael Große     * @return bool
78549a0837SAndreas Gohr     */
79c07703d4SAndreas Gohr    public function handle_editform(Doku_Event $event, $param) {
8065598e4aSAndreas Gohr        global $ID;
81c2fd0bf0SMichael Große
82fb31ca9fSAndreas Gohr        $assignments = new Assignments();
83fb31ca9fSAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
84c2fd0bf0SMichael Große
8565598e4aSAndreas Gohr        $html = '';
86c2fd0bf0SMichael Große        foreach($tables as $table) {
8765598e4aSAndreas Gohr            $html .= $this->createForm($table);
88c2fd0bf0SMichael Große        }
89c2fd0bf0SMichael Große
9065598e4aSAndreas Gohr        /** @var Doku_Form $form */
9165598e4aSAndreas Gohr        $form = $event->data;
9265598e4aSAndreas Gohr        $html = "<div class=\"struct\">$html</div>";
9365598e4aSAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
9465598e4aSAndreas Gohr        $form->insertElement($pos, $html);
9565598e4aSAndreas Gohr
96c2fd0bf0SMichael Große        return true;
97c2fd0bf0SMichael Große    }
98c2fd0bf0SMichael Große
99c2fd0bf0SMichael Große    /**
10065598e4aSAndreas Gohr     * Create the form to edit schemadata
101f36fda9dSAndreas Gohr     *
102c2fd0bf0SMichael Große     * @param string $tablename
10365598e4aSAndreas Gohr     * @return string The HTML for this schema's form
104c2fd0bf0SMichael Große     */
10565598e4aSAndreas Gohr    protected function createForm($tablename) {
106c2fd0bf0SMichael Große        global $ID;
10783beda18SAndreas Gohr        global $REV;
10883beda18SAndreas Gohr        $schema = new SchemaData($tablename, $ID, $REV);
109c2fd0bf0SMichael Große        $schemadata = $schema->getData();
110c2fd0bf0SMichael Große
11165598e4aSAndreas Gohr        $html = "<h3>$tablename</h3>";
112053212b1SAndreas Gohr        foreach($schemadata as $field) {
113053212b1SAndreas Gohr            $label = $field->getColumn()->getLabel();
1149e9bee91SAndreas Gohr            $trans = hsc($field->getColumn()->getTranslatedLabel());
115c2fd0bf0SMichael Große            $name = "Schema[$tablename][$label]";
116053212b1SAndreas Gohr            $input = $field->getValueEditor($name);
1179e9bee91SAndreas Gohr            $element = "<label>$trans $input</label><br />";
11865598e4aSAndreas Gohr            $html .= $element;
119c2fd0bf0SMichael Große        }
12065598e4aSAndreas Gohr
12165598e4aSAndreas Gohr        return $html;
122549a0837SAndreas Gohr    }
123549a0837SAndreas Gohr
124549a0837SAndreas Gohr}
125549a0837SAndreas Gohr
126549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
127