xref: /plugin/struct/admin/schemas.php (revision 79c83e060d46aeaaef23a55d59fca96b542ba82a)
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\CSVExporter;
11use dokuwiki\plugin\struct\meta\CSVImporter;
12use dokuwiki\plugin\struct\meta\Schema;
13use dokuwiki\plugin\struct\meta\SchemaBuilder;
14use dokuwiki\plugin\struct\meta\SchemaEditor;
15use dokuwiki\plugin\struct\meta\SchemaImporter;
16use dokuwiki\plugin\struct\meta\StructException;
17
18// must be run within Dokuwiki
19if(!defined('DOKU_INC')) die();
20
21class admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
22
23    /**
24     * @return int sort number in admin menu
25     */
26    public function getMenuSort() {
27        return 500;
28    }
29
30    /**
31     * @return bool true if only access for superuser, false is for superusers and moderators
32     */
33    public function forAdminOnly() {
34        return false;
35    }
36
37    /**
38     * Should carry out any processing required by the plugin.
39     */
40    public function handle() {
41        global $INPUT;
42        global $ID;
43        global $config_cascade;
44        $config_file_path = end($config_cascade['main']['local']);
45
46        // form submit
47        $table = Schema::cleanTableName($INPUT->str('table'));
48        if($table && $INPUT->bool('save') && checkSecurityToken()) {
49            $builder = new SchemaBuilder($table, $INPUT->arr('schema'));
50            if(!$builder->build()) {
51                msg('something went wrong while saving', -1);
52            }
53            touch(action_plugin_struct_cache::getSchemaRefreshFile());
54        }
55        // export
56        if($table && $INPUT->bool('export')) {
57            $builder = new Schema($table);
58            header('Content-Type: application/json');
59            header("Content-Disposition: attachment; filename=$table.struct.json");
60            echo $builder->toJSON();
61            exit;
62        }
63        // import
64        if($table && $INPUT->bool('import')) {
65            if(isset($_FILES['schemafile']['tmp_name'])) {
66                $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
67                if(!$json) {
68                    msg('Something went wrong with the upload', -1);
69                } else {
70                    $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup'));
71                    if(!$builder->build()) {
72                        msg('something went wrong while saving', -1);
73                    }
74                    touch(action_plugin_struct_cache::getSchemaRefreshFile());
75                }
76            }
77        }
78
79        // import CSV
80        if($table && $INPUT->bool('importcsv')) {
81            if(isset($_FILES['csvfile']['tmp_name'])) {
82                try {
83                    new CSVImporter($table, $_FILES['csvfile']['tmp_name']);
84                    msg($this->getLang('admin_csvdone'), 1);
85                } catch(StructException $e) {
86                    msg(hsc($e->getMessage()), -1);
87                }
88            }
89        }
90
91        // export CSV
92        if($table && $INPUT->bool('exportcsv')) {
93            header('Content-Type: text/csv');
94            header('Content-Disposition: attachment; filename="' . $table . '.csv";');
95            new CSVExporter($table);
96            exit();
97        }
98
99        // delete
100        if($table && $INPUT->bool('delete')) {
101            if($table != $INPUT->str('confirm')) {
102                msg($this->getLang('del_fail'), -1);
103            } else {
104                try {
105                    $schema = new Schema($table);
106                    $schema->delete();
107                    msg($this->getLang('del_ok'), 1);
108                    touch(action_plugin_struct_cache::getSchemaRefreshFile());
109                    send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
110                } catch(StructException $e) {
111                    msg(hsc($e->getMessage()), -1);
112                }
113            }
114        }
115
116        // clear
117        if($table && $INPUT->bool('clear')) {
118            if($table != $INPUT->str('confirm_clear')) {
119                msg($this->getLang('clear_fail'), -1);
120            } else {
121                try {
122                    $schema = new Schema($table);
123                    $schema->clear();
124                    msg($this->getLang('clear_ok'), 1);
125                    touch(action_plugin_struct_cache::getSchemaRefreshFile());
126                    send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
127                } catch(StructException $e) {
128                    msg(hsc($e->getMessage()), -1);
129                }
130            }
131        }
132
133    }
134
135    /**
136     * Render HTML output, e.g. helpful text and a form
137     */
138    public function html() {
139        global $INPUT;
140
141        $table = Schema::cleanTableName($INPUT->str('table'));
142        if($table) {
143            $schema = new Schema($table, 0, $INPUT->bool('lookup'));
144            if($schema->isLookup()) {
145                $hl = 'edithl lookup';
146            } else {
147                $hl = 'edithl page';
148            }
149
150            echo $this->locale_xhtml('editor_edit');
151            echo '<h2>' . sprintf($this->getLang($hl), hsc($table)) . '</h2>';
152
153            echo '<ul class="tabs" id="plugin__struct_tabs">';
154            /** @noinspection HtmlUnknownAnchorTarget */
155            echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>';
156            /** @noinspection HtmlUnknownAnchorTarget */
157            echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>';
158            /** @noinspection HtmlUnknownAnchorTarget */
159            echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>';
160            echo '</ul>';
161            echo '<div class="panelHeader"></div>';
162
163            $editor = new SchemaEditor($schema);
164            echo $editor->getEditor();
165            echo $this->html_json($schema);
166            echo $this->html_delete($schema);
167
168        } else {
169            echo $this->locale_xhtml('editor_intro');
170            echo $this->html_newschema();
171        }
172    }
173
174    /**
175     * Form for handling import/export from/to JSON
176     *
177     * @param Schema $schema
178     * @return string
179     */
180    protected function html_json(Schema $schema) {
181        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
182        $form->setHiddenField('do', 'admin');
183        $form->setHiddenField('page', 'struct_schemas');
184        $form->setHiddenField('table', $schema->getTable());
185        $form->setHiddenField('lookup', $schema->isLookup());
186
187        $form->addFieldsetOpen($this->getLang('export'));
188        $form->addButton('export', $this->getLang('btn_export'));
189        $form->addFieldsetClose();
190
191        $form->addFieldsetOpen($this->getLang('import'));
192        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
193        $form->addButton('import', $this->getLang('btn_import'));
194        $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>');
195        $form->addFieldsetClose();
196
197        $form->addFieldsetOpen($this->getLang('admin_csvexport'));
198        $form->addButton('exportcsv', $this->getLang('btn_export'));
199        $form->addFieldsetClose();
200
201        if($schema->isLookup()) {
202            $form->addFieldsetOpen($this->getLang('admin_csvimport'));
203            $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile'));
204            $form->addButton('importcsv', $this->getLang('btn_import'));
205            $form->addHTML('<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . $this->getLang('admin_csvhelp') . '</a></p>');
206            $form->addFieldsetClose();
207        }
208
209        return $form->toHTML();
210    }
211
212    /**
213     * Form for deleting schemas
214     *
215     * @param Schema $schema
216     * @return string
217     */
218    protected function html_delete(Schema $schema) {
219        $form = new Form(array('id' => 'plugin__struct_delete'));
220        $form->setHiddenField('do', 'admin');
221        $form->setHiddenField('page', 'struct_schemas');
222        $form->setHiddenField('table', $schema->getTable());
223
224        $form->addFieldsetOpen($this->getLang('btn_delete'));
225        $form->addHTML($this->locale_xhtml('delete_intro'));
226        $form->addTextInput('confirm', $this->getLang('del_confirm'));
227        $form->addButton('delete', $this->getLang('btn_delete'));
228        $form->addFieldsetClose();
229
230        $form->addFieldsetOpen($this->getLang('btn_clear'));
231        $form->addHTML($this->locale_xhtml('clear_intro'));
232        $form->addTextInput('confirm_clear', $this->getLang('clear_confirm'));
233        $form->addButton('clear', $this->getLang('btn_clear'));
234        $form->addFieldsetClose();
235
236        return $form->toHTML();
237    }
238
239    /**
240     * Form to add a new schema
241     *
242     * @return string
243     */
244    protected function html_newschema() {
245        $form = new Form();
246        $form->addClass('struct_newschema');
247        $form->addFieldsetOpen($this->getLang('create'));
248        $form->setHiddenField('do', 'admin');
249        $form->setHiddenField('page', 'struct_schemas');
250        $form->addTextInput('table', $this->getLang('schemaname'));
251        $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked');
252        $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1');
253        $form->addButton('', $this->getLang('save'));
254        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
255        $form->addFieldsetClose();
256        return $form->toHTML();
257    }
258
259    /**
260     * Adds all available schemas to the Table of Contents
261     *
262     * @return array
263     */
264    public function getTOC() {
265        global $ID;
266
267        $toc = array();
268        $link = wl(
269            $ID, array(
270                   'do' => 'admin',
271                   'page' => 'struct_assignments'
272               )
273        );
274        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
275        $slink = wl(
276            $ID, array(
277                   'do' => 'admin',
278                   'page' => 'struct_schemas'
279               )
280        );
281        $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, '');
282
283        $tables = Schema::getAll('page');
284        if($tables) {
285            $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, '');
286            foreach($tables as $table) {
287                $link = wl(
288                    $ID, array(
289                           'do' => 'admin',
290                           'page' => 'struct_schemas',
291                           'table' => $table
292                       )
293                );
294
295                $toc[] = html_mktocitem($link, hsc($table), 2, '');
296            }
297        }
298
299        $tables = Schema::getAll('lookup');
300        if($tables) {
301            $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, '');
302            foreach($tables as $table) {
303                $link = wl(
304                    $ID, array(
305                           'do' => 'admin',
306                           'page' => 'struct_schemas',
307                           'table' => $table
308                       )
309                );
310
311                $toc[] = html_mktocitem($link, hsc($table), 2, '');
312            }
313        }
314
315        return $toc;
316    }
317
318
319
320}
321
322// vim:ts=4:sw=4:et:
323