2 // Copyright (C) 2010 Brady Miller <brady@sparmy.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
10 // Set effective user - If no user id is provided, then use the currently logged in user
11 function effectiveUser($user) {
12 return (is_null($user) ? $_SESSION['authUserID'] : $user);
16 * Return user setting(s) from the 'users' table
18 * @param string $label - Setting key
19 * @param int $user - user id number from users table
20 * @param int $defaultUser - user id to check as alternative/default
21 * @return Effective user setting for $label (NULL if does not exist)
23 function getUserSetting($label, $user=NULL, $defaultUser=0) {
25 $user = effectiveUser($user);
27 // Collect entry for specified user or 0 (global default user)
28 $res = sqlQuery("SELECT setting_value FROM user_settings
29 WHERE (setting_user=? OR setting_user=?) AND setting_label=?
30 ORDER BY setting_user DESC", array($user, $defaultUser, $label));
32 // If no entries exist, then return NULL.
33 return (isset($res['setting_value']) ? $res['setting_value'] : NULL);
37 * Check if effective user setting matches given value
39 * @param string $label - Setting key
40 * @param string $value - Setting value
41 * @param int $user - user id number from users table
42 * @return boolean - true if setting exist and false if does not exist
44 function checkUserSetting($label, $value, $user=NULL) {
46 $user = effectiveUser($user);
48 $curval = getUserSetting ($label, $user);
49 if (is_null($curval)) {
52 return ($curval === $value);
59 * @param string $label - Setting key
60 * @param string $value - Setting value
61 * @param int $user - user id number from users table
62 * @param boolean $createDefault - If no current global default value, create one.
63 * @param boolean $overwrite - If this is set to true, then overwrite the current setting
65 function setUserSetting($label, $value, $user=NULL, $createDefault=TRUE, $overwrite=TRUE) {
67 $user = effectiveUser($user);
69 $cur_value = getUserSetting($label, $user, $user);
71 // Check for a custom settings
72 if (is_null($cur_value)) {
73 sqlStatement("INSERT INTO user_settings(setting_user, setting_label, setting_value) " .
74 "VALUES (?,?,?)", array($user, $label, $value) );
75 } elseif ( ($cur_value !== $value) && $overwrite ) {
76 sqlStatement("UPDATE user_settings SET setting_value=? " .
77 "WHERE setting_user=? AND setting_label=?", array($value, $user, $label) );
80 // Call self to create default token
81 // (Note this is only done if a default token does not yet exist, thus set overwrite to FALSE))
83 setUserSetting($label, $value, 0, false, false);
87 //This will remove the selected user setting from the 'user_settings' table.
88 // $label is used to determine which setting to remove
89 // $user is the user id number from users table
90 function removeUserSetting($label,$user=NULL) {
92 $user = effectiveUser($user);
94 // mdsupport - DELETE has implicit select, no need to check and delete
95 sqlQuery("DELETE FROM user_settings " .
96 "WHERE setting_user=? AND setting_label=?", array($user, $label) );
99 function getUserIDInfo($id)
101 return sqlQuery("SELECT fname, lname, username FROM users where id=?", array($id));
105 * Function to retain current user's choices from prior sessions
106 * @param string $uspfx - Caller specified prefix to be used in settings key, typically script name
107 * @param string $postvar - $_POST variable name containing current value
108 * @param string $label - Caller specified constant added to $uspfx to create settings key
109 * @param string $initval - Initial value to be saved in case user setting does not exist
110 * @return Prior setting (if found) or initial value to be used in script
112 function prevSetting($uspfx, $postvar, $label, $initval) {
114 $setting_key = $uspfx.$label;
116 if (isset($_POST[$postvar])) {
117 // If script provides current value, store it for future use.
118 $pset = $_POST[$postvar];
119 if ($pset != getUserSetting($setting_key)) {
120 setUserSetting($setting_key, $_POST[$postvar]);
123 // Script requires prior value
124 $pset = getUserSetting($setting_key);
125 if (is_null($pset)) {
126 setUserSetting($setting_key, $initval);
127 $pset = getUserSetting($setting_key);