xref: /plugin/struct/action/entry.php (revision 04641d56b98a11787b60aacd7b571069c5f77580)
1<?php
2/**
3 * DokuWiki Plugin struct (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12use plugin\struct\meta\SchemaData;
13
14class action_plugin_struct_entry extends DokuWiki_Action_Plugin {
15
16
17    /** @var helper_plugin_sqlite */
18    protected $sqlite;
19
20    /**
21     * Registers a callback function for a given event
22     *
23     * @param Doku_Event_Handler $controller DokuWiki's event controller object
24     * @return void
25     */
26    public function register(Doku_Event_Handler $controller) {
27
28        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
29        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_pagesave');
30
31    }
32
33    /**
34     * Save values of Schemas but do not interfere with saving the page.
35     *
36     * @param Doku_Event $event  event object by reference
37     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
38     *                           handler was registered]
39     * @return bool
40     */
41    public function handle_pagesave(Doku_Event &$event, $param) {
42        if (act_clean($event->data) !== "save") return false;
43
44        /** @var \helper_plugin_struct_db $helper */
45        $helper = plugin_load('helper', 'struct_db');
46        $this->sqlite = $helper->getDB();
47
48        global $ID, $INPUT;
49
50        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,));
51        if (!$this->sqlite->res2count($res)) return false;
52
53        $tables = array_map(function ($value){return $value['tbl'];},$this->sqlite->res2arr($res));
54        $this->sqlite->res_close($res);
55
56        $structData = $INPUT->arr('Schema');
57        $timestamp = $INPUT->int('date');
58
59        foreach ($tables as $table) {
60            $schema = new SchemaData($table, $ID, $timestamp);
61            $schemaData = $structData[$table];
62            foreach ($schema->getColumns() as $col) {
63                if ($col->getType()->isMulti()) {
64                    $schemaData[$col->getType()->getLabel()] = explode(',',$schemaData[$col->getType()->getLabel()]);
65                    $schemaData[$col->getType()->getLabel()] = array_map(function($value){return trim($value);}, $schemaData[$col->getType()->getLabel()]);
66                }
67            }
68            $schema->saveData($schemaData);
69        }
70        return false;
71    }
72
73    /**
74     * [Custom event handler which performs action]
75     *
76     * @param Doku_Event $event  event object by reference
77     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
78     *                           handler was registered]
79     * @return bool
80     */
81    public function handle_editform(Doku_Event &$event, $param) {
82
83        /** @var \helper_plugin_struct_db $helper */
84        $helper = plugin_load('helper', 'struct_db');
85        $this->sqlite = $helper->getDB();
86
87        global $ID;
88
89        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,));
90        if (!$this->sqlite->res2count($res)) return false;
91
92        $tables = array_map(function ($value){return $value['tbl'];},$this->sqlite->res2arr($res));
93        $this->sqlite->res_close($res);
94
95        foreach ($tables as $table) {
96            $this->createForm($table, $event->data);
97        }
98
99        return true;
100    }
101
102    /**
103     * @param string $tablename
104     * @param Doku_Form $data
105     */
106    private function createForm($tablename, &$data) {
107        global $ID;
108        $schema = new SchemaData($tablename, $ID, 0);
109        $schemadata = $schema->getData();
110
111        $data->insertElement(4, "<h3>$tablename</h3>");
112        $cols = $schema->getColumns();
113        usort($cols, function($a, $b){if ($a->getSort()<$b->getSort())return -1;return 1;});
114
115        foreach ($cols as $index => $col) {
116            $type = $col->getType();
117            $label = $type->getLabel();
118            $name = "Schema[$tablename][$label]";
119            $input = $type->valueEditor($name, $schemadata[$label]);
120            $element = "<label>$label $input</label><br />";
121            $data->insertElement(5 + $index, $element);
122        }
123    }
124
125}
126
127// vim:ts=4:sw=4:et:
128