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