xref: /plugin/approve/admin.php (revision 1b552e8770c2762b98667d8e9f261a9c379a4ec6)
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    /**
55     * Should carry out any processing required by the plugin.
56     */
57    public function handle()
58    {
59        /* @var Input */
60        global $INPUT;
61        global $lang;
62
63        $action = $INPUT->str('action');
64        $updated = [];
65        if ($action == 'save_config') {
66            $res = $this->sqlite()->query('SELECT key, value FROM config');
67            $config_options = $this->sqlite()->res2arr($res);
68            foreach ($config_options as $option) {
69                $key = $option['key'];
70                $value = $option['value'];
71                $new_value = $INPUT->str($key);
72
73                if ($value != $new_value) {
74                    $updated[$key] = $new_value;
75                    $this->sqlite()->query('UPDATE config SET value=? WHERE key=?', $new_value, $key);
76                }
77            }
78
79            if (array_key_exists('no_apr_namespaces', $updated)) {
80                $res = $this->sqlite()->query('SELECT page, hidden FROM page');
81                $pages = $this->sqlite()->res2arr($res);
82                foreach ($pages as $page) {
83                    $id = $page['page'];
84                    $hidden = $page['hidden'];
85                    $in_hidden_namespace = $this->helper()->in_hidden_namespace($id, $updated['no_apr_namespaces']);
86                    $new_hidden = $in_hidden_namespace ? '1' : '0';
87
88                    if ($hidden != $new_hidden) {
89                        $this->sqlite()->query('UPDATE page SET hidden=? WHERE page=?', $new_hidden, $id);
90                    }
91                }
92            }
93            msg($this->getLang('admin updated'), 1);
94        }
95    }
96
97    /**
98     * Render HTML output, e.g. helpful text and a form
99     */
100    public function html()
101    {
102        global $lang;
103
104        global $ID;
105        /* @var Input */
106        global $INPUT;
107
108        ptln('<h1>' . $this->getLang('menu') . '</h1>');
109
110        ptln('<div id="plugin__approve_admin">');
111
112        $res = $this->sqlite()->query('SELECT key, value FROM config');
113        $config_options = $this->sqlite()->res2arr($res);
114
115        $form = new \dokuwiki\Form\Form();
116        $form->setHiddenField('action', 'save_config');
117        $form->addFieldsetOpen($this->getLang('admin settings'));
118        $form->addHTML('<table>');
119        foreach ($config_options as $option) {
120            $key = $option['key'];
121            $value = $option['value'];
122
123            $id = "plugin__approve_config_$key";
124
125            $input = new \dokuwiki\Form\InputElement('text', $key);
126            $input->id($id);
127
128            $form->addHTML('<tr>');
129
130            $form->addHTML('<td>');
131            $label = $this->getLang("admin config $key");
132            $form->addHTML("<label for=\"$id\">$label</label>");
133            $form->addHTML('</td>');
134
135
136            $form->addHTML('<td>');
137
138            $input->val($value);
139            $form->addElement($input);
140            $form->addHTML('</td>');
141
142            $form->addHTML('</tr>');
143        }
144        $form->addHTML('</table>');
145        $form->addButton('', $lang['btn_save']);
146
147        $form->addFieldsetClose();
148
149
150        ptln($form->toHTML());
151
152        return;
153
154        $form = new \dokuwiki\Form\Form();
155        $filter_input = new \dokuwiki\Form\InputElement('text', 'filter');
156        $filter_input->attr('placeholder', $this->getLang('search page'));
157        $form->addElement($filter_input);
158
159        $form->addButton('', $this->getLang('btn filter'));
160
161        $form->addHTML('<label class="outdated">');
162        $form->addCheckbox('outdated');
163        $form->addHTML($this->getLang('show outdated only'));
164        $form->addHTML('</label>');
165
166
167        ptln($form->toHTML());
168        ptln('<table>');
169        ptln('<tr>');
170        $headers = ['page', 'maintainer', 'cycle', 'current', 'uptodate'];
171        foreach ($headers as $header) {
172            $lang = $this->getLang("h $header");
173            $param = [
174                'do' => 'admin',
175                'page' => 'watchcycle',
176                'sortby' => $header,
177            ];
178            $icon = '';
179            if ($INPUT->str('sortby') == $header) {
180                if ($INPUT->int('desc') == 0) {
181                    $param['desc'] = 1;
182                    $icon = '↑';
183                } else {
184                    $param['desc'] = 0;
185                    $icon = '↓';
186                }
187            }
188            $href = wl($ID, $param);
189
190            ptln('<th><a href="' . $href . '">' . $icon . ' ' . $lang . '</a></th>');
191        }
192        $q = 'SELECT page, maintainer, cycle, DAYS_AGO(last_maintainer_rev) AS current, uptodate FROM watchcycle';
193        $where = [];
194        $q_args = [];
195        if ($INPUT->str('filter') != '') {
196            $where[] = 'page LIKE ?';
197            $q_args[] = '%' . $INPUT->str('filter') . '%';
198        }
199        if ($INPUT->has('outdated')) {
200            $where[] = 'uptodate=0';
201        }
202
203        if (count($where) > 0) {
204            $q .= ' WHERE ';
205            $q .= implode(' AND ', $where);
206        }
207
208        if ($INPUT->has('sortby') && in_array($INPUT->str('sortby'), $headers)) {
209            $q .= ' ORDER BY ' . $INPUT->str('sortby');
210            if ($INPUT->int('desc') == 1) {
211                $q .= ' DESC';
212            }
213        }
214
215        $res = $sqlite->query($q, $q_args);
216        while ($row = $sqlite->res2row($res)) {
217            ptln('<tr>');
218            ptln('<td><a href="' . wl($row['page']) . '" class="wikilink1">' . $row['page'] . '</a></td>');
219            ptln('<td>' . $row['maintainer'] . '</td>');
220            ptln('<td>' . $row['cycle'] . '</td>');
221            ptln('<td>' . $row['current'] . '</td>');
222            $icon = $row['uptodate'] == 1 ? '✓' : '✕';
223            ptln('<td>' . $icon . '</td>');
224            ptln('</tr>');
225        }
226
227        ptln('</tr>');
228        ptln('</table>');
229
230        ptln('</div>');
231    }
232}
233
234// vim:ts=4:sw=4:et:
235