xref: /plugin/struct/action/entry.php (revision 899b86ac23d349e6879d67ecb49ebd8a8fa5be6d)
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
12c2fd0bf0SMichael Großeuse plugin\struct\meta\SchemaData;
13c2fd0bf0SMichael Große
14549a0837SAndreas Gohrclass action_plugin_struct_entry extends DokuWiki_Action_Plugin {
15549a0837SAndreas Gohr
16c2fd0bf0SMichael Große
17c2fd0bf0SMichael Große    /** @var helper_plugin_sqlite */
18c2fd0bf0SMichael Große    protected $sqlite;
19c2fd0bf0SMichael 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
28c2fd0bf0SMichael Große       $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
29549a0837SAndreas Gohr
30549a0837SAndreas Gohr    }
31549a0837SAndreas Gohr
32549a0837SAndreas Gohr    /**
33f36fda9dSAndreas Gohr     * Enhance the editing form with structural data editing
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]
38c2fd0bf0SMichael Große     * @return bool
39549a0837SAndreas Gohr     */
40c07703d4SAndreas Gohr    public function handle_editform(Doku_Event $event, $param) {
4165598e4aSAndreas Gohr        global $ID;
42c2fd0bf0SMichael Große        /** @var \helper_plugin_struct_db $helper */
43c2fd0bf0SMichael Große        $helper = plugin_load('helper', 'struct_db');
44c2fd0bf0SMichael Große        $this->sqlite = $helper->getDB();
45c2fd0bf0SMichael Große
46c2fd0bf0SMichael Große        $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?", array($ID,));
47c2fd0bf0SMichael Große        if(!$this->sqlite->res2count($res)) return false;
48c2fd0bf0SMichael Große
4965598e4aSAndreas Gohr        $tables = array_map(
5065598e4aSAndreas Gohr            function ($value) {
5165598e4aSAndreas Gohr                return $value['tbl'];
5265598e4aSAndreas Gohr            },
5365598e4aSAndreas Gohr            $this->sqlite->res2arr($res)
5465598e4aSAndreas Gohr        );
55c2fd0bf0SMichael Große
5665598e4aSAndreas Gohr        $html = '';
57c2fd0bf0SMichael Große        foreach($tables as $table) {
5865598e4aSAndreas Gohr            $html .= $this->createForm($table);
59c2fd0bf0SMichael Große        }
60c2fd0bf0SMichael Große
6165598e4aSAndreas Gohr        /** @var Doku_Form $form */
6265598e4aSAndreas Gohr        $form = $event->data;
6365598e4aSAndreas Gohr        $html = "<div class=\"struct\">$html</div>";
6465598e4aSAndreas Gohr        $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
6565598e4aSAndreas Gohr        $form->insertElement($pos, $html);
6665598e4aSAndreas Gohr
67c2fd0bf0SMichael Große        return true;
68c2fd0bf0SMichael Große    }
69c2fd0bf0SMichael Große
70c2fd0bf0SMichael Große    /**
7165598e4aSAndreas Gohr     * Create the form to edit schemadata
72f36fda9dSAndreas Gohr     *
73c2fd0bf0SMichael Große     * @param string $tablename
7465598e4aSAndreas Gohr     * @return string The HTML for this schema's form
75c2fd0bf0SMichael Große     */
7665598e4aSAndreas Gohr    protected function createForm($tablename) {
77c2fd0bf0SMichael Große        global $ID;
7883beda18SAndreas Gohr        global $REV;
7983beda18SAndreas Gohr        $schema = new SchemaData($tablename, $ID, $REV);
80c2fd0bf0SMichael Große        $schemadata = $schema->getData();
81c2fd0bf0SMichael Große
8265598e4aSAndreas Gohr        $html = "<h3>$tablename</h3>";
83750a393bSAndreas Gohr        $cols = $schema->getColumns(false);
84c2fd0bf0SMichael Große        foreach ($cols as $index => $col) {
85c2fd0bf0SMichael Große            $type = $col->getType();
86c2fd0bf0SMichael Große            $label = $type->getLabel();
87c2fd0bf0SMichael Große            $name = "Schema[$tablename][$label]";
88*899b86acSAndreas Gohr            if($type->isMulti()) {
89*899b86acSAndreas Gohr                $input = $type->multiValueEditor($name, $schemadata[$label]);
90*899b86acSAndreas Gohr            } else {
91c2fd0bf0SMichael Große                $input = $type->valueEditor($name, $schemadata[$label]);
92*899b86acSAndreas Gohr            }
93c2fd0bf0SMichael Große            $element = "<label>$label $input</label><br />";
9465598e4aSAndreas Gohr            $html .= $element;
95c2fd0bf0SMichael Große        }
9665598e4aSAndreas Gohr
9765598e4aSAndreas Gohr        return $html;
98549a0837SAndreas Gohr    }
99549a0837SAndreas Gohr
100549a0837SAndreas Gohr}
101549a0837SAndreas Gohr
102549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
103