xref: /plugin/struct/action/entry.php (revision 016bf38258856475d69b0cb580f5acb575a07799)
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     * Enhance the editing form with structural data editing
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        global $ID;
83        /** @var \helper_plugin_struct_db $helper */
84        $helper = plugin_load('helper', 'struct_db');
85        $this->sqlite = $helper->getDB();
86
87        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?", array($ID,));
88        if(!$this->sqlite->res2count($res)) return false;
89
90        $tables = array_map(
91            function ($value) {
92                return $value['tbl'];
93            },
94            $this->sqlite->res2arr($res)
95        );
96
97        $html = '';
98        foreach($tables as $table) {
99            $html .= $this->createForm($table);
100        }
101
102        /** @var Doku_Form $form */
103        $form = $event->data;
104        $html = "<div class=\"struct\">$html</div>";
105        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
106        $form->insertElement($pos, $html);
107
108        return true;
109    }
110
111    /**
112     * Create the form to edit schemadata
113     *
114     * @param string $tablename
115     * @return string The HTML for this schema's form
116     */
117    protected function createForm($tablename) {
118        global $ID;
119        global $REV;
120        $schema = new SchemaData($tablename, $ID, $REV);
121        $schemadata = $schema->getData();
122
123        $html = "<h3>$tablename</h3>";
124        $cols = $schema->getColumns(false);
125        foreach ($cols as $index => $col) {
126            $type = $col->getType();
127            $label = $type->getLabel();
128            $name = "Schema[$tablename][$label]";
129            $input = $type->valueEditor($name, $schemadata[$label]);
130            $element = "<label>$label $input</label><br />";
131            $html .= $element;
132        }
133
134        return $html;
135    }
136
137}
138
139// vim:ts=4:sw=4:et:
140