xref: /plugin/approve/admin.php (revision 0c60a293fb92117ae4211b9e6d37af69354c10f0)
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                }
103                $sqlite->storeEntry('maintainer', $data);
104
105                $this->updatePage($sqlite, $helper);
106            }
107
108            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'approve'), true, '&'));
109        }
110    }
111
112    /**
113     * Render HTML output, e.g. helpful text and a form
114     */
115    public function html()
116    {
117        global $ID;
118        /* @var DokuWiki_Auth_Plugin $auth */
119        global $auth;
120
121        try {
122            /** @var \helper_plugin_approve_db $db_helper */
123            $db_helper = plugin_load('helper', 'approve_db');
124            $sqlite = $db_helper->getDB();
125        } catch (Exception $e) {
126            msg($e->getMessage(), -1);
127            return;
128        }
129
130        $res = $sqlite->query('SELECT * FROM maintainer ORDER BY namespace');
131        $assignments = $sqlite->res2arr($res);
132
133        echo $this->locale_xhtml('assignments_intro');
134
135        echo '<form action="' . wl($ID) . '" action="post">';
136        echo '<input type="hidden" name="do" value="admin" />';
137        echo '<input type="hidden" name="page" value="approve" />';
138        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
139        echo '<table class="inline">';
140
141        // header
142        echo '<tr>';
143        echo '<th>'.$this->getLang('admin h_assignment_namespace').'</th>';
144        echo '<th>'.$this->getLang('admin h_assignment_approver').'</th>';
145        echo '<th></th>';
146        echo '</tr>';
147
148        // existing assignments
149        foreach($assignments as $assignment) {
150            $id = $assignment['id'];
151            $namespace = $assignment['namespace'];
152            $approver = $assignment['approver'] ? $assignment['approver'] : '---';
153
154            $link = wl(
155                $ID, array(
156                    'do' => 'admin',
157                    'page' => 'approve',
158                    'action' => 'delete',
159                    'sectok' => getSecurityToken(),
160                    'assignment[id]' => $id
161                )
162            );
163
164            echo '<tr>';
165            echo '<td>' . hsc($namespace) . '</td>';
166            $user = $auth->getUserData($approver);
167            if ($user) {
168                echo '<td>' . hsc($user['name']) . '</td>';
169            } else {
170                echo '<td>' . hsc($approver) . '</td>';
171            }
172            echo '<td><a href="' . $link . '">'.$this->getLang('admin btn_delete').'</a></td>';
173            echo '</tr>';
174        }
175
176        // new assignment form
177        echo '<tr>';
178        echo '<td><input type="text" name="assignment[assign]" /></td>';
179        echo '<td>';
180        if ($auth->canDo('getUsers')) {
181            echo '<select name="assignment[approver]">';
182            echo '<option value="">---</option>';
183            foreach($auth->retrieveUsers() as $login => $data) {
184                echo '<option value="' . hsc($login) . '">' . hsc($data['name']) . '</option>';
185            }
186            echo '</select>';
187
188        } else {
189            echo '<input name="assignment[approver]">';
190        }
191        echo '</td>';
192
193        echo '<td><button type="submit" name="action" value="add">'.$this->getLang('admin btn_add').'</button></td>';
194        echo '</tr>';
195
196        echo '</table>';
197    }
198}
199
200// vim:ts=4:sw=4:et:
201