xref: /plugin/struct/admin/assignments.php (revision 5f803f498403358330397b3dee32a0229ce4906a)
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\Assignments;
12use plugin\struct\meta\Schema;
13use plugin\struct\meta\SchemaEditor;
14
15if(!defined('DOKU_INC')) die();
16
17class admin_plugin_struct_assignments extends DokuWiki_Admin_Plugin {
18
19    /**
20     * @return int sort number in admin menu
21     */
22    public function getMenuSort() {
23        return 501;
24    }
25
26    /**
27     * Return the text that is displayed at the main admin menu
28     *
29     * @param string $language language code
30     * @return string menu string
31     */
32    public function getMenuText($language) {
33        return $this->getLang('menu_assignments');
34    }
35
36    /**
37     * @return bool true if only access for superuser, false is for superusers and moderators
38     */
39    public function forAdminOnly() {
40        return true;
41    }
42
43    /**
44     * Should carry out any processing required by the plugin.
45     */
46    public function handle() {
47        global $INPUT;
48
49        $assignments = new Assignments();
50        if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
51            $assignment = $INPUT->arr('assignment');
52            $ok = false;
53            if ($INPUT->str('action') === 'delete') {
54                $ok = $assignments->remove($assignment['assign'], $assignment['tbl']);
55            } else if($INPUT->str('action') === 'add') {
56                $ok = $assignments->add($assignment['assign'], $assignment['tbl']);
57            }
58            if(empty($sql) || empty($assignment['assign']) || empty($assignment['tbl']) || !$ok) {
59                msg('something went wrong while saving', -1);
60            }
61        }
62    }
63
64    /**
65     * Render HTML output, e.g. helpful text and a form
66     */
67    public function html() {
68        global $ID;
69
70        echo $this->locale_xhtml('assignments_intro');
71
72        //fixme listing schema tables should be moved to one of the meta classes
73        /** @var helper_plugin_struct_db $helper */
74        $helper = plugin_load('helper', 'struct_db');
75        $sqlite = $helper->getDB();
76        $res = $sqlite->query('SELECT tbl FROM schemas GROUP BY tbl');
77        $schemas = $sqlite->res2arr($res);
78        $sqlite->res_close($res);
79
80        $ass = new Assignments();
81        $assignments = $ass->getAll();
82
83
84
85        echo '<form action="'.wl($ID).'">';
86        echo '<input type="hidden" name="do" value="admin" />';
87        echo '<input type="hidden" name="page" value="struct_assignments" />';
88        echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />';
89        echo '<table class="inline">';
90
91        // header
92        echo '<tr>';
93        echo '<th>Page/Namespace</th>'; // FIXME localize
94        echo '<th>Schema</th>'; // FIXME localize
95        echo '<th></th>';
96        echo '</tr>';
97
98        // existing assignments
99        foreach ($assignments as $assignment) {
100            $schema = $assignment['tbl'];
101            $assignee = $assignment['assign'];
102
103            $link = wl($ID, array(
104                'do' => 'admin',
105                'page' => 'struct_assignments',
106                'action' => 'delete',
107                'sectok' => getSecurityToken(),
108                'assignment[tbl]' => $schema,
109                'assignment[assign]' => $assignee,
110            ));
111
112            echo '<tr>';
113            echo '<td>'.hsc($assignee).'</td>';
114            echo '<td>'.hsc($schema).'</td>';
115            echo '<td><a href="'.$link.'">Delete</a></td>'; //FIXME localize
116            echo '</tr>';
117        }
118
119        // new assignment form
120        echo '<tr>';
121        echo '<td><input type="text" name="assignment[assign]" /></td>';
122        echo '<td>';
123        echo '<select name="assignment[tbl]">';
124        foreach ($schemas as $schema){
125            echo '<option value="'. hsc($schema['tbl']) .'">'. hsc($schema['tbl']) . '</option>';
126        }
127        echo '</select>';
128        echo '</td>';
129        echo '<td><button type="submit" name="action" value="add">Add</button></td>'; // FIXME localize
130        echo '</tr>';
131
132        echo '</table>';
133    }
134
135    /**
136     * Copies the TOC from the Schema Editor
137     *
138     * @return array
139     */
140    public function getTOC() {
141        /** @var admin_plugin_struct_schemas $plugin */
142        $plugin = plugin_load('admin', 'struct_schemas');
143        return $plugin->getTOC();
144    }
145
146}
147
148// vim:ts=4:sw=4:et:
149