xref: /plugin/struct/admin/assignments.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_assignments extends DokuWiki_Admin_Plugin {
17*87fdbc6bSMichael Große
18*87fdbc6bSMichael Große    /** @var helper_plugin_sqlite */
19*87fdbc6bSMichael Große    protected $sqlite;
20*87fdbc6bSMichael Große
21*87fdbc6bSMichael Große    /**
22*87fdbc6bSMichael Große     * @return int sort number in admin menu
23*87fdbc6bSMichael Große     */
24*87fdbc6bSMichael Große    public function getMenuSort() {
25*87fdbc6bSMichael Große        return 501;
26*87fdbc6bSMichael Große    }
27*87fdbc6bSMichael Große
28*87fdbc6bSMichael Große    public function getMenuText() {
29*87fdbc6bSMichael Große        return 'struct plugin schema page assignments';
30*87fdbc6bSMichael Große    }
31*87fdbc6bSMichael Große
32*87fdbc6bSMichael Große    /**
33*87fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
34*87fdbc6bSMichael Große     */
35*87fdbc6bSMichael Große    public function forAdminOnly() {
36*87fdbc6bSMichael Große        return true;
37*87fdbc6bSMichael Große    }
38*87fdbc6bSMichael Große
39*87fdbc6bSMichael Große    /**
40*87fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
41*87fdbc6bSMichael Große     */
42*87fdbc6bSMichael Große    public function handle() {
43*87fdbc6bSMichael Große        global $INPUT;
44*87fdbc6bSMichael Große
45*87fdbc6bSMichael Große        /** @var \helper_plugin_struct_db $helper */
46*87fdbc6bSMichael Große        $helper = plugin_load('helper', 'struct_db');
47*87fdbc6bSMichael Große        $this->sqlite = $helper->getDB();
48*87fdbc6bSMichael Große
49*87fdbc6bSMichael Große        if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
50*87fdbc6bSMichael Große            $assignment = $INPUT->arr('assignment');
51*87fdbc6bSMichael Große            if ($INPUT->str('action') === 'delete') {
52*87fdbc6bSMichael Große                $sql = 'DELETE FROM schema_assignments WHERE assign = ? AND tbl = ?';
53*87fdbc6bSMichael Große            }
54*87fdbc6bSMichael Große            if ($INPUT->str('action') === 'add') {
55*87fdbc6bSMichael Große                $sql = 'REPLACE INTO schema_assignments (assign, tbl) VALUES (?,?)';
56*87fdbc6bSMichael Große            }
57*87fdbc6bSMichael Große            $opts = array($assignment['assign'], $assignment['tbl']);
58*87fdbc6bSMichael Große            if(empty($sql) || empty($assignment['assign']) || empty($assignment['tbl']) || !$this->sqlite->query($sql, $opts)) {
59*87fdbc6bSMichael Große                msg('something went wrong while saving', -1);
60*87fdbc6bSMichael Große            }
61*87fdbc6bSMichael Große        }
62*87fdbc6bSMichael Große
63*87fdbc6bSMichael Große    }
64*87fdbc6bSMichael Große
65*87fdbc6bSMichael Große    /**
66*87fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
67*87fdbc6bSMichael Große     */
68*87fdbc6bSMichael Große    public function html() {
69*87fdbc6bSMichael Große        global $INPUT;
70*87fdbc6bSMichael Große
71*87fdbc6bSMichael Große        echo $this->locale_xhtml('assignments_intro');
72*87fdbc6bSMichael Große
73*87fdbc6bSMichael Große        $res = $this->sqlite->query('SELECT tbl FROM schemas GROUP BY tbl');
74*87fdbc6bSMichael Große        $schemas = $this->sqlite->res2arr($res);
75*87fdbc6bSMichael Große        $this->sqlite->res_close($res);
76*87fdbc6bSMichael Große
77*87fdbc6bSMichael Große        $res = $this->sqlite->query('SELECT * FROM schema_assignments ORDER BY assign');
78*87fdbc6bSMichael Große        $assignments = $this->sqlite->res2arr($res);
79*87fdbc6bSMichael Große        $this->sqlite->res_close($res);
80*87fdbc6bSMichael Große
81*87fdbc6bSMichael Große        echo '<ul>';
82*87fdbc6bSMichael Große        foreach ($assignments as $assignment) {
83*87fdbc6bSMichael Große            $schema = $assignment['tbl'];
84*87fdbc6bSMichael Große            $assignee = $assignment['assign'];
85*87fdbc6bSMichael Große            $form = new Form();
86*87fdbc6bSMichael Große            $form->setHiddenField("assignment[assign]", $assignee);
87*87fdbc6bSMichael Große            $form->setHiddenField("assignment[tbl]", $schema);
88*87fdbc6bSMichael Große            $form->addHTML("<button type=\"submit\" name=\"action\" value=\"delete\">Delete</button>");
89*87fdbc6bSMichael Große            $html = "<li class=\"level1\"><div class=\"li\">$assignee - $schema ";
90*87fdbc6bSMichael Große            $html .= $form->toHTML();
91*87fdbc6bSMichael Große            $html .= "</div></li>";
92*87fdbc6bSMichael Große            echo $html;
93*87fdbc6bSMichael Große        }
94*87fdbc6bSMichael Große        $form = new Form();
95*87fdbc6bSMichael Große        $form->addTextInput("assignment[assign]",'Page or Namespace: ');
96*87fdbc6bSMichael Große        $form->addLabel('Schema','schemaSelect');
97*87fdbc6bSMichael Große        $form->addHTML('<select id="schemaSelect" name="assignment[tbl]">');
98*87fdbc6bSMichael Große        foreach ($schemas as $schema){
99*87fdbc6bSMichael Große            $form->addHTML('<option value="'. $schema['tbl'] .'">'. $schema['tbl'] . '</option>');
100*87fdbc6bSMichael Große        }
101*87fdbc6bSMichael Große        $form->addHTML('</select>');
102*87fdbc6bSMichael Große        $form->addHTML("<button type=\"submit\" name=\"action\" value=\"add\">Add</button>");
103*87fdbc6bSMichael Große        $html = "<li class=\"level1\"><div class=\"li\">";
104*87fdbc6bSMichael Große        $html .= $form->toHTML();
105*87fdbc6bSMichael Große        $html .= "</div></li>";
106*87fdbc6bSMichael Große        echo $html;
107*87fdbc6bSMichael Große        echo '</ul>';
108*87fdbc6bSMichael Große
109*87fdbc6bSMichael Große
110*87fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
111*87fdbc6bSMichael Große        if($table) {
112*87fdbc6bSMichael Große            echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>';
113*87fdbc6bSMichael Große
114*87fdbc6bSMichael Große            $editor = new SchemaEditor(new Schema($table));
115*87fdbc6bSMichael Große            echo $editor->getEditor();
116*87fdbc6bSMichael Große        } else {
117*87fdbc6bSMichael Große            $this->html_newschema();
118*87fdbc6bSMichael Große        }
119*87fdbc6bSMichael Große    }
120*87fdbc6bSMichael Große
121*87fdbc6bSMichael Große}
122*87fdbc6bSMichael Große
123*87fdbc6bSMichael Große// vim:ts=4:sw=4:et:
124