Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

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

    r42 r136  
    11<?php
    22/**
    3  * Prefs:: provides an API for saving arbitrary values in a user's session.
    4  * Database save routines to come.
     3 * Prefs.inc.php
     4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
    55 *
    6  * @inspiration  Horde 2.0's Prefs class. This one is much simpler, but
    7  *               the API for the methods that exist are sort of like Horde's
    8  *               in case we want to be more like it in the future.
     6 * Prefs provides an API for saving arbitrary values in a user's session.
     7 *
    98 * @author  Quinn Comendant <quinn@strangecode.com>
    10  * @version 1.1
     9 * @version 2.1
    1110 */
    1211class Prefs {
    1312
    14     /**
    15      * Where these preferences can be used. To differentiate between preferences
    16      * for a specific script, application, or global.
    17      * @var string $scope
    18      */
    19     var $scope = 'global';
    20 
    21     /**
    22      * Indicator whether we save in the database or not.
    23      * @var boolean $_perpetual
    24      */
    25     var $_perpetual = false;
    26 
    27     /**
    28      * Hash containing connection parameters.
    29      * @var array $params
    30      */
    31     var $params = array();
    32 
     13    // Namespace of this instance of Prefs.
     14    var $_ns;
    3315
    3416    /**
    3517     * Prefs constructor.
    3618     */
    37     function Prefs($dbh=false, $params=array())
     19    function Prefs($namespace='')
    3820    {
    39         $this->params = $params;
    40         $_perpetual = false; // Until database routines are completed.
     21        $this->_ns = '_prefs_' . $namespace;
     22       
     23        // Initialized the prefs array.
     24        if (!isset($_SESSION[$this->_ns])) {
     25            $_SESSION[$this->_ns] = array();
     26        }
    4127    }
    4228
     
    4531     * is not set already.
    4632     *
    47      * @param  string $pref      The name of the preference to modify.
     33     * @param  string $key       The name of the preference to modify.
    4834     * @param  string $val       The new value for this preference.
    49      * @param  string $scope     The scope for this preference.
    50      *
    51      * @return boolean  True if the value was successfully set.
    5235     */
    53     function setDefault($pref, $val, $scope=null)
     36    function setDefault($key, $val)
    5437    {
    55         if (!isset($scope)) {
    56             $scope =& $this->scope;
    57         }
    58 
    59         // No empty values allowed.
    60         if ('' == $pref || '' == $val || '' == $scope) {
    61             return false;
    62         }
    63 
    64         // Initialized the prefs array.
    65         if (!isset($_SESSION['_prefs'])) {
    66             $_SESSION['_prefs'] = array();
    67         }
    68 
    69         // In case boolean or null values are passed as a string.
    70         if ($val == 'true') {
    71             $val = true;
    72         } else if ($val == 'false') {
    73             $val = false;
    74         } else if ($val == 'null') {
    75             $val = null;
    76         }
    77 
    7838        // Set it only if not set already.
    79         if (!isset($_SESSION['_prefs'][$scope][$pref])) {
    80             $_SESSION['_prefs'][$scope][$pref] = $val;
    81             return true;
     39        if (!isset($_SESSION[$this->_ns][$key])) {
     40            $_SESSION[$this->_ns][$key] = $val;
    8241        }
    8342    }
     
    8645     * Sets the given preferences to the specific value,
    8746     *
    88      * @param  string $pref      The name of the preference to modify.
     47     * @param  string $key       The name of the preference to modify.
    8948     * @param  string $val       The new value for this preference.
    90      * @param  string $scope     The scope for this preference.
    91      *
    92      * @return boolean  True if the value was successfully set.
    9349     */
    94     function setValue($pref, $val, $scope=null)
     50    function set($key, $val)
    9551    {
    96         if (!isset($scope)) {
    97             $scope =& $this->scope;
    98         }
    99 
    100         // No empty values allowed.
    101         if ('' == $pref || '' == $val || '' == $scope) {
    102             return false;
    103         }
    104 
    105         // Initialized the prefs array.
    106         if (!isset($_SESSION['_prefs'])) {
    107             $_SESSION['_prefs'] = array();
    108         }
    109 
    110         // In case boolean or null values are passed as a string.
    111         if ($val == 'true') {
    112             $val = true;
    113         } else if ($val == 'false') {
    114             $val = false;
    115         } else if ($val == 'null') {
    116             $val = null;
    117         }
    118 
    119         $_SESSION['_prefs'][$scope][$pref] = $val;
    120         return true;
     52        $_SESSION[$this->_ns][$key] = $val;
    12153    }
    12254
     
    12456     * Returns the value of the requested preference.
    12557     *
    126      * @param string $pref      The name of the preference to retrieve.
    127      * @param string $scope     The scope for this preference.
     58     * @param string $key       The name of the preference to retrieve.
    12859     *
    12960     * @return string           The value of the preference.
    13061     */
    131     function getValue($pref, $scope=null)
     62    function get($key)
    13263    {
    133         if (!isset($scope)) {
    134             $scope =& $this->scope;
    135         }
    136 
    137         return (isset($_SESSION['_prefs'][$scope][$pref])) ? $_SESSION['_prefs'][$scope][$pref] : null;
     64        return (isset($_SESSION[$this->_ns][$key])) ? $_SESSION[$this->_ns][$key] : null;
    13865    }
    13966
     
    14168     * To see if a preference has been set.
    14269     *
    143      * @param string $pref      The name of the preference to check.
    144      * @param string $scope     The scope for this preference.
     70     * @param string $key       The name of the preference to check.
    14571     *
    14672     * @return boolean          True if the preference isset and not empty
    14773     *                          false otherwise.
    14874     */
    149     function exists($pref, $scope=null)
     75    function exists($key)
    15076    {
    151         if (!isset($scope)) {
    152             $scope =& $this->scope;
    153         }
    154 
    155         return isset($_SESSION['_prefs'][$scope][$pref]);
     77        return isset($_SESSION[$this->_ns][$key]);
    15678    }
    15779
     
    15981     * Clear a set preference value.
    16082     *
    161      * @param string $pref      The name of the preference to check.
    162      * @param string $scope     The scope for this preference.
     83     * @param string $key       The name of the preference to check.
    16384     */
    164     function clearValue($pref, $scope=null)
     85    function delete($key)
    16586    {
    166         if (!isset($scope)) {
    167             $scope =& $this->scope;
     87        if (isset($_SESSION[$this->_ns][$key])) {
     88            unset($_SESSION[$this->_ns][$key]);
    16889        }
    169 
    170         if (isset($_SESSION['_prefs'][$scope][$pref])) {
    171             unset($_SESSION['_prefs'][$scope][$pref]);
    172         }
    173     }
    174 
    175     /**
    176      * Retrieves the requested set of preferences from the user's database
    177      * entry.
    178      *
    179      * @param optional array $prefs  An array listing the preferences to
    180      *                     retrieve. If not specified, retrieve all of the
    181      *                     preferences listed in the $prefs hash.
    182      *
    183      * @return mixed       True on success or a PEAR_Error object on failure.
    184      */
    185     function retrieve()
    186     {
    187         // Check that we don't have the preferences loaded yet.
    188         if ($_SESSION['_prefs']['loaded'] == true || !$_perpetual) {
    189             return true;
    190         }
    191 
    192         // FIXME: Database query goes here....
    193         return false;
    194 
    195         $_SESSION['_prefs']['loaded'] = true;
    196     }
    197 
    198     /**
    199      * Stores preferences to SQL server.
    200      *
    201      * @param array $prefs (optional) An array listing the preferences to be
    202      *                     stored.  If not specified, store all of the
    203      *                     preferences listed in the $prefs hash.
    204      *
    205      * @return mixed       True on success or a PEAR_Error object on failure.
    206      */
    207     function store($prefs=array())
    208     {
    209         // If we are not storing prefs in perpetually.
    210         if (!$_perpetual) {
    211             return true;
    212         }
    213 
    214         /*
    215          * If a list of preferences to store hasn't been provided in
    216          * $prefs, assume all preferences are desired.
    217          */
    218         if (empty($prefs)) {
    219             $prefs =& $_SESSION['_prefs'];
    220         } else if ($_SESSION['_prefs']['loaded'] == true) {
    221             $prefs = array_merge($_SESSION['_prefs'], $prefs);
    222         }
    223 
    224         if (!is_array($prefs) || empty($prefs)) {
    225             App::raiseMsg(_("No preferences are available."), MSG_ERR, __FILE__, __LINE__);
    226             App::dieBoomerangURL();
    227         }
    228 
    229         // FIXME: Database query goes here....
    230         return false;
    231 
    23290    }
    23391
     
    23593     * Perform cleanup operations.
    23694     */
    237     function cleanup()
     95    function clear()
    23896    {
    239         $_SESSION['_prefs'] = array();
     97        $_SESSION[$this->_ns] = array();
    24098    }
    24199}
    242100
    243101
    244 
    245 
    246102?>
Note: See TracChangeset for help on using the changeset viewer.