Overview

Packages

  • Prefs

Classes

  • Horde_Prefs
  • Horde_Prefs_Cache_Base
  • Horde_Prefs_Cache_Null
  • Horde_Prefs_Cache_Session
  • Horde_Prefs_CategoryManager
  • Horde_Prefs_Exception
  • Horde_Prefs_Identity
  • Horde_Prefs_Scope
  • Horde_Prefs_Storage_Base
  • Horde_Prefs_Storage_File
  • Horde_Prefs_Storage_Imsp
  • Horde_Prefs_Storage_KolabImap
  • Horde_Prefs_Storage_Ldap
  • Horde_Prefs_Storage_Null
  • Horde_Prefs_Storage_Sql
  • Horde_Prefs_Translation
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Class for handling a list of categories stored in a user's
  4:  * preferences.
  5:  *
  6:  * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (LGPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 10:  *
 11:  * @author   Chuck Hagenbuch <chuck@horde.org>
 12:  * @category Horde
 13:  * @package  Prefs
 14:  */
 15: class Horde_Prefs_CategoryManager
 16: {
 17:     /**
 18:      * Get all categories.
 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:      * TODO
 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:         // Always add an Unfiled option.
 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:      * TODO
 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:      * Add a new category.
112:      *
113:      * @param string $category  The name of the category to add.
114:      *
115:      * @return mixed  False on failure, or the new category's name.
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:      * Delete a category.
136:      *
137:      * @param string $category  The category to remove.
138:      *
139:      * @return boolean  True on success, false on failure.
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:         // Remove any color preferences for $category.
158:         $colors = self::colors();
159:         unset($colors[$category]);
160:         self::setColors($colors);
161: 
162:         return true;
163:     }
164: 
165:     /**
166:      * Returns the color for each of the user's categories.
167:      *
168:      * @return array  A list of colors, key is the category name, value is the
169:      *                HTML color code.
170:      */
171:     static public function colors()
172:     {
173:         /* Default values that can be overridden but must always be
174:          * present. */
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:      * Returns the foreground color for each of the user's categories.
193:      *
194:      * @return array  A list of colors, key is the category name, value is the
195:      *                HTML color code.
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:      * TODO
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:      * TODO
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: 
API documentation generated by ApiGen