xref: /plugin/struct/action/entry.php (revision c2fd0bf0f1e5f70408ef7f28c9c9bb2ddde010e7)
1549a0837SAndreas Gohr<?php
2549a0837SAndreas Gohr/**
3549a0837SAndreas Gohr * DokuWiki Plugin struct (Action Component)
4549a0837SAndreas Gohr *
5549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6549a0837SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7549a0837SAndreas Gohr */
8549a0837SAndreas Gohr
9549a0837SAndreas Gohr// must be run within Dokuwiki
10549a0837SAndreas Gohrif(!defined('DOKU_INC')) die();
11549a0837SAndreas Gohr
12*c2fd0bf0SMichael Großeuse plugin\struct\meta\SchemaData;
13*c2fd0bf0SMichael Große
14549a0837SAndreas Gohrclass action_plugin_struct_entry extends DokuWiki_Action_Plugin {
15549a0837SAndreas Gohr
16*c2fd0bf0SMichael Große
17*c2fd0bf0SMichael Große    /** @var helper_plugin_sqlite */
18*c2fd0bf0SMichael Große    protected $sqlite;
19*c2fd0bf0SMichael Große
20549a0837SAndreas Gohr    /**
21549a0837SAndreas Gohr     * Registers a callback function for a given event
22549a0837SAndreas Gohr     *
23549a0837SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
24549a0837SAndreas Gohr     * @return void
25549a0837SAndreas Gohr     */
26549a0837SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
27549a0837SAndreas Gohr
28*c2fd0bf0SMichael Große       $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
29549a0837SAndreas Gohr
30549a0837SAndreas Gohr    }
31549a0837SAndreas Gohr
32549a0837SAndreas Gohr    /**
33549a0837SAndreas Gohr     * [Custom event handler which performs action]
34549a0837SAndreas Gohr     *
35549a0837SAndreas Gohr     * @param Doku_Event $event  event object by reference
36549a0837SAndreas Gohr     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
37549a0837SAndreas Gohr     *                           handler was registered]
38*c2fd0bf0SMichael Große     * @return bool
39549a0837SAndreas Gohr     */
40549a0837SAndreas Gohr
41*c2fd0bf0SMichael Große    public function handle_editform(Doku_Event &$event, $param) {
42*c2fd0bf0SMichael Große
43*c2fd0bf0SMichael Große        /** @var \helper_plugin_struct_db $helper */
44*c2fd0bf0SMichael Große        $helper = plugin_load('helper', 'struct_db');
45*c2fd0bf0SMichael Große        $this->sqlite = $helper->getDB();
46*c2fd0bf0SMichael Große
47*c2fd0bf0SMichael Große        global $ID;
48*c2fd0bf0SMichael Große
49*c2fd0bf0SMichael Große        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,));
50*c2fd0bf0SMichael Große        if (!$this->sqlite->res2count($res)) return false;
51*c2fd0bf0SMichael Große
52*c2fd0bf0SMichael Große        $tables = array_map(function ($value){return $value['tbl'];},$this->sqlite->res2arr($res));
53*c2fd0bf0SMichael Große
54*c2fd0bf0SMichael Große
55*c2fd0bf0SMichael Große        foreach ($tables as $table) {
56*c2fd0bf0SMichael Große            $this->createForm($table, $event->data);
57*c2fd0bf0SMichael Große        }
58*c2fd0bf0SMichael Große
59*c2fd0bf0SMichael Große        return true;
60*c2fd0bf0SMichael Große    }
61*c2fd0bf0SMichael Große
62*c2fd0bf0SMichael Große    /**
63*c2fd0bf0SMichael Große     * @param string $tablename
64*c2fd0bf0SMichael Große     * @param Doku_Form $data
65*c2fd0bf0SMichael Große     */
66*c2fd0bf0SMichael Große    private function createForm($tablename, &$data) {
67*c2fd0bf0SMichael Große        global $ID;
68*c2fd0bf0SMichael Große        $schema = new SchemaData($tablename, $ID, 0);
69*c2fd0bf0SMichael Große        $schemadata = $schema->getData();
70*c2fd0bf0SMichael Große
71*c2fd0bf0SMichael Große        $data->insertElement(4, "<h3>$tablename</h3>");
72*c2fd0bf0SMichael Große        $cols = $schema->getColumns();
73*c2fd0bf0SMichael Große        usort($cols, function($a, $b){if ($a->getSort()<$b->getSort())return -1;return 1;});
74*c2fd0bf0SMichael Große
75*c2fd0bf0SMichael Große        foreach ($cols as $index => $col) {
76*c2fd0bf0SMichael Große            $type = $col->getType();
77*c2fd0bf0SMichael Große            $label = $type->getLabel();
78*c2fd0bf0SMichael Große            $name = "Schema[$tablename][$label]";
79*c2fd0bf0SMichael Große            $input = $type->valueEditor($name, $schemadata[$label]);
80*c2fd0bf0SMichael Große            $element = "<label>$label $input</label><br />";
81*c2fd0bf0SMichael Große            $data->insertElement(5 + $index, $element);
82*c2fd0bf0SMichael Große        }
83549a0837SAndreas Gohr    }
84549a0837SAndreas Gohr
85549a0837SAndreas Gohr}
86549a0837SAndreas Gohr
87549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
88