xref: /plugin/struct/action/entry.php (revision 15929be2d8f1b3ae684ef1e73b344ee5c125512e)
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\Assignments;
13use plugin\struct\meta\SchemaData;
14
15class action_plugin_struct_entry extends DokuWiki_Action_Plugin {
16
17
18    /** @var helper_plugin_sqlite */
19    protected $sqlite;
20
21    /**
22     * Registers a callback function for a given event
23     *
24     * @param Doku_Event_Handler $controller DokuWiki's event controller object
25     * @return void
26     */
27    public function register(Doku_Event_Handler $controller) {
28
29        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
30        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_pagesave');
31
32    }
33
34    /**
35     * Save values of Schemas but do not interfere with saving the page.
36     *
37     * @param Doku_Event $event  event object by reference
38     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
39     *                           handler was registered]
40     * @return bool
41     */
42    public function handle_pagesave(Doku_Event &$event, $param) {
43        global $ID, $INPUT;
44        if (act_clean($event->data) !== "save") return false;
45
46        /** @var \helper_plugin_struct_db $helper */
47        $helper = plugin_load('helper', 'struct_db');
48        $this->sqlite = $helper->getDB();
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(
54            function ($value) {
55                return $value['tbl'];
56            },
57            $this->sqlite->res2arr($res)
58        );
59        $this->sqlite->res_close($res);
60
61        $structData = $INPUT->arr('Schema');
62        $timestamp = $INPUT->int('date');
63
64        foreach ($tables as $table) {
65            $schema = new SchemaData($table, $ID, $timestamp);
66            $schemaData = $structData[$table];
67            foreach ($schema->getColumns() as $col) {
68                $type = $col->getType();
69                $label = $type->getLabel();
70                if ($type->isMulti() && !is_array($schemaData[$label])) {
71                    $schemaData[$label] = $type->splitValues($schemaData[$label]);
72                }
73            }
74            $schema->saveData($schemaData);
75        }
76        return false;
77    }
78
79    /*
80     * Enhance the editing form with structural data editing
81     *
82     * @param Doku_Event $event  event object by reference
83     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
84     *                           handler was registered]
85     * @return bool
86     */
87    public function handle_editform(Doku_Event $event, $param) {
88        global $ID;
89
90        $assignments = new Assignments();
91        $tables = $assignments->getPageAssignments($ID);
92
93        $html = '';
94        foreach($tables as $table) {
95            $html .= $this->createForm($table);
96        }
97
98        /** @var Doku_Form $form */
99        $form = $event->data;
100        $html = "<div class=\"struct\">$html</div>";
101        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
102        $form->insertElement($pos, $html);
103
104        return true;
105    }
106
107    /**
108     * Create the form to edit schemadata
109     *
110     * @param string $tablename
111     * @return string The HTML for this schema's form
112     */
113    protected function createForm($tablename) {
114        global $ID;
115        global $REV;
116        $schema = new SchemaData($tablename, $ID, $REV);
117        $schemadata = $schema->getData();
118
119        $html = "<h3>$tablename</h3>";
120        $cols = $schema->getColumns(false);
121        foreach ($cols as $index => $col) {
122            $type = $col->getType();
123            $label = $type->getLabel();
124            $name = "Schema[$tablename][$label]";
125            if($type->isMulti()) {
126                $input = $type->multiValueEditor($name, $schemadata[$label]);
127            } else {
128                $input = $type->valueEditor($name, $schemadata[$label]);
129            }
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