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