Changeset 478 for trunk/lib/Prefs.inc.php
- Timestamp:
- May 3, 2014 9:13:05 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Prefs.inc.php
r477 r478 24 24 * Prefs.inc.php 25 25 * 26 * Prefs provides an API for saving arbitrary values in a user's session .27 * Session prefs can be stored into a database with the optional save() and load() methods.26 * Prefs provides an API for saving arbitrary values in a user's session, in cookies, and in the database. 27 * Prefs can be stored into a database with the optional save() and load() methods. 28 28 * 29 29 * @author Quinn Comendant <quinn@strangecode.com> 30 * @version 2.130 * @version 3.0 31 31 * 32 32 * Example of use (database storagetype): … … 98 98 * Prefs constructor. 99 99 */ 100 public function __construct($namespace='' )100 public function __construct($namespace='', array $params=null) 101 101 { 102 102 $app =& App::getInstance(); 103 103 104 104 $this->_ns = $namespace; 105 106 // Initialized the prefs array.107 if (!isset($_SESSION['_prefs'][$this->_ns]['saved'])) {108 $this->clear();109 }110 105 111 106 // Get create tables config from global context. 112 107 if (!is_null($app->getParam('db_create_tables'))) { 113 108 $this->setParam(array('create_table' => $app->getParam('db_create_tables'))); 109 } 110 111 // Optional initial params. 112 $this->setParam($params); 113 114 // Run Prefs->save() upon script completion if we're using the database storagetype. 115 // This only works if 'storagetype' is provided as a parameter to the constructor rather than via setParam() later. 116 if ('database' == $this->getParam('storagetype')) { 117 register_shutdown_function(array($this, 'save')); 114 118 } 115 119 } … … 160 164 * @param array $params Array of param keys and values to set. 161 165 */ 162 public function setParam( $params=null)166 public function setParam(array $params=null) 163 167 { 164 168 // CLI scripts can't use prefs stored in HTTP-based protocols. … … 191 195 $app =& App::getInstance(); 192 196 193 if ( isset($this->_params[$param])) {197 if (array_key_exists($param, $this->_params)) { 194 198 return $this->_params[$param]; 195 199 } else { … … 202 206 * Sets the default values for preferences. If a preference is not explicitly 203 207 * set, the value set here will be used. Can be called multiple times to merge additional 204 * defaults together. 208 * defaults together. This is mostly only useful for the database storetype, when you have 209 * values you want to use as default, and those are not stored to the database (so the defaults 210 * can be changed later and apply to all users who haven't make s specific setting). 211 * For the cookie storetype, using setDefaults just sets cookies but only if a cookie with 212 * the same name is not already set. 205 213 * 206 214 * @param array $defaults Array of key-value pairs … … 208 216 public function setDefaults($defaults) 209 217 { 218 $app =& App::getInstance(); 219 210 220 if (isset($defaults) && is_array($defaults)) { 211 $_SESSION['_prefs'][$this->_ns]['defaults'] = array_merge($_SESSION['_prefs'][$this->_ns]['defaults'], $defaults); 212 } 213 } 214 215 /** 216 * Store a key-value pair in the session. If the value is different than what is set by setDefaults 217 * the value will be scheduled to be saved in the database. 218 * This function determines what data is saved to the database. Ensure clean values! 221 switch ($this->getParam('storagetype')) { 222 case 'session': 223 case 'database': 224 $_SESSION['_prefs'][$this->_ns]['defaults'] = array_merge($_SESSION['_prefs'][$this->_ns]['defaults'], $defaults); 225 break; 226 227 case 'cookie': 228 foreach ($defaults as $key => $val) { 229 if (!$this->exists($key)) { 230 $this->set($key, $val); 231 } 232 } 233 unset($key, $val); 234 break; 235 } 236 } else { 237 $app->logMsg(sprintf('Wrong data-type passed to Prefs->setDefaults().', null), LOG_NOTICE, __FILE__, __LINE__); 238 } 239 } 240 241 /** 242 * Store a key-value pair. 243 * When using the database storagetype, if the value is different than what is set by setDefaults the value will be scheduled to be saved in the database. 219 244 * 220 245 * @param string $key The name of the preference to modify. … … 238 263 } 239 264 240 // Set a saved preference if...241 // - there isn't a default.242 // - or the new value is different than the default243 // - or there is a previously existing saved key.244 265 switch ($this->getParam('storagetype')) { 266 // Both session and database prefs are saved in the session (for database, only temporarily until they are saved). 245 267 case 'session': 246 case 'database': // DB prefs are saved in the session temporarily until they are saved. 247 if (!isset($_SESSION['_prefs'][$this->_ns]['defaults'][$key]) 268 case 'database': 269 // Initialized the prefs array. 270 if (!isset($_SESSION['_prefs'][$this->_ns]['saved'])) { 271 $this->clear(); 272 } 273 // Set a saved preference if... 274 // - there isn't a default. 275 // - or the new value is different than the default 276 // - or there is a previously existing saved key. 277 if (!array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['defaults']) 248 278 || $_SESSION['_prefs'][$this->_ns]['defaults'][$key] != $val 249 || isset($_SESSION['_prefs'][$this->_ns]['saved'][$key])) {279 || array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved'])) { 250 280 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val; 251 281 $app->logMsg(sprintf('Setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); 252 282 } else { 253 283 $app->logMsg(sprintf('Not setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); … … 259 289 $val = json_encode($val); 260 290 $app->setCookie($name, $val, $this->getParam('cookie_expire'), $this->getParam('cookie_path'), $this->getParam('cookie_domain')); 291 $_COOKIE[$name] = $val; 261 292 $app->logMsg(sprintf('Setting cookie preference %s => %s', $key, $val), LOG_DEBUG, __FILE__, __LINE__); 262 293 break; … … 321 352 case 'cookie': 322 353 $name = $this->_getCookieName($key); 323 return isset($_COOKIE[$name]);324 } 325 326 } 327 328 /** 329 * Clear a set preference value. This will also remove the value from the database.354 return array_key_exists($name, $_COOKIE); 355 } 356 357 } 358 359 /** 360 * Delete an existing preference value. This will also remove the value from the database, once save() is called. 330 361 * 331 362 * @param string $key The name of the preference to delete. … … 355 386 356 387 /** 357 * Resets the $_SESSION cache. This should be executed with the same consideration 358 * as $auth->clear(), such as when logging out. 359 */ 360 public function clear($focus='all') 361 { 362 363 switch ($focus) { 388 * Resets all existing values under this namespace. This should be executed with the same consideration as $auth->clear(), such as when logging out. 389 */ 390 public function clear($scope='all') 391 { 392 switch ($scope) { 364 393 case 'all' : 365 394 switch ($this->getParam('storagetype')) { … … 376 405 foreach ($_COOKIE as $key => $value) { 377 406 // All cookie keys with our internal prefix. Use only the last part as the key. 378 if (preg_match('/^' . preg_quote(sprintf(' strangecode-%s-', $this->_ns)) . '(.+)$/i', $key, $match)) {407 if (preg_match('/^' . preg_quote(sprintf('_prefs-%s-', $this->_ns)) . '(.+)$/i', $key, $match)) { 379 408 $this->delete($match[1]); 380 409 } … … 383 412 } 384 413 break; 414 385 415 case 'defaults' : 386 416 $_SESSION['_prefs'][$this->_ns]['defaults'] = array(); 387 417 break; 418 388 419 case 'saved' : 389 420 $_SESSION['_prefs'][$this->_ns]['saved'] = array(); … … 409 440 // Skip this method if not using the db. 410 441 if ('database' != $this->getParam('storagetype')) { 442 $app->logMsg('Prefs->load() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); 411 443 return true; 412 444 } … … 462 494 { 463 495 if ('database' != $this->getParam('storagetype')) { 496 $app->logMsg('Prefs->_isLoaded() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); 464 497 return true; 465 498 } … … 491 524 // Skip this method if not using the db. 492 525 if ('database' != $this->getParam('storagetype')) { 526 $app->logMsg('Prefs->save() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); 493 527 return true; 494 528 } … … 548 582 $app =& App::getInstance(); 549 583 550 if (mb_strpos($key, sprintf('strangecode-%s', $this->_ns)) === 0) { 551 $app->logMsg(sprintf('Invalid key name (%s). Leave off "strangecode-%s-" and it should work.', $key, $this->_ns), LOG_NOTICE, __FILE__, __LINE__); 552 } 553 return sprintf('strangecode-%s-%s', $this->_ns, $key); 584 if (mb_strpos($key, sprintf('_prefs-%s', $this->_ns)) === 0) { 585 $app->logMsg(sprintf('Invalid key name (%s). Leave off "_prefs-%s-" and it should work.', $key, $this->_ns), LOG_NOTICE, __FILE__, __LINE__); 586 } 587 // Use standardized class data names: _ + classname + namespace + variablekey 588 return sprintf('_prefs-%s-%s', $this->_ns, $key); 554 589 } 555 590 }
Note: See TracChangeset
for help on using the changeset viewer.