xref: /plugin/struct/action/entry.php (revision 3ece90749e271e0c0c0f7efb19f1f862a7b5b65b)
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        global $ID, $INPUT;
43        if (act_clean($event->data) !== "save") return false;
44
45        /** @var \helper_plugin_struct_db $helper */
46        $helper = plugin_load('helper', 'struct_db');
47        $this->sqlite = $helper->getDB();
48
49        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,));
50        if (!$this->sqlite->res2count($res)) return false;
51
52        $tables = array_map(
53            function ($value) {
54                return $value['tbl'];
55            },
56            $this->sqlite->res2arr($res)
57        );
58        $this->sqlite->res_close($res);
59
60        $structData = $INPUT->arr('Schema');
61        $timestamp = $INPUT->int('date');
62
63        foreach ($tables as $table) {
64            $schema = new SchemaData($table, $ID, $timestamp);
65            $schemaData = $structData[$table];
66            foreach ($schema->getColumns() as $col) {
67                if ($col->getType()->isMulti()) {
68                    $label = $col->getType()->getLabel();
69                    $schemaData[$label] = explode(',',$schemaData[$label]);
70                    $schemaData[$label] = array_map(
71                        function($value) {
72                            return trim($value);
73                        },
74                        $schemaData[$label]
75                    );
76                }
77            }
78            $schema->saveData($schemaData);
79        }
80        return false;
81    }
82
83    /*
84     * Enhance the editing form with structural data editing
85     *
86     * @param Doku_Event $event  event object by reference
87     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
88     *                           handler was registered]
89     * @return bool
90     */
91    public function handle_editform(Doku_Event $event, $param) {
92        global $ID;
93        /** @var \helper_plugin_struct_db $helper */
94        $helper = plugin_load('helper', 'struct_db');
95        $this->sqlite = $helper->getDB();
96
97        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?", array($ID,));
98        if(!$this->sqlite->res2count($res)) return false;
99
100        $tables = array_map(
101            function ($value) {
102                return $value['tbl'];
103            },
104            $this->sqlite->res2arr($res)
105        );
106        $this->sqlite->res_close($res);
107
108        $html = '';
109        foreach($tables as $table) {
110            $html .= $this->createForm($table);
111        }
112
113        /** @var Doku_Form $form */
114        $form = $event->data;
115        $html = "<div class=\"struct\">$html</div>";
116        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
117        $form->insertElement($pos, $html);
118
119        return true;
120    }
121
122    /**
123     * Create the form to edit schemadata
124     *
125     * @param string $tablename
126     * @return string The HTML for this schema's form
127     */
128    protected function createForm($tablename) {
129        global $ID;
130        global $REV;
131        $schema = new SchemaData($tablename, $ID, $REV);
132        $schemadata = $schema->getData();
133
134        $html = "<h3>$tablename</h3>";
135        $cols = $schema->getColumns(false);
136        foreach ($cols as $index => $col) {
137            $type = $col->getType();
138            $label = $type->getLabel();
139            $name = "Schema[$tablename][$label]";
140            $input = $type->valueEditor($name, $schemadata[$label]);
141            $element = "<label>$label $input</label><br />";
142            $html .= $element;
143        }
144
145        return $html;
146    }
147
148}
149
150// vim:ts=4:sw=4:et:
151