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