1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\Extension\Event;
5
6class admin_plugin_approve extends AdminPlugin
7{
8    /**
9     * @return int sort number in admin menu
10     */
11    public function getMenuSort()
12    {
13        return 1;
14    }
15
16    /**
17     * Should carry out any processing required by the plugin.
18     */
19    public function handle()
20    {
21        global $ID;
22        /* @var Input */
23        global $INPUT;
24
25        /** @var helper_plugin_approve_db $db */
26        $db = $this->loadHelper('approve_db');
27
28        if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
29            $assignment = $INPUT->arr('assignment');
30            //insert empty string as NULL
31            if ($INPUT->str('action') === 'delete') {
32                $db->deleteMaintainer((int) $assignment['id']);
33                $db->updatePagesAssignments();
34            } else if ($INPUT->str('action') === 'add' && !blank($assignment['assign'])) {
35                $approver = '';
36                if (!blank($assignment['approver'])) {
37                    $approver = $assignment['approver'];
38                } elseif (!blank($assignment['approver_fb'])) {
39                    $approver = $assignment['approver_fb'];
40                }
41                $db->addMaintainer($assignment['assign'], $approver);
42                $db->updatePagesAssignments();
43            }
44
45            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'approve'), true, '&'));
46        }
47    }
48
49    /**
50     * Render HTML output, e.g. helpful text and a form
51     */
52    public function html()
53    {
54        global $ID;
55        /* @var DokuWiki_Auth_Plugin $auth */
56        global $auth;
57
58        echo $this->locale_xhtml('assignments_intro');
59
60        echo '<form action="' . wl($ID) . '" action="post">';
61        echo '<input type="hidden" name="do" value="admin" />';
62        echo '<input type="hidden" name="page" value="approve" />';
63        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
64        echo '<table class="inline">';
65
66        // header
67        echo '<tr>';
68        echo '<th>'.$this->getLang('admin h_assignment_namespace').'</th>';
69        echo '<th>'.$this->getLang('admin h_assignment_approver').'</th>';
70        echo '<th></th>';
71        echo '</tr>';
72
73        /** @var helper_plugin_approve_db $db */
74        $db = $this->loadHelper('approve_db');
75        $assignments = $db->getMaintainers();
76
77        foreach($assignments as $assignment) {
78            $id = $assignment['id'];
79            $namespace = $assignment['namespace'];
80            $approver = $assignment['approver'] ?: '---';
81
82            $link = wl(
83                $ID, array(
84                    'do' => 'admin',
85                    'page' => 'approve',
86                    'action' => 'delete',
87                    'sectok' => getSecurityToken(),
88                    'assignment[id]' => $id
89                )
90            );
91
92            echo '<tr>';
93            echo '<td>' . hsc($namespace) . '</td>';
94            $user = $auth->getUserData($approver);
95            if ($user) {
96                echo '<td>' . hsc($user['name']) . '</td>';
97            } else {
98                echo '<td>' . hsc($approver) . '</td>';
99            }
100            echo '<td><a href="' . $link . '">'.$this->getLang('admin btn_delete').'</a></td>';
101            echo '</tr>';
102        }
103
104        // new assignment form
105        echo '<tr>';
106        echo '<td><input type="text" name="assignment[assign]" /></td>';
107        echo '<td>';
108        if ($auth->canDo('getUsers')) {
109            echo '<select name="assignment[approver]">';
110            echo '<option value="">---</option>';
111            if ($auth->canDo('getGroups')) {
112                foreach($auth->retrieveGroups() as $group) {
113                    echo '<option value="@' . hsc($group) . '">' . '@' . hsc($group) . '</option>';
114                }
115            }
116            foreach($auth->retrieveUsers() as $login => $data) {
117                echo '<option value="' . hsc($login) . '">' . hsc($data['name']) . '</option>';
118            }
119            echo '</select>';
120            // in case your auth plugin can do groups, but not list them (like the default one),
121            // leave a text field as backup
122            if (!$auth->canDo('getGroups')) {
123                echo '<input name="assignment[approver_fb]" id="plugin__approve_group_input">';
124            }
125        } else {
126            echo '<input name="assignment[approver]">';
127        }
128        echo '</td>';
129
130        echo '<td><button type="submit" name="action" value="add">'.$this->getLang('admin btn_add').'</button></td>';
131        echo '</tr>';
132
133        echo '</table>';
134    }
135}
136
137// vim:ts=4:sw=4:et:
138