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_watchcycle extends DokuWiki_Admin_Plugin
15{
16
17    /**
18     * @return int sort number in admin menu
19     */
20    public function getMenuSort()
21    {
22        return 1;
23    }
24
25    /**
26     * @return bool true if only access for superuser, false is for superusers and moderators
27     */
28    public function forAdminOnly()
29    {
30        return false;
31    }
32
33    /**
34     * Should carry out any processing required by the plugin.
35     */
36    public function handle()
37    {
38    }
39
40    /**
41     * Render HTML output, e.g. helpful text and a form
42     */
43    public function html()
44    {
45        global $ID;
46        /* @var Input */
47        global $INPUT;
48
49        /** @var \helper_plugin_sqlite $sqlite */
50        $sqlite = plugin_load('helper', 'watchcycle_db')->getDB();
51        /* @var \helper_plugin_watchcycle */
52        $helper = plugin_load('helper', 'watchcycle');
53
54        ptln('<h1>' . $this->getLang('menu') . '</h1>');
55
56        ptln('<div id="plugin__watchcycle_admin">');
57
58        $form = new \dokuwiki\Form\Form();
59        $filter_input = new \dokuwiki\Form\InputElement('text', 'filter');
60        $filter_input->attr('placeholder', $this->getLang('search page'));
61        $form->addElement($filter_input);
62
63        $form->addButton('', $this->getLang('btn filter'));
64
65        $form->addHTML('<label class="outdated">');
66        $form->addCheckbox('outdated');
67        $form->addHTML($this->getLang('show outdated only'));
68        $form->addHTML('</label>');
69
70
71        ptln($form->toHTML());
72        ptln('<table>');
73        ptln('<tr>');
74        $headers = ['page', 'maintainer', 'cycle', 'current', 'uptodate'];
75        foreach ($headers as $header) {
76            $lang = $this->getLang("h $header");
77            $param = [
78                'do' => 'admin',
79                'page' => 'watchcycle',
80                'sortby' => $header,
81            ];
82            $icon = '';
83            if ($INPUT->str('sortby') == $header) {
84                if ($INPUT->int('desc') == 0) {
85                    $param['desc'] = 1;
86                    $icon = '↑';
87                } else {
88                    $param['desc'] = 0;
89                    $icon = '↓';
90                }
91            }
92            $href = wl($ID, $param);
93
94            ptln('<th><a href="' . $href . '">' . $icon . ' ' . $lang . '</a></th>');
95        }
96        $q = 'SELECT page, maintainer, cycle, DAYS_AGO(last_maintainer_rev) AS current, uptodate FROM watchcycle';
97        $where = [];
98        $q_args = [];
99        if ($INPUT->str('filter') != '') {
100            $where[] = 'page LIKE ?';
101            $q_args[] = '%' . $INPUT->str('filter') . '%';
102        }
103        if ($INPUT->has('outdated')) {
104            $where[] = 'uptodate=0';
105        }
106
107        if (count($where) > 0) {
108            $q .= ' WHERE ';
109            $q .= implode(' AND ', $where);
110        }
111
112        if ($INPUT->has('sortby') && in_array($INPUT->str('sortby'), $headers)) {
113            $q .= ' ORDER BY ' . $INPUT->str('sortby');
114            if ($INPUT->int('desc') == 1) {
115                $q .= ' DESC';
116            }
117        }
118
119        $res = $sqlite->query($q, $q_args);
120        while ($row = $sqlite->res2row($res)) {
121            ptln('<tr>');
122            ptln('<td><a href="' . wl($row['page']) . '" class="wikilink1">' . $row['page'] . '</a></td>');
123            ptln('<td>' . $row['maintainer'] . '</td>');
124            ptln('<td>' . $row['cycle'] . '</td>');
125            ptln('<td>' . $row['current'] . '</td>');
126            $icon = $row['uptodate'] == 1 ? '✓' : '✕';
127            ptln('<td>' . $icon . '</td>');
128            ptln('</tr>');
129        }
130
131        ptln('</tr>');
132        ptln('</table>');
133
134        ptln('</div>');
135    }
136}
137
138// vim:ts=4:sw=4:et:
139