xref: /plugin/struct/admin/schemas.php (revision 9e7c5990c4fd72885372fd25ef4b773e6325df54)
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_schemas');
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        if(!$db) return parent::getTOC();
94
95
96        $res = $db->query("SELECT DISTINCT tbl FROM schemas ORDER BY tbl");
97        $tables = $db->res2arr($res);
98        $db->res_close($res);
99
100        $toc = array();
101        $link = wl($ID, array(
102            'do' => 'admin',
103            'page' => 'struct_assignments'
104        ));
105        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
106        $link = wl($ID, array(
107            'do' => 'admin',
108            'page' => 'struct_schemas'
109        ));
110        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
111
112        foreach($tables as $row) {
113            $link = wl($ID, array(
114                'do' => 'admin',
115                'page' => 'struct_schemas',
116                'table' => $row['tbl']
117            ));
118
119            $toc[] = html_mktocitem($link, hsc($row['tbl']), 1, '');
120        }
121        return $toc;
122    }
123
124}
125
126// vim:ts=4:sw=4:et:
127