Changeset 152 for trunk/lib/Prefs.inc.php
- Timestamp:
- Jun 7, 2006 5:35:16 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Prefs.inc.php
r151 r152 14 14 // Load preferences for the user's session. 15 15 require_once 'codebase/lib/Prefs.inc.php'; 16 $prefs = new Prefs('bURB');16 $prefs =& Prefs::getInstance('my-namespace'); 17 17 $prefs->setParam(array( 18 ' enable_db' => $auth->isLoggedIn(),18 'persistent' => $auth->isLoggedIn(), 19 19 'user_id' => $auth->get('user_id'), 20 20 )); … … 37 37 var $_ns; 38 38 39 // Configuration ofthis object.39 // Configuration parameters for this object. 40 40 var $_params = array( 41 41 42 // The current user_id for which to load/save preferences. 42 // Enable database storage. If this is false, all prefs will live only as long as the session. 43 'persistent' => false, 44 45 // The current user_id for which to load/save persistent preferences. 43 46 'user_id' => null, 44 47 45 // How long before we force a reload of the p refs data? 3600 = once every hour.48 // How long before we force a reload of the persistent prefs data? 3600 = once every hour. 46 49 'load_timeout' => 3600, 47 50 48 // Enable database storage. 49 'enable_db' => true, 50 51 // Name of database table to store prefs. 51 // Name of database table to store persistent prefs. 52 52 'db_table' => 'pref_tbl', 53 53 … … 74 74 $this->setParam(array('create_table' => $app->getParam('db_create_tables'))); 75 75 } 76 } 77 78 /** 79 * This method enforces the singleton pattern for this class. 80 * 81 * @return object Reference to the global Prefs object. 82 * @access public 83 * @static 84 */ 85 function &getInstance($namespace='') 86 { 87 static $instances = array(); 88 89 if (!array_key_exists($namespace, $instances)) { 90 $instances[$namespace] = new Prefs($namespace); 91 } 92 93 return $instances[$namespace]; 76 94 } 77 95 … … 167 185 * This function determins what data is saved to the database. Ensure clean values! 168 186 * 169 * @param string $key The name of the preference to modify. 170 * @param string $val The new value for this preference. 187 * @param string $key The name of the preference to modify. 188 * @param string $val The new value for this preference. 189 * @param bool $persistent Save this value forever? Set to false and value will exist as long as the session is in use. 171 190 */ 172 191 function set($key, $val) 173 192 { 174 193 $app =& App::getInstance(); 194 175 195 if ('' == $key) { 176 196 $app->logMsg(sprintf('Key is empty (provided with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__); 177 197 return false; 178 198 } 199 179 200 if (!isset($_SESSION[$this->_ns]['defaults'][$key]) || $_SESSION[$this->_ns]['defaults'][$key] != $val || isset($_SESSION[$this->_ns]['persistent'][$key])) { 180 201 $_SESSION[$this->_ns]['persistent'][$key] = $val; … … 193 214 { 194 215 $app =& App::getInstance(); 195 if ( isset($_SESSION[$this->_ns]['persistent'][$key])) {216 if (array_key_exists($key, $_SESSION[$this->_ns]['persistent'])) { 196 217 return $_SESSION[$this->_ns]['persistent'][$key]; 197 } else if ( isset($_SESSION[$this->_ns]['defaults'][$key])) {218 } else if (array_key_exists($key, $_SESSION[$this->_ns]['defaults'])) { 198 219 return $_SESSION[$this->_ns]['defaults'][$key]; 199 220 } else { 200 $app->logMsg(sprintf('Key not defined in default or persistentprefs cache: %s', $key), LOG_NOTICE, __FILE__, __LINE__);221 $app->logMsg(sprintf('Key not found in prefs cache: %s', $key), LOG_NOTICE, __FILE__, __LINE__); 201 222 return null; 202 223 } … … 204 225 205 226 /** 206 * To see if a p ersistent preference has been set.227 * To see if a preference has been set. 207 228 * 208 229 * @param string $key The name of the preference to check. … … 211 232 function exists($key) 212 233 { 213 return isset($_SESSION[$this->_ns]['persistent'][$key]);234 return array_key_exists($key, $_SESSION[$this->_ns]['persistent']); 214 235 } 215 236 … … 254 275 255 276 // Skip this method if not using the db. 256 if (true !== $this->getParam(' enable_db')) {277 if (true !== $this->getParam('persistent')) { 257 278 return true; 258 279 } … … 332 353 333 354 // Skip this method if not using the db. 334 if (true !== $this->getParam(' enable_db')) {355 if (true !== $this->getParam('persistent')) { 335 356 return true; 336 357 }
Note: See TracChangeset
for help on using the changeset viewer.