xref: /plugin/struct/admin/assignments.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
1<?php
2
3/**
4 * DokuWiki Plugin struct (Admin Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10// must be run within Dokuwiki
11use dokuwiki\Extension\AdminPlugin;
12use dokuwiki\plugin\struct\meta\Assignments;
13use dokuwiki\plugin\struct\meta\Schema;
14use dokuwiki\plugin\struct\meta\StructException;
15
16class admin_plugin_struct_assignments extends AdminPlugin
17{
18    /**
19     * @return int sort number in admin menu
20     */
21    public function getMenuSort()
22    {
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    {
34        return $this->getLang('menu_assignments');
35    }
36
37    /**
38     * @return bool true if only access for superuser, false is for superusers and moderators
39     */
40    public function forAdminOnly()
41    {
42        return false;
43    }
44
45    /**
46     * Should carry out any processing required by the plugin.
47     */
48    public function handle()
49    {
50        global $INPUT;
51        global $ID;
52
53        try {
54            $assignments = Assignments::getInstance();
55        } catch (StructException $e) {
56            msg($e->getMessage(), -1);
57            return false;
58        }
59
60        if ($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
61            $assignment = $INPUT->arr('assignment');
62            if (!blank($assignment['assign']) && !blank($assignment['tbl'])) {
63                if ($INPUT->str('action') === 'delete') {
64                    $ok = $assignments->removePattern($assignment['assign'], $assignment['tbl']);
65                    if (!$ok) msg('failed to remove pattern', -1);
66                } elseif ($INPUT->str('action') === 'add') {
67                    if ($assignment['assign'][0] == '/') {
68                        if (@preg_match($assignment['assign'], null) === false) {
69                            msg('Invalid regular expression. Pattern not saved', -1);
70                        } else {
71                            $ok = $assignments->addPattern($assignment['assign'], $assignment['tbl']);
72                            if (!$ok) msg('failed to add pattern', -1);
73                        }
74                    } else {
75                        $ok = $assignments->addPattern($assignment['assign'], $assignment['tbl']);
76                        if (!$ok) msg('failed to add pattern', -1);
77                    }
78                }
79            }
80
81
82            send_redirect(wl($ID, ['do' => 'admin', 'page' => 'struct_assignments'], true, '&'));
83        }
84    }
85
86    /**
87     * Render HTML output, e.g. helpful text and a form
88     */
89    public function html()
90    {
91        global $ID;
92
93        echo $this->locale_xhtml('assignments_intro');
94
95        try {
96            $ass = Assignments::getInstance();
97        } catch (StructException $e) {
98            msg($e->getMessage(), -1);
99            return false;
100        }
101        $assignments = $ass->getAllPatterns();
102
103        echo '<form action="' . wl($ID) . '" action="post">';
104        echo '<input type="hidden" name="do" value="admin" />';
105        echo '<input type="hidden" name="page" value="struct_assignments" />';
106        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
107        echo '<table class="inline">';
108
109        // header
110        echo '<tr>';
111        echo '<th>' . $this->getLang('assign_assign') . '</th>';
112        echo '<th>' . $this->getLang('assign_tbl') . '</th>';
113        echo '<th></th>';
114        echo '</tr>';
115
116        // existing assignments
117        foreach ($assignments as $assignment) {
118            $schema = $assignment['tbl'];
119            $assignee = $assignment['pattern'];
120
121            $link = wl(
122                $ID,
123                ['do' => 'admin', 'page' => 'struct_assignments', 'action' => 'delete', 'sectok' => getSecurityToken(), 'assignment[tbl]' => $schema, 'assignment[assign]' => $assignee]
124            );
125
126            echo '<tr>';
127            echo '<td>' . hsc($assignee) . '</td>';
128            echo '<td>' . hsc($schema) . '</td>';
129            echo '<td><a class="deleteSchema" href="' . $link . '">' . $this->getLang('assign_del') . '</a></td>';
130            echo '</tr>';
131        }
132
133        // new assignment form
134        echo '<tr>';
135        echo '<td><input type="text" name="assignment[assign]" /></td>';
136        echo '<td>';
137        echo '<select name="assignment[tbl]">';
138        $schemas = helper_plugin_struct::getSchema();
139        foreach ($schemas as $schema) {
140            if ($schema->isInternal()) continue;
141            $table = $schema->getTable();
142            echo '<option value="' . hsc($table) . '">' . hsc($table) . '</option>';
143        }
144        echo '</select>';
145        echo '</td>';
146        echo '<td><button type="submit" name="action" value="add">' . $this->getLang('assign_add') . '</button></td>';
147        echo '</tr>';
148
149        echo '</table>';
150        echo '</form>';
151    }
152
153    /**
154     * Copies the TOC from the Schema Editor
155     *
156     * @return array
157     */
158    public function getTOC()
159    {
160        /** @var admin_plugin_struct_schemas $plugin */
161        $plugin = plugin_load('admin', 'struct_schemas');
162        return $plugin->getTOC();
163    }
164}
165
166// vim:ts=4:sw=4:et:
167