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