Ejemplo n.º 1
0
 /**
  * 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();
         }
     }
 }