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