source: trunk/lib/Prefs.inc.php @ 136

Last change on this file since 136 was 136, checked in by scdev, 18 years ago

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

File size: 2.5 KB
RevLine 
[1]1<?php
2/**
[136]3 * Prefs.inc.php
4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
[1]5 *
[136]6 * Prefs provides an API for saving arbitrary values in a user's session.
7 *
[1]8 * @author  Quinn Comendant <quinn@strangecode.com>
[136]9 * @version 2.1
[1]10 */
11class Prefs {
12
[136]13    // Namespace of this instance of Prefs.
14    var $_ns;
[1]15
16    /**
17     * Prefs constructor.
18     */
[136]19    function Prefs($namespace='')
[1]20    {
[136]21        $this->_ns = '_prefs_' . $namespace;
22       
23        // Initialized the prefs array.
24        if (!isset($_SESSION[$this->_ns])) {
25            $_SESSION[$this->_ns] = array();
26        }
[1]27    }
28
29    /**
30     * Sets the default value of a preference. The pref will be set only if
[42]31     * is not set already.
[1]32     *
[136]33     * @param  string $key       The name of the preference to modify.
[1]34     * @param  string $val       The new value for this preference.
35     */
[136]36    function setDefault($key, $val)
[1]37    {
38        // Set it only if not set already.
[136]39        if (!isset($_SESSION[$this->_ns][$key])) {
40            $_SESSION[$this->_ns][$key] = $val;
[1]41        }
42    }
43
44    /**
[42]45     * Sets the given preferences to the specific value,
[1]46     *
[136]47     * @param  string $key       The name of the preference to modify.
[1]48     * @param  string $val       The new value for this preference.
49     */
[136]50    function set($key, $val)
[1]51    {
[136]52        $_SESSION[$this->_ns][$key] = $val;
[1]53    }
[42]54
[1]55    /**
56     * Returns the value of the requested preference.
57     *
[136]58     * @param string $key       The name of the preference to retrieve.
[1]59     *
60     * @return string           The value of the preference.
61     */
[136]62    function get($key)
[1]63    {
[136]64        return (isset($_SESSION[$this->_ns][$key])) ? $_SESSION[$this->_ns][$key] : null;
[1]65    }
[42]66
[1]67    /**
68     * To see if a preference has been set.
69     *
[136]70     * @param string $key       The name of the preference to check.
[1]71     *
72     * @return boolean          True if the preference isset and not empty
73     *                          false otherwise.
74     */
[136]75    function exists($key)
[1]76    {
[136]77        return isset($_SESSION[$this->_ns][$key]);
[1]78    }
[42]79
[1]80    /**
81     * Clear a set preference value.
82     *
[136]83     * @param string $key       The name of the preference to check.
[1]84     */
[136]85    function delete($key)
[1]86    {
[136]87        if (isset($_SESSION[$this->_ns][$key])) {
88            unset($_SESSION[$this->_ns][$key]);
[1]89        }
90    }
91
92    /**
93     * Perform cleanup operations.
94     */
[136]95    function clear()
[1]96    {
[136]97        $_SESSION[$this->_ns] = array();
[1]98    }
99}
100
101
102?>
Note: See TracBrowser for help on using the repository browser.