xref: /plugin/approve/admin.php (revision 91f47af056bbfa2a51e81e40a373071c36edfd48)
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 getPages() {
25        global $conf;
26        $datadir = $conf['datadir'];
27        if (substr($datadir, -1) != '/') {
28            $datadir .= '/';
29        }
30
31        $directory = new RecursiveDirectoryIterator($datadir, FilesystemIterator::SKIP_DOTS);
32        $iterator = new RecursiveIteratorIterator($directory);
33
34        $pages = [];
35        /** @var SplFileInfo $fileinfo */
36        foreach ($iterator as $fileinfo) {
37            if (!$fileinfo->isFile()) continue;
38
39            $path = $fileinfo->getPathname();
40            //remove .txt
41            $id = str_replace('/', ':', substr($path, strlen($datadir), -4));
42            $pages[] = $id;
43        }
44
45        return $pages;
46    }
47
48    protected function updatePage(helper_plugin_sqlite $sqlite, helper_plugin_approve $helper)
49    {
50        //clean current settings
51        $sqlite->query('DELETE FROM page');
52
53        $wikiPages = $this->getPages();
54        $no_apr_namespace = $helper->no_apr_namespace($sqlite);
55        $weighted_assignments = $helper->weighted_assignments($sqlite);
56        foreach ($wikiPages as $id) {
57            if ($helper->isPageAssigned($sqlite, $id, $approver, $weighted_assignments)) {
58                $data = [
59                    'page' => $id,
60                    'hidden' => $helper->in_hidden_namespace($sqlite, $id, $no_apr_namespace) ? '1' : '0'
61                ];
62                if (!blank($approver)) {
63                    $data['approver'] = $approver;
64                }
65                $sqlite->storeEntry('page', $data);
66            }
67        }
68    }
69
70    /**
71     * Should carry out any processing required by the plugin.
72     */
73    public function handle()
74    {
75        global $ID;
76        /* @var Input */
77        global $INPUT;
78
79        try {
80            /** @var \helper_plugin_approve_db $db_helper */
81            $db_helper = plugin_load('helper', 'approve_db');
82            $sqlite = $db_helper->getDB();
83        } catch (Exception $e) {
84            msg($e->getMessage(), -1);
85            return;
86        }
87        /** @var helper_plugin_approve $helper */
88        $helper = plugin_load('helper', 'approve');
89
90        if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
91            $assignment = $INPUT->arr('assignment');
92            //insert empty string as NULL
93            if ($INPUT->str('action') === 'delete') {
94                $sqlite->query('DELETE FROM maintainer WHERE id=?', $assignment['id']);
95                $this->updatePage($sqlite, $helper);
96            } else if ($INPUT->str('action') === 'add' && !blank($assignment['assign'])) {
97                $data = [
98                    'namespace' => $assignment['assign']
99                ];
100                if (!blank($assignment['approver'])) {
101                    $data['approver'] = $assignment['approver'];
102                } else if (!blank($assignment['approver_fb'])) {
103                    $data['approver'] = $assignment['approver_fb'];
104                }
105                $sqlite->storeEntry('maintainer', $data);
106
107                $this->updatePage($sqlite, $helper);
108            }
109
110            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'approve'), true, '&'));
111        }
112    }
113
114    /**
115     * Render HTML output, e.g. helpful text and a form
116     */
117    public function html()
118    {
119        global $ID;
120        /* @var DokuWiki_Auth_Plugin $auth */
121        global $auth;
122
123        try {
124            /** @var \helper_plugin_approve_db $db_helper */
125            $db_helper = plugin_load('helper', 'approve_db');
126            $sqlite = $db_helper->getDB();
127        } catch (Exception $e) {
128            msg($e->getMessage(), -1);
129            return;
130        }
131
132        $res = $sqlite->query('SELECT * FROM maintainer ORDER BY namespace');
133        $assignments = $sqlite->res2arr($res);
134
135        echo $this->locale_xhtml('assignments_intro');
136
137        echo '<form action="' . wl($ID) . '" action="post">';
138        echo '<input type="hidden" name="do" value="admin" />';
139        echo '<input type="hidden" name="page" value="approve" />';
140        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
141        echo '<table class="inline">';
142
143        // header
144        echo '<tr>';
145        echo '<th>'.$this->getLang('admin h_assignment_namespace').'</th>';
146        echo '<th>'.$this->getLang('admin h_assignment_approver').'</th>';
147        echo '<th></th>';
148        echo '</tr>';
149
150        // existing assignments
151        foreach($assignments as $assignment) {
152            $id = $assignment['id'];
153            $namespace = $assignment['namespace'];
154            $approver = $assignment['approver'] ? $assignment['approver'] : '---';
155
156            $link = wl(
157                $ID, array(
158                    'do' => 'admin',
159                    'page' => 'approve',
160                    'action' => 'delete',
161                    'sectok' => getSecurityToken(),
162                    'assignment[id]' => $id
163                )
164            );
165
166            echo '<tr>';
167            echo '<td>' . hsc($namespace) . '</td>';
168            $user = $auth->getUserData($approver);
169            if ($user) {
170                echo '<td>' . hsc($user['name']) . '</td>';
171            } else {
172                echo '<td>' . hsc($approver) . '</td>';
173            }
174            echo '<td><a href="' . $link . '">'.$this->getLang('admin btn_delete').'</a></td>';
175            echo '</tr>';
176        }
177
178        // new assignment form
179        echo '<tr>';
180        echo '<td><input type="text" name="assignment[assign]" /></td>';
181        echo '<td>';
182        if ($auth->canDo('getUsers')) {
183            echo '<select name="assignment[approver]">';
184            echo '<option value="">---</option>';
185            if ($auth->canDo('getGroups')) {
186                foreach($auth->retrieveGroups() as $group) {
187                    echo '<option value="@' . hsc($group) . '">' . '@' . hsc($group) . '</option>';
188                }
189            }
190            foreach($auth->retrieveUsers() as $login => $data) {
191                echo '<option value="' . hsc($login) . '">' . hsc($data['name']) . '</option>';
192            }
193            echo '</select>';
194            // in case your auth plugin can do groups, but not list them (like the default one),
195            // leave a text field as backup
196            echo '<input name="assignment[approver_fb]">';
197        } else {
198            echo '<input name="assignment[approver]">';
199        }
200        echo '</td>';
201
202        echo '<td><button type="submit" name="action" value="add">'.$this->getLang('admin btn_add').'</button></td>';
203        echo '</tr>';
204
205        echo '</table>';
206    }
207}
208
209// vim:ts=4:sw=4:et:
210