1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Prefs_CategoryManager
16: {
17: 18: 19:
20: static public function get()
21: {
22: $string = $GLOBALS['prefs']->getValue('categories');
23: if (empty($string)) {
24: return array();
25: }
26:
27: $categories = explode('|', $string);
28: asort($categories);
29:
30: return $categories;
31: }
32:
33: 34: 35:
36: static public function getSelect($id, $current = null)
37: {
38: $categories = self::get();
39: $colors = self::colors();
40: $fgcolors = self::fgColors();
41:
42: $id_html = htmlspecialchars($id);
43: $html = '<select id="' . preg_replace('/[^A-Za-z0-9-_:.]+/', '_', $id_html) . '" name="' . $id_html . '">';
44:
45: if (!in_array($current, $categories) && !empty($current)) {
46: $curr_html = htmlspecialchars($current);
47: $html .= '<option value="*new*' . $curr_html . '">'
48: . sprintf(Horde_Prefs_Translation::t("Use Current: %s"), $curr_html)
49: . '</option>'
50: . '<option value="" disabled="disabled">- - - - - - - - -</option>';
51: }
52:
53: if (!$GLOBALS['prefs']->isLocked('categories')) {
54: $html .= '<option value="*new*">' . Horde_Prefs_Translation::t("New Category")
55: . "</option>\n"
56: . '<option value="" disabled="disabled">- - - - - - - - -</option>';
57: }
58:
59:
60: $html .= '<option value="" style="background:'
61: . $colors['_unfiled_'] . ';color:' . $fgcolors['_unfiled_'] . '"'
62: . (empty($current) ? ' selected="selected">' : '>')
63: . htmlspecialchars(Horde_Prefs_Translation::t("Unfiled")) . '</option>';
64:
65: foreach ($categories as $name) {
66: $name_html = htmlspecialchars($name);
67: $html .= '<option value="' . $name_html
68: . '" style="background:' . (isset($colors[$name]) ? $colors[$name] : '#fff')
69: . ';color:' . (isset($fgcolors[$name]) ? $fgcolors[$name] : '#000') . '"'
70: . ($name === $current ? ' selected="selected">' : '>')
71: . $name_html . '</option>';
72: }
73:
74: return $html . '</select>';
75: }
76:
77: 78: 79:
80: static public function getJavaScript($formname, $elementname)
81: {
82: $prompt = addslashes(Horde_Prefs_Translation::t("Please type the new category name:"));
83: $error = addslashes(Horde_Prefs_Translation::t("You must type a new category name."));
84:
85: return <<<JAVASCRIPT
86:
87: <script type="text/javascript">
88: <!--
89: function checkCategory()
90: {
91: if (document.${formname}['$elementname'].value == '*new*') {
92: var category = window.prompt('$prompt', '');
93: if (category != null && category != '') {
94: document.$formname.new_category.value = category;
95: } else {
96: window.alert('$error');
97: return false;
98: }
99: } else if (document.${formname}['$elementname'].value.indexOf('*new*') != -1) {
100: document.$formname.new_category.value = document.${formname}['$elementname'].value.substr(5, document.${formname}['$elementname'].value.length);
101: }
102:
103: return true;
104: }
105: //-->
106: </script>
107: JAVASCRIPT;
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: static public function add($category)
118: {
119: if ($GLOBALS['prefs']->isLocked('categories') || empty($category)) {
120: return false;
121: }
122:
123: $categories = self::get();
124: if (in_array($category, $categories)) {
125: return $category;
126: }
127:
128: $categories[] = $category;
129: $GLOBALS['prefs']->setValue('categories', implode('|', $categories));
130:
131: return $category;
132: }
133:
134: 135: 136: 137: 138: 139: 140:
141: static public function remove($category)
142: {
143: if ($GLOBALS['prefs']->isLocked('categories')) {
144: return false;
145: }
146:
147: $categories = self::get();
148:
149: $key = array_search($category, $categories);
150: if ($key === false) {
151: return $key;
152: }
153:
154: unset($categories[$key]);
155: $GLOBALS['prefs']->setValue('categories', implode('|', $categories));
156:
157:
158: $colors = self::colors();
159: unset($colors[$category]);
160: self::setColors($colors);
161:
162: return true;
163: }
164:
165: 166: 167: 168: 169: 170:
171: static public function colors()
172: {
173: 174:
175: $colors['_default_'] = '#FFFFFF';
176: $colors['_unfiled_'] = '#DDDDDD';
177:
178: $pairs = explode('|', $GLOBALS['prefs']->getValue('category_colors'));
179: foreach ($pairs as $pair) {
180: if (!empty($pair)) {
181: list($category, $color) = explode(':', $pair);
182: $colors[$category] = $color;
183: }
184: }
185:
186: $colors[''] = $colors['_unfiled_'];
187:
188: return $colors;
189: }
190:
191: 192: 193: 194: 195: 196:
197: static public function fgColors()
198: {
199: $colors = self::colors();
200: $fgcolors = array();
201: foreach ($colors as $name => $color) {
202: $fgcolors[$name] = Horde_Image::brightness($color) < 128 ? '#f6f6f6' : '#000';
203: }
204:
205: return $fgcolors;
206: }
207:
208: 209: 210:
211: static public function setColor($category, $color)
212: {
213: $colors = self::colors();
214: $colors[$category] = $color;
215: self::setColors($colors);
216: }
217:
218: 219: 220:
221: static public function setColors($colors)
222: {
223: $pairs = array();
224: foreach ($colors as $category => $color) {
225: if ($color[0] != '#') {
226: $color = '#' . $color;
227: }
228: if (!empty($category)) {
229: $pairs[] = $category . ':' . $color;
230: }
231: }
232:
233: $GLOBALS['prefs']->setValue('category_colors', implode('|', $pairs));
234: }
235:
236: }
237: