Changeset 150


Ignore:
Timestamp:
Jun 5, 2006 3:05:39 AM (18 years ago)
Author:
scdev
Message:

Q - okay now finished with Prefs.inc.php; it is sweeeet!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Prefs.inc.php

    r149 r150  
    99 * @author  Quinn Comendant <quinn@strangecode.com>
    1010 * @version 2.1
     11 *
     12 * Example of use:
     13---------------------------------------------------------------------
     14// Load preferences for the user.
     15require_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---------------------------------------------------------------------
    1127 */
    1228class Prefs {
     
    4460       
    4561        // 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();
    4864        }
    4965
     
    135151    function setDefaults($defaults)
    136152    {
    137         $app =& App::getInstance();
    138153        if (isset($defaults) && is_array($defaults)) {
    139154            // 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__);
    141155            $_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__);
    143156        }
    144157    }
     
    197210    function clear()
    198211    {
    199         $_SESSION[$this->_ns] = array();
     212        $_SESSION[$this->_ns] = array(
     213            'loaded' => false,
     214            'load_datetime' => '1970-01-01',
     215            'data' => array()
     216        );
    200217    }
    201218   
     
    204221    *
    205222    * @access   public
     223    * @param    bool    $force  Set to always load from database, regardless if _isLoaded() or not.
    206224    * @return   bool    True if loading succeeded.
    207225    * @author   Quinn Comendant <quinn@strangecode.com>
     
    209227    * @since    04 Jun 2006 16:56:53
    210228    */
    211     function load()
     229    function load($force=false)
    212230    {
    213231        $app =& App::getInstance();
     
    219237        }
    220238
     239        $this->initDB();
     240
    221241        // Prefs already loaded for this session.
    222         if ($this->_isLoaded()) {
     242        if (!$force && $this->_isLoaded()) {
    223243            return true;
    224244        }
     
    229249            return false;
    230250        }
    231 
    232         $this->initDB();
     251       
     252        // Clear existing cache.
     253        $this->clear();
    233254       
    234255        // Retreive all prefs for this user and namespace.
     
    243264            $_SESSION[$this->_ns]['data'][$key] = $val;
    244265        }
     266       
     267        $app->logMsg(sprintf('%s prefs loaded from database.', sizeof($_SESSION[$this->_ns]['data'])), LOG_DEBUG, __FILE__, __LINE__);
    245268       
    246269        // Data loaded only once per session.
Note: See TracChangeset for help on using the changeset viewer.