Ignore:
Timestamp:
Jun 7, 2006 5:35:16 AM (18 years ago)
Author:
scdev
Message:

Q - In the middle of working on the Prefs and Cache instantiation mode...can't decide to use singleton pattern or global vars. Updated ImageThumb? to allow filenames with path elements such as 01/23/4567_file.jpg.

File:
1 edited

Legend:

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

    r151 r152  
    1414// Load preferences for the user's session.
    1515require_once 'codebase/lib/Prefs.inc.php';
    16 $prefs = new Prefs('bURB');
     16$prefs =& Prefs::getInstance('my-namespace');
    1717$prefs->setParam(array(
    18     'enable_db' => $auth->isLoggedIn(),
     18    'persistent' => $auth->isLoggedIn(),
    1919    'user_id' => $auth->get('user_id'),
    2020));
     
    3737    var $_ns;
    3838
    39     // Configuration of this object.
     39    // Configuration parameters for this object.
    4040    var $_params = array(
    4141       
    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.
    4346        'user_id' => null,
    4447       
    45         // How long before we force a reload of the prefs data? 3600 = once every hour.
     48        // How long before we force a reload of the persistent prefs data? 3600 = once every hour.
    4649        'load_timeout' => 3600,
    4750       
    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.
    5252        'db_table' => 'pref_tbl',
    5353
     
    7474            $this->setParam(array('create_table' => $app->getParam('db_create_tables')));
    7575        }
     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];
    7694    }
    7795
     
    167185     * This function determins what data is saved to the database. Ensure clean values!
    168186     *
    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.
    171190     */
    172191    function set($key, $val)
    173192    {
    174193        $app =& App::getInstance();
     194
    175195        if ('' == $key) {
    176196            $app->logMsg(sprintf('Key is empty (provided with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__);
    177197            return false;
    178198        }
     199       
    179200        if (!isset($_SESSION[$this->_ns]['defaults'][$key]) || $_SESSION[$this->_ns]['defaults'][$key] != $val || isset($_SESSION[$this->_ns]['persistent'][$key])) {
    180201            $_SESSION[$this->_ns]['persistent'][$key] = $val;           
     
    193214    {
    194215        $app =& App::getInstance();
    195         if (isset($_SESSION[$this->_ns]['persistent'][$key])) {
     216        if (array_key_exists($key, $_SESSION[$this->_ns]['persistent'])) {
    196217            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'])) {
    198219            return $_SESSION[$this->_ns]['defaults'][$key];
    199220        } else {
    200             $app->logMsg(sprintf('Key not defined in default or persistent prefs cache: %s', $key), LOG_NOTICE, __FILE__, __LINE__);
     221            $app->logMsg(sprintf('Key not found in prefs cache: %s', $key), LOG_NOTICE, __FILE__, __LINE__);
    201222            return null;
    202223        }
     
    204225
    205226    /**
    206      * To see if a persistent preference has been set.
     227     * To see if a preference has been set.
    207228     *
    208229     * @param string $key       The name of the preference to check.
     
    211232    function exists($key)
    212233    {
    213         return isset($_SESSION[$this->_ns]['persistent'][$key]);
     234        return array_key_exists($key, $_SESSION[$this->_ns]['persistent']);
    214235    }
    215236
     
    254275       
    255276        // Skip this method if not using the db.
    256         if (true !== $this->getParam('enable_db')) {
     277        if (true !== $this->getParam('persistent')) {
    257278            return true;
    258279        }
     
    332353       
    333354        // Skip this method if not using the db.
    334         if (true !== $this->getParam('enable_db')) {
     355        if (true !== $this->getParam('persistent')) {
    335356            return true;
    336357        }
Note: See TracChangeset for help on using the changeset viewer.