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