xref: /plugin/struct/action/entry.php (revision 053212b194c66249cf544d22d6235bed793fcf3f)
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
4604641d56SMichael Große        /** @var \helper_plugin_struct_db $helper */
4704641d56SMichael Große        $helper = plugin_load('helper', 'struct_db');
4804641d56SMichael Große        $this->sqlite = $helper->getDB();
4904641d56SMichael Große
5004641d56SMichael Große        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,));
5104641d56SMichael Große        if (!$this->sqlite->res2count($res)) return false;
5204641d56SMichael Große
533ece9074SMichael Große        $tables = array_map(
543ece9074SMichael Große            function ($value) {
553ece9074SMichael Große                return $value['tbl'];
563ece9074SMichael Große            },
573ece9074SMichael Große            $this->sqlite->res2arr($res)
583ece9074SMichael Große        );
5904641d56SMichael Große        $this->sqlite->res_close($res);
6004641d56SMichael Große
6104641d56SMichael Große        $structData = $INPUT->arr('Schema');
62bd92cd68SAndreas Gohr        $timestamp = time(); //FIXME we should use the time stamp used to save the page data
6304641d56SMichael Große
6404641d56SMichael Große        foreach ($tables as $table) {
6504641d56SMichael Große            $schema = new SchemaData($table, $ID, $timestamp);
66bd92cd68SAndreas Gohr            if(!$schema->getId()) {
67bd92cd68SAndreas Gohr                // this schema is not available for some reason. skip it
68bd92cd68SAndreas Gohr                continue;
69bd92cd68SAndreas Gohr            }
70bd92cd68SAndreas Gohr
7104641d56SMichael Große            $schemaData = $structData[$table];
7204641d56SMichael Große            foreach ($schema->getColumns() as $col) {
73afbd4e60SMichael Große                $type = $col->getType();
74afbd4e60SMichael Große                $label = $type->getLabel();
75afbd4e60SMichael Große                if ($type->isMulti() && !is_array($schemaData[$label])) {
76afbd4e60SMichael Große                    $schemaData[$label] = $type->splitValues($schemaData[$label]);
7704641d56SMichael Große                }
7804641d56SMichael Große            }
7904641d56SMichael Große            $schema->saveData($schemaData);
8004641d56SMichael Große        }
8104641d56SMichael Große        return false;
8204641d56SMichael Große    }
8304641d56SMichael Große
84016bf382SMichael Große    /*
85f36fda9dSAndreas Gohr     * Enhance the editing form with structural data editing
86549a0837SAndreas Gohr     *
87549a0837SAndreas Gohr     * @param Doku_Event $event  event object by reference
88549a0837SAndreas Gohr     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
89549a0837SAndreas Gohr     *                           handler was registered]
90c2fd0bf0SMichael Große     * @return bool
91549a0837SAndreas Gohr     */
92c07703d4SAndreas Gohr    public function handle_editform(Doku_Event $event, $param) {
9365598e4aSAndreas Gohr        global $ID;
94c2fd0bf0SMichael Große
95fb31ca9fSAndreas Gohr        $assignments = new Assignments();
96fb31ca9fSAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
97c2fd0bf0SMichael Große
9865598e4aSAndreas Gohr        $html = '';
99c2fd0bf0SMichael Große        foreach($tables as $table) {
10065598e4aSAndreas Gohr            $html .= $this->createForm($table);
101c2fd0bf0SMichael Große        }
102c2fd0bf0SMichael Große
10365598e4aSAndreas Gohr        /** @var Doku_Form $form */
10465598e4aSAndreas Gohr        $form = $event->data;
10565598e4aSAndreas Gohr        $html = "<div class=\"struct\">$html</div>";
10665598e4aSAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
10765598e4aSAndreas Gohr        $form->insertElement($pos, $html);
10865598e4aSAndreas Gohr
109c2fd0bf0SMichael Große        return true;
110c2fd0bf0SMichael Große    }
111c2fd0bf0SMichael Große
112c2fd0bf0SMichael Große    /**
11365598e4aSAndreas Gohr     * Create the form to edit schemadata
114f36fda9dSAndreas Gohr     *
115c2fd0bf0SMichael Große     * @param string $tablename
11665598e4aSAndreas Gohr     * @return string The HTML for this schema's form
117c2fd0bf0SMichael Große     */
11865598e4aSAndreas Gohr    protected function createForm($tablename) {
119c2fd0bf0SMichael Große        global $ID;
12083beda18SAndreas Gohr        global $REV;
12183beda18SAndreas Gohr        $schema = new SchemaData($tablename, $ID, $REV);
122c2fd0bf0SMichael Große        $schemadata = $schema->getData();
123c2fd0bf0SMichael Große
12465598e4aSAndreas Gohr        $html = "<h3>$tablename</h3>";
125*053212b1SAndreas Gohr        foreach($schemadata as $field) {
126*053212b1SAndreas Gohr            $label = $field->getColumn()->getLabel();
127c2fd0bf0SMichael Große            $name = "Schema[$tablename][$label]";
128*053212b1SAndreas Gohr            $input = $field->getValueEditor($name);
129c2fd0bf0SMichael Große            $element = "<label>$label $input</label><br />";
13065598e4aSAndreas Gohr            $html .= $element;
131c2fd0bf0SMichael Große        }
13265598e4aSAndreas Gohr
13365598e4aSAndreas Gohr        return $html;
134549a0837SAndreas Gohr    }
135549a0837SAndreas Gohr
136549a0837SAndreas Gohr}
137549a0837SAndreas Gohr
138549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
139