2 // Copyright (C) 2010 Brady Miller <brady.g.miller@gmail.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)
13 return (is_null($user) ? $_SESSION['authUserID'] : $user);
17 * Return user setting(s) from the 'users' table
19 * @param string $label - Setting key
20 * @param int $user - user id number from users table
21 * @param int $defaultUser - user id to check as alternative/default
22 * @return Effective user setting for $label (NULL if does not exist)
24 function getUserSetting($label, $user = null, $defaultUser = 0)
27 $user = effectiveUser($user);
29 // Collect entry for specified user or 0 (global default user)
30 $res = sqlQuery("SELECT setting_value FROM user_settings
31 WHERE (setting_user=? OR setting_user=?) AND setting_label=?
32 ORDER BY setting_user DESC", array($user, $defaultUser, $label));
34 // If no entries exist, then return NULL.
35 return (isset($res['setting_value']) ? $res['setting_value'] : null);
39 * Check if effective user setting matches given value
41 * @param string $label - Setting key
42 * @param string $value - Setting value
43 * @param int $user - user id number from users table
44 * @return boolean - true if setting exist and false if does not exist
46 function checkUserSetting($label, $value, $user = null)
49 $user = effectiveUser($user);
51 $curval = getUserSetting($label, $user);
52 if (is_null($curval)) {
55 return ($curval === $value);
62 * @param string $label - Setting key
63 * @param string $value - Setting value
64 * @param int $user - user id number from users table
65 * @param boolean $createDefault - If no current global default value, create one.
66 * @param boolean $overwrite - If this is set to true, then overwrite the current setting
68 function setUserSetting($label, $value, $user = null, $createDefault = true, $overwrite = true)
71 $user = effectiveUser($user);
73 $cur_value = getUserSetting($label, $user, $user);
75 // Check for a custom settings
76 if (is_null($cur_value)) {
77 sqlStatement("INSERT INTO user_settings(setting_user, setting_label, setting_value) " .
78 "VALUES (?,?,?)", array($user, $label, $value));
79 } elseif (($cur_value !== $value) && $overwrite) {
80 sqlStatement("UPDATE user_settings SET setting_value=? " .
81 "WHERE setting_user=? AND setting_label=?", array($value, $user, $label));
84 // Call self to create default token
85 // (Note this is only done if a default token does not yet exist, thus set overwrite to FALSE))
87 setUserSetting($label, $value, 0, false, false);
91 //This will remove the selected user setting from the 'user_settings' table.
92 // $label is used to determine which setting to remove
93 // $user is the user id number from users table
94 function removeUserSetting($label, $user = null)
97 $user = effectiveUser($user);
99 // mdsupport - DELETE has implicit select, no need to check and delete
100 sqlQuery("DELETE FROM user_settings " .
101 "WHERE setting_user=? AND setting_label=?", array($user, $label));
104 function getUserIDInfo($id)
106 return sqlQuery("SELECT fname, lname, username FROM users where id=?", array($id));
110 * Function to retain current user's choices from prior sessions
111 * @param string $uspfx - Caller specified prefix to be used in settings key, typically script name
112 * @param string $postvar - $_POST variable name containing current value
113 * @param string $label - Caller specified constant added to $uspfx to create settings key
114 * @param string $initval - Initial value to be saved in case user setting does not exist
115 * @return Prior setting (if found) or initial value to be used in script
117 function prevSetting($uspfx, $postvar, $label, $initval)
120 $setting_key = $uspfx.$label;
122 if (isset($_POST[$postvar])) {
123 // If script provides current value, store it for future use.
124 $pset = $_POST[$postvar];
125 if ($pset != getUserSetting($setting_key)) {
126 setUserSetting($setting_key, $_POST[$postvar]);
129 // Script requires prior value
130 $pset = getUserSetting($setting_key);
131 if (is_null($pset)) {
132 setUserSetting($setting_key, $initval);
133 $pset = getUserSetting($setting_key);
141 * Function to set the state of expandable forms as per user choice, user default or global default
142 * @return the current state of the file after updating table user_settings
145 function collectAndOrganizeExpandSetting($filenames = array())
147 $current_filename = $filenames[0];
148 $global_value = $GLOBALS['expand_form'];
150 if (getUserSetting($current_filename) > -1) {
151 $current_state = getUserSetting($current_filename);
152 } elseif ($global_value) {
153 $current_state = $global_value;
158 if ($filenames.length) {
159 foreach ($filenames as $filename) {
160 setUserSetting($filename, $current_state);
164 return $current_state;