/**
  * Saves all preferences into the database that are in the session. Expensive, this is called by default in
  * sugar_cleanup if a setPreference has been called during one round trip.
  *
  * @global user will use current_user if no user specificed in $user param
  * @param user $user User object to retrieve, otherwise user current_user
  * @param bool $all save all of the preferences? (Dangerous)
  *
  */
 public function savePreferencesToDB($all = false)
 {
     global $sugar_config;
     $GLOBALS['savePreferencesToDB'] = false;
     $user = $this->_userFocus;
     // these are not the preferences you are looking for [ hand waving ]
     if (!empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']) {
         return;
     }
     $GLOBALS['log']->debug('Saving Preferences to DB ' . $user->user_name);
     if (isset($_SESSION[$user->user_name . '_PREFERENCES']) && is_array($_SESSION[$user->user_name . '_PREFERENCES'])) {
         $GLOBALS['log']->debug("Saving Preferences to DB: {$user->user_name}");
         // only save the categories that have been modified or all?
         if (!$all && isset($GLOBALS['savePreferencesToDBCats']) && is_array($GLOBALS['savePreferencesToDBCats'])) {
             $catsToSave = array();
             foreach ($GLOBALS['savePreferencesToDBCats'] as $category => $value) {
                 if (isset($_SESSION[$user->user_name . '_PREFERENCES'][$category])) {
                     $catsToSave[$category] = $_SESSION[$user->user_name . '_PREFERENCES'][$category];
                 }
             }
         } else {
             $catsToSave = $_SESSION[$user->user_name . '_PREFERENCES'];
         }
         foreach ($catsToSave as $category => $contents) {
             $focus = new UserPreference($this->_userFocus);
             $result = $focus->retrieve_by_string_fields(array('assigned_user_id' => $user->id, 'category' => $category));
             $focus->assigned_user_id = $user->id;
             // MFH Bug #13862
             $focus->deleted = 0;
             $focus->contents = base64_encode(serialize($contents));
             $focus->category = $category;
             //save if length is under column max
             if (strlen($focus->contents) > 65535) {
                 //log error and add to error message
                 $GLOBALS['log']->fatal("USERPREFERENCE ERROR:: User preference  for user: '******' and category '{$focus->category}' is " . strlen($focus->contents) . " characters long which is too big to save");
                 $errors[] = 'category: ' . $focus->category . ' is ' . strlen($focus->contents) . ' characters long which is too big and will cause the query to fail.';
                 //set global flag to indicate error has ocurred.  This will cause sugar_cleanup() in utils.php to flash a warning message to user.
                 $_SESSION['USER_PREFRENCE_ERRORS'] = true;
             } else {
                 $focus->save();
             }
         }
     }
 }
 /**
  * Saves all preferences into the database that are in the session. Expensive, this is called by default in
  * sugar_cleanup if a setPreference has been called during one round trip.
  *
  * @global user will use current_user if no user specificed in $user param
  * @param user $user User object to retrieve, otherwise user current_user
  * @param bool $all save all of the preferences? (Dangerous)
  *
  */
 public function savePreferencesToDB($all = false)
 {
     global $sugar_config;
     $GLOBALS['savePreferencesToDB'] = false;
     $user = $this->_userFocus;
     // these are not the preferences you are looking for [ hand waving ]
     if (empty($GLOBALS['installing']) && !empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']) {
         return;
     }
     $GLOBALS['log']->debug('Saving Preferences to DB ' . $user->user_name);
     if (isset($_SESSION[$user->user_name . '_PREFERENCES']) && is_array($_SESSION[$user->user_name . '_PREFERENCES'])) {
         $GLOBALS['log']->debug("Saving Preferences to DB: {$user->user_name}");
         // only save the categories that have been modified or all?
         if (!$all && isset($GLOBALS['savePreferencesToDBCats']) && is_array($GLOBALS['savePreferencesToDBCats'])) {
             $catsToSave = array();
             foreach ($GLOBALS['savePreferencesToDBCats'] as $category => $value) {
                 if (isset($_SESSION[$user->user_name . '_PREFERENCES'][$category])) {
                     $catsToSave[$category] = $_SESSION[$user->user_name . '_PREFERENCES'][$category];
                 }
             }
         } else {
             $catsToSave = $_SESSION[$user->user_name . '_PREFERENCES'];
         }
         foreach ($catsToSave as $category => $contents) {
             $focus = new UserPreference($this->_userFocus);
             $result = $focus->retrieve_by_string_fields(array('assigned_user_id' => $user->id, 'category' => $category));
             $focus->assigned_user_id = $user->id;
             // MFH Bug #13862
             $focus->deleted = 0;
             $focus->contents = base64_encode(serialize($contents));
             $focus->category = $category;
             $focus->save();
         }
     }
 }
 /**
  * Saves all preferences into the database that are in the session. Expensive, this is called by default in
  * sugar_cleanup if a setPreference has been called during one round trip.
  *
  * @global user will use current_user if no user specificed in $user param
  * @param user $user User object to retrieve, otherwise user current_user
  * @param bool $all save all of the preferences? (Dangerous)
  *
  */
 function savePreferencesToDB($user = null, $all = false)
 {
     global $sugar_config;
     $GLOBALS['savePreferencesToDB'] = false;
     global $db;
     if (!isset($user)) {
         if (!empty($GLOBALS['current_user'])) {
             $user = $GLOBALS['current_user'];
         } else {
             $GLOBALS['log']->fatal('No User Defined: UserPreferences::savePreferencesToDB');
             return;
             // no user defined, sad panda
         }
     }
     // these are not the preferences you are looking for [ hand waving ]
     if (!empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']) {
         return;
     }
     $GLOBALS['log']->debug('Saving Preferences to DB ' . $user->user_name);
     if (isset($_SESSION[$user->user_name . '_PREFERENCES']) && is_array($_SESSION[$user->user_name . '_PREFERENCES'])) {
         $GLOBALS['log']->debug("Saving Preferences to DB: {$user->user_name}");
         // only save the categories that have been modified or all?
         if (!$all && isset($GLOBALS['savePreferencesToDBCats']) && is_array($GLOBALS['savePreferencesToDBCats'])) {
             $catsToSave = array();
             foreach ($GLOBALS['savePreferencesToDBCats'] as $category => $value) {
                 if (isset($_SESSION[$user->user_name . '_PREFERENCES'][$category])) {
                     $catsToSave[$category] = $_SESSION[$user->user_name . '_PREFERENCES'][$category];
                 }
             }
         } else {
             $catsToSave = $_SESSION[$user->user_name . '_PREFERENCES'];
         }
         $focus = new UserPreference();
         foreach ($catsToSave as $category => $contents) {
             unset($focus->id);
             $query = "SELECT id, contents FROM user_preferences WHERE deleted = 0 AND assigned_user_id = '" . $user->id . "' AND category = '" . $category . "'";
             $result = $db->query($query);
             $row = $db->fetchByAssoc($result);
             if (!empty($row['id'])) {
                 // update
                 $focus->assigned_user_id = $user->id;
                 // MFH Bug #13862
                 $focus->retrieve($row['id'], true, false);
                 $focus->deleted = 0;
                 $focus->contents = base64_encode(serialize($contents));
             } else {
                 // insert new
                 $focus->assigned_user_id = $user->id;
                 $focus->contents = base64_encode(serialize($contents));
                 $focus->category = $category;
             }
             $focus->save();
         }
     }
 }