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