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