[1] | 1 | <?php |
---|
| 2 | /** |
---|
[135] | 3 | * Prefs.inc.php |
---|
| 4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
| 5 | * |
---|
[1] | 6 | * Prefs:: provides an API for saving arbitrary values in a user's session. |
---|
| 7 | * |
---|
| 8 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[135] | 9 | * @version 2.1 |
---|
[1] | 10 | */ |
---|
| 11 | class Prefs { |
---|
| 12 | |
---|
[135] | 13 | // Namespace of preferences. |
---|
| 14 | var $ns; |
---|
[1] | 15 | |
---|
| 16 | /** |
---|
| 17 | * Prefs constructor. |
---|
| 18 | */ |
---|
[135] | 19 | function Prefs($ns='') |
---|
[1] | 20 | { |
---|
[135] | 21 | $this->ns = '_pref_' . $ns; |
---|
| 22 | |
---|
| 23 | // Initialized the prefs array. |
---|
| 24 | if (!isset($_SESSION[$this->ns])) { |
---|
| 25 | $_SESSION[$this->ns] = array(); |
---|
| 26 | } |
---|
[1] | 27 | } |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Sets the default value of a preference. The pref will be set only if |
---|
[42] | 31 | * is not set already. |
---|
[1] | 32 | * |
---|
[135] | 33 | * @param string $key The name of the preference to modify. |
---|
[1] | 34 | * @param string $val The new value for this preference. |
---|
| 35 | */ |
---|
[135] | 36 | function default($key, $val) |
---|
[1] | 37 | { |
---|
| 38 | // Set it only if not set already. |
---|
[135] | 39 | if (!isset($_SESSION[$this->ns][$key])) { |
---|
| 40 | $_SESSION[$this->ns][$key] = $val; |
---|
[1] | 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /** |
---|
[42] | 45 | * Sets the given preferences to the specific value, |
---|
[1] | 46 | * |
---|
[135] | 47 | * @param string $key The name of the preference to modify. |
---|
[1] | 48 | * @param string $val The new value for this preference. |
---|
| 49 | */ |
---|
[135] | 50 | function set($key, $val) |
---|
[1] | 51 | { |
---|
[135] | 52 | $_SESSION[$this->ns][$key] = $val; |
---|
[1] | 53 | } |
---|
[42] | 54 | |
---|
[1] | 55 | /** |
---|
| 56 | * Returns the value of the requested preference. |
---|
| 57 | * |
---|
[135] | 58 | * @param string $key The name of the preference to retrieve. |
---|
[1] | 59 | * |
---|
| 60 | * @return string The value of the preference. |
---|
| 61 | */ |
---|
[135] | 62 | function get($key) |
---|
[1] | 63 | { |
---|
[135] | 64 | return (isset($_SESSION[$this->ns][$key])) ? $_SESSION[$this->ns][$key] : null; |
---|
[1] | 65 | } |
---|
[42] | 66 | |
---|
[1] | 67 | /** |
---|
| 68 | * To see if a preference has been set. |
---|
| 69 | * |
---|
[135] | 70 | * @param string $key The name of the preference to check. |
---|
[1] | 71 | * |
---|
| 72 | * @return boolean True if the preference isset and not empty |
---|
| 73 | * false otherwise. |
---|
| 74 | */ |
---|
[135] | 75 | function exists($key) |
---|
[1] | 76 | { |
---|
[135] | 77 | return isset($_SESSION[$this->ns][$key]); |
---|
[1] | 78 | } |
---|
[42] | 79 | |
---|
[1] | 80 | /** |
---|
| 81 | * Clear a set preference value. |
---|
| 82 | * |
---|
[135] | 83 | * @param string $key The name of the preference to check. |
---|
[1] | 84 | */ |
---|
[135] | 85 | function delete($key) |
---|
[1] | 86 | { |
---|
[135] | 87 | if (isset($_SESSION[$this->ns][$key])) { |
---|
| 88 | unset($_SESSION[$this->ns][$key]); |
---|
[1] | 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Perform cleanup operations. |
---|
| 94 | */ |
---|
[135] | 95 | function clear() |
---|
[1] | 96 | { |
---|
[135] | 97 | $_SESSION[$this->ns] = array(); |
---|
[1] | 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | ?> |
---|