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

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

File size: 6.9 KB
Line 
1<?php
2/**
3 * Prefs:: provides an API for saving arbitrary values in a user's session.
4 * Database save routines to come.
5 *
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.
9 * @author  Quinn Comendant <quinn@strangecode.com>
10 * @version 1.1
11 */
12class Prefs {
13
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   
33   
34    /**
35     * Prefs constructor.
36     */
37    function Prefs($dbh=false, $params=array())
38    {
39        $this->params = $params;
40        $_perpetual = false; // Until database routines are completed.
41    }
42
43    /**
44     * Sets the default value of a preference. The pref will be set only if
45     * is not set already.
46     *
47     * @param  string $pref      The name of the preference to modify.
48     * @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.
52     */
53    function setDefault($pref, $val, $scope=null)
54    {
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
78        // Set it only if not set already.
79        if (!isset($_SESSION['_prefs'][$scope][$pref])) {
80            $_SESSION['_prefs'][$scope][$pref] = $val;
81            return true;
82        }
83    }
84
85    /**
86     * Sets the given preferences to the specific value,
87     *
88     * @param  string $pref      The name of the preference to modify.
89     * @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.
93     */
94    function setValue($pref, $val, $scope=null)
95    {
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;
121    }
122   
123    /**
124     * Returns the value of the requested preference.
125     *
126     * @param string $pref      The name of the preference to retrieve.
127     * @param string $scope     The scope for this preference.
128     *
129     * @return string           The value of the preference.
130     */
131    function getValue($pref, $scope=null)
132    {
133        if (!isset($scope)) {
134            $scope =& $this->scope;
135        }
136       
137        return (isset($_SESSION['_prefs'][$scope][$pref])) ? $_SESSION['_prefs'][$scope][$pref] : null;
138    }
139   
140    /**
141     * To see if a preference has been set.
142     *
143     * @param string $pref      The name of the preference to check.
144     * @param string $scope     The scope for this preference.
145     *
146     * @return boolean          True if the preference isset and not empty
147     *                          false otherwise.
148     */
149    function exists($pref, $scope=null)
150    {
151        if (!isset($scope)) {
152            $scope =& $this->scope;
153        }
154       
155        return isset($_SESSION['_prefs'][$scope][$pref]);
156    }
157   
158    /**
159     * Clear a set preference value.
160     *
161     * @param string $pref      The name of the preference to check.
162     * @param string $scope     The scope for this preference.
163     */
164    function clearValue($pref, $scope=null)
165    {
166        if (!isset($scope)) {
167            $scope =& $this->scope;
168        }
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
232    }
233
234    /**
235     * Perform cleanup operations.
236     */
237    function cleanup()
238    {
239        $_SESSION['_prefs'] = array();
240    }
241}
242
243
244
245
246?>
Note: See TracBrowser for help on using the repository browser.