xref: /plugin/tagging/admin.php (revision f1059d49c47ad297549030e90600931f0d436fa8)
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        }
56    }
57
58    /**
59     * Draw the interface
60     */
61    function html() {
62        echo $this->locale_xhtml('intro');
63        $this->html_form();
64        echo '<br />';
65        $this->html_table();
66    }
67
68    /**
69     * Show form for renaming tags
70     */
71    protected function html_form() {
72        global $ID, $INPUT;
73
74        $form = new dokuwiki\Form\Form();
75        $form->addClass('plugin_tagging');
76
77        $form->setHiddenField('do',    'admin');
78        $form->setHiddenField('page',  'tagging');
79        $form->setHiddenField('id',     $ID);
80        $form->setHiddenField('filter', $INPUT->str('filter'));
81
82        $form->addFieldsetOpen($this->getLang('admin rename tag'));
83        $form->addTextInput('old', $this->getLang('admin find tag'))->addClass('block');
84        $form->addTagClose('br');
85        $form->addTextInput('new', $this->getLang('admin new name'))->addClass('block');
86        $form->addTagClose('br');
87        $form->addButton('fn[rename]', $this->getLang('admin save'));
88        $form->addFieldsetClose();
89
90        echo $form->toHTML();
91    }
92
93    /**
94     * Display ALL the tags!
95     */
96    protected function html_table() {
97        global $ID, $INPUT;
98
99        $headers = array(
100            '&#160;',
101            $this->getLang('admin tag'),
102            $this->getLang('admin occurrence'),
103            $this->getLang('admin writtenas'),
104            $this->getLang('admin taggers')
105        );
106        $tags = $this->hlp->getAllTags($INPUT->str('filter'));
107
108        $form = new dokuwiki\Form\Form();
109        $form->setHiddenField('do',   'admin');
110        $form->setHiddenField('page', 'tagging');
111        $form->setHiddenField('id',    $ID);
112
113        $form->addTagOpen('table')->addClass('inline plugin_tagging');
114        $form->addTagOpen('tr');
115        $form->addTagOpen('th')->attr('colspan', count($headers));
116
117        /**
118         * Show form for filtering the tags by namespaces
119         */
120        $form->addTextInput('filter', $this->getLang('admin filter').': ');
121        $form->addButton('fn[filter]', $this->getLang('admin filter button'));
122
123        $form->addTagClose('th');
124        $form->addTagClose('tr');
125
126        /**
127         * Table headers
128         */
129        $form->addTagOpen('tr');
130        foreach ($headers as $header) {
131            $form->addHTML('<th>' . $header . '</th>');
132        }
133        $form->addTagClose('tr');
134
135        foreach ($tags as $tagname => $taginfo) {
136            $taggers = array_unique($taginfo['tagger']);
137            sort($taggers);
138            $written = array_unique($taginfo['orig']);
139            $taggers = join(', ', $taggers);
140            $written = join(', ', $written);
141
142            $form->addTagOpen('tr');
143            $form->addTagOpen('td')->addClass('centeralign');
144            $form->addCheckbox('tags['.hsc($tagname).']');
145            $form->addTagClose('td');
146            $form->addHTML('<td><a class="tagslist" href="' .
147                    $this->hlp->getTagSearchURL($tagname) . '">' . hsc($tagname) . '</a></td>');
148            $form->addHTML('<td>' . $taginfo['count'] . '</td>');
149            $form->addHTML('<td>' . hsc($written) . '</td>');
150            $form->addHTML('<td>' . hsc($taggers) . '</td>');
151
152            $form->addTagClose('tr');
153        }
154
155        $form->addTagOpen('tr');
156        $form->addHTML('<td colspan="5" class="centeralign"><span class="medialeft">');
157        $form->addButton('fn[delete]', $this->getLang('admin delete_selected'))->id('tagging__del');
158        $form->addHTML('</span></td>');
159        $form->addTagClose('tr');
160
161        $form->addTagClose('table');
162        echo $form->toHTML();
163    }
164
165    /**
166     * Rename a tag
167     *
168     */
169    protected function _renameTag() {
170        global $INPUT;
171
172        if ($INPUT->post->has('old') && $INPUT->post->has('new')) {
173            $this->hlp->renameTag($INPUT->post->str('old'), $INPUT->post->str('new'));
174        }
175    }
176
177    /**
178     * Delete tags
179     *
180     */
181    protected function _deleteTags() {
182        global $INPUT;
183
184        if ($INPUT->post->has('tags')) {
185            $this->hlp->deleteTags(array_keys($INPUT->post->arr('tags')), $INPUT->str('filter'));
186        }
187    }
188}
189