xref: /plugin/struct/admin/schemas.php (revision 16b7d914c0b69bf27d1ddd8a08e05beddbeedf02)
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        // form submit
39        $table = Schema::cleanTableName($INPUT->str('table'));
40        if($table && $INPUT->bool('save') && checkSecurityToken()) {
41            $builder = new \plugin\struct\meta\SchemaBuilder($table, $INPUT->arr('schema'));
42            if(!$builder->build()) {
43                msg('something went wrong while saving', -1);
44            }
45        }
46        // export
47        if($table && $INPUT->bool('export')) {
48            $builder = new \plugin\struct\meta\Schema($table);
49            header('Content-Type: application/json');
50            header("Content-Disposition: attachment; filename=$table.struct.json");
51            echo $builder->toJSON();
52            exit;
53        }
54        // import
55        if($table && $INPUT->bool('import')) {
56            if(isset($_FILES['schemafile']['tmp_name'])) {
57                $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
58                if(!$json) {
59                    msg('Something went wrong with the upload', -1);
60                } else {
61                    $builder = new \plugin\struct\meta\SchemaImporter($table, $json);
62                    if(!$builder->build()) {
63                        msg('something went wrong while saving', -1);
64                    }
65                }
66            }
67        }
68    }
69
70    /**
71     * Render HTML output, e.g. helpful text and a form
72     */
73    public function html() {
74        global $INPUT;
75
76        $table = Schema::cleanTableName($INPUT->str('table'));
77        if($table) {
78            echo $this->locale_xhtml('editor_edit');
79            echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>';
80
81            echo '<ul class="tabs" id="plugin__struct_tabs">';
82            /** @noinspection HtmlUnknownAnchorTarget */
83            echo '<li class="active"><a href="#plugin__struct_editor">'.$this->getLang('tab_edit').'</a></li>';
84            /** @noinspection HtmlUnknownAnchorTarget */
85            echo '<li><a href="#plugin__struct_json">'.$this->getLang('tab_export').'</a></li>';
86            echo '</ul>';
87            echo '<div class="panelHeader"></div>';
88
89            $editor = new SchemaEditor(new Schema($table));
90            echo $editor->getEditor();
91            echo $this->html_json();
92
93        } else {
94            echo $this->locale_xhtml('editor_intro');
95            echo $this->html_newschema();
96        }
97    }
98
99    /**
100     * Form for handling import/export from/to JSON
101     * @return string
102     */
103    protected function html_json() {
104        global $INPUT;
105        $table = Schema::cleanTableName($INPUT->str('table'));
106
107        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
108        $form->setHiddenField('do', 'admin');
109        $form->setHiddenField('page', 'struct_schemas');
110        $form->setHiddenField('table', $table);
111
112        $form->addFieldsetOpen($this->getLang('export'));
113        $form->addButton('export', $this->getLang('btn_export'));
114        $form->addFieldsetClose();
115
116        $form->addFieldsetOpen($this->getLang('import'));
117        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
118        $form->addButton('import', $this->getLang('btn_import'));
119        $form->addHTML('<p>'.$this->getLang('import_warning').'</p>');
120        $form->addFieldsetClose();
121        return $form->toHTML();
122    }
123
124    /**
125     * Form to add a new schema
126     *
127     * @return string
128     */
129    protected function html_newschema() {
130        $form = new Form();
131        $form->addFieldsetOpen($this->getLang('create'));
132        $form->setHiddenField('do', 'admin');
133        $form->setHiddenField('page', 'struct_schemas');
134        $form->addTextInput('table', $this->getLang('schemaname'));
135        $form->addButton('', $this->getLang('save'));
136        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
137        $form->addFieldsetClose();
138        return $form->toHTML();
139    }
140
141    /**
142     * Adds all available schemas to the Table of Contents
143     *
144     * @return array
145     */
146    public function getTOC() {
147        global $ID;
148
149        $toc = array();
150        $link = wl(
151            $ID, array(
152            'do' => 'admin',
153            'page' => 'struct_assignments'
154        )
155        );
156        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
157        $link = wl(
158            $ID, array(
159            'do' => 'admin',
160            'page' => 'struct_schemas'
161        )
162        );
163        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
164
165        $tables = Schema::getAll();
166        foreach($tables as $table) {
167            $link = wl(
168                $ID, array(
169                'do' => 'admin',
170                'page' => 'struct_schemas',
171                'table' => $table
172            )
173            );
174
175            $toc[] = html_mktocitem($link, hsc($table), 1, '');
176        }
177        return $toc;
178    }
179
180}
181
182// vim:ts=4:sw=4:et:
183