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