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    /**
25     * Should carry out any processing required by the plugin.
26     */
27    public function handle()
28    {
29        global $ID;
30        /* @var Input */
31        global $INPUT;
32
33        try {
34            /** @var \helper_plugin_approve_db $db_helper */
35            $db_helper = plugin_load('helper', 'approve_db');
36            $sqlite = $db_helper->getDB();
37        } catch (Exception $e) {
38            msg($e->getMessage(), -1);
39            return;
40        }
41        /** @var helper_plugin_approve $helper */
42        $helper = plugin_load('helper', 'approve');
43
44        if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
45            $assignment = $INPUT->arr('assignment');
46            //insert empty string as NULL
47            if ($INPUT->str('action') === 'delete') {
48                $sqlite->query('DELETE FROM maintainer WHERE id=?', $assignment['id']);
49                $helper->updatePagesAssignments($sqlite);
50            } else if ($INPUT->str('action') === 'add' && !blank($assignment['assign'])) {
51                $data = [
52                    'namespace' => $assignment['assign']
53                ];
54                if (!blank($assignment['approver'])) {
55                    $data['approver'] = $assignment['approver'];
56                } else if (!blank($assignment['approver_fb'])) {
57                    $data['approver'] = $assignment['approver_fb'];
58                }
59                $sqlite->storeEntry('maintainer', $data);
60
61                $helper->updatePagesAssignments($sqlite);
62            }
63
64            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'approve'), true, '&'));
65        }
66    }
67
68    /**
69     * Render HTML output, e.g. helpful text and a form
70     */
71    public function html()
72    {
73        global $ID;
74        /* @var DokuWiki_Auth_Plugin $auth */
75        global $auth;
76
77        try {
78            /** @var \helper_plugin_approve_db $db_helper */
79            $db_helper = plugin_load('helper', 'approve_db');
80            $sqlite = $db_helper->getDB();
81        } catch (Exception $e) {
82            msg($e->getMessage(), -1);
83            return;
84        }
85
86        $res = $sqlite->query('SELECT * FROM maintainer ORDER BY namespace');
87        $assignments = $sqlite->res2arr($res);
88
89        echo $this->locale_xhtml('assignments_intro');
90
91        echo '<form action="' . wl($ID) . '" action="post">';
92        echo '<input type="hidden" name="do" value="admin" />';
93        echo '<input type="hidden" name="page" value="approve" />';
94        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
95        echo '<table class="inline">';
96
97        // header
98        echo '<tr>';
99        echo '<th>'.$this->getLang('admin h_assignment_namespace').'</th>';
100        echo '<th>'.$this->getLang('admin h_assignment_approver').'</th>';
101        echo '<th></th>';
102        echo '</tr>';
103
104        // existing assignments
105        foreach($assignments as $assignment) {
106            $id = $assignment['id'];
107            $namespace = $assignment['namespace'];
108            $approver = $assignment['approver'] ? $assignment['approver'] : '---';
109
110            $link = wl(
111                $ID, array(
112                    'do' => 'admin',
113                    'page' => 'approve',
114                    'action' => 'delete',
115                    'sectok' => getSecurityToken(),
116                    'assignment[id]' => $id
117                )
118            );
119
120            echo '<tr>';
121            echo '<td>' . hsc($namespace) . '</td>';
122            $user = $auth->getUserData($approver);
123            if ($user) {
124                echo '<td>' . hsc($user['name']) . '</td>';
125            } else {
126                echo '<td>' . hsc($approver) . '</td>';
127            }
128            echo '<td><a href="' . $link . '">'.$this->getLang('admin btn_delete').'</a></td>';
129            echo '</tr>';
130        }
131
132        // new assignment form
133        echo '<tr>';
134        echo '<td><input type="text" name="assignment[assign]" /></td>';
135        echo '<td>';
136        if ($auth->canDo('getUsers')) {
137            echo '<select name="assignment[approver]">';
138            echo '<option value="">---</option>';
139            if ($auth->canDo('getGroups')) {
140                foreach($auth->retrieveGroups() as $group) {
141                    echo '<option value="@' . hsc($group) . '">' . '@' . hsc($group) . '</option>';
142                }
143            }
144            foreach($auth->retrieveUsers() as $login => $data) {
145                echo '<option value="' . hsc($login) . '">' . hsc($data['name']) . '</option>';
146            }
147            echo '</select>';
148            // in case your auth plugin can do groups, but not list them (like the default one),
149            // leave a text field as backup
150            if (!$auth->canDo('getGroups')) {
151                echo '<input name="assignment[approver_fb]" id="plugin__approve_group_input">';
152            }
153        } else {
154            echo '<input name="assignment[approver]">';
155        }
156        echo '</td>';
157
158        echo '<td><button type="submit" name="action" value="add">'.$this->getLang('admin btn_add').'</button></td>';
159        echo '</tr>';
160
161        echo '</table>';
162    }
163}
164
165// vim:ts=4:sw=4:et:
166