xref: /plugin/struct/admin/schemas.php (revision 097f4a53a9f2c54a89643d357681041c89816aed)
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
3887fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
3987fdbc6bSMichael Große        if($table && $INPUT->bool('save') && checkSecurityToken()) {
4087fdbc6bSMichael Große            $builder = new \plugin\struct\meta\SchemaBuilder($table, $INPUT->arr('schema'));
4187fdbc6bSMichael Große            if(!$builder->build()) {
4287fdbc6bSMichael Große                msg('something went wrong while saving', -1);
4387fdbc6bSMichael Große            }
4487fdbc6bSMichael Große        }
4587fdbc6bSMichael Große
4687fdbc6bSMichael Große    }
4787fdbc6bSMichael Große
4887fdbc6bSMichael Große    /**
4987fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
5087fdbc6bSMichael Große     */
5187fdbc6bSMichael Große    public function html() {
5287fdbc6bSMichael Große        global $INPUT;
5387fdbc6bSMichael Große
546af24d3eSAndreas Gohr
5587fdbc6bSMichael Große
5687fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
5787fdbc6bSMichael Große        if($table) {
586af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_edit');
5987fdbc6bSMichael Große
606af24d3eSAndreas Gohr            echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>';
6187fdbc6bSMichael Große            $editor = new SchemaEditor(new Schema($table));
6287fdbc6bSMichael Große            echo $editor->getEditor();
6387fdbc6bSMichael Große        } else {
646af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_intro');
6587fdbc6bSMichael Große            $this->html_newschema();
6687fdbc6bSMichael Große        }
6787fdbc6bSMichael Große    }
6887fdbc6bSMichael Große
6987fdbc6bSMichael Große    /**
7087fdbc6bSMichael Große     * Form to add a new schema
7187fdbc6bSMichael Große     */
7287fdbc6bSMichael Große    protected function html_newschema() {
7387fdbc6bSMichael Große        $form = new Form();
7487fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
7587fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
76dbffe06eSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
7787fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
7887fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
7987fdbc6bSMichael Große        $form->addHTML('<p>'.$this->getLang('createhint').'</p>'); // FIXME is that true? we probably could
8087fdbc6bSMichael Große        $form->addFieldsetClose();
8187fdbc6bSMichael Große        echo $form->toHTML();
8287fdbc6bSMichael Große    }
8387fdbc6bSMichael Große
8487fdbc6bSMichael Große    /**
8587fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
8687fdbc6bSMichael Große     *
8787fdbc6bSMichael Große     * @return array
8887fdbc6bSMichael Große     */
8987fdbc6bSMichael Große    public function getTOC() {
9087fdbc6bSMichael Große        global $ID;
9187fdbc6bSMichael Große
9287fdbc6bSMichael Große        $toc = array();
9387fdbc6bSMichael Große        $link = wl($ID, array(
9487fdbc6bSMichael Große            'do' => 'admin',
95dbffe06eSAndreas Gohr            'page' => 'struct_assignments'
96dbffe06eSAndreas Gohr        ));
97dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
98dbffe06eSAndreas Gohr        $link = wl($ID, array(
99dbffe06eSAndreas Gohr            'do' => 'admin',
100dbffe06eSAndreas Gohr            'page' => 'struct_schemas'
10187fdbc6bSMichael Große        ));
10287fdbc6bSMichael Große        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
10387fdbc6bSMichael Große
104*097f4a53SAndreas Gohr        $tables = Schema::getAll();
105*097f4a53SAndreas Gohr        foreach($tables as $table) {
10687fdbc6bSMichael Große            $link = wl($ID, array(
10787fdbc6bSMichael Große                'do' => 'admin',
108dbffe06eSAndreas Gohr                'page' => 'struct_schemas',
109*097f4a53SAndreas Gohr                'table' => $table
11087fdbc6bSMichael Große            ));
11187fdbc6bSMichael Große
112*097f4a53SAndreas Gohr            $toc[] = html_mktocitem($link, hsc($table), 1, '');
11387fdbc6bSMichael Große        }
11487fdbc6bSMichael Große        return $toc;
11587fdbc6bSMichael Große    }
11687fdbc6bSMichael Große
11787fdbc6bSMichael Große}
11887fdbc6bSMichael Große
11987fdbc6bSMichael Große// vim:ts=4:sw=4:et:
120