xref: /plugin/struct/admin/schemas.php (revision ba76620163eb4cb2b8d6042c6bec7725074f508c)
187fdbc6bSMichael Große<?php
287fdbc6bSMichael Große/**
387fdbc6bSMichael Große * DokuWiki Plugin struct (Admin Component)
487fdbc6bSMichael Große *
587fdbc6bSMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
687fdbc6bSMichael Große * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
787fdbc6bSMichael Große */
887fdbc6bSMichael Große
987fdbc6bSMichael Großeuse dokuwiki\Form\Form;
10*ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
11*ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder;
12*ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaEditor;
13*ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter;
14*ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
1587fdbc6bSMichael Große
16d5a1a6dcSAndreas Gohr// must be run within Dokuwiki
1787fdbc6bSMichael Großeif(!defined('DOKU_INC')) die();
1887fdbc6bSMichael Große
1987fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
2087fdbc6bSMichael Große
2187fdbc6bSMichael Große    /**
2287fdbc6bSMichael Große     * @return int sort number in admin menu
2387fdbc6bSMichael Große     */
2487fdbc6bSMichael Große    public function getMenuSort() {
2587fdbc6bSMichael Große        return 500;
2687fdbc6bSMichael Große    }
2787fdbc6bSMichael Große
2887fdbc6bSMichael Große    /**
2987fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
3087fdbc6bSMichael Große     */
3187fdbc6bSMichael Große    public function forAdminOnly() {
324d220607SAndreas Gohr        return false;
3387fdbc6bSMichael Große    }
3487fdbc6bSMichael Große
3587fdbc6bSMichael Große    /**
3687fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
3787fdbc6bSMichael Große     */
3887fdbc6bSMichael Große    public function handle() {
3987fdbc6bSMichael Große        global $INPUT;
40d5a1a6dcSAndreas Gohr        global $ID;
4187fdbc6bSMichael Große
428ddf87afSAndreas Gohr        // form submit
4387fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
4487fdbc6bSMichael Große        if($table && $INPUT->bool('save') && checkSecurityToken()) {
45d5a1a6dcSAndreas Gohr            $builder = new SchemaBuilder($table, $INPUT->arr('schema'));
4687fdbc6bSMichael Große            if(!$builder->build()) {
4787fdbc6bSMichael Große                msg('something went wrong while saving', -1);
4887fdbc6bSMichael Große            }
4987fdbc6bSMichael Große        }
508ddf87afSAndreas Gohr        // export
51d486d6d7SAndreas Gohr        if($table && $INPUT->bool('export')) {
52d5a1a6dcSAndreas Gohr            $builder = new Schema($table);
53d486d6d7SAndreas Gohr            header('Content-Type: application/json');
54d486d6d7SAndreas Gohr            header("Content-Disposition: attachment; filename=$table.struct.json");
55d486d6d7SAndreas Gohr            echo $builder->toJSON();
56d486d6d7SAndreas Gohr            exit;
57d486d6d7SAndreas Gohr        }
588ddf87afSAndreas Gohr        // import
598ddf87afSAndreas Gohr        if($table && $INPUT->bool('import')) {
608ddf87afSAndreas Gohr            if(isset($_FILES['schemafile']['tmp_name'])) {
618ddf87afSAndreas Gohr                $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
628ddf87afSAndreas Gohr                if(!$json) {
638ddf87afSAndreas Gohr                    msg('Something went wrong with the upload', -1);
648ddf87afSAndreas Gohr                } else {
65d5a1a6dcSAndreas Gohr                    $builder = new SchemaImporter($table, $json);
668ddf87afSAndreas Gohr                    if(!$builder->build()) {
678ddf87afSAndreas Gohr                        msg('something went wrong while saving', -1);
688ddf87afSAndreas Gohr                    }
698ddf87afSAndreas Gohr                }
708ddf87afSAndreas Gohr            }
718ddf87afSAndreas Gohr        }
72d5a1a6dcSAndreas Gohr        // delete
73d5a1a6dcSAndreas Gohr        if($table && $INPUT->bool('delete')) {
74d5a1a6dcSAndreas Gohr            if($table != $INPUT->str('confirm')) {
75d5a1a6dcSAndreas Gohr                msg($this->getLang('del_fail'), -1);
76d5a1a6dcSAndreas Gohr            } else {
77d5a1a6dcSAndreas Gohr                try {
78d5a1a6dcSAndreas Gohr                    $schema = new Schema($table);
79d5a1a6dcSAndreas Gohr                    $schema->delete();
80d5a1a6dcSAndreas Gohr                    msg($this->getLang('del_ok'), 1);
81d5a1a6dcSAndreas Gohr                    send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
82d5a1a6dcSAndreas Gohr                } catch(StructException $e) {
83d5a1a6dcSAndreas Gohr                    msg(hsc($e->getMessage()), -1);
84d5a1a6dcSAndreas Gohr                }
85d5a1a6dcSAndreas Gohr            }
86d5a1a6dcSAndreas Gohr        }
87d5a1a6dcSAndreas Gohr
8887fdbc6bSMichael Große    }
8987fdbc6bSMichael Große
9087fdbc6bSMichael Große    /**
9187fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
9287fdbc6bSMichael Große     */
9387fdbc6bSMichael Große    public function html() {
9487fdbc6bSMichael Große        global $INPUT;
9587fdbc6bSMichael Große
9687fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
9787fdbc6bSMichael Große        if($table) {
986af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_edit');
996af24d3eSAndreas Gohr            echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>';
1008ddf87afSAndreas Gohr
1018ddf87afSAndreas Gohr            echo '<ul class="tabs" id="plugin__struct_tabs">';
1028ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
1038ddf87afSAndreas Gohr            echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>';
1048ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
1058ddf87afSAndreas Gohr            echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>';
106d5a1a6dcSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
107d5a1a6dcSAndreas Gohr            echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>';
1088ddf87afSAndreas Gohr            echo '</ul>';
1098ddf87afSAndreas Gohr            echo '<div class="panelHeader"></div>';
1108ddf87afSAndreas Gohr
11187fdbc6bSMichael Große            $editor = new SchemaEditor(new Schema($table));
11287fdbc6bSMichael Große            echo $editor->getEditor();
1138ddf87afSAndreas Gohr            echo $this->html_json();
114d5a1a6dcSAndreas Gohr            echo $this->html_delete();
115d486d6d7SAndreas Gohr
11687fdbc6bSMichael Große        } else {
1176af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_intro');
1188ddf87afSAndreas Gohr            echo $this->html_newschema();
11987fdbc6bSMichael Große        }
12087fdbc6bSMichael Große    }
12187fdbc6bSMichael Große
12287fdbc6bSMichael Große    /**
1238ddf87afSAndreas Gohr     * Form for handling import/export from/to JSON
1248ddf87afSAndreas Gohr     * @return string
1258ddf87afSAndreas Gohr     */
1268ddf87afSAndreas Gohr    protected function html_json() {
1278ddf87afSAndreas Gohr        global $INPUT;
1288ddf87afSAndreas Gohr        $table = Schema::cleanTableName($INPUT->str('table'));
1298ddf87afSAndreas Gohr
1308ddf87afSAndreas Gohr        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
1318ddf87afSAndreas Gohr        $form->setHiddenField('do', 'admin');
1328ddf87afSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
1338ddf87afSAndreas Gohr        $form->setHiddenField('table', $table);
1348ddf87afSAndreas Gohr
1358ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('export'));
1368ddf87afSAndreas Gohr        $form->addButton('export', $this->getLang('btn_export'));
1378ddf87afSAndreas Gohr        $form->addFieldsetClose();
1388ddf87afSAndreas Gohr
1398ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('import'));
1408ddf87afSAndreas Gohr        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
1418ddf87afSAndreas Gohr        $form->addButton('import', $this->getLang('btn_import'));
1428ddf87afSAndreas Gohr        $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>');
1438ddf87afSAndreas Gohr        $form->addFieldsetClose();
1448ddf87afSAndreas Gohr        return $form->toHTML();
1458ddf87afSAndreas Gohr    }
1468ddf87afSAndreas Gohr
1478ddf87afSAndreas Gohr    /**
148d5a1a6dcSAndreas Gohr     * Form for deleting schemas
149d5a1a6dcSAndreas Gohr     * @return string
150d5a1a6dcSAndreas Gohr     */
151d5a1a6dcSAndreas Gohr    protected function html_delete() {
152d5a1a6dcSAndreas Gohr        global $INPUT;
153d5a1a6dcSAndreas Gohr        $table = Schema::cleanTableName($INPUT->str('table'));
154d5a1a6dcSAndreas Gohr
155d5a1a6dcSAndreas Gohr        $form = new Form(array('id' => 'plugin__struct_delete'));
156d5a1a6dcSAndreas Gohr        $form->setHiddenField('do', 'admin');
157d5a1a6dcSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
158d5a1a6dcSAndreas Gohr        $form->setHiddenField('table', $table);
159d5a1a6dcSAndreas Gohr
160d5a1a6dcSAndreas Gohr        $form->addHTML($this->locale_xhtml('delete_intro'));
161d5a1a6dcSAndreas Gohr
162d5a1a6dcSAndreas Gohr        $form->addFieldsetOpen($this->getLang('tab_delete'));
163d5a1a6dcSAndreas Gohr        $form->addTextInput('confirm', $this->getLang('del_confirm'));
164d5a1a6dcSAndreas Gohr        $form->addButton('delete', $this->getLang('btn_delete'));
165d5a1a6dcSAndreas Gohr        $form->addFieldsetClose();
166d5a1a6dcSAndreas Gohr        return $form->toHTML();
167d5a1a6dcSAndreas Gohr    }
168d5a1a6dcSAndreas Gohr
169d5a1a6dcSAndreas Gohr    /**
17087fdbc6bSMichael Große     * Form to add a new schema
1718ddf87afSAndreas Gohr     *
1728ddf87afSAndreas Gohr     * @return string
17387fdbc6bSMichael Große     */
17487fdbc6bSMichael Große    protected function html_newschema() {
17587fdbc6bSMichael Große        $form = new Form();
17687fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
17787fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
178dbffe06eSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
17987fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
18087fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
18187fdbc6bSMichael Große        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
18287fdbc6bSMichael Große        $form->addFieldsetClose();
1838ddf87afSAndreas Gohr        return $form->toHTML();
18487fdbc6bSMichael Große    }
18587fdbc6bSMichael Große
18687fdbc6bSMichael Große    /**
18787fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
18887fdbc6bSMichael Große     *
18987fdbc6bSMichael Große     * @return array
19087fdbc6bSMichael Große     */
19187fdbc6bSMichael Große    public function getTOC() {
19287fdbc6bSMichael Große        global $ID;
19387fdbc6bSMichael Große
19487fdbc6bSMichael Große        $toc = array();
1958ddf87afSAndreas Gohr        $link = wl(
1968ddf87afSAndreas Gohr            $ID, array(
19787fdbc6bSMichael Große                   'do' => 'admin',
198dbffe06eSAndreas Gohr                   'page' => 'struct_assignments'
1998ddf87afSAndreas Gohr               )
2008ddf87afSAndreas Gohr        );
201dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
2028ddf87afSAndreas Gohr        $link = wl(
2038ddf87afSAndreas Gohr            $ID, array(
204dbffe06eSAndreas Gohr                   'do' => 'admin',
205dbffe06eSAndreas Gohr                   'page' => 'struct_schemas'
2068ddf87afSAndreas Gohr               )
2078ddf87afSAndreas Gohr        );
20887fdbc6bSMichael Große        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
20987fdbc6bSMichael Große
210097f4a53SAndreas Gohr        $tables = Schema::getAll();
211097f4a53SAndreas Gohr        foreach($tables as $table) {
2128ddf87afSAndreas Gohr            $link = wl(
2138ddf87afSAndreas Gohr                $ID, array(
21487fdbc6bSMichael Große                       'do' => 'admin',
215dbffe06eSAndreas Gohr                       'page' => 'struct_schemas',
216097f4a53SAndreas Gohr                       'table' => $table
2178ddf87afSAndreas Gohr                   )
2188ddf87afSAndreas Gohr            );
21987fdbc6bSMichael Große
220097f4a53SAndreas Gohr            $toc[] = html_mktocitem($link, hsc($table), 1, '');
22187fdbc6bSMichael Große        }
22287fdbc6bSMichael Große        return $toc;
22387fdbc6bSMichael Große    }
22487fdbc6bSMichael Große
22587fdbc6bSMichael Große}
22687fdbc6bSMichael Große
22787fdbc6bSMichael Große// vim:ts=4:sw=4:et:
228