xref: /plugin/struct/admin/schemas.php (revision 87fdbc6b82d3e7a1a263ca77b8909a1a130dbf1c)
1*87fdbc6bSMichael Große<?php
2*87fdbc6bSMichael Große/**
3*87fdbc6bSMichael Große * DokuWiki Plugin struct (Admin Component)
4*87fdbc6bSMichael Große *
5*87fdbc6bSMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*87fdbc6bSMichael Große * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7*87fdbc6bSMichael Große */
8*87fdbc6bSMichael Große
9*87fdbc6bSMichael Große// must be run within Dokuwiki
10*87fdbc6bSMichael Großeuse dokuwiki\Form\Form;
11*87fdbc6bSMichael Großeuse plugin\struct\meta\Schema;
12*87fdbc6bSMichael Großeuse plugin\struct\meta\SchemaEditor;
13*87fdbc6bSMichael Große
14*87fdbc6bSMichael Großeif(!defined('DOKU_INC')) die();
15*87fdbc6bSMichael Große
16*87fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
17*87fdbc6bSMichael Große
18*87fdbc6bSMichael Große    /**
19*87fdbc6bSMichael Große     * @return int sort number in admin menu
20*87fdbc6bSMichael Große     */
21*87fdbc6bSMichael Große    public function getMenuSort() {
22*87fdbc6bSMichael Große        return 500;
23*87fdbc6bSMichael Große    }
24*87fdbc6bSMichael Große
25*87fdbc6bSMichael Große    /**
26*87fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
27*87fdbc6bSMichael Große     */
28*87fdbc6bSMichael Große    public function forAdminOnly() {
29*87fdbc6bSMichael Große        return true;
30*87fdbc6bSMichael Große    }
31*87fdbc6bSMichael Große
32*87fdbc6bSMichael Große    /**
33*87fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
34*87fdbc6bSMichael Große     */
35*87fdbc6bSMichael Große    public function handle() {
36*87fdbc6bSMichael Große        global $INPUT;
37*87fdbc6bSMichael Große
38*87fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
39*87fdbc6bSMichael Große        if($table && $INPUT->bool('save') && checkSecurityToken()) {
40*87fdbc6bSMichael Große            $builder = new \plugin\struct\meta\SchemaBuilder($table, $INPUT->arr('schema'));
41*87fdbc6bSMichael Große            if(!$builder->build()) {
42*87fdbc6bSMichael Große                msg('something went wrong while saving', -1);
43*87fdbc6bSMichael Große            }
44*87fdbc6bSMichael Große        }
45*87fdbc6bSMichael Große
46*87fdbc6bSMichael Große    }
47*87fdbc6bSMichael Große
48*87fdbc6bSMichael Große    /**
49*87fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
50*87fdbc6bSMichael Große     */
51*87fdbc6bSMichael Große    public function html() {
52*87fdbc6bSMichael Große        global $INPUT;
53*87fdbc6bSMichael Große
54*87fdbc6bSMichael Große        echo $this->locale_xhtml('intro');
55*87fdbc6bSMichael Große
56*87fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
57*87fdbc6bSMichael Große        if($table) {
58*87fdbc6bSMichael Große            echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>';
59*87fdbc6bSMichael Große
60*87fdbc6bSMichael Große            $editor = new SchemaEditor(new Schema($table));
61*87fdbc6bSMichael Große            echo $editor->getEditor();
62*87fdbc6bSMichael Große        } else {
63*87fdbc6bSMichael Große            $this->html_newschema();
64*87fdbc6bSMichael Große        }
65*87fdbc6bSMichael Große    }
66*87fdbc6bSMichael Große
67*87fdbc6bSMichael Große    /**
68*87fdbc6bSMichael Große     * Form to add a new schema
69*87fdbc6bSMichael Große     */
70*87fdbc6bSMichael Große    protected function html_newschema() {
71*87fdbc6bSMichael Große        $form = new Form();
72*87fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
73*87fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
74*87fdbc6bSMichael Große        $form->setHiddenField('page', 'struct');
75*87fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
76*87fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
77*87fdbc6bSMichael Große        $form->addHTML('<p>'.$this->getLang('createhint').'</p>'); // FIXME is that true? we probably could
78*87fdbc6bSMichael Große        $form->addFieldsetClose();
79*87fdbc6bSMichael Große        echo $form->toHTML();
80*87fdbc6bSMichael Große    }
81*87fdbc6bSMichael Große
82*87fdbc6bSMichael Große    /**
83*87fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
84*87fdbc6bSMichael Große     *
85*87fdbc6bSMichael Große     * @return array
86*87fdbc6bSMichael Große     */
87*87fdbc6bSMichael Große    public function getTOC() {
88*87fdbc6bSMichael Große        global $ID;
89*87fdbc6bSMichael Große
90*87fdbc6bSMichael Große        /** @var helper_plugin_struct_db $helper */
91*87fdbc6bSMichael Große        $helper = plugin_load('helper', 'struct_db');
92*87fdbc6bSMichael Große        $db = $helper->getDB();
93*87fdbc6bSMichael Große
94*87fdbc6bSMichael Große        parent::getTOC();
95*87fdbc6bSMichael Große        if(!$db) return parent::getTOC();
96*87fdbc6bSMichael Große
97*87fdbc6bSMichael Große        $res = $db->query("SELECT DISTINCT tbl FROM schemas ORDER BY tbl");
98*87fdbc6bSMichael Große        $tables = $db->res2arr($res);
99*87fdbc6bSMichael Große        $db->res_close($res);
100*87fdbc6bSMichael Große
101*87fdbc6bSMichael Große        $toc = array();
102*87fdbc6bSMichael Große        $link = wl($ID, array(
103*87fdbc6bSMichael Große            'do' => 'admin',
104*87fdbc6bSMichael Große            'page' => 'struct'
105*87fdbc6bSMichael Große        ));
106*87fdbc6bSMichael Große        $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, '');
107*87fdbc6bSMichael Große
108*87fdbc6bSMichael Große        foreach($tables as $row) {
109*87fdbc6bSMichael Große            $link = wl($ID, array(
110*87fdbc6bSMichael Große                'do' => 'admin',
111*87fdbc6bSMichael Große                'page' => 'struct',
112*87fdbc6bSMichael Große                'table' => $row['tbl']
113*87fdbc6bSMichael Große            ));
114*87fdbc6bSMichael Große
115*87fdbc6bSMichael Große            $toc[] = html_mktocitem($link, hsc($row['tbl']), 1, '');
116*87fdbc6bSMichael Große        }
117*87fdbc6bSMichael Große        return $toc;
118*87fdbc6bSMichael Große    }
119*87fdbc6bSMichael Große
120*87fdbc6bSMichael Große}
121*87fdbc6bSMichael Große
122*87fdbc6bSMichael Große// vim:ts=4:sw=4:et:
123