1<?php
2
3if(!defined('DOKU_INC')) die();
4class action_plugin_labeled_change extends DokuWiki_Action_Plugin {
5
6    var $hlp;
7
8    public static $act = 'labeled_do';
9
10    /**
11     * Register handlers
12     */
13    function register(Doku_Event_Handler $controller) {
14        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'change_action');
15    }
16
17    function change_action(&$event, $param) {
18        if ($event->data !== 'labeled') {
19            return;
20        }
21        $event->preventDefault();
22        $this->hlp = plugin_load('helper', 'labeled');
23        if (is_null($this->hlp)) {
24            msg('Labeled plugin corrupted, please reinstall', -1);
25            return false;
26        }
27        $this->handle();
28        global $ACT;
29        $ACT = 'show';
30    }
31
32    /**
33     * Handle label change actions.
34     */
35    private function handle() {
36        global $ID;
37        $this->hlp->getDb();
38
39        if (!isset($_REQUEST[action_plugin_labeled_change::$act])) return;
40        $do = $_REQUEST[action_plugin_labeled_change::$act];
41        switch ($do) {
42            case 'set': /* set all string of many labels separated by , */
43                $this->_set();break;
44            case 'add': /* add a label to a page*/
45                $this->_add();break;
46            case 'remove': /* delete a label from a page*/
47                $this->_remove();break;
48            case 'create': /* create a new label */
49                $this->_create();break;
50            case 'delete': /* delete a label */
51                $this->_delete();break;
52
53        }
54    }
55
56    private function _delete() {
57        if (!isset($_REQUEST['label'])) return;
58        global $ID;
59        $this->hlp->deleteLabel($_REQUEST['label'], $ID);
60    }
61
62    private function _remove() {
63        if (!isset($_REQUEST['label'])) return;
64        global $ID;
65        $this->hlp->removeLabel($_REQUEST['label'], $ID);
66    }
67
68    private function _add() {
69        if (!isset($_REQUEST['label'])) return;
70        $label = $_REQUEST['label'];
71        global $ID;
72
73        $this->hlp->addLabel($label, $ID);
74
75    }
76
77    private function _set() {
78        if (!isset($_REQUEST['labels'])) return;
79        global $ID;
80
81        $labels = $this->hlp->parseLabels($_REQUEST['labels']);
82        $this->hlp->setLabels($labels, $ID);
83    }
84
85    private function _create() {
86        foreach (array('label', 'color') as $attr) {
87            if (!isset($_REQUEST[$attr])) return;
88            $$attr = $_REQUEST[$attr];
89        }
90        $ns = '';
91        if (isset($_REQUEST['ns'])) $ns = $_REQUEST['ns'];
92
93        $this->hlp->createLabel($label, $color, $ns = '');
94    }
95
96
97
98}
99