xref: /plugin/struct/admin/schemas.php (revision dbffe06e4b7c04e2a050a4457f623f3d34cafbf1)
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
5487fdbc6bSMichael Große        echo $this->locale_xhtml('intro');
5587fdbc6bSMichael Große
5687fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
5787fdbc6bSMichael Große        if($table) {
5887fdbc6bSMichael Große            echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>';
5987fdbc6bSMichael Große
6087fdbc6bSMichael Große            $editor = new SchemaEditor(new Schema($table));
6187fdbc6bSMichael Große            echo $editor->getEditor();
6287fdbc6bSMichael Große        } else {
6387fdbc6bSMichael Große            $this->html_newschema();
6487fdbc6bSMichael Große        }
6587fdbc6bSMichael Große    }
6687fdbc6bSMichael Große
6787fdbc6bSMichael Große    /**
6887fdbc6bSMichael Große     * Form to add a new schema
6987fdbc6bSMichael Große     */
7087fdbc6bSMichael Große    protected function html_newschema() {
7187fdbc6bSMichael Große        $form = new Form();
7287fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
7387fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
74*dbffe06eSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
7587fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
7687fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
7787fdbc6bSMichael Große        $form->addHTML('<p>'.$this->getLang('createhint').'</p>'); // FIXME is that true? we probably could
7887fdbc6bSMichael Große        $form->addFieldsetClose();
7987fdbc6bSMichael Große        echo $form->toHTML();
8087fdbc6bSMichael Große    }
8187fdbc6bSMichael Große
8287fdbc6bSMichael Große    /**
8387fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
8487fdbc6bSMichael Große     *
8587fdbc6bSMichael Große     * @return array
8687fdbc6bSMichael Große     */
8787fdbc6bSMichael Große    public function getTOC() {
8887fdbc6bSMichael Große        global $ID;
8987fdbc6bSMichael Große
9087fdbc6bSMichael Große        /** @var helper_plugin_struct_db $helper */
9187fdbc6bSMichael Große        $helper = plugin_load('helper', 'struct_db');
9287fdbc6bSMichael Große        $db = $helper->getDB();
9387fdbc6bSMichael Große        if(!$db) return parent::getTOC();
9487fdbc6bSMichael Große
95*dbffe06eSAndreas Gohr
9687fdbc6bSMichael Große        $res = $db->query("SELECT DISTINCT tbl FROM schemas ORDER BY tbl");
9787fdbc6bSMichael Große        $tables = $db->res2arr($res);
9887fdbc6bSMichael Große        $db->res_close($res);
9987fdbc6bSMichael Große
10087fdbc6bSMichael Große        $toc = array();
10187fdbc6bSMichael Große        $link = wl($ID, array(
10287fdbc6bSMichael Große            'do' => 'admin',
103*dbffe06eSAndreas Gohr            'page' => 'struct_assignments'
104*dbffe06eSAndreas Gohr        ));
105*dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
106*dbffe06eSAndreas Gohr        $link = wl($ID, array(
107*dbffe06eSAndreas Gohr            'do' => 'admin',
108*dbffe06eSAndreas Gohr            'page' => 'struct_schemas'
10987fdbc6bSMichael Große        ));
11087fdbc6bSMichael Große        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
11187fdbc6bSMichael Große
11287fdbc6bSMichael Große        foreach($tables as $row) {
11387fdbc6bSMichael Große            $link = wl($ID, array(
11487fdbc6bSMichael Große                'do' => 'admin',
115*dbffe06eSAndreas Gohr                'page' => 'struct_schemas',
11687fdbc6bSMichael Große                'table' => $row['tbl']
11787fdbc6bSMichael Große            ));
11887fdbc6bSMichael Große
11987fdbc6bSMichael Große            $toc[] = html_mktocitem($link, hsc($row['tbl']), 1, '');
12087fdbc6bSMichael Große        }
12187fdbc6bSMichael Große        return $toc;
12287fdbc6bSMichael Große    }
12387fdbc6bSMichael Große
12487fdbc6bSMichael Große}
12587fdbc6bSMichael Große
12687fdbc6bSMichael Große// vim:ts=4:sw=4:et:
127