xref: /plugin/tagging/admin.php (revision 31396860a53990a8943f194ef4e93ea5fcf51370)
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    /**
28*31396860SSzymon Olewniczak     * Handle tag actions
298a1a3846SAndreas Gohr     */
308a1a3846SAndreas Gohr    function handle() {
31*31396860SSzymon Olewniczak        global $ID, $INPUT;
32*31396860SSzymon Olewniczak
33*31396860SSzymon Olewniczak        //by default use current page namespace
34*31396860SSzymon Olewniczak        if (!$INPUT->has('filter')) $INPUT->set('filter', getNS($ID));
35*31396860SSzymon Olewniczak
36*31396860SSzymon Olewniczak        //now starts functions handle
37*31396860SSzymon Olewniczak        if (!$INPUT->has('fn')) return false;
38*31396860SSzymon Olewniczak        if (!checkSecurityToken()) return false;
39*31396860SSzymon Olewniczak
40*31396860SSzymon Olewniczak        // extract the command and any specific parameters
41*31396860SSzymon Olewniczak        // submit button name is of the form - fn[cmd][param(s)]
42*31396860SSzymon Olewniczak        $fn   = $INPUT->param('fn');
43*31396860SSzymon Olewniczak
44*31396860SSzymon Olewniczak        if (is_array($fn)) {
45*31396860SSzymon Olewniczak            $cmd = key($fn);
46*31396860SSzymon Olewniczak            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
47*31396860SSzymon Olewniczak        } else {
48*31396860SSzymon Olewniczak            $cmd = $fn;
49*31396860SSzymon Olewniczak            $param = null;
50*31396860SSzymon Olewniczak        }
51*31396860SSzymon Olewniczak
52*31396860SSzymon Olewniczak        switch ($cmd) {
53*31396860SSzymon Olewniczak            case 'rename'    : $this->_renameTag(); break;
54*31396860SSzymon 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*31396860SSzymon Olewniczak        global $ID;
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);
788a1a3846SAndreas Gohr
798a1a3846SAndreas Gohr        $form->startFieldset($this->getLang('admin rename tag'));
808a1a3846SAndreas Gohr        $form->addElement(form_makeTextField('old', '', $this->getLang('admin find tag'), '', 'block'));
818a1a3846SAndreas Gohr        $form->addElement(form_makeTextField('new', '', $this->getLang('admin new name'), '', 'block'));
828a1a3846SAndreas Gohr        $form->addElement(form_makeButton('submit', 'admin', $this->getLang('admin save')));
838a1a3846SAndreas Gohr
848a1a3846SAndreas Gohr        $form->printForm();
858a1a3846SAndreas Gohr    }
868a1a3846SAndreas Gohr
878a1a3846SAndreas Gohr    /**
888a1a3846SAndreas Gohr     * Display ALL the tags!
898a1a3846SAndreas Gohr     */
908a1a3846SAndreas Gohr    protected function html_table() {
91a99b66c1SSzymon Olewniczak        global $ID, $INPUT;
928f630140SSzymon Olewniczak
93a99b66c1SSzymon Olewniczak        $tags = $this->hlp->getAllTags($INPUT->str('filter'));
94a99b66c1SSzymon Olewniczak
95a99b66c1SSzymon Olewniczak        echo '<table class="inline plugin_tagging">';
96a99b66c1SSzymon Olewniczak        echo '<tr>';
97a99b66c1SSzymon Olewniczak        echo '<th colspan="5">';
98a99b66c1SSzymon Olewniczak        /**
99a99b66c1SSzymon Olewniczak         * Show form for filtering the tags by namespaces
100a99b66c1SSzymon Olewniczak         */
101*31396860SSzymon Olewniczak        $filter_form = new dokuwiki\Form\Form();
102*31396860SSzymon Olewniczak        $filter_form->setHiddenField('do',   'admin');
103*31396860SSzymon Olewniczak        $filter_form->setHiddenField('page', 'tagging');
104*31396860SSzymon Olewniczak        $filter_form->setHiddenField('id',    $ID);
105*31396860SSzymon Olewniczak        $filter_form->addTextInput('filter', $this->getLang('admin filter').': ');
106*31396860SSzymon Olewniczak        $filter_form->addButton('fn[filter]', $this->getLang('admin filter button'));
107*31396860SSzymon Olewniczak        echo $filter_form->toHTML();
108a99b66c1SSzymon Olewniczak        echo '</th>';
109a99b66c1SSzymon Olewniczak        echo '</tr>';
1108a1a3846SAndreas Gohr
1118f630140SSzymon Olewniczak        echo '<form action="'.wl().'" method="post" accept-charset="utf-8">';
1128f630140SSzymon Olewniczak        formSecurityToken();
1138f630140SSzymon Olewniczak        echo '<input type="hidden" name="do"     value="admin" />';
1148f630140SSzymon Olewniczak        echo '<input type="hidden" name="page"   value="tagging" />';
1158f630140SSzymon Olewniczak        echo '<input type="hidden" name="id"     value="'.$ID.'" />';
116*31396860SSzymon Olewniczak        echo '<input type="hidden" name="filter" value="'.$INPUT->str('filter').'" />';
1178f630140SSzymon Olewniczak
1188a1a3846SAndreas Gohr        echo '<tr>';
119a0c45f7bSSzymon Olewniczak        echo '<th>&#160;</th>';
1208a1a3846SAndreas Gohr        echo '<th>' . $this->getLang('admin tag') . '</th>';
1218a1a3846SAndreas Gohr        echo '<th>' . $this->getLang('admin occurrence') . '</th>';
122fb1d0583SAndreas Gohr        echo '<th>' . $this->getLang('admin writtenas') . '</th>';
1238a1a3846SAndreas Gohr        echo '<th>' . $this->getLang('admin taggers') . '</th>';
1248a1a3846SAndreas Gohr        echo '</tr>';
1258a1a3846SAndreas Gohr
1268a1a3846SAndreas Gohr        foreach ($tags as $tagname => $taginfo) {
1278a1a3846SAndreas Gohr            $taggers = array_unique($taginfo['tagger']);
1288a1a3846SAndreas Gohr            sort($taggers);
129fb1d0583SAndreas Gohr            $written = array_unique($taginfo['orig']);
1308a1a3846SAndreas Gohr            $taggers = join(', ', $taggers);
131fb1d0583SAndreas Gohr            $written = join(', ', $written);
1328a1a3846SAndreas Gohr
1338a1a3846SAndreas Gohr            echo '<tr>';
134a0c45f7bSSzymon Olewniczak            echo '<td class="centeralign"><input type="checkbox" name="tags['.hsc($tagname).']" /></td>';
135302a38efSAndreas Gohr            echo '<td><a class="tagslist" href="' . $this->hlp->getTagSearchURL($tagname) . '">' . hsc($tagname) . '</a></td>';
1368a1a3846SAndreas Gohr            echo '<td>' . $taginfo['count'] . '</td>';
137fb1d0583SAndreas Gohr            echo '<td>' . hsc($written) . '</td>';
1388a1a3846SAndreas Gohr            echo '<td>' . hsc($taggers) . '</td>';
1398a1a3846SAndreas Gohr            echo '</tr>';
1408a1a3846SAndreas Gohr        }
1418f630140SSzymon Olewniczak        echo '<tr>';
1428f630140SSzymon Olewniczak        echo '<td colspan="5" class="centeralign">';
1438f630140SSzymon Olewniczak        echo '<span class="medialeft">';
1448f630140SSzymon Olewniczak        echo '<button type="submit" name="fn[delete]" id="tagging__del">'.$this->getLang('admin delete_selected').'</button>';
1458f630140SSzymon Olewniczak        echo '</tr>';
1468f630140SSzymon Olewniczak        echo '</form>';
147a99b66c1SSzymon Olewniczak        echo '</table>';
148a99b66c1SSzymon Olewniczak
1498f630140SSzymon Olewniczak    }
1508f630140SSzymon Olewniczak
1518f630140SSzymon Olewniczak    /**
1528f630140SSzymon Olewniczak     * Rename a tag
1538f630140SSzymon Olewniczak     *
1548f630140SSzymon Olewniczak     */
1558f630140SSzymon Olewniczak    protected function _renameTag() {
1568f630140SSzymon Olewniczak        global $INPUT;
1578f630140SSzymon Olewniczak
1588f630140SSzymon Olewniczak        if ($INPUT->post->has('old') && $INPUT->post->has('new')) {
1598f630140SSzymon Olewniczak            $this->hlp->renameTag($INPUT->post->str('old'), $INPUT->post->str('new'));
1608f630140SSzymon Olewniczak        }
1618f630140SSzymon Olewniczak    }
1628f630140SSzymon Olewniczak
1638f630140SSzymon Olewniczak    /**
1648f630140SSzymon Olewniczak     * Delete tags
1658f630140SSzymon Olewniczak     *
1668f630140SSzymon Olewniczak     */
1678f630140SSzymon Olewniczak    protected function _deleteTags() {
1688f630140SSzymon Olewniczak        global $INPUT;
1698f630140SSzymon Olewniczak
1708f630140SSzymon Olewniczak        if ($INPUT->post->has('tags')) {
171*31396860SSzymon Olewniczak            $this->hlp->deleteTags(array_keys($INPUT->post->arr('tags')), $INPUT->str('filter'));
1728f630140SSzymon Olewniczak        }
1738a1a3846SAndreas Gohr    }
174872edc7cSRené Corinth}
175