xref: /plugin/tagging/admin.php (revision 850ef65fb5af2f1497cf7a1e78a3583873f6de8c)
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->hlp->renameTag($INPUT->str('old'), $INPUT->str('new')); break;
54            case 'delete'    : $this->hlp->deleteTags(array_keys($INPUT->arr('tags')), $INPUT->str('filter')); 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            array('value' => '&#160;',                           'sort_by' => false),
101            array('value' => $this->getLang('admin tag'),        'sort_by' => 'tid'),
102            array('value' => $this->getLang('admin occurrence'), 'sort_by' => 'count'),
103            array('value' => $this->getLang('admin writtenas'),  'sort_by' => 'orig'),
104            array('value' => $this->getLang('admin taggers'),    'sort_by' => '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->addTagOpen('th');
132            $form->addHTML($header['value']);
133            if ($header['sort_by'] !== false) {
134                $form->addButton("fn[sort][$header[sort_by]]", 'sort');
135            }
136            $form->addTagClose('th');
137        }
138        $form->addTagClose('tr');
139
140        foreach ($tags as $taginfo) {
141            $tagname = $taginfo['tid'];
142            $taggers = $taginfo['taggers'];
143            $written = $taginfo['orig'];
144
145            $form->addTagOpen('tr');
146            $form->addTagOpen('td')->addClass('centeralign');
147            $form->addCheckbox('tags['.hsc($tagname).']');
148            $form->addTagClose('td');
149            $form->addHTML('<td><a class="tagslist" href="' .
150                    $this->hlp->getTagSearchURL($tagname) . '">' . hsc($tagname) . '</a></td>');
151            $form->addHTML('<td>' . $taginfo['count'] . '</td>');
152            $form->addHTML('<td>' . hsc($written) . '</td>');
153            $form->addHTML('<td>' . hsc($taggers) . '</td>');
154
155            $form->addTagClose('tr');
156        }
157
158        $form->addTagOpen('tr');
159        $form->addHTML('<td colspan="5" class="centeralign"><span class="medialeft">');
160        $form->addButton('fn[delete]', $this->getLang('admin delete_selected'))->id('tagging__del');
161        $form->addHTML('</span></td>');
162        $form->addTagClose('tr');
163
164        $form->addTagClose('table');
165        echo $form->toHTML();
166    }
167}
168