xref: /plugin/struct/admin/schemas.php (revision d486d6d7d6c607c391a45aa431842e8fbc9d74c3)
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        if($table && $INPUT->bool('export')) {
46            $builder = new \plugin\struct\meta\Schema($table);
47            header('Content-Type: application/json');
48            header("Content-Disposition: attachment; filename=$table.struct.json");
49            echo $builder->toJSON();
50            exit;
51        }
52    }
53
54    /**
55     * Render HTML output, e.g. helpful text and a form
56     */
57    public function html() {
58        global $INPUT;
59        global $ID;
60
61        $table = Schema::cleanTableName($INPUT->str('table'));
62        if($table) {
63            echo $this->locale_xhtml('editor_edit');
64
65            echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>';
66            $editor = new SchemaEditor(new Schema($table));
67            echo $editor->getEditor();
68
69            echo '<p>';
70            $export = wl($ID, array('do' => 'admin', 'page' => 'struct_schemas', 'table' => $table, 'export' => 1));
71            echo '<a href="' . $export . '">' . $this->getLang('export') . '</a>';
72            echo '</p>';
73        } else {
74            echo $this->locale_xhtml('editor_intro');
75            $this->html_newschema();
76        }
77    }
78
79    /**
80     * Form to add a new schema
81     */
82    protected function html_newschema() {
83        $form = new Form();
84        $form->addFieldsetOpen($this->getLang('create'));
85        $form->setHiddenField('do', 'admin');
86        $form->setHiddenField('page', 'struct_schemas');
87        $form->addTextInput('table', $this->getLang('schemaname'));
88        $form->addButton('', $this->getLang('save'));
89        $form->addHTML('<p>'.$this->getLang('createhint').'</p>'); // FIXME is that true? we probably could
90        $form->addFieldsetClose();
91        echo $form->toHTML();
92    }
93
94    /**
95     * Adds all available schemas to the Table of Contents
96     *
97     * @return array
98     */
99    public function getTOC() {
100        global $ID;
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        $tables = Schema::getAll();
115        foreach($tables as $table) {
116            $link = wl($ID, array(
117                'do' => 'admin',
118                'page' => 'struct_schemas',
119                'table' => $table
120            ));
121
122            $toc[] = html_mktocitem($link, hsc($table), 1, '');
123        }
124        return $toc;
125    }
126
127}
128
129// vim:ts=4:sw=4:et:
130