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