xref: /plugin/struct/admin/schemas.php (revision fb31ca9fa3249b10a91554bcd044f27dda620b99)
1<?php
2/**
3 * DokuWiki Plugin struct (Admin 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
10use dokuwiki\Form\Form;
11use plugin\struct\meta\Schema;
12use plugin\struct\meta\SchemaEditor;
13
14if(!defined('DOKU_INC')) die();
15
16class admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
17
18    /**
19     * @return int sort number in admin menu
20     */
21    public function getMenuSort() {
22        return 500;
23    }
24
25    /**
26     * @return bool true if only access for superuser, false is for superusers and moderators
27     */
28    public function forAdminOnly() {
29        return true;
30    }
31
32    /**
33     * Should carry out any processing required by the plugin.
34     */
35    public function handle() {
36        global $INPUT;
37
38        $table = Schema::cleanTableName($INPUT->str('table'));
39        if($table && $INPUT->bool('save') && checkSecurityToken()) {
40            $builder = new \plugin\struct\meta\SchemaBuilder($table, $INPUT->arr('schema'));
41            if(!$builder->build()) {
42                msg('something went wrong while saving', -1);
43            }
44        }
45
46    }
47
48    /**
49     * Render HTML output, e.g. helpful text and a form
50     */
51    public function html() {
52        global $INPUT;
53
54        echo $this->locale_xhtml('intro');
55
56        $table = Schema::cleanTableName($INPUT->str('table'));
57        if($table) {
58            echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>';
59
60            $editor = new SchemaEditor(new Schema($table));
61            echo $editor->getEditor();
62        } else {
63            $this->html_newschema();
64        }
65    }
66
67    /**
68     * Form to add a new schema
69     */
70    protected function html_newschema() {
71        $form = new Form();
72        $form->addFieldsetOpen($this->getLang('create'));
73        $form->setHiddenField('do', 'admin');
74        $form->setHiddenField('page', 'struct');
75        $form->addTextInput('table', $this->getLang('schemaname'));
76        $form->addButton('', $this->getLang('save'));
77        $form->addHTML('<p>'.$this->getLang('createhint').'</p>'); // FIXME is that true? we probably could
78        $form->addFieldsetClose();
79        echo $form->toHTML();
80    }
81
82    /**
83     * Adds all available schemas to the Table of Contents
84     *
85     * @return array
86     */
87    public function getTOC() {
88        global $ID;
89
90        /** @var helper_plugin_struct_db $helper */
91        $helper = plugin_load('helper', 'struct_db');
92        $db = $helper->getDB();
93
94        parent::getTOC();
95        if(!$db) return parent::getTOC();
96
97        $res = $db->query("SELECT DISTINCT tbl FROM schemas ORDER BY tbl");
98        $tables = $db->res2arr($res);
99        $db->res_close($res);
100
101        $toc = array();
102        $link = wl($ID, array(
103            'do' => 'admin',
104            'page' => 'struct'
105        ));
106        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
107
108        foreach($tables as $row) {
109            $link = wl($ID, array(
110                'do' => 'admin',
111                'page' => 'struct',
112                'table' => $row['tbl']
113            ));
114
115            $toc[] = html_mktocitem($link, hsc($row['tbl']), 1, '');
116        }
117        return $toc;
118    }
119
120}
121
122// vim:ts=4:sw=4:et:
123