xref: /plugin/tagging/admin.php (revision ca455b8ec188423658629387848549f7a4ea1aaa)
1<?php
2// must be run within Dokuwiki
3if (!defined('DOKU_INC')) {
4    die();
5}
6
7class admin_plugin_tagging extends DokuWiki_Admin_Plugin {
8
9    /** @var helper_plugin_tagging */
10    private $hlp;
11
12    public function __construct() {
13        $this->hlp = plugin_load('helper', 'tagging');
14    }
15
16    /**
17     * We allow use by managers
18     *
19     * @return bool always false
20     */
21    function forAdminOnly() {
22        return false;
23    }
24
25    /**
26     * Handle tag actions
27     */
28    function handle() {
29        global $ID, $INPUT;
30
31        //by default use current page namespace
32        if (!$INPUT->has('filter')) {
33            $INPUT->set('filter', getNS($ID));
34        }
35
36
37        //by default sort by tag name
38        if (!$INPUT->has('sort')) {
39            $INPUT->set('sort', 'tid');
40        }
41
42        //now starts functions handle
43        if (!$INPUT->has('fn')) {
44            return false;
45        }
46        if (!checkSecurityToken()) {
47            return false;
48        }
49
50        // extract the command and any specific parameters
51        // submit button name is of the form - fn[cmd][param(s)]
52        $fn = $INPUT->param('fn');
53
54        if (is_array($fn)) {
55            $cmd = key($fn);
56            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
57        } else {
58            $cmd = $fn;
59            $param = null;
60        }
61
62        switch ($cmd) {
63            case 'rename':
64                $this->hlp->renameTag($INPUT->str('old'), $INPUT->str('new'));
65                break;
66            case 'delete':
67                $this->hlp->deleteTags(array_keys($INPUT->arr('tags')), $INPUT->str('filter'));
68                break;
69            case 'sort':
70                $INPUT->set('sort', $param);
71                break;
72
73        }
74    }
75
76    /**
77     * Draw the interface
78     */
79    function html() {
80        echo $this->locale_xhtml('intro');
81        $this->html_form();
82        echo '<br />';
83        $this->html_table();
84    }
85
86    /**
87     * Show form for renaming tags
88     */
89    protected function html_form() {
90        global $ID, $INPUT;
91
92        $form = new dokuwiki\Form\Form();
93        $form->addClass('plugin_tagging');
94
95        $form->setHiddenField('do', 'admin');
96        $form->setHiddenField('page', 'tagging');
97        $form->setHiddenField('id', $ID);
98        $form->setHiddenField('filter', $INPUT->str('filter'));
99        $form->setHiddenField('sort', $INPUT->str('sort'));
100
101        $form->addFieldsetOpen($this->getLang('admin rename tag'));
102        $form->addTextInput('old', $this->getLang('admin find tag'))->addClass('block');
103        $form->addTagClose('br');
104        $form->addTextInput('new', $this->getLang('admin new name'))->addClass('block');
105        $form->addTagClose('br');
106        $form->addButton('fn[rename]', $this->getLang('admin save'));
107        $form->addFieldsetClose();
108
109        echo $form->toHTML();
110    }
111
112    /**
113     * Display ALL the tags!
114     */
115    protected function html_table() {
116        global $ID, $INPUT;
117
118        $headers = array(
119            array('value' => '&#160;', 'sort_by' => false),
120            array('value' => $this->getLang('admin tag'), 'sort_by' => 'tid'),
121            array('value' => $this->getLang('admin occurrence'), 'sort_by' => 'count'),
122            array('value' => $this->getLang('admin writtenas'), 'sort_by' => 'orig'),
123            array('value' => $this->getLang('admin taggers'), 'sort_by' => 'taggers'),
124        );
125
126        $sort = explode(',', $INPUT->str('sort'));
127        $order_by = $sort[0];
128        $desc = false;
129        if (isset($sort[1]) && $sort[1] === 'desc') {
130            $desc = true;
131        }
132        $tags = $this->hlp->getAllTags($INPUT->str('filter'), $order_by, $desc);
133
134        $form = new dokuwiki\Form\Form();
135        $form->setHiddenField('do', 'admin');
136        $form->setHiddenField('page', 'tagging');
137        $form->setHiddenField('id', $ID);
138        $form->setHiddenField('sort', $INPUT->str('sort'));
139
140        $form->addTagOpen('table')->addClass('inline plugin_tagging');
141        $form->addTagOpen('tr');
142        $form->addTagOpen('th')->attr('colspan', count($headers));
143
144        /**
145         * Show form for filtering the tags by namespaces
146         */
147        $form->addTextInput('filter', $this->getLang('admin filter') . ': ');
148        $form->addButton('fn[filter]', $this->getLang('admin filter button'));
149
150        $form->addTagClose('th');
151        $form->addTagClose('tr');
152
153        /**
154         * Table headers
155         */
156        $form->addTagOpen('tr');
157        foreach ($headers as $header) {
158            $form->addTagOpen('th');
159            if ($header['sort_by'] !== false) {
160                $param = $header['sort_by'];
161                $icon = 'arrow-both';
162                $title = $this->getLang('admin sort ascending');
163                if ($header['sort_by'] === $order_by) {
164                    if ($desc === false) {
165                        $icon = 'arrow-up';
166                        $title = $this->getLang('admin sort descending');
167                        $param .= ',desc';
168                    } else {
169                        $icon = 'arrow-down';
170                    }
171                }
172                $form->addButtonHTML("fn[sort][$param]", $header['value'] . ' ' . inlineSVG(dirname(__FILE__) . "/images/$icon.svg"))
173                    ->addClass('plugin_tagging sort_button')
174                    ->attr('title', $title);
175            }
176            $form->addTagClose('th');
177        }
178        $form->addTagClose('tr');
179
180        foreach ($tags as $taginfo) {
181            $tagname = $taginfo['tid'];
182            $taggers = $taginfo['taggers'];
183            $written = $taginfo['orig'];
184
185            $form->addTagOpen('tr');
186            $form->addTagOpen('td')->addClass('centeralign');
187            $form->addCheckbox('tags[' . hsc($tagname) . ']');
188            $form->addTagClose('td');
189            $form->addHTML('<td><a class="tagslist" href="' .
190                $this->hlp->getTagSearchURL($tagname) . '">' . hsc($tagname) . '</a></td>');
191            $form->addHTML('<td>' . $taginfo['count'] . '</td>');
192            $form->addHTML('<td>' . hsc($written) . '</td>');
193            $form->addHTML('<td>' . hsc($taggers) . '</td>');
194
195            $form->addTagClose('tr');
196        }
197
198        $form->addTagOpen('tr');
199        $form->addHTML('<td colspan="5" class="centeralign"><span class="medialeft">');
200        $form->addButton('fn[delete]', $this->getLang('admin delete_selected'))->id('tagging__del');
201        $form->addHTML('</span></td>');
202        $form->addTagClose('tr');
203
204        $form->addTagClose('table');
205        echo $form->toHTML();
206    }
207}
208