xref: /plugin/struct/admin/schemas.php (revision 07993756737e69247e6a2b15c689ba4f83661a7c)
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 false;
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
55
56        $table = Schema::cleanTableName($INPUT->str('table'));
57        if($table) {
58            echo $this->locale_xhtml('editor_edit');
59
60            echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>';
61            $editor = new SchemaEditor(new Schema($table));
62            echo $editor->getEditor();
63        } else {
64            echo $this->locale_xhtml('editor_intro');
65            $this->html_newschema();
66        }
67    }
68
69    /**
70     * Form to add a new schema
71     */
72    protected function html_newschema() {
73        $form = new Form();
74        $form->addFieldsetOpen($this->getLang('create'));
75        $form->setHiddenField('do', 'admin');
76        $form->setHiddenField('page', 'struct_schemas');
77        $form->addTextInput('table', $this->getLang('schemaname'));
78        $form->addButton('', $this->getLang('save'));
79        $form->addHTML('<p>'.$this->getLang('createhint').'</p>'); // FIXME is that true? we probably could
80        $form->addFieldsetClose();
81        echo $form->toHTML();
82    }
83
84    /**
85     * Adds all available schemas to the Table of Contents
86     *
87     * @return array
88     */
89    public function getTOC() {
90        global $ID;
91
92        /** @var helper_plugin_struct_db $helper */
93        $helper = plugin_load('helper', 'struct_db');
94        $db = $helper->getDB();
95        if(!$db) return parent::getTOC();
96
97
98        $res = $db->query("SELECT DISTINCT tbl FROM schemas ORDER BY tbl");
99        $tables = $db->res2arr($res);
100        $db->res_close($res);
101
102        $toc = array();
103        $link = wl($ID, array(
104            'do' => 'admin',
105            'page' => 'struct_assignments'
106        ));
107        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
108        $link = wl($ID, array(
109            'do' => 'admin',
110            'page' => 'struct_schemas'
111        ));
112        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
113
114        foreach($tables as $row) {
115            $link = wl($ID, array(
116                'do' => 'admin',
117                'page' => 'struct_schemas',
118                'table' => $row['tbl']
119            ));
120
121            $toc[] = html_mktocitem($link, hsc($row['tbl']), 1, '');
122        }
123        return $toc;
124    }
125
126}
127
128// vim:ts=4:sw=4:et:
129