xref: /plugin/tagging/admin.php (revision fece5bdca82f27f1a3dbaf8cb424a82fb570fb6a)
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
12    public function __construct() {
13        $this->hlp = plugin_load('helper', 'tagging');
14    }
15
16    /**
17     * We allow use by managers
18     *
19     * @return bool always false
20     */
21    function forAdminOnly() {
22        return false;
23    }
24
25    /**
26     * Handle tag actions
27     *
28     * FIXME remove obsolete actions
29     */
30    function handle() {
31        global $ID, $INPUT;
32
33        //by default use current page namespace
34        if (!$INPUT->has('filter')) {
35            $INPUT->set('filter', getNS($ID));
36        }
37
38
39        //by default sort by tag name
40        if (!$INPUT->has('sort')) {
41            $INPUT->set('sort', 'tid');
42        }
43
44        //now starts functions handle
45        if (!$INPUT->has('fn')) {
46            return false;
47        }
48        if (!checkSecurityToken()) {
49            return false;
50        }
51
52        // extract the command and any specific parameters
53        // submit button name is of the form - fn[cmd][param(s)]
54        $fn = $INPUT->param('fn');
55
56        if (is_array($fn)) {
57            $cmd = key($fn);
58            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
59        } else {
60            $cmd = $fn;
61            $param = null;
62        }
63
64        switch ($cmd) {
65            case 'rename':
66                $this->hlp->renameTag($INPUT->str('old'), $INPUT->str('new'));
67                break;
68            case 'delete':
69                $this->hlp->deleteTags(array_keys($INPUT->arr('tags')), $INPUT->str('filter'));
70                break;
71            case 'sort':
72                $INPUT->set('sort', $param);
73                break;
74
75        }
76    }
77
78    /**
79     * Draw the interface
80     */
81    function html() {
82        echo $this->locale_xhtml('intro');
83        $this->html_table();
84    }
85
86    /**
87     * Display ALL the tags!
88     */
89    protected function html_table() {
90        global $ID, $INPUT;
91
92        $headers = array(
93            array('value' => $this->getLang('admin tag'), 'sort_by' => 'tid'),
94            array('value' => $this->getLang('admin occurrence'), 'sort_by' => 'count'),
95            array('value' => $this->getLang('admin writtenas'), 'sort_by' => 'orig'),
96            array('value' => $this->getLang('admin taggers'), 'sort_by' => 'taggers'),
97            array('value' => $this->getLang('admin actions'), 'sort_by' => false),
98        );
99
100        $sort = explode(',', $INPUT->str('sort'));
101        $order_by = $sort[0];
102        $desc = false;
103        if (isset($sort[1]) && $sort[1] === 'desc') {
104            $desc = true;
105        }
106        $tags = $this->hlp->getAllTags($INPUT->str('filter'), $order_by, $desc);
107
108        $form = new dokuwiki\Form\Form();
109        $form->setHiddenField('do', 'admin');
110        $form->setHiddenField('page', 'tagging');
111        $form->setHiddenField('id', $ID);
112        $form->setHiddenField('sort', $INPUT->str('sort'));
113
114        // actions dialog
115        $form->addTagOpen('div')->id('tagging__action-dialog')->attr('style', "display:none;");
116        $form->addTagClose('div');
117
118        $form->addTagOpen('table')->addClass('inline plugin_tagging');
119
120
121        /**
122         * Table headers
123         */
124        $form->addTagOpen('tr');
125        foreach ($headers as $header) {
126            $form->addTagOpen('th');
127            if ($header['sort_by'] !== false) {
128                $param = $header['sort_by'];
129                $icon = 'arrow-both';
130                $title = $this->getLang('admin sort ascending');
131                if ($header['sort_by'] === $order_by) {
132                    if ($desc === false) {
133                        $icon = 'arrow-up';
134                        $title = $this->getLang('admin sort descending');
135                        $param .= ',desc';
136                    } else {
137                        $icon = 'arrow-down';
138                    }
139                }
140                $form->addButtonHTML("fn[sort][$param]", $header['value'] . ' ' . inlineSVG(dirname(__FILE__) . "/images/$icon.svg"))
141                    ->addClass('plugin_tagging sort_button')
142                    ->attr('title', $title);
143            } else {
144                $form->addHTML($header['value']);
145            }
146            $form->addTagClose('th');
147        }
148        $form->addTagClose('tr');
149
150        foreach ($tags as $taginfo) {
151            $tagname = $taginfo['tid'];
152            $taggers = $taginfo['taggers'];
153            $written = $taginfo['orig'];
154
155            $form->addTagOpen('tr');
156            $form->addHTML('<td><a class="tagslist" href="' .
157                $this->hlp->getTagSearchURL($tagname) . '">' . hsc($tagname) . '</a></td>');
158            $form->addHTML('<td>' . $taginfo['count'] . '</td>');
159            $form->addHTML('<td>' . hsc($written) . '</td>');
160            $form->addHTML('<td>' . hsc($taggers) . '</td>');
161            // action buttons
162            $form->addHTML('<td>');
163            $form->addButtonHTML('fn[actions][rename][' . $taginfo['tid'] . ']', inlineSVG(dirname(__FILE__) . '/images/edit.svg'))
164                ->addClass('plugin_tagging action_button')->attr('data-action', 'rename')->attr('data-tid', $taginfo['tid']);
165            $form->addButtonHTML('fn[actions][delete][' . $taginfo['tid'] . ']', inlineSVG(dirname(__FILE__) . '/images/delete.svg'))
166                ->addClass('plugin_tagging action_button')->attr('data-action', 'delete')->attr('data-tid', $taginfo['tid']);
167            $form->addHTML('</td>');
168            $form->addTagClose('tr');
169        }
170
171        $form->addTagClose('table');
172        echo $form->toHTML();
173    }
174}
175