Changeset 151 for trunk/lib/Prefs.inc.php
- Timestamp:
- Jun 6, 2006 3:19:17 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Prefs.inc.php
r150 r151 12 12 * Example of use: 13 13 --------------------------------------------------------------------- 14 // Load preferences for the user .14 // Load preferences for the user's session. 15 15 require_once 'codebase/lib/Prefs.inc.php'; 16 16 $prefs = new Prefs('bURB'); … … 24 24 )); 25 25 $prefs->load(); 26 27 // Update preferences. Make sure to validate this input first! 28 $prefs->set('search_num_results', getFormData('search_num_results')); 29 $prefs->set('datalog_num_entries', getFormData('datalog_num_entries')); 30 $prefs->save(); 31 26 32 --------------------------------------------------------------------- 27 33 */ … … 60 66 61 67 // Initialized the prefs array. 62 if (!isset($_SESSION[$this->_ns] ['data'])) {68 if (!isset($_SESSION[$this->_ns])) { 63 69 $this->clear(); 64 70 } … … 143 149 144 150 /** 145 * Sets the default value of a preference. The pref will be set only if 146 * is not set already. 151 * Sets the default values for preferences. If a preference is not explicitly 152 * set, the value set here will be used. Can be called multiple times to merge additional 153 * defaults together. 154 * 155 * @param array $defaults Array of key-value pairs 156 */ 157 function setDefaults($defaults) 158 { 159 if (isset($defaults) && is_array($defaults)) { 160 $_SESSION[$this->_ns]['defaults'] = array_merge($_SESSION[$this->_ns]['defaults'], $defaults); 161 } 162 } 163 164 /** 165 * Store a key-value pair in the session. If the value is different than what is set by setDefaults 166 * the value will be scheduled to be saved in the database. 167 * This function determins what data is saved to the database. Ensure clean values! 147 168 * 148 169 * @param string $key The name of the preference to modify. 149 170 * @param string $val The new value for this preference. 150 171 */ 151 function setDefaults($defaults)152 {153 if (isset($defaults) && is_array($defaults)) {154 // Apply defaults to the session, setting only non-existing values.155 $_SESSION[$this->_ns]['data'] = array_merge($defaults, $_SESSION[$this->_ns]['data']);156 }157 }158 159 /**160 * Sets the given preferences to the specific value,161 *162 * @param string $key The name of the preference to modify.163 * @param string $val The new value for this preference.164 */165 172 function set($key, $val) 166 173 { 167 if ('' != $key && '' != $val) { 168 $_SESSION[$this->_ns]['data'][$key] = $val; 169 } 170 } 171 172 /** 173 * Returns the value of the requested preference. 174 $app =& App::getInstance(); 175 if ('' == $key) { 176 $app->logMsg(sprintf('Key is empty (provided with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__); 177 return false; 178 } 179 if (!isset($_SESSION[$this->_ns]['defaults'][$key]) || $_SESSION[$this->_ns]['defaults'][$key] != $val || isset($_SESSION[$this->_ns]['persistent'][$key])) { 180 $_SESSION[$this->_ns]['persistent'][$key] = $val; 181 } 182 } 183 184 /** 185 * Returns the value of the requested preference. Persistent values take precedence, but if none is set 186 * a default value is returned, or if not that, null. 174 187 * 175 188 * @param string $key The name of the preference to retrieve. … … 179 192 function get($key) 180 193 { 181 return (isset($_SESSION[$this->_ns]['data'][$key])) ? $_SESSION[$this->_ns]['data'][$key] : null; 182 } 183 184 /** 185 * To see if a preference has been set. 194 $app =& App::getInstance(); 195 if (isset($_SESSION[$this->_ns]['persistent'][$key])) { 196 return $_SESSION[$this->_ns]['persistent'][$key]; 197 } else if (isset($_SESSION[$this->_ns]['defaults'][$key])) { 198 return $_SESSION[$this->_ns]['defaults'][$key]; 199 } else { 200 $app->logMsg(sprintf('Key not defined in default or persistent prefs cache: %s', $key), LOG_NOTICE, __FILE__, __LINE__); 201 return null; 202 } 203 } 204 205 /** 206 * To see if a persistent preference has been set. 186 207 * 187 208 * @param string $key The name of the preference to check. 188 * 189 * @return boolean True if the preference isset and not empty 190 * false otherwise. 209 * @return boolean True if the preference isset and not empty false otherwise. 191 210 */ 192 211 function exists($key) 193 212 { 194 return isset($_SESSION[$this->_ns][' data'][$key]);195 } 196 197 /** 198 * Clear a set preference value. 213 return isset($_SESSION[$this->_ns]['persistent'][$key]); 214 } 215 216 /** 217 * Clear a set preference value. This will also remove the value from the database. 199 218 * 200 219 * @param string $key The name of the preference to delete. … … 202 221 function delete($key) 203 222 { 204 unset($_SESSION[$this->_ns]['data'][$key]); 205 } 206 207 /** 208 * Empty the $_SESSION cache. This should be executed with the same consideration as $auth->clear() 223 unset($_SESSION[$this->_ns]['persistent'][$key]); 224 } 225 226 /** 227 * Resets the $_SESSION cache. This should be executed with the same consideration 228 * as $auth->clear(), such as when logging out. 209 229 */ 210 230 function clear() … … 213 233 'loaded' => false, 214 234 'load_datetime' => '1970-01-01', 215 'data' => array() 235 'defaults' => array(), 236 'persistent' => array(), 216 237 ); 217 238 } 218 239 219 240 /* 220 * Retr eives all prefs from the database and stores them in the $_SESSION.241 * Retrieves all prefs from the database and stores them in the $_SESSION. 221 242 * 222 243 * @access public … … 246 267 // User_id must not be empty. 247 268 if ('' == $this->getParam('user_id')) { 248 $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_ ERR, __FILE__, __LINE__);269 $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__); 249 270 return false; 250 271 } … … 253 274 $this->clear(); 254 275 255 // Retr eive all prefs for this user and namespace.276 // Retrieve all prefs for this user and namespace. 256 277 $qid = $db->query(" 257 278 SELECT pref_key, pref_value … … 262 283 "); 263 284 while (list($key, $val) = mysql_fetch_row($qid)) { 264 $_SESSION[$this->_ns][' data'][$key] = $val;285 $_SESSION[$this->_ns]['persistent'][$key] = $val; 265 286 } 266 287 267 $app->logMsg(sprintf(' %s prefs loaded from database.', sizeof($_SESSION[$this->_ns]['data'])), LOG_DEBUG, __FILE__, __LINE__);288 $app->logMsg(sprintf('Loaded %s prefs from database.', sizeof($_SESSION[$this->_ns]['persistent'])), LOG_DEBUG, __FILE__, __LINE__); 268 289 269 290 // Data loaded only once per session. … … 317 338 // User_id must not be empty. 318 339 if ('' == $this->getParam('user_id')) { 319 $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_ ERR, __FILE__, __LINE__);340 $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__); 320 341 return false; 321 342 } … … 323 344 $this->initDB(); 324 345 325 if (isset($_SESSION[$this->_ns][' data']) && is_array($_SESSION[$this->_ns]['data'])) {346 if (isset($_SESSION[$this->_ns]['persistent']) && is_array($_SESSION[$this->_ns]['persistent'])) { 326 347 // Delete old prefs from database. 327 348 $db->query(" … … 333 354 // Insert new prefs. 334 355 $insert_values = array(); 335 foreach ($_SESSION[$this->_ns]['data'] as $key => $val) { 336 if ('' != trim($key) && '' != trim($val)) { 337 $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", $db->escapeString($this->getParam('user_id')), $db->escapeString($this->_ns), $db->escapeString($key), $db->escapeString($val)); 338 } 356 foreach ($_SESSION[$this->_ns]['persistent'] as $key => $val) { 357 $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", $db->escapeString($this->getParam('user_id')), $db->escapeString($this->_ns), $db->escapeString($key), $db->escapeString($val)); 339 358 } 340 359 $db->query(" … … 344 363 "); 345 364 346 $app->logMsg(sprintf('Saved %s pref erences to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);365 $app->logMsg(sprintf('Saved %s prefs to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__); 347 366 return true; 348 367 }
Note: See TracChangeset
for help on using the changeset viewer.