1<?php
2
3// must be run within Dokuwiki
4if (!defined('DOKU_INC')) die();
5
6class admin_plugin_labeled extends DokuWiki_Admin_Plugin {
7
8    private $hlp;
9
10    function getMenuSort() { return 400; }
11    function forAdminOnly() { return false; }
12
13    function handle() {
14        $this->hlp = plugin_load('helper', 'labeled');
15
16        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
17            if (checkSecurityToken()) {
18                if (isset($_POST['action']['delete'])) {
19                   $this->delLabel();
20                }
21                if (isset($_POST['action']['create'])) {
22                    $this->create();
23                } else if (isset($_POST['action']['save'])) {
24                    $this->applyChanges();
25                    $this->create(true);
26                }
27            }
28        }
29    }
30
31    function html() {
32        global $ID;
33        $labels = $this->hlp->getAllLabels();
34        include dirname(__FILE__) . '/admin_tpl.php';
35    }
36
37    /**
38     * Try to delete a label
39     */
40    private function delLabel() {
41        $labels = array_keys($_POST['action']['delete']);
42        foreach ($labels as $label) {
43            $this->hlp->deleteLabel($label);
44        }
45        msg($this->getLang('label deleted'));
46    }
47
48    private function applyChanges() {
49        $labels = $this->hlp->getAllLabels();
50
51        if (!isset($_POST['labels'])) return; // nothing to do
52
53        foreach ($_POST['labels'] as $oldName => $newValues) {
54
55            // apply color
56            if ($labels[$oldName]['color'] != $newValues['color']) {
57                if ($this->validateColor($newValues['color'])) {
58                    $this->hlp->changeColor($oldName, $newValues['color']);
59                } else {
60                    msg('invalid color', -1);
61                }
62            }
63
64            // apply order number
65            if ($labels[$oldName]['ordernr'] != $newValues['order']) {
66                $this->hlp->changeOrder($oldName, $newValues['order']);
67            } else if (empty($newValues['order'])) {
68                $this->hlp->changeOrder($oldName, 2147483647);
69                $labels = $this->hlp->getAllLabels();
70            }
71
72            // apply renaming
73            if ($oldName !== $newValues['name'] && !empty($newValues['name'])) {
74                if ($this->validateName($newValues['name'])) {
75                    $this->hlp->renameLabel($oldName, $newValues['name']);
76                } else {
77                    msg('name already exists');
78                }
79            }
80        }
81
82        $this->hlp->getAllLabels(true);
83
84    }
85
86    /**
87     * create a label using the request parameter
88     */
89    private function create($applyMode = false) {
90        if (!isset($_POST['newlabel'])) return;
91
92        $name = (isset($_POST['newlabel']['name']))?$_POST['newlabel']['name']:'';
93        $color = (isset($_POST['newlabel']['color']))?$_POST['newlabel']['color']:'';
94        $order = (isset($_POST['newlabel']['order']))?$_POST['newlabel']['order']:'';
95
96        if (empty($order)) $order = 2147483647; // maxint - last element
97        $order = floatval($order);
98
99        if ($applyMode && empty($name) && empty($color)) return;
100
101        if (!$this->validateName($name)) {
102            return;
103        }
104
105        if (!$this->validateColor($color)) {
106            return;
107        }
108
109        $this->hlp->createLabel($name, $color);
110        $this->hlp->changeOrder($name, $order);
111        msg($this->getLang('label created'));
112        $this->hlp->getAllLabels(true);
113    }
114
115    /**
116     * validate if a color is correct.
117     * @param string $color the color string
118     * @return boolean true if the color is correct
119     */
120    private function validateColor($color) {
121        if (!preg_match('/^#[0-9a-f]{3}([0-9a-f]{3})?$/i', $color)) {
122            msg($this->getLang('invalid color', -1));
123            return false;
124        }
125        return true;
126    }
127
128    /**
129     * check if a label name is correct
130     * @param string $name label name
131     * @return boolean true if everything is correct
132     */
133    private function validateName($name) {
134        if ($this->hlp->labelExists($name)) {
135            msg($this->getLang('label already exists', -1));
136            return false;
137        }
138        if (empty($name)) {
139            msg($this->getLang('no name', 1));
140            return false;
141        }
142        return true;
143    }
144
145}
146
147// vim:ts=4:sw=4:et:enc=utf-8:
148