xref: /plugin/struct/admin/schemas.php (revision 6af24d3eff33249280549e60e18474e2cc0bf9d0)
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() {
2987fdbc6bSMichael Große        return true;
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
54*6af24d3eSAndreas Gohr
5587fdbc6bSMichael Große
5687fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
5787fdbc6bSMichael Große        if($table) {
58*6af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_edit');
5987fdbc6bSMichael Große
60*6af24d3eSAndreas 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 {
64*6af24d3eSAndreas 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        /** @var helper_plugin_struct_db $helper */
9387fdbc6bSMichael Große        $helper = plugin_load('helper', 'struct_db');
9487fdbc6bSMichael Große        $db = $helper->getDB();
9587fdbc6bSMichael Große        if(!$db) return parent::getTOC();
9687fdbc6bSMichael Große
97dbffe06eSAndreas Gohr
9887fdbc6bSMichael Große        $res = $db->query("SELECT DISTINCT tbl FROM schemas ORDER BY tbl");
9987fdbc6bSMichael Große        $tables = $db->res2arr($res);
10087fdbc6bSMichael Große        $db->res_close($res);
10187fdbc6bSMichael Große
10287fdbc6bSMichael Große        $toc = array();
10387fdbc6bSMichael Große        $link = wl($ID, array(
10487fdbc6bSMichael Große            'do' => 'admin',
105dbffe06eSAndreas Gohr            'page' => 'struct_assignments'
106dbffe06eSAndreas Gohr        ));
107dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
108dbffe06eSAndreas Gohr        $link = wl($ID, array(
109dbffe06eSAndreas Gohr            'do' => 'admin',
110dbffe06eSAndreas Gohr            'page' => 'struct_schemas'
11187fdbc6bSMichael Große        ));
11287fdbc6bSMichael Große        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
11387fdbc6bSMichael Große
11487fdbc6bSMichael Große        foreach($tables as $row) {
11587fdbc6bSMichael Große            $link = wl($ID, array(
11687fdbc6bSMichael Große                'do' => 'admin',
117dbffe06eSAndreas Gohr                'page' => 'struct_schemas',
11887fdbc6bSMichael Große                'table' => $row['tbl']
11987fdbc6bSMichael Große            ));
12087fdbc6bSMichael Große
12187fdbc6bSMichael Große            $toc[] = html_mktocitem($link, hsc($row['tbl']), 1, '');
12287fdbc6bSMichael Große        }
12387fdbc6bSMichael Große        return $toc;
12487fdbc6bSMichael Große    }
12587fdbc6bSMichael Große
12687fdbc6bSMichael Große}
12787fdbc6bSMichael Große
12887fdbc6bSMichael Große// vim:ts=4:sw=4:et:
129