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