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