xref: /plugin/struct/admin/schemas.php (revision d5a1a6dcd8c74df8dbdb93e23d6c53ae8324501e)
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
9use dokuwiki\Form\Form;
10use plugin\struct\meta\Schema;
11use plugin\struct\meta\SchemaBuilder;
12use plugin\struct\meta\SchemaEditor;
13use plugin\struct\meta\SchemaImporter;
14use plugin\struct\meta\StructException;
15
16// must be run within Dokuwiki
17if(!defined('DOKU_INC')) die();
18
19class admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
20
21    /**
22     * @return int sort number in admin menu
23     */
24    public function getMenuSort() {
25        return 500;
26    }
27
28    /**
29     * @return bool true if only access for superuser, false is for superusers and moderators
30     */
31    public function forAdminOnly() {
32        return false;
33    }
34
35    /**
36     * Should carry out any processing required by the plugin.
37     */
38    public function handle() {
39        global $INPUT;
40        global $ID;
41
42        // form submit
43        $table = Schema::cleanTableName($INPUT->str('table'));
44        if($table && $INPUT->bool('save') && checkSecurityToken()) {
45            $builder = new SchemaBuilder($table, $INPUT->arr('schema'));
46            if(!$builder->build()) {
47                msg('something went wrong while saving', -1);
48            }
49        }
50        // export
51        if($table && $INPUT->bool('export')) {
52            $builder = new Schema($table);
53            header('Content-Type: application/json');
54            header("Content-Disposition: attachment; filename=$table.struct.json");
55            echo $builder->toJSON();
56            exit;
57        }
58        // import
59        if($table && $INPUT->bool('import')) {
60            if(isset($_FILES['schemafile']['tmp_name'])) {
61                $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
62                if(!$json) {
63                    msg('Something went wrong with the upload', -1);
64                } else {
65                    $builder = new SchemaImporter($table, $json);
66                    if(!$builder->build()) {
67                        msg('something went wrong while saving', -1);
68                    }
69                }
70            }
71        }
72        // delete
73        if($table && $INPUT->bool('delete')) {
74            if($table != $INPUT->str('confirm')) {
75                msg($this->getLang('del_fail'), -1);
76            } else {
77                try {
78                    $schema = new Schema($table);
79                    $schema->delete();
80                    msg($this->getLang('del_ok'), 1);
81                    send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
82                } catch(StructException $e) {
83                    msg(hsc($e->getMessage()), -1);
84                }
85            }
86        }
87
88    }
89
90    /**
91     * Render HTML output, e.g. helpful text and a form
92     */
93    public function html() {
94        global $INPUT;
95
96        $table = Schema::cleanTableName($INPUT->str('table'));
97        if($table) {
98            echo $this->locale_xhtml('editor_edit');
99            echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>';
100
101            echo '<ul class="tabs" id="plugin__struct_tabs">';
102            /** @noinspection HtmlUnknownAnchorTarget */
103            echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>';
104            /** @noinspection HtmlUnknownAnchorTarget */
105            echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>';
106            /** @noinspection HtmlUnknownAnchorTarget */
107            echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>';
108            echo '</ul>';
109            echo '<div class="panelHeader"></div>';
110
111            $editor = new SchemaEditor(new Schema($table));
112            echo $editor->getEditor();
113            echo $this->html_json();
114            echo $this->html_delete();
115
116        } else {
117            echo $this->locale_xhtml('editor_intro');
118            echo $this->html_newschema();
119        }
120    }
121
122    /**
123     * Form for handling import/export from/to JSON
124     * @return string
125     */
126    protected function html_json() {
127        global $INPUT;
128        $table = Schema::cleanTableName($INPUT->str('table'));
129
130        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
131        $form->setHiddenField('do', 'admin');
132        $form->setHiddenField('page', 'struct_schemas');
133        $form->setHiddenField('table', $table);
134
135        $form->addFieldsetOpen($this->getLang('export'));
136        $form->addButton('export', $this->getLang('btn_export'));
137        $form->addFieldsetClose();
138
139        $form->addFieldsetOpen($this->getLang('import'));
140        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
141        $form->addButton('import', $this->getLang('btn_import'));
142        $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>');
143        $form->addFieldsetClose();
144        return $form->toHTML();
145    }
146
147    /**
148     * Form for deleting schemas
149     * @return string
150     */
151    protected function html_delete() {
152        global $INPUT;
153        $table = Schema::cleanTableName($INPUT->str('table'));
154
155        $form = new Form(array('id' => 'plugin__struct_delete'));
156        $form->setHiddenField('do', 'admin');
157        $form->setHiddenField('page', 'struct_schemas');
158        $form->setHiddenField('table', $table);
159
160        $form->addHTML($this->locale_xhtml('delete_intro'));
161
162        $form->addFieldsetOpen($this->getLang('tab_delete'));
163        $form->addTextInput('confirm', $this->getLang('del_confirm'));
164        $form->addButton('delete', $this->getLang('btn_delete'));
165        $form->addFieldsetClose();
166        return $form->toHTML();
167    }
168
169    /**
170     * Form to add a new schema
171     *
172     * @return string
173     */
174    protected function html_newschema() {
175        $form = new Form();
176        $form->addFieldsetOpen($this->getLang('create'));
177        $form->setHiddenField('do', 'admin');
178        $form->setHiddenField('page', 'struct_schemas');
179        $form->addTextInput('table', $this->getLang('schemaname'));
180        $form->addButton('', $this->getLang('save'));
181        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
182        $form->addFieldsetClose();
183        return $form->toHTML();
184    }
185
186    /**
187     * Adds all available schemas to the Table of Contents
188     *
189     * @return array
190     */
191    public function getTOC() {
192        global $ID;
193
194        $toc = array();
195        $link = wl(
196            $ID, array(
197                   'do' => 'admin',
198                   'page' => 'struct_assignments'
199               )
200        );
201        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
202        $link = wl(
203            $ID, array(
204                   'do' => 'admin',
205                   'page' => 'struct_schemas'
206               )
207        );
208        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
209
210        $tables = Schema::getAll();
211        foreach($tables as $table) {
212            $link = wl(
213                $ID, array(
214                       'do' => 'admin',
215                       'page' => 'struct_schemas',
216                       'table' => $table
217                   )
218            );
219
220            $toc[] = html_mktocitem($link, hsc($table), 1, '');
221        }
222        return $toc;
223    }
224
225}
226
227// vim:ts=4:sw=4:et:
228