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