Changeset 136 for trunk/lib/Prefs.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Prefs.inc.php
r42 r136 1 1 <?php 2 2 /** 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 5 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. 6 * Prefs provides an API for saving arbitrary values in a user's session. 7 * 9 8 * @author Quinn Comendant <quinn@strangecode.com> 10 * @version 1.19 * @version 2.1 11 10 */ 12 11 class Prefs { 13 12 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; 33 15 34 16 /** 35 17 * Prefs constructor. 36 18 */ 37 function Prefs($ dbh=false, $params=array())19 function Prefs($namespace='') 38 20 { 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 } 41 27 } 42 28 … … 45 31 * is not set already. 46 32 * 47 * @param string $ prefThe name of the preference to modify.33 * @param string $key The name of the preference to modify. 48 34 * @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 35 */ 53 function setDefault($ pref, $val, $scope=null)36 function setDefault($key, $val) 54 37 { 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 38 // 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; 82 41 } 83 42 } … … 86 45 * Sets the given preferences to the specific value, 87 46 * 88 * @param string $ prefThe name of the preference to modify.47 * @param string $key The name of the preference to modify. 89 48 * @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 49 */ 94 function set Value($pref, $val, $scope=null)50 function set($key, $val) 95 51 { 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; 121 53 } 122 54 … … 124 56 * Returns the value of the requested preference. 125 57 * 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. 128 59 * 129 60 * @return string The value of the preference. 130 61 */ 131 function get Value($pref, $scope=null)62 function get($key) 132 63 { 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; 138 65 } 139 66 … … 141 68 * To see if a preference has been set. 142 69 * 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. 145 71 * 146 72 * @return boolean True if the preference isset and not empty 147 73 * false otherwise. 148 74 */ 149 function exists($ pref, $scope=null)75 function exists($key) 150 76 { 151 if (!isset($scope)) { 152 $scope =& $this->scope; 153 } 154 155 return isset($_SESSION['_prefs'][$scope][$pref]); 77 return isset($_SESSION[$this->_ns][$key]); 156 78 } 157 79 … … 159 81 * Clear a set preference value. 160 82 * 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. 163 84 */ 164 function clearValue($pref, $scope=null)85 function delete($key) 165 86 { 166 if ( !isset($scope)) {167 $scope =& $this->scope;87 if (isset($_SESSION[$this->_ns][$key])) { 88 unset($_SESSION[$this->_ns][$key]); 168 89 } 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 database177 * entry.178 *179 * @param optional array $prefs An array listing the preferences to180 * retrieve. If not specified, retrieve all of the181 * 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 be202 * stored. If not specified, store all of the203 * 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 in216 * $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 90 } 233 91 … … 235 93 * Perform cleanup operations. 236 94 */ 237 function clea nup()95 function clear() 238 96 { 239 $_SESSION[ '_prefs'] = array();97 $_SESSION[$this->_ns] = array(); 240 98 } 241 99 } 242 100 243 101 244 245 246 102 ?>
Note: See TracChangeset
for help on using the changeset viewer.