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