xref: /plugin/struct/admin/assignments.php (revision 153400c71e41c0c8fc5200665818073ebd21c3b6)
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 plugin\struct\meta\Assignments;
11use 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            $ok = true;
52            if(!blank($assignment['assign']) && !blank($assignment['tbl'])) {
53                if($INPUT->str('action') === 'delete') {
54                    $ok = $assignments->removePattern($assignment['assign'], $assignment['tbl']);
55                } else if($INPUT->str('action') === 'add') {
56                    $ok = $assignments->addPattern($assignment['assign'], $assignment['tbl']);
57                }
58            }
59
60            if(!$ok) {
61                msg('something went wrong while saving', -1);
62            }
63
64            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_assignments'), true, '&'));
65        }
66    }
67
68    /**
69     * Render HTML output, e.g. helpful text and a form
70     */
71    public function html() {
72        global $ID;
73
74        echo $this->locale_xhtml('assignments_intro');
75
76        $ass = new Assignments();
77        $assignments = $ass->getAllPatterns();
78
79        echo '<form action="' . wl($ID) . '" action="post">';
80        echo '<input type="hidden" name="do" value="admin" />';
81        echo '<input type="hidden" name="page" value="struct_assignments" />';
82        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
83        echo '<table class="inline">';
84
85        // header
86        echo '<tr>';
87        echo '<th>'.$this->getLang('assign_assign').'</th>';
88        echo '<th>'.$this->getLang('assign_tbl').'</th>';
89        echo '<th></th>';
90        echo '</tr>';
91
92        // existing assignments
93        foreach($assignments as $assignment) {
94            $schema = $assignment['tbl'];
95            $assignee = $assignment['pattern'];
96
97            $link = wl(
98                $ID, array(
99                'do' => 'admin',
100                'page' => 'struct_assignments',
101                'action' => 'delete',
102                'sectok' => getSecurityToken(),
103                'assignment[tbl]' => $schema,
104                'assignment[assign]' => $assignee,
105            )
106            );
107
108            echo '<tr>';
109            echo '<td>' . hsc($assignee) . '</td>';
110            echo '<td>' . hsc($schema) . '</td>';
111            echo '<td><a href="' . $link . '">'.$this->getLang('assign_del').'</a></td>';
112            echo '</tr>';
113        }
114
115        // new assignment form
116        echo '<tr>';
117        echo '<td><input type="text" name="assignment[assign]" /></td>';
118        echo '<td>';
119        echo '<select name="assignment[tbl]">';
120        foreach(Schema::getAll() as $table) {
121            echo '<option value="' . hsc($table) . '">' . hsc($table) . '</option>';
122        }
123        echo '</select>';
124        echo '</td>';
125        echo '<td><button type="submit" name="action" value="add">'.$this->getLang('assign_add').'</button></td>';
126        echo '</tr>';
127
128        echo '</table>';
129    }
130
131    /**
132     * Copies the TOC from the Schema Editor
133     *
134     * @return array
135     */
136    public function getTOC() {
137        /** @var admin_plugin_struct_schemas $plugin */
138        $plugin = plugin_load('admin', 'struct_schemas');
139        return $plugin->getTOC();
140    }
141
142}
143
144// vim:ts=4:sw=4:et:
145