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