Changeset 150 for trunk/lib/Prefs.inc.php
- Timestamp:
- Jun 5, 2006 3:05:39 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Prefs.inc.php
r149 r150 9 9 * @author Quinn Comendant <quinn@strangecode.com> 10 10 * @version 2.1 11 * 12 * Example of use: 13 --------------------------------------------------------------------- 14 // Load preferences for the user. 15 require_once 'codebase/lib/Prefs.inc.php'; 16 $prefs = new Prefs('bURB'); 17 $prefs->setParam(array( 18 'enable_db' => $auth->isLoggedIn(), 19 'user_id' => $auth->get('user_id'), 20 )); 21 $prefs->setDefaults(array( 22 'search_num_results' => 25, 23 'datalog_num_entries' => 25, 24 )); 25 $prefs->load(); 26 --------------------------------------------------------------------- 11 27 */ 12 28 class Prefs { … … 44 60 45 61 // Initialized the prefs array. 46 if (!isset($_SESSION[$this->_ns] )) {47 $ _SESSION[$this->_ns] = array('loaded' => false, 'data' => array());62 if (!isset($_SESSION[$this->_ns]['data'])) { 63 $this->clear(); 48 64 } 49 65 … … 135 151 function setDefaults($defaults) 136 152 { 137 $app =& App::getInstance();138 153 if (isset($defaults) && is_array($defaults)) { 139 154 // Apply defaults to the session, setting only non-existing values. 140 $app->logMsg(sprintf('Pre-defaulting: %s', getDump($_SESSION[$this->_ns]['data'])), LOG_DEBUG, __FILE__, __LINE__);141 155 $_SESSION[$this->_ns]['data'] = array_merge($defaults, $_SESSION[$this->_ns]['data']); 142 $app->logMsg(sprintf('Post-defaulting: %s', getDump($_SESSION[$this->_ns]['data'])), LOG_DEBUG, __FILE__, __LINE__);143 156 } 144 157 } … … 197 210 function clear() 198 211 { 199 $_SESSION[$this->_ns] = array(); 212 $_SESSION[$this->_ns] = array( 213 'loaded' => false, 214 'load_datetime' => '1970-01-01', 215 'data' => array() 216 ); 200 217 } 201 218 … … 204 221 * 205 222 * @access public 223 * @param bool $force Set to always load from database, regardless if _isLoaded() or not. 206 224 * @return bool True if loading succeeded. 207 225 * @author Quinn Comendant <quinn@strangecode.com> … … 209 227 * @since 04 Jun 2006 16:56:53 210 228 */ 211 function load( )229 function load($force=false) 212 230 { 213 231 $app =& App::getInstance(); … … 219 237 } 220 238 239 $this->initDB(); 240 221 241 // Prefs already loaded for this session. 222 if ( $this->_isLoaded()) {242 if (!$force && $this->_isLoaded()) { 223 243 return true; 224 244 } … … 229 249 return false; 230 250 } 231 232 $this->initDB(); 251 252 // Clear existing cache. 253 $this->clear(); 233 254 234 255 // Retreive all prefs for this user and namespace. … … 243 264 $_SESSION[$this->_ns]['data'][$key] = $val; 244 265 } 266 267 $app->logMsg(sprintf('%s prefs loaded from database.', sizeof($_SESSION[$this->_ns]['data'])), LOG_DEBUG, __FILE__, __LINE__); 245 268 246 269 // Data loaded only once per session.
Note: See TracChangeset
for help on using the changeset viewer.