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