xref: /plugin/struct/admin/schemas.php (revision 8ddf87af744169eff8f5d143d3a526ba2a2e2987)
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ße// must be run within Dokuwiki
1087fdbc6bSMichael Großeuse dokuwiki\Form\Form;
1187fdbc6bSMichael Großeuse plugin\struct\meta\Schema;
1287fdbc6bSMichael Großeuse plugin\struct\meta\SchemaEditor;
1387fdbc6bSMichael Große
1487fdbc6bSMichael Großeif(!defined('DOKU_INC')) die();
1587fdbc6bSMichael Große
1687fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
1787fdbc6bSMichael Große
1887fdbc6bSMichael Große    /**
1987fdbc6bSMichael Große     * @return int sort number in admin menu
2087fdbc6bSMichael Große     */
2187fdbc6bSMichael Große    public function getMenuSort() {
2287fdbc6bSMichael Große        return 500;
2387fdbc6bSMichael Große    }
2487fdbc6bSMichael Große
2587fdbc6bSMichael Große    /**
2687fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
2787fdbc6bSMichael Große     */
2887fdbc6bSMichael Große    public function forAdminOnly() {
294d220607SAndreas Gohr        return false;
3087fdbc6bSMichael Große    }
3187fdbc6bSMichael Große
3287fdbc6bSMichael Große    /**
3387fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
3487fdbc6bSMichael Große     */
3587fdbc6bSMichael Große    public function handle() {
3687fdbc6bSMichael Große        global $INPUT;
3787fdbc6bSMichael Große
38*8ddf87afSAndreas Gohr        // form submit
3987fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
4087fdbc6bSMichael Große        if($table && $INPUT->bool('save') && checkSecurityToken()) {
4187fdbc6bSMichael Große            $builder = new \plugin\struct\meta\SchemaBuilder($table, $INPUT->arr('schema'));
4287fdbc6bSMichael Große            if(!$builder->build()) {
4387fdbc6bSMichael Große                msg('something went wrong while saving', -1);
4487fdbc6bSMichael Große            }
4587fdbc6bSMichael Große        }
46*8ddf87afSAndreas Gohr        // export
47d486d6d7SAndreas Gohr        if($table && $INPUT->bool('export')) {
48d486d6d7SAndreas Gohr            $builder = new \plugin\struct\meta\Schema($table);
49d486d6d7SAndreas Gohr            header('Content-Type: application/json');
50d486d6d7SAndreas Gohr            header("Content-Disposition: attachment; filename=$table.struct.json");
51d486d6d7SAndreas Gohr            echo $builder->toJSON();
52d486d6d7SAndreas Gohr            exit;
53d486d6d7SAndreas Gohr        }
54*8ddf87afSAndreas Gohr        // import
55*8ddf87afSAndreas Gohr        if($table && $INPUT->bool('import')) {
56*8ddf87afSAndreas Gohr            if(isset($_FILES['schemafile']['tmp_name'])) {
57*8ddf87afSAndreas Gohr                $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
58*8ddf87afSAndreas Gohr                if(!$json) {
59*8ddf87afSAndreas Gohr                    msg('Something went wrong with the upload', -1);
60*8ddf87afSAndreas Gohr                } else {
61*8ddf87afSAndreas Gohr                    $builder = new \plugin\struct\meta\SchemaImporter($table, $json);
62*8ddf87afSAndreas Gohr                    if(!$builder->build()) {
63*8ddf87afSAndreas Gohr                        msg('something went wrong while saving', -1);
64*8ddf87afSAndreas Gohr                    }
65*8ddf87afSAndreas Gohr                }
66*8ddf87afSAndreas Gohr            }
67*8ddf87afSAndreas Gohr        }
6887fdbc6bSMichael Große    }
6987fdbc6bSMichael Große
7087fdbc6bSMichael Große    /**
7187fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
7287fdbc6bSMichael Große     */
7387fdbc6bSMichael Große    public function html() {
7487fdbc6bSMichael Große        global $INPUT;
7587fdbc6bSMichael Große
7687fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
7787fdbc6bSMichael Große        if($table) {
786af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_edit');
796af24d3eSAndreas Gohr            echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>';
80*8ddf87afSAndreas Gohr
81*8ddf87afSAndreas Gohr            echo '<ul class="tabs" id="plugin__struct_tabs">';
82*8ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
83*8ddf87afSAndreas Gohr            echo '<li class="active"><a href="#plugin__struct_editor">'.$this->getLang('tab_edit').'</a></li>';
84*8ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
85*8ddf87afSAndreas Gohr            echo '<li><a href="#plugin__struct_json">'.$this->getLang('tab_export').'</a></li>';
86*8ddf87afSAndreas Gohr            echo '</ul>';
87*8ddf87afSAndreas Gohr            echo '<div class="panelHeader"></div>';
88*8ddf87afSAndreas Gohr
8987fdbc6bSMichael Große            $editor = new SchemaEditor(new Schema($table));
9087fdbc6bSMichael Große            echo $editor->getEditor();
91*8ddf87afSAndreas Gohr            echo $this->html_json();
92d486d6d7SAndreas Gohr
9387fdbc6bSMichael Große        } else {
946af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_intro');
95*8ddf87afSAndreas Gohr            echo $this->html_newschema();
9687fdbc6bSMichael Große        }
9787fdbc6bSMichael Große    }
9887fdbc6bSMichael Große
9987fdbc6bSMichael Große    /**
100*8ddf87afSAndreas Gohr     * Form for handling import/export from/to JSON
101*8ddf87afSAndreas Gohr     * @return string
102*8ddf87afSAndreas Gohr     */
103*8ddf87afSAndreas Gohr    protected function html_json() {
104*8ddf87afSAndreas Gohr        global $INPUT;
105*8ddf87afSAndreas Gohr        $table = Schema::cleanTableName($INPUT->str('table'));
106*8ddf87afSAndreas Gohr
107*8ddf87afSAndreas Gohr        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
108*8ddf87afSAndreas Gohr        $form->setHiddenField('do', 'admin');
109*8ddf87afSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
110*8ddf87afSAndreas Gohr        $form->setHiddenField('table', $table);
111*8ddf87afSAndreas Gohr
112*8ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('export'));
113*8ddf87afSAndreas Gohr        $form->addButton('export', $this->getLang('btn_export'));
114*8ddf87afSAndreas Gohr        $form->addFieldsetClose();
115*8ddf87afSAndreas Gohr
116*8ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('import'));
117*8ddf87afSAndreas Gohr        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
118*8ddf87afSAndreas Gohr        $form->addButton('import', $this->getLang('btn_import'));
119*8ddf87afSAndreas Gohr        $form->addHTML('<p>'.$this->getLang('import_warning').'</p>');
120*8ddf87afSAndreas Gohr        $form->addFieldsetClose();
121*8ddf87afSAndreas Gohr        return $form->toHTML();
122*8ddf87afSAndreas Gohr    }
123*8ddf87afSAndreas Gohr
124*8ddf87afSAndreas Gohr    /**
12587fdbc6bSMichael Große     * Form to add a new schema
126*8ddf87afSAndreas Gohr     *
127*8ddf87afSAndreas Gohr     * @return string
12887fdbc6bSMichael Große     */
12987fdbc6bSMichael Große    protected function html_newschema() {
13087fdbc6bSMichael Große        $form = new Form();
13187fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
13287fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
133dbffe06eSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
13487fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
13587fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
13687fdbc6bSMichael Große        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
13787fdbc6bSMichael Große        $form->addFieldsetClose();
138*8ddf87afSAndreas Gohr        return $form->toHTML();
13987fdbc6bSMichael Große    }
14087fdbc6bSMichael Große
14187fdbc6bSMichael Große    /**
14287fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
14387fdbc6bSMichael Große     *
14487fdbc6bSMichael Große     * @return array
14587fdbc6bSMichael Große     */
14687fdbc6bSMichael Große    public function getTOC() {
14787fdbc6bSMichael Große        global $ID;
14887fdbc6bSMichael Große
14987fdbc6bSMichael Große        $toc = array();
150*8ddf87afSAndreas Gohr        $link = wl(
151*8ddf87afSAndreas Gohr            $ID, array(
15287fdbc6bSMichael Große            'do' => 'admin',
153dbffe06eSAndreas Gohr            'page' => 'struct_assignments'
154*8ddf87afSAndreas Gohr        )
155*8ddf87afSAndreas Gohr        );
156dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
157*8ddf87afSAndreas Gohr        $link = wl(
158*8ddf87afSAndreas Gohr            $ID, array(
159dbffe06eSAndreas Gohr            'do' => 'admin',
160dbffe06eSAndreas Gohr            'page' => 'struct_schemas'
161*8ddf87afSAndreas Gohr        )
162*8ddf87afSAndreas Gohr        );
16387fdbc6bSMichael Große        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
16487fdbc6bSMichael Große
165097f4a53SAndreas Gohr        $tables = Schema::getAll();
166097f4a53SAndreas Gohr        foreach($tables as $table) {
167*8ddf87afSAndreas Gohr            $link = wl(
168*8ddf87afSAndreas Gohr                $ID, array(
16987fdbc6bSMichael Große                'do' => 'admin',
170dbffe06eSAndreas Gohr                'page' => 'struct_schemas',
171097f4a53SAndreas Gohr                'table' => $table
172*8ddf87afSAndreas Gohr            )
173*8ddf87afSAndreas Gohr            );
17487fdbc6bSMichael Große
175097f4a53SAndreas Gohr            $toc[] = html_mktocitem($link, hsc($table), 1, '');
17687fdbc6bSMichael Große        }
17787fdbc6bSMichael Große        return $toc;
17887fdbc6bSMichael Große    }
17987fdbc6bSMichael Große
18087fdbc6bSMichael Große}
18187fdbc6bSMichael Große
18287fdbc6bSMichael Große// vim:ts=4:sw=4:et:
183