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