xref: /plugin/tagging/admin.php (revision 0c8678733d7c0b78cbfafb1c6b7c5f1180435ce2)
1872edc7cSRené Corinth<?php
2872edc7cSRené Corinth// must be run within Dokuwiki
30cfde7e9SMichael Großeif (!defined('DOKU_INC')) {
40cfde7e9SMichael Große    die();
50cfde7e9SMichael Große}
6872edc7cSRené Corinth
7872edc7cSRené Corinthclass admin_plugin_tagging extends DokuWiki_Admin_Plugin {
8872edc7cSRené Corinth
98a1a3846SAndreas Gohr    /** @var helper_plugin_tagging */
10872edc7cSRené Corinth    private $hlp;
118a1a3846SAndreas Gohr    /** @var  string message to show */
128b352fbcSRené Corinth    private $message;
13872edc7cSRené Corinth
148a1a3846SAndreas Gohr    public function __construct() {
15872edc7cSRené Corinth        $this->hlp = plugin_load('helper', 'tagging');
168a1a3846SAndreas Gohr    }
17872edc7cSRené Corinth
188a1a3846SAndreas Gohr    /**
198a1a3846SAndreas Gohr     * We allow use by managers
208a1a3846SAndreas Gohr     *
218a1a3846SAndreas Gohr     * @return bool always false
228a1a3846SAndreas Gohr     */
238a1a3846SAndreas Gohr    function forAdminOnly() {
248a1a3846SAndreas Gohr        return false;
258a1a3846SAndreas Gohr    }
268a1a3846SAndreas Gohr
278a1a3846SAndreas Gohr    /**
2831396860SSzymon Olewniczak     * Handle tag actions
298a1a3846SAndreas Gohr     */
308a1a3846SAndreas Gohr    function handle() {
3131396860SSzymon Olewniczak        global $ID, $INPUT;
3231396860SSzymon Olewniczak
3331396860SSzymon Olewniczak        //by default use current page namespace
3431396860SSzymon Olewniczak        if (!$INPUT->has('filter')) $INPUT->set('filter', getNS($ID));
3531396860SSzymon Olewniczak
3631396860SSzymon Olewniczak        //now starts functions handle
3731396860SSzymon Olewniczak        if (!$INPUT->has('fn')) return false;
3831396860SSzymon Olewniczak        if (!checkSecurityToken()) return false;
3931396860SSzymon Olewniczak
4031396860SSzymon Olewniczak        // extract the command and any specific parameters
4131396860SSzymon Olewniczak        // submit button name is of the form - fn[cmd][param(s)]
4231396860SSzymon Olewniczak        $fn   = $INPUT->param('fn');
4331396860SSzymon Olewniczak
4431396860SSzymon Olewniczak        if (is_array($fn)) {
4531396860SSzymon Olewniczak            $cmd = key($fn);
4631396860SSzymon Olewniczak            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
4731396860SSzymon Olewniczak        } else {
4831396860SSzymon Olewniczak            $cmd = $fn;
4931396860SSzymon Olewniczak            $param = null;
5031396860SSzymon Olewniczak        }
5131396860SSzymon Olewniczak
5231396860SSzymon Olewniczak        switch ($cmd) {
5331396860SSzymon Olewniczak            case 'rename'    : $this->_renameTag(); break;
5431396860SSzymon Olewniczak            case 'delete'    : $this->_deleteTags(); break;
55872edc7cSRené Corinth        }
56872edc7cSRené Corinth    }
57872edc7cSRené Corinth
588a1a3846SAndreas Gohr    /**
598a1a3846SAndreas Gohr     * Draw the interface
608a1a3846SAndreas Gohr     */
61872edc7cSRené Corinth    function html() {
628a1a3846SAndreas Gohr        echo $this->locale_xhtml('intro');
638a1a3846SAndreas Gohr        $this->html_form();
648a1a3846SAndreas Gohr        echo '<br />';
658a1a3846SAndreas Gohr        $this->html_table();
66872edc7cSRené Corinth    }
67872edc7cSRené Corinth
688a1a3846SAndreas Gohr    /**
698a1a3846SAndreas Gohr     * Show form for renaming tags
708a1a3846SAndreas Gohr     */
718a1a3846SAndreas Gohr    protected function html_form() {
72*0c867873SSzymon Olewniczak        global $ID, $INPUT;
738a1a3846SAndreas Gohr
748a1a3846SAndreas Gohr        $form = new Doku_Form(array('action' => script(), 'method' => 'post', 'class' => 'plugin_tagging'));
758a1a3846SAndreas Gohr        $form->addHidden('do', 'admin');
768a1a3846SAndreas Gohr        $form->addHidden('page', 'tagging');
778a1a3846SAndreas Gohr        $form->addHidden('id', $ID);
78*0c867873SSzymon Olewniczak        $form->addHidden('filter', $INPUT->str('filter'));
798a1a3846SAndreas Gohr
808a1a3846SAndreas Gohr        $form->startFieldset($this->getLang('admin rename tag'));
818a1a3846SAndreas Gohr        $form->addElement(form_makeTextField('old', '', $this->getLang('admin find tag'), '', 'block'));
828a1a3846SAndreas Gohr        $form->addElement(form_makeTextField('new', '', $this->getLang('admin new name'), '', 'block'));
838a1a3846SAndreas Gohr        $form->addElement(form_makeButton('submit', 'admin', $this->getLang('admin save')));
848a1a3846SAndreas Gohr
858a1a3846SAndreas Gohr        $form->printForm();
868a1a3846SAndreas Gohr    }
878a1a3846SAndreas Gohr
888a1a3846SAndreas Gohr    /**
898a1a3846SAndreas Gohr     * Display ALL the tags!
908a1a3846SAndreas Gohr     */
918a1a3846SAndreas Gohr    protected function html_table() {
92a99b66c1SSzymon Olewniczak        global $ID, $INPUT;
938f630140SSzymon Olewniczak
94*0c867873SSzymon Olewniczak        $headers = array(
95*0c867873SSzymon Olewniczak            '&#160;',
96*0c867873SSzymon Olewniczak            $this->getLang('admin tag'),
97*0c867873SSzymon Olewniczak            $this->getLang('admin occurrence'),
98*0c867873SSzymon Olewniczak            $this->getLang('admin writtenas'),
99*0c867873SSzymon Olewniczak            $this->getLang('admin taggers')
100*0c867873SSzymon Olewniczak        );
101a99b66c1SSzymon Olewniczak        $tags = $this->hlp->getAllTags($INPUT->str('filter'));
102a99b66c1SSzymon Olewniczak
103*0c867873SSzymon Olewniczak        $form = new dokuwiki\Form\Form();
104*0c867873SSzymon Olewniczak        $form->setHiddenField('do',   'admin');
105*0c867873SSzymon Olewniczak        $form->setHiddenField('page', 'tagging');
106*0c867873SSzymon Olewniczak        $form->setHiddenField('id',    $ID);
107*0c867873SSzymon Olewniczak
108*0c867873SSzymon Olewniczak        $form->addTagOpen('table')->addClass('inline plugin_tagging');
109*0c867873SSzymon Olewniczak        $form->addTagOpen('tr');
110*0c867873SSzymon Olewniczak        $form->addTagOpen('th')->attr('colspan', count($headers));
111*0c867873SSzymon Olewniczak
112a99b66c1SSzymon Olewniczak        /**
113a99b66c1SSzymon Olewniczak         * Show form for filtering the tags by namespaces
114a99b66c1SSzymon Olewniczak         */
115*0c867873SSzymon Olewniczak        $form->addTextInput('filter', $this->getLang('admin filter').': ');
116*0c867873SSzymon Olewniczak        $form->addButton('fn[filter]', $this->getLang('admin filter button'));
1178a1a3846SAndreas Gohr
118*0c867873SSzymon Olewniczak        $form->addTagClose('th');
119*0c867873SSzymon Olewniczak        $form->addTagClose('tr');
1208f630140SSzymon Olewniczak
121*0c867873SSzymon Olewniczak        /**
122*0c867873SSzymon Olewniczak         * Table headers
123*0c867873SSzymon Olewniczak         */
124*0c867873SSzymon Olewniczak        $form->addTagOpen('tr');
125*0c867873SSzymon Olewniczak        foreach ($headers as $header) {
126*0c867873SSzymon Olewniczak            $form->addHTML('<th>' . $header . '</th>');
127*0c867873SSzymon Olewniczak        }
128*0c867873SSzymon Olewniczak        $form->addTagClose('tr');
1298a1a3846SAndreas Gohr
1308a1a3846SAndreas Gohr        foreach ($tags as $tagname => $taginfo) {
1318a1a3846SAndreas Gohr            $taggers = array_unique($taginfo['tagger']);
1328a1a3846SAndreas Gohr            sort($taggers);
133fb1d0583SAndreas Gohr            $written = array_unique($taginfo['orig']);
1348a1a3846SAndreas Gohr            $taggers = join(', ', $taggers);
135fb1d0583SAndreas Gohr            $written = join(', ', $written);
1368a1a3846SAndreas Gohr
137*0c867873SSzymon Olewniczak            $form->addTagOpen('tr');
138*0c867873SSzymon Olewniczak            $form->addTagOpen('td')->addClass('centeralign');
139*0c867873SSzymon Olewniczak            $form->addCheckbox('tags['.hsc($tagname).']');
140*0c867873SSzymon Olewniczak            $form->addTagClose('td');
141*0c867873SSzymon Olewniczak            $form->addHTML('<td><a class="tagslist" href="' .
142*0c867873SSzymon Olewniczak                    $this->hlp->getTagSearchURL($tagname) . '">' . hsc($tagname) . '</a></td>');
143*0c867873SSzymon Olewniczak            $form->addHTML('<td>' . $taginfo['count'] . '</td>');
144*0c867873SSzymon Olewniczak            $form->addHTML('<td>' . hsc($written) . '</td>');
145*0c867873SSzymon Olewniczak            $form->addHTML('<td>' . hsc($taggers) . '</td>');
146a99b66c1SSzymon Olewniczak
147*0c867873SSzymon Olewniczak            $form->addTagClose('tr');
148*0c867873SSzymon Olewniczak        }
149*0c867873SSzymon Olewniczak
150*0c867873SSzymon Olewniczak        $form->addTagOpen('tr');
151*0c867873SSzymon Olewniczak        $form->addHTML('<td colspan="5" class="centeralign"><span class="medialeft">');
152*0c867873SSzymon Olewniczak        $form->addButton('fn[delete]', $this->getLang('admin delete_selected'))->id('tagging__del');
153*0c867873SSzymon Olewniczak        $form->addHTML('</span></td>');
154*0c867873SSzymon Olewniczak        $form->addTagClose('tr');
155*0c867873SSzymon Olewniczak
156*0c867873SSzymon Olewniczak        $form->addTagClose('table');
157*0c867873SSzymon Olewniczak        echo $form->toHTML();
1588f630140SSzymon Olewniczak    }
1598f630140SSzymon Olewniczak
1608f630140SSzymon Olewniczak    /**
1618f630140SSzymon Olewniczak     * Rename a tag
1628f630140SSzymon Olewniczak     *
1638f630140SSzymon Olewniczak     */
1648f630140SSzymon Olewniczak    protected function _renameTag() {
1658f630140SSzymon Olewniczak        global $INPUT;
1668f630140SSzymon Olewniczak
1678f630140SSzymon Olewniczak        if ($INPUT->post->has('old') && $INPUT->post->has('new')) {
1688f630140SSzymon Olewniczak            $this->hlp->renameTag($INPUT->post->str('old'), $INPUT->post->str('new'));
1698f630140SSzymon Olewniczak        }
1708f630140SSzymon Olewniczak    }
1718f630140SSzymon Olewniczak
1728f630140SSzymon Olewniczak    /**
1738f630140SSzymon Olewniczak     * Delete tags
1748f630140SSzymon Olewniczak     *
1758f630140SSzymon Olewniczak     */
1768f630140SSzymon Olewniczak    protected function _deleteTags() {
1778f630140SSzymon Olewniczak        global $INPUT;
1788f630140SSzymon Olewniczak
1798f630140SSzymon Olewniczak        if ($INPUT->post->has('tags')) {
18031396860SSzymon Olewniczak            $this->hlp->deleteTags(array_keys($INPUT->post->arr('tags')), $INPUT->str('filter'));
1818f630140SSzymon Olewniczak        }
1828a1a3846SAndreas Gohr    }
183872edc7cSRené Corinth}
184