xref: /plugin/approve/admin.php (revision 8320d203413cb46ad3a581fdf91120fe653c0920)
1<?php
2/**
3 * DokuWiki Plugin watchcycle (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Szymon Olewniczak <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class admin_plugin_approve extends DokuWiki_Admin_Plugin
15{
16    /**
17     * @return int sort number in admin menu
18     */
19    public function getMenuSort()
20    {
21        return 1;
22    }
23
24    protected function updatePage(helper_plugin_sqlite $sqlite, helper_plugin_approve $helper)
25    {
26        //clean current settings
27        $sqlite->query('DELETE FROM page');
28
29        $wikiPages = $helper->getPages();
30        $no_apr_namespace = $helper->no_apr_namespace($sqlite);
31        $weighted_assignments = $helper->weighted_assignments($sqlite);
32        foreach ($wikiPages as $id) {
33            if ($helper->isPageAssigned($sqlite, $id, $approver, $weighted_assignments)) {
34                $data = [
35                    'page' => $id,
36                    'hidden' => $helper->in_hidden_namespace($sqlite, $id, $no_apr_namespace) ? '1' : '0'
37                ];
38                if (!blank($approver)) {
39                    $data['approver'] = $approver;
40                }
41                $sqlite->storeEntry('page', $data);
42            }
43        }
44    }
45
46    /**
47     * Should carry out any processing required by the plugin.
48     */
49    public function handle()
50    {
51        global $ID;
52        /* @var Input */
53        global $INPUT;
54
55        try {
56            /** @var \helper_plugin_approve_db $db_helper */
57            $db_helper = plugin_load('helper', 'approve_db');
58            $sqlite = $db_helper->getDB();
59        } catch (Exception $e) {
60            msg($e->getMessage(), -1);
61            return;
62        }
63        /** @var helper_plugin_approve $helper */
64        $helper = plugin_load('helper', 'approve');
65
66        if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
67            $assignment = $INPUT->arr('assignment');
68            //insert empty string as NULL
69            if ($INPUT->str('action') === 'delete') {
70                $sqlite->query('DELETE FROM maintainer WHERE id=?', $assignment['id']);
71                $this->updatePage($sqlite, $helper);
72            } else if ($INPUT->str('action') === 'add' && !blank($assignment['assign'])) {
73                $data = [
74                    'namespace' => $assignment['assign']
75                ];
76                if (!blank($assignment['approver'])) {
77                    $data['approver'] = $assignment['approver'];
78                }
79                $sqlite->storeEntry('maintainer', $data);
80
81                $this->updatePage($sqlite, $helper);
82            }
83
84            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'approve'), true, '&'));
85        }
86    }
87
88    /**
89     * Render HTML output, e.g. helpful text and a form
90     */
91    public function html()
92    {
93        global $ID;
94        /* @var DokuWiki_Auth_Plugin $auth */
95        global $auth;
96
97        try {
98            /** @var \helper_plugin_approve_db $db_helper */
99            $db_helper = plugin_load('helper', 'approve_db');
100            $sqlite = $db_helper->getDB();
101        } catch (Exception $e) {
102            msg($e->getMessage(), -1);
103            return;
104        }
105
106        $res = $sqlite->query('SELECT * FROM maintainer ORDER BY namespace');
107        $assignments = $sqlite->res2arr($res);
108
109        echo $this->locale_xhtml('assignments_intro');
110
111        echo '<form action="' . wl($ID) . '" action="post">';
112        echo '<input type="hidden" name="do" value="admin" />';
113        echo '<input type="hidden" name="page" value="approve" />';
114        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
115        echo '<table class="inline">';
116
117        // header
118        echo '<tr>';
119        echo '<th>'.$this->getLang('admin h_assignment_namespace').'</th>';
120        echo '<th>'.$this->getLang('admin h_assignment_approver').'</th>';
121        echo '<th></th>';
122        echo '</tr>';
123
124        // existing assignments
125        foreach($assignments as $assignment) {
126            $id = $assignment['id'];
127            $namespace = $assignment['namespace'];
128            $approver = $assignment['approver'] ? $assignment['approver'] : '---';
129
130            $link = wl(
131                $ID, array(
132                    'do' => 'admin',
133                    'page' => 'approve',
134                    'action' => 'delete',
135                    'sectok' => getSecurityToken(),
136                    'assignment[id]' => $id
137                )
138            );
139
140            echo '<tr>';
141            echo '<td>' . hsc($namespace) . '</td>';
142            $user = $auth->getUserData($approver);
143            if ($user) {
144                echo '<td>' . hsc($user['name']) . '</td>';
145            } else {
146                echo '<td>' . hsc($approver) . '</td>';
147            }
148            echo '<td><a href="' . $link . '">'.$this->getLang('admin btn_delete').'</a></td>';
149            echo '</tr>';
150        }
151
152        // new assignment form
153        echo '<tr>';
154        echo '<td><input type="text" name="assignment[assign]" /></td>';
155        echo '<td>';
156        if ($auth->canDo('getUsers')) {
157            echo '<select name="assignment[approver]">';
158            echo '<option value="">---</option>';
159            foreach($auth->retrieveUsers() as $login => $data) {
160                echo '<option value="' . hsc($login) . '">' . hsc($data['name']) . '</option>';
161            }
162            echo '</select>';
163
164        } else {
165            echo '<input name="assignment[approver]">';
166        }
167        echo '</td>';
168
169        echo '<td><button type="submit" name="action" value="add">'.$this->getLang('admin btn_add').'</button></td>';
170        echo '</tr>';
171
172        echo '</table>';
173    }
174}
175
176// vim:ts=4:sw=4:et:
177