2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
40 * Description: Handles the User Preferences and stores them in a seperate table.
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
46 class UserPreference extends SugarBean
49 public $field_name_map;
54 public $date_modified;
55 public $assigned_user_id;
56 public $assigned_user_name;
62 public $object_name = 'UserPreference';
63 public $table_name = 'user_preferences';
65 public $disable_row_level_security = true;
66 public $module_dir = 'UserPreferences';
67 public $field_defs = array();
68 public $field_defs_map = array();
69 public $new_schema = true;
71 protected $_userFocus;
73 // Do not actually declare, use the functions statically
74 public function __construct(
80 $this->_userFocus = $user;
84 * Get preference by name and category. Lazy loads preferences from the database per category
86 * @param string $name name of the preference to retreive
87 * @param string $category name of the category to retreive, defaults to global scope
88 * @return mixed the value of the preference (string, array, int etc)
90 public function getPreference(
97 $user = $this->_userFocus;
99 // if the unique key in session doesn't match the app or prefereces are empty
100 if(!isset($_SESSION[$user->user_name.'_PREFERENCES'][$category]) || (!empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key'])) {
101 $this->loadPreferences($category);
103 if(isset($_SESSION[$user->user_name.'_PREFERENCES'][$category][$name])) {
104 return $_SESSION[$user->user_name.'_PREFERENCES'][$category][$name];
107 // check to see if a default preference ( i.e. $sugar_config setting ) exists for this value )
109 $value = $this->getDefaultPreference($name,$category);
110 if ( !is_null($value) ) {
117 * Get preference by name and category from the system settings.
119 * @param string $name name of the preference to retreive
120 * @param string $category name of the category to retreive, defaults to global scope
121 * @return mixed the value of the preference (string, array, int etc)
123 public function getDefaultPreference(
128 global $sugar_config;
130 // Doesn't support any prefs but global ones
131 if ( $category != 'global' )
134 // First check for name matching $sugar_config variable
135 if ( isset($sugar_config[$name]) )
136 return $sugar_config[$name];
138 // Next, check to see if it's one of the common problem ones
139 if ( isset($sugar_config['default_'.$name]) )
140 return $sugar_config['default_'.$name];
141 if ( $name == 'datef' )
142 return $sugar_config['default_date_format'];
143 if ( $name == 'timef' )
144 return $sugar_config['default_time_format'];
145 if ( $name == 'email_link_type' )
146 return $sugar_config['email_default_client'];
150 * Set preference by name and category. Saving will be done in utils.php -> sugar_cleanup
152 * @param string $name name of the preference to retreive
153 * @param mixed $value value of the preference to set
154 * @param string $category name of the category to retreive, defaults to global scope
156 public function setPreference(
162 $user = $this->_userFocus;
164 if ( empty($user->user_name) )
167 if(!isset($_SESSION[$user->user_name.'_PREFERENCES'][$category])) {
168 if(!$user->loadPreferences($category))
169 $_SESSION[$user->user_name.'_PREFERENCES'][$category] = array();
172 // preferences changed or a new preference, save it to DB
173 if(!isset($_SESSION[$user->user_name.'_PREFERENCES'][$category][$name])
174 || (isset($_SESSION[$user->user_name.'_PREFERENCES'][$category][$name]) && $_SESSION[$user->user_name.'_PREFERENCES'][$category][$name] != $value)) {
175 $GLOBALS['savePreferencesToDB'] = true;
176 if(!isset($GLOBALS['savePreferencesToDBCats'])) $GLOBALS['savePreferencesToDBCats'] = array();
177 $GLOBALS['savePreferencesToDBCats'][$category] = true;
180 $_SESSION[$user->user_name.'_PREFERENCES'][$category][$name] = $value;
184 * Loads preference by category from database. Saving will be done in utils.php -> sugar_cleanup
186 * @param string $category name of the category to retreive, defaults to global scope
187 * @return bool successful?
189 public function loadPreferences(
193 global $sugar_config;
195 $user = $this->_userFocus;
197 if($user->object_name != 'User')
199 if(!empty($user->id) && (!isset($_SESSION[$user->user_name . '_PREFERENCES'][$category]) || (!empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']))) {
200 // cn: moving this to only log when valid - throwing errors on install
201 return $this->reloadPreferences($category);
207 * Unconditionally reloads user preferences from the DB and updates the session
208 * @param string $category name of the category to retreive, defaults to global scope
209 * @return bool successful?
211 public function reloadPreferences($category = 'global')
213 $user = $this->_userFocus;
215 if($user->object_name != 'User' || empty($user->id) || empty($user->user_name)) {
218 $GLOBALS['log']->debug('Loading Preferences DB ' . $user->user_name);
219 if(!isset($_SESSION[$user->user_name . '_PREFERENCES'])) $_SESSION[$user->user_name . '_PREFERENCES'] = array();
220 if(!isset($user->user_preferences) || !is_array($user->user_preferences)) $user->user_preferences = array();
221 $result = $GLOBALS['db']->query("SELECT contents FROM user_preferences WHERE assigned_user_id='$user->id' AND category = '" . $category . "' AND deleted = 0", false, 'Failed to load user preferences');
222 $row = $GLOBALS['db']->fetchByAssoc($result);
224 $_SESSION[$user->user_name . '_PREFERENCES'][$category] = unserialize(base64_decode($row['contents']));
225 $user->user_preferences[$category] = unserialize(base64_decode($row['contents']));
228 $_SESSION[$user->user_name . '_PREFERENCES'][$category] = array();
229 $user->user_preferences[$category] = array();
235 * Loads users timedate preferences
237 * @return array 'date' - date format for user ; 'time' - time format for user
239 public function getUserDateTimePreferences()
241 global $sugar_config, $db, $timedate, $current_user;
243 $user = $this->_userFocus;
247 if(!empty($user) && $this->loadPreferences('global')) {
248 // forced to set this to a variable to compare b/c empty() wasn't working
249 $timeZone = TimeDate::userTimezone($user);
250 $timeFormat = $user->getPreference("timef");
251 $dateFormat = $user->getPreference("datef");
253 // cn: bug xxxx cron.php fails because of missing preference when admin hasn't logged in yet
254 $timeZone = empty($timeZone) ? 'America/Los_Angeles' : $timeZone;
256 if(empty($timeFormat)) $timeFormat = $sugar_config['default_time_format'];
257 if(empty($dateFormat)) $dateFormat = $sugar_config['default_date_format'];
259 $prefDate['date'] = $dateFormat;
260 $prefDate['time'] = $timeFormat;
261 $prefDate['userGmt'] = TimeDate::tzName($timeZone);
262 $prefDate['userGmtOffset'] = $timedate->getUserUTCOffset($user);
266 $prefDate['date'] = $timedate->get_date_format();
267 $prefDate['time'] = $timedate->get_time_format();
269 if(!empty($user) && $user->object_name == 'User') {
270 $timeZone = TimeDate::userTimezone($user);
271 // cn: bug 9171 - if user has no time zone, cron.php fails for InboundEmail
272 if(!empty($timeZone)) {
273 $prefDate['userGmt'] = TimeDate::tzName($timeZone);
274 $prefDate['userGmtOffset'] = $timedate->getUserUTCOffset($user);
277 $timeZone = TimeDate::userTimezone($current_user);
278 if(!empty($timeZone)) {
279 $prefDate['userGmt'] = TimeDate::tzName($timeZone);
280 $prefDate['userGmtOffset'] = $timedate->getUserUTCOffset($current_user);
289 * Saves all preferences into the database that are in the session. Expensive, this is called by default in
290 * sugar_cleanup if a setPreference has been called during one round trip.
292 * @global user will use current_user if no user specificed in $user param
293 * @param user $user User object to retrieve, otherwise user current_user
294 * @param bool $all save all of the preferences? (Dangerous)
297 public function savePreferencesToDB(
301 global $sugar_config;
302 $GLOBALS['savePreferencesToDB'] = false;
304 $user = $this->_userFocus;
306 // these are not the preferences you are looking for [ hand waving ]
307 if(!empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']) return;
309 $GLOBALS['log']->debug('Saving Preferences to DB ' . $user->user_name);
310 if(isset($_SESSION[$user->user_name. '_PREFERENCES']) && is_array($_SESSION[$user->user_name. '_PREFERENCES'])) {
311 $GLOBALS['log']->debug("Saving Preferences to DB: {$user->user_name}");
312 // only save the categories that have been modified or all?
313 if(!$all && isset($GLOBALS['savePreferencesToDBCats']) && is_array($GLOBALS['savePreferencesToDBCats'])) {
314 $catsToSave = array();
315 foreach($GLOBALS['savePreferencesToDBCats'] as $category => $value) {
316 if ( isset($_SESSION[$user->user_name. '_PREFERENCES'][$category]) )
317 $catsToSave[$category] = $_SESSION[$user->user_name. '_PREFERENCES'][$category];
321 $catsToSave = $_SESSION[$user->user_name. '_PREFERENCES'];
324 foreach ($catsToSave as $category => $contents) {
325 $focus = new UserPreference($this->_userFocus);
326 $result = $focus->retrieve_by_string_fields(array(
327 'assigned_user_id' => $user->id,
328 'category' => $category,
330 $focus->assigned_user_id = $user->id; // MFH Bug #13862
332 $focus->contents = base64_encode(serialize($contents));
333 $focus->category = $category;
340 * Resets preferences for a particular user. If $category is null all user preferences will be reset
342 * @param string $category category to reset
344 public function resetPreferences(
348 $user = $this->_userFocus;
350 $GLOBALS['log']->debug('Reseting Preferences for user ' . $user->user_name);
352 $remove_tabs = $this->getPreference('remove_tabs');
353 $favorite_reports = $this->getPreference('favorites', 'Reports');
354 $home_pages = $this->getPreference('pages', 'home');
355 $home_dashlets = $this->getPreference('dashlets', 'home');
356 $ut = $this->getPreference('ut');
357 $timezone = $this->getPreference('timezone');
359 $query = "UPDATE user_preferences SET deleted = 1 WHERE assigned_user_id = '" . $user->id . "'";
361 $query .= " AND category = '" . $category . "'";
362 $this->db->query($query);
366 unset($_SESSION[$user->user_name."_PREFERENCES"][$category]);
369 if(!empty($_COOKIE['sugar_user_theme']) && !headers_sent()){
370 setcookie('sugar_user_theme', '', time() - 3600); // expire the sugar_user_theme cookie
372 unset($_SESSION[$user->user_name."_PREFERENCES"]);
373 if($user->id == $GLOBALS['current_user']->id) {
376 $this->setPreference('remove_tabs', $remove_tabs);
377 $this->setPreference('favorites', $favorite_reports, 'Reports');
378 $this->setPreference('pages', $home_pages, 'home');
379 $this->setPreference('dashlets', $home_dashlets, 'home');
380 $this->setPreference('ut', $ut);
381 $this->setPreference('timezone', $timezone);
382 $this->savePreferencesToDB();
383 if($user->id == $GLOBALS['current_user']->id) {
384 SugarApplication::redirect('index.php');
390 * Updates every user pref with a new key value supports 2 levels deep, use append to
391 * array if you want to append the value to an array
393 public static function updateAllUserPrefs(
397 $is_value_array = false,
398 $unset_value = false )
400 global $current_user, $db;
402 // Admin-only function; die if calling as a non-admin
403 if(!is_admin($current_user)){
404 sugar_die('only admins may call this function');
407 // we can skip this if we've already upgraded to the user_preferences format.
408 if ( !array_key_exists('user_preferences',$db->getHelper()->get_columns('users')) )
411 $result = $db->query("SELECT id, user_preferences, user_name FROM users");
412 while ($row = $db->fetchByAssoc($result)) {
416 $prefs = unserialize(base64_decode($row['user_preferences']));
418 if(!empty($sub_key)){
419 if($is_value_array ){
420 if(!isset($prefs[$key][$sub_key])){
424 if(empty($prefs[$key][$sub_key])){
425 $prefs[$key][$sub_key] = array();
427 $already_exists = false;
428 foreach($prefs[$key][$sub_key] as $k=>$value){
429 if($value == $new_value){
431 $already_exists = true;
433 unset($prefs[$key][$sub_key][$k]);
437 if(!$already_exists && !$unset_value){
438 $prefs[$key][$sub_key][] = $new_value;
442 if(!$unset_value)$prefs[$key][$sub_key] = $new_value;
446 if($is_value_array ){
447 if(!isset($prefs[$key])){
451 if(empty($prefs[$key])){
452 $prefs[$key] = array();
454 $already_exists = false;
455 foreach($prefs[$key] as $k=>$value){
456 if($value == $new_value){
457 $already_exists = true;
460 unset($prefs[$key][$k]);
464 if(!$already_exists && !$unset_value){
466 $prefs[$key][] = $new_value;
469 if(!$unset_value)$prefs[$key] = $new_value;
473 $newstr = $GLOBALS['db']->quote(base64_encode(serialize($prefs)));
474 $db->query("UPDATE users SET user_preferences = '{$newstr}' WHERE id = '{$row['id']}'");