1 | <?php |
---|
2 | /** |
---|
3 | * Prefs:: provides an API for saving arbitrary values in a user's session. |
---|
4 | * Database save routines to come. |
---|
5 | * |
---|
6 | * @inspiration Horde 2.0's Prefs class. This one is much simpler, but |
---|
7 | * the API for the methods that exist are sort of like Horde's |
---|
8 | * in case we want to be more like it in the future. |
---|
9 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
10 | * @version 1.1 |
---|
11 | */ |
---|
12 | class Prefs { |
---|
13 | |
---|
14 | /** |
---|
15 | * Where these preferences can be used. To differentiate between preferences |
---|
16 | * for a specific script, application, or global. |
---|
17 | * @var string $scope |
---|
18 | */ |
---|
19 | var $scope = 'global'; |
---|
20 | |
---|
21 | /** |
---|
22 | * Indicator whether we save in the database or not. |
---|
23 | * @var boolean $_perpetual |
---|
24 | */ |
---|
25 | var $_perpetual = false; |
---|
26 | |
---|
27 | /** |
---|
28 | * Hash containing connection parameters. |
---|
29 | * @var array $params |
---|
30 | */ |
---|
31 | var $params = array(); |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * Prefs constructor. |
---|
36 | */ |
---|
37 | function Prefs($dbh=false, $params=array()) |
---|
38 | { |
---|
39 | $this->params = $params; |
---|
40 | $_perpetual = false; // Until database routines are completed. |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * Sets the default value of a preference. The pref will be set only if |
---|
45 | * is not set already. |
---|
46 | * |
---|
47 | * @param string $pref The name of the preference to modify. |
---|
48 | * @param string $val The new value for this preference. |
---|
49 | * @param string $scope The scope for this preference. |
---|
50 | * |
---|
51 | * @return boolean True if the value was successfully set. |
---|
52 | */ |
---|
53 | function setDefault($pref, $val, $scope=null) |
---|
54 | { |
---|
55 | if (!isset($scope)) { |
---|
56 | $scope =& $this->scope; |
---|
57 | } |
---|
58 | |
---|
59 | // No empty values allowed. |
---|
60 | if ('' == $pref || '' == $val || '' == $scope) { |
---|
61 | return false; |
---|
62 | } |
---|
63 | |
---|
64 | // Initialized the prefs array. |
---|
65 | if (!isset($_SESSION['_prefs'])) { |
---|
66 | $_SESSION['_prefs'] = array(); |
---|
67 | } |
---|
68 | |
---|
69 | // In case boolean or null values are passed as a string. |
---|
70 | if ($val == 'true') { |
---|
71 | $val = true; |
---|
72 | } else if ($val == 'false') { |
---|
73 | $val = false; |
---|
74 | } else if ($val == 'null') { |
---|
75 | $val = null; |
---|
76 | } |
---|
77 | |
---|
78 | // Set it only if not set already. |
---|
79 | if (!isset($_SESSION['_prefs'][$scope][$pref])) { |
---|
80 | $_SESSION['_prefs'][$scope][$pref] = $val; |
---|
81 | return true; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Sets the given preferences to the specific value, |
---|
87 | * |
---|
88 | * @param string $pref The name of the preference to modify. |
---|
89 | * @param string $val The new value for this preference. |
---|
90 | * @param string $scope The scope for this preference. |
---|
91 | * |
---|
92 | * @return boolean True if the value was successfully set. |
---|
93 | */ |
---|
94 | function setValue($pref, $val, $scope=null) |
---|
95 | { |
---|
96 | if (!isset($scope)) { |
---|
97 | $scope =& $this->scope; |
---|
98 | } |
---|
99 | |
---|
100 | // No empty values allowed. |
---|
101 | if ('' == $pref || '' == $val || '' == $scope) { |
---|
102 | return false; |
---|
103 | } |
---|
104 | |
---|
105 | // Initialized the prefs array. |
---|
106 | if (!isset($_SESSION['_prefs'])) { |
---|
107 | $_SESSION['_prefs'] = array(); |
---|
108 | } |
---|
109 | |
---|
110 | // In case boolean or null values are passed as a string. |
---|
111 | if ($val == 'true') { |
---|
112 | $val = true; |
---|
113 | } else if ($val == 'false') { |
---|
114 | $val = false; |
---|
115 | } else if ($val == 'null') { |
---|
116 | $val = null; |
---|
117 | } |
---|
118 | |
---|
119 | $_SESSION['_prefs'][$scope][$pref] = $val; |
---|
120 | return true; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Returns the value of the requested preference. |
---|
125 | * |
---|
126 | * @param string $pref The name of the preference to retrieve. |
---|
127 | * @param string $scope The scope for this preference. |
---|
128 | * |
---|
129 | * @return string The value of the preference. |
---|
130 | */ |
---|
131 | function getValue($pref, $scope=null) |
---|
132 | { |
---|
133 | if (!isset($scope)) { |
---|
134 | $scope =& $this->scope; |
---|
135 | } |
---|
136 | |
---|
137 | return (isset($_SESSION['_prefs'][$scope][$pref])) ? $_SESSION['_prefs'][$scope][$pref] : null; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * To see if a preference has been set. |
---|
142 | * |
---|
143 | * @param string $pref The name of the preference to check. |
---|
144 | * @param string $scope The scope for this preference. |
---|
145 | * |
---|
146 | * @return boolean True if the preference isset and not empty |
---|
147 | * false otherwise. |
---|
148 | */ |
---|
149 | function exists($pref, $scope=null) |
---|
150 | { |
---|
151 | if (!isset($scope)) { |
---|
152 | $scope =& $this->scope; |
---|
153 | } |
---|
154 | |
---|
155 | return isset($_SESSION['_prefs'][$scope][$pref]); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Clear a set preference value. |
---|
160 | * |
---|
161 | * @param string $pref The name of the preference to check. |
---|
162 | * @param string $scope The scope for this preference. |
---|
163 | */ |
---|
164 | function clearValue($pref, $scope=null) |
---|
165 | { |
---|
166 | if (!isset($scope)) { |
---|
167 | $scope =& $this->scope; |
---|
168 | } |
---|
169 | |
---|
170 | if (isset($_SESSION['_prefs'][$scope][$pref])) { |
---|
171 | unset($_SESSION['_prefs'][$scope][$pref]); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Retrieves the requested set of preferences from the user's database |
---|
177 | * entry. |
---|
178 | * |
---|
179 | * @param optional array $prefs An array listing the preferences to |
---|
180 | * retrieve. If not specified, retrieve all of the |
---|
181 | * preferences listed in the $prefs hash. |
---|
182 | * |
---|
183 | * @return mixed True on success or a PEAR_Error object on failure. |
---|
184 | */ |
---|
185 | function retrieve() |
---|
186 | { |
---|
187 | // Check that we don't have the preferences loaded yet. |
---|
188 | if ($_SESSION['_prefs']['loaded'] == true || !$_perpetual) { |
---|
189 | return true; |
---|
190 | } |
---|
191 | |
---|
192 | // Database query goes here.... |
---|
193 | |
---|
194 | $_SESSION['_prefs']['loaded'] = true; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Stores preferences to SQL server. |
---|
199 | * |
---|
200 | * @param array $prefs (optional) An array listing the preferences to be |
---|
201 | * stored. If not specified, store all of the |
---|
202 | * preferences listed in the $prefs hash. |
---|
203 | * |
---|
204 | * @return mixed True on success or a PEAR_Error object on failure. |
---|
205 | */ |
---|
206 | function store($prefs=array()) |
---|
207 | { |
---|
208 | // If we are not storing prefs in perpetually. |
---|
209 | if (!$_perpetual) { |
---|
210 | return true; |
---|
211 | } |
---|
212 | |
---|
213 | /* |
---|
214 | * If a list of preferences to store hasn't been provided in |
---|
215 | * $prefs, assume all preferences are desired. |
---|
216 | */ |
---|
217 | if (sizeof($prefs) < 1) { |
---|
218 | $prefs =& $_SESSION['_prefs']; |
---|
219 | } else if ($_SESSION['_prefs']['loaded'] == true) { |
---|
220 | $prefs = array_merge($_SESSION['_prefs'], $prefs); |
---|
221 | } |
---|
222 | |
---|
223 | if (!is_array($prefs) || sizeof($prefs) < 1) { |
---|
224 | raiseMsg(_("No preferences are available."), MSG_ERR, __FILE__, __LINE__); |
---|
225 | dieBoomerangURL(); |
---|
226 | } |
---|
227 | |
---|
228 | // Database query goes here.... |
---|
229 | |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * Perform cleanup operations. |
---|
234 | */ |
---|
235 | function cleanup() |
---|
236 | { |
---|
237 | $_SESSION['_prefs'] = array(); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | ?> |
---|