xref: /plugin/approve/admin.php (revision 329c5e3a9200fcfb318ccf6094221a0c6a817f34)
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                } else if (!blank($assignment['approver_fb'])) {
79                    $data['approver'] = $assignment['approver_fb'];
80                }
81                $sqlite->storeEntry('maintainer', $data);
82
83                $this->updatePage($sqlite, $helper);
84            }
85
86            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'approve'), true, '&'));
87        }
88    }
89
90    /**
91     * Render HTML output, e.g. helpful text and a form
92     */
93    public function html()
94    {
95        global $ID;
96        /* @var DokuWiki_Auth_Plugin $auth */
97        global $auth;
98
99        try {
100            /** @var \helper_plugin_approve_db $db_helper */
101            $db_helper = plugin_load('helper', 'approve_db');
102            $sqlite = $db_helper->getDB();
103        } catch (Exception $e) {
104            msg($e->getMessage(), -1);
105            return;
106        }
107
108        $res = $sqlite->query('SELECT * FROM maintainer ORDER BY namespace');
109        $assignments = $sqlite->res2arr($res);
110
111        echo $this->locale_xhtml('assignments_intro');
112
113        echo '<form action="' . wl($ID) . '" action="post">';
114        echo '<input type="hidden" name="do" value="admin" />';
115        echo '<input type="hidden" name="page" value="approve" />';
116        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
117        echo '<table class="inline">';
118
119        // header
120        echo '<tr>';
121        echo '<th>'.$this->getLang('admin h_assignment_namespace').'</th>';
122        echo '<th>'.$this->getLang('admin h_assignment_approver').'</th>';
123        echo '<th></th>';
124        echo '</tr>';
125
126        // existing assignments
127        foreach($assignments as $assignment) {
128            $id = $assignment['id'];
129            $namespace = $assignment['namespace'];
130            $approver = $assignment['approver'] ? $assignment['approver'] : '---';
131
132            $link = wl(
133                $ID, array(
134                    'do' => 'admin',
135                    'page' => 'approve',
136                    'action' => 'delete',
137                    'sectok' => getSecurityToken(),
138                    'assignment[id]' => $id
139                )
140            );
141
142            echo '<tr>';
143            echo '<td>' . hsc($namespace) . '</td>';
144            $user = $auth->getUserData($approver);
145            if ($user) {
146                echo '<td>' . hsc($user['name']) . '</td>';
147            } else {
148                echo '<td>' . hsc($approver) . '</td>';
149            }
150            echo '<td><a href="' . $link . '">'.$this->getLang('admin btn_delete').'</a></td>';
151            echo '</tr>';
152        }
153
154        // new assignment form
155        echo '<tr>';
156        echo '<td><input type="text" name="assignment[assign]" /></td>';
157        echo '<td>';
158        if ($auth->canDo('getUsers')) {
159            echo '<select name="assignment[approver]">';
160            echo '<option value="">---</option>';
161            if ($auth->canDo('getGroups')) {
162                foreach($auth->retrieveGroups() as $group) {
163                    echo '<option value="@' . hsc($group) . '">' . '@' . hsc($group) . '</option>';
164                }
165            }
166            foreach($auth->retrieveUsers() as $login => $data) {
167                echo '<option value="' . hsc($login) . '">' . hsc($data['name']) . '</option>';
168            }
169            echo '</select>';
170            // in case your auth plugin can do groups, but not list them (like the default one),
171            // leave a text field as backup
172            if (!$auth->canDo('getGroups')) {
173                echo '<input name="assignment[approver_fb]" id="plugin__approve_group_input">';
174            }
175        } else {
176            echo '<input name="assignment[approver]">';
177        }
178        echo '</td>';
179
180        echo '<td><button type="submit" name="action" value="add">'.$this->getLang('admin btn_add').'</button></td>';
181        echo '</tr>';
182
183        echo '</table>';
184    }
185}
186
187// vim:ts=4:sw=4:et:
188