xref: /plugin/struct/action/entry.php (revision f36fda9d82d876a83911d463438621b5c9fe1785)
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
42        /** @var \helper_plugin_struct_db $helper */
43        $helper = plugin_load('helper', 'struct_db');
44        $this->sqlite = $helper->getDB();
45
46        global $ID;
47
48        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,));
49        if (!$this->sqlite->res2count($res)) return false;
50
51        $tables = array_map(function ($value){return $value['tbl'];},$this->sqlite->res2arr($res));
52
53
54        foreach ($tables as $table) {
55            $this->createForm($table, $event->data);
56        }
57
58        return true;
59    }
60
61    /**
62     * Adds the form to edit schemadata
63     *
64     * @param string $tablename
65     * @param Doku_Form $form The editor form
66     */
67    protected function createForm($tablename, $form) {
68        global $ID;
69        global $REV;
70        $schema = new SchemaData($tablename, $ID, $REV);
71        $schemadata = $schema->getData();
72
73        $form->insertElement(4, "<h3>$tablename</h3>");
74
75        $cols = $schema->getColumns(false);
76        foreach ($cols as $index => $col) {
77            $type = $col->getType();
78            $label = $type->getLabel();
79            $name = "Schema[$tablename][$label]";
80            $input = $type->valueEditor($name, $schemadata[$label]);
81            $element = "<label>$label $input</label><br />";
82            $form->insertElement(5 + $index, $element);
83        }
84    }
85
86}
87
88// vim:ts=4:sw=4:et:
89