xref: /plugin/struct/action/entry.php (revision 65598e4ad0bbbc3997ce2b1cac93fe5ed199a78d)
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
30    }
31
32    /**
33     * Enhance the editing form with structural data editing
34     *
35     * @param Doku_Event $event  event object by reference
36     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
37     *                           handler was registered]
38     * @return bool
39     */
40    public function handle_editform(Doku_Event $event, $param) {
41        global $ID;
42        /** @var \helper_plugin_struct_db $helper */
43        $helper = plugin_load('helper', 'struct_db');
44        $this->sqlite = $helper->getDB();
45
46        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?", array($ID,));
47        if(!$this->sqlite->res2count($res)) return false;
48
49        $tables = array_map(
50            function ($value) {
51                return $value['tbl'];
52            },
53            $this->sqlite->res2arr($res)
54        );
55
56        $html = '';
57        foreach($tables as $table) {
58            $html .= $this->createForm($table);
59        }
60
61        /** @var Doku_Form $form */
62        $form = $event->data;
63        $html = "<div class=\"struct\">$html</div>";
64        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
65        $form->insertElement($pos, $html);
66
67        return true;
68    }
69
70    /**
71     * Create the form to edit schemadata
72     *
73     * @param string $tablename
74     * @return string The HTML for this schema's form
75     */
76    protected function createForm($tablename) {
77        global $ID;
78        global $REV;
79        $schema = new SchemaData($tablename, $ID, $REV);
80        $schemadata = $schema->getData();
81
82        $html = "<h3>$tablename</h3>";
83        $cols = $schema->getColumns(false);
84        foreach ($cols as $index => $col) {
85            $type = $col->getType();
86            $label = $type->getLabel();
87            $name = "Schema[$tablename][$label]";
88            $input = $type->valueEditor($name, $schemadata[$label]);
89            $element = "<label>$label $input</label><br />";
90            $html .= $element;
91        }
92
93        return $html;
94    }
95
96}
97
98// vim:ts=4:sw=4:et:
99