xref: /plugin/struct/admin/schemas.php (revision 93ca6f4f4326051070b84b0a5019072b884cd2c5)
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, $INPUT->bool('lookup'));
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            $schema = new Schema($table, 0, $INPUT->bool('lookup'));
104            if($schema->isLookup()) {
105                $hl = 'edithl lookup';
106            } else {
107                $hl = 'edithl page';
108            }
109
110            echo $this->locale_xhtml('editor_edit');
111            echo '<h2>' . sprintf($this->getLang($hl), hsc($table)) . '</h2>';
112
113            echo '<ul class="tabs" id="plugin__struct_tabs">';
114            /** @noinspection HtmlUnknownAnchorTarget */
115            echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>';
116            /** @noinspection HtmlUnknownAnchorTarget */
117            echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>';
118            /** @noinspection HtmlUnknownAnchorTarget */
119            echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>';
120            echo '</ul>';
121            echo '<div class="panelHeader"></div>';
122
123            $editor = new SchemaEditor($schema);
124            echo $editor->getEditor();
125            echo $this->html_json($schema);
126            echo $this->html_delete($schema);
127
128        } else {
129            echo $this->locale_xhtml('editor_intro');
130            echo $this->html_newschema();
131        }
132    }
133
134    /**
135     * Form for handling import/export from/to JSON
136     *
137     * @param Schema $schema
138     * @return string
139     */
140    protected function html_json(Schema $schema) {
141        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
142        $form->setHiddenField('do', 'admin');
143        $form->setHiddenField('page', 'struct_schemas');
144        $form->setHiddenField('table', $schema->getTable());
145        $form->setHiddenField('lookup', $schema->isLookup());
146
147        $form->addFieldsetOpen($this->getLang('export'));
148        $form->addButton('export', $this->getLang('btn_export'));
149        $form->addFieldsetClose();
150
151        $form->addFieldsetOpen($this->getLang('import'));
152        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
153        $form->addButton('import', $this->getLang('btn_import'));
154        $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>');
155        $form->addFieldsetClose();
156        return $form->toHTML();
157    }
158
159    /**
160     * Form for deleting schemas
161     *
162     * @param Schema $schema
163     * @return string
164     */
165    protected function html_delete(Schema $schema) {
166        $form = new Form(array('id' => 'plugin__struct_delete'));
167        $form->setHiddenField('do', 'admin');
168        $form->setHiddenField('page', 'struct_schemas');
169        $form->setHiddenField('table', $schema->getTable());
170
171        $form->addHTML($this->locale_xhtml('delete_intro'));
172
173        $form->addFieldsetOpen($this->getLang('tab_delete'));
174        $form->addTextInput('confirm', $this->getLang('del_confirm'));
175        $form->addButton('delete', $this->getLang('btn_delete'));
176        $form->addFieldsetClose();
177        return $form->toHTML();
178    }
179
180    /**
181     * Form to add a new schema
182     *
183     * @return string
184     */
185    protected function html_newschema() {
186        $form = new Form();
187        $form->addClass('struct_newschema');
188        $form->addFieldsetOpen($this->getLang('create'));
189        $form->setHiddenField('do', 'admin');
190        $form->setHiddenField('page', 'struct_schemas');
191        $form->addTextInput('table', $this->getLang('schemaname'));
192        $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked');
193        $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1');
194        $form->addButton('', $this->getLang('save'));
195        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
196        $form->addFieldsetClose();
197        return $form->toHTML();
198    }
199
200    /**
201     * Adds all available schemas to the Table of Contents
202     *
203     * @return array
204     */
205    public function getTOC() {
206        global $ID;
207
208        $toc = array();
209        $link = wl(
210            $ID, array(
211                   'do' => 'admin',
212                   'page' => 'struct_assignments'
213               )
214        );
215        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
216        $slink = wl(
217            $ID, array(
218                   'do' => 'admin',
219                   'page' => 'struct_schemas'
220               )
221        );
222        $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, '');
223
224        $tables = Schema::getAll('page');
225        if($tables) {
226            $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, '');
227            foreach($tables as $table) {
228                $link = wl(
229                    $ID, array(
230                           'do' => 'admin',
231                           'page' => 'struct_schemas',
232                           'table' => $table
233                       )
234                );
235
236                $toc[] = html_mktocitem($link, hsc($table), 2, '');
237            }
238        }
239
240        $tables = Schema::getAll('lookup');
241        if($tables) {
242            $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, '');
243            foreach($tables as $table) {
244                $link = wl(
245                    $ID, array(
246                           'do' => 'admin',
247                           'page' => 'struct_schemas',
248                           'table' => $table
249                       )
250                );
251
252                $toc[] = html_mktocitem($link, hsc($table), 2, '');
253            }
254        }
255
256        return $toc;
257    }
258
259}
260
261// vim:ts=4:sw=4:et:
262