Ignore:
Timestamp:
Jun 6, 2006 3:19:17 AM (18 years ago)
Author:
scdev
Message:

Q - Changed one more SessionCache? -> Cache, small bug fixt to Prefs, added operation help to modulemaker scripts.

File:
1 edited

Legend:

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

    r150 r151  
    1212 * Example of use:
    1313---------------------------------------------------------------------
    14 // Load preferences for the user.
     14// Load preferences for the user's session.
    1515require_once 'codebase/lib/Prefs.inc.php';
    1616$prefs = new Prefs('bURB');
     
    2424));
    2525$prefs->load();
     26
     27// Update preferences. Make sure to validate this input first!
     28$prefs->set('search_num_results', getFormData('search_num_results'));
     29$prefs->set('datalog_num_entries', getFormData('datalog_num_entries'));
     30$prefs->save();
     31
    2632---------------------------------------------------------------------
    2733 */
     
    6066       
    6167        // Initialized the prefs array.
    62         if (!isset($_SESSION[$this->_ns]['data'])) {
     68        if (!isset($_SESSION[$this->_ns])) {
    6369            $this->clear();
    6470        }
     
    143149
    144150    /**
    145      * Sets the default value of a preference. The pref will be set only if
    146      * is not set already.
     151     * Sets the default values for preferences. If a preference is not explicitly
     152     * set, the value set here will be used. Can be called multiple times to merge additional
     153     * defaults together.
     154     *
     155     * @param  array $defaults  Array of key-value pairs
     156     */
     157    function setDefaults($defaults)
     158    {
     159        if (isset($defaults) && is_array($defaults)) {
     160            $_SESSION[$this->_ns]['defaults'] = array_merge($_SESSION[$this->_ns]['defaults'], $defaults);
     161        }
     162    }
     163
     164    /**
     165     * Store a key-value pair in the session. If the value is different than what is set by setDefaults
     166     * the value will be scheduled to be saved in the database.
     167     * This function determins what data is saved to the database. Ensure clean values!
    147168     *
    148169     * @param  string $key       The name of the preference to modify.
    149170     * @param  string $val       The new value for this preference.
    150171     */
    151     function setDefaults($defaults)
    152     {
    153         if (isset($defaults) && is_array($defaults)) {
    154             // Apply defaults to the session, setting only non-existing values.
    155             $_SESSION[$this->_ns]['data'] = array_merge($defaults, $_SESSION[$this->_ns]['data']);
    156         }
    157     }
    158 
    159     /**
    160      * Sets the given preferences to the specific value,
    161      *
    162      * @param  string $key       The name of the preference to modify.
    163      * @param  string $val       The new value for this preference.
    164      */
    165172    function set($key, $val)
    166173    {
    167         if ('' != $key && '' != $val) {
    168             $_SESSION[$this->_ns]['data'][$key] = $val;
    169         }
    170     }
    171 
    172     /**
    173      * Returns the value of the requested preference.
     174        $app =& App::getInstance();
     175        if ('' == $key) {
     176            $app->logMsg(sprintf('Key is empty (provided with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__);
     177            return false;
     178        }
     179        if (!isset($_SESSION[$this->_ns]['defaults'][$key]) || $_SESSION[$this->_ns]['defaults'][$key] != $val || isset($_SESSION[$this->_ns]['persistent'][$key])) {
     180            $_SESSION[$this->_ns]['persistent'][$key] = $val;           
     181        }
     182    }
     183
     184    /**
     185     * Returns the value of the requested preference. Persistent values take precedence, but if none is set
     186     * a default value is returned, or if not that, null.
    174187     *
    175188     * @param string $key       The name of the preference to retrieve.
     
    179192    function get($key)
    180193    {
    181         return (isset($_SESSION[$this->_ns]['data'][$key])) ? $_SESSION[$this->_ns]['data'][$key] : null;
    182     }
    183 
    184     /**
    185      * To see if a preference has been set.
     194        $app =& App::getInstance();
     195        if (isset($_SESSION[$this->_ns]['persistent'][$key])) {
     196            return $_SESSION[$this->_ns]['persistent'][$key];
     197        } else if (isset($_SESSION[$this->_ns]['defaults'][$key])) {
     198            return $_SESSION[$this->_ns]['defaults'][$key];
     199        } else {
     200            $app->logMsg(sprintf('Key not defined in default or persistent prefs cache: %s', $key), LOG_NOTICE, __FILE__, __LINE__);
     201            return null;
     202        }
     203    }
     204
     205    /**
     206     * To see if a persistent preference has been set.
    186207     *
    187208     * @param string $key       The name of the preference to check.
    188      *
    189      * @return boolean          True if the preference isset and not empty
    190      *                          false otherwise.
     209     * @return boolean          True if the preference isset and not empty false otherwise.
    191210     */
    192211    function exists($key)
    193212    {
    194         return isset($_SESSION[$this->_ns]['data'][$key]);
    195     }
    196 
    197     /**
    198      * Clear a set preference value.
     213        return isset($_SESSION[$this->_ns]['persistent'][$key]);
     214    }
     215
     216    /**
     217     * Clear a set preference value. This will also remove the value from the database.
    199218     *
    200219     * @param string $key       The name of the preference to delete.
     
    202221    function delete($key)
    203222    {
    204         unset($_SESSION[$this->_ns]['data'][$key]);
    205     }
    206 
    207     /**
    208      * Empty the $_SESSION cache. This should be executed with the same consideration as $auth->clear()
     223        unset($_SESSION[$this->_ns]['persistent'][$key]);
     224    }
     225
     226    /**
     227     * Resets the $_SESSION cache. This should be executed with the same consideration
     228     * as $auth->clear(), such as when logging out.
    209229     */
    210230    function clear()
     
    213233            'loaded' => false,
    214234            'load_datetime' => '1970-01-01',
    215             'data' => array()
     235            'defaults' => array(),
     236            'persistent' => array(),
    216237        );
    217238    }
    218239   
    219240    /*
    220     * Retreives all prefs from the database and stores them in the $_SESSION.
     241    * Retrieves all prefs from the database and stores them in the $_SESSION.
    221242    *
    222243    * @access   public
     
    246267        // User_id must not be empty.
    247268        if ('' == $this->getParam('user_id')) {
    248             $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_ERR, __FILE__, __LINE__);
     269            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
    249270            return false;
    250271        }
     
    253274        $this->clear();
    254275       
    255         // Retreive all prefs for this user and namespace.
     276        // Retrieve all prefs for this user and namespace.
    256277        $qid = $db->query("
    257278            SELECT pref_key, pref_value
     
    262283        ");
    263284        while (list($key, $val) = mysql_fetch_row($qid)) {
    264             $_SESSION[$this->_ns]['data'][$key] = $val;
     285            $_SESSION[$this->_ns]['persistent'][$key] = $val;
    265286        }
    266287       
    267         $app->logMsg(sprintf('%s prefs loaded from database.', sizeof($_SESSION[$this->_ns]['data'])), LOG_DEBUG, __FILE__, __LINE__);
     288        $app->logMsg(sprintf('Loaded %s prefs from database.', sizeof($_SESSION[$this->_ns]['persistent'])), LOG_DEBUG, __FILE__, __LINE__);
    268289       
    269290        // Data loaded only once per session.
     
    317338        // User_id must not be empty.
    318339        if ('' == $this->getParam('user_id')) {
    319             $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_ERR, __FILE__, __LINE__);
     340            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
    320341            return false;
    321342        }
     
    323344        $this->initDB();
    324345
    325         if (isset($_SESSION[$this->_ns]['data']) && is_array($_SESSION[$this->_ns]['data'])) {
     346        if (isset($_SESSION[$this->_ns]['persistent']) && is_array($_SESSION[$this->_ns]['persistent'])) {
    326347            // Delete old prefs from database.
    327348            $db->query("
     
    333354            // Insert new prefs.
    334355            $insert_values = array();
    335             foreach ($_SESSION[$this->_ns]['data'] as $key => $val) {
    336                 if ('' != trim($key) && '' != trim($val)) {
    337                     $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", $db->escapeString($this->getParam('user_id')), $db->escapeString($this->_ns), $db->escapeString($key), $db->escapeString($val));
    338                 }
     356            foreach ($_SESSION[$this->_ns]['persistent'] as $key => $val) {
     357                $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", $db->escapeString($this->getParam('user_id')), $db->escapeString($this->_ns), $db->escapeString($key), $db->escapeString($val));
    339358            }
    340359            $db->query("
     
    344363            ");
    345364           
    346             $app->logMsg(sprintf('Saved %s preferences to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);
     365            $app->logMsg(sprintf('Saved %s prefs to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);
    347366            return true;
    348367        }
Note: See TracChangeset for help on using the changeset viewer.