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)
  *
  */
 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();
             }
         }
     }
 }
Ejemplo n.º 2
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)
  *
  */
 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();
         }
     }
 }
Ejemplo n.º 3
0
 public function testSavePreferencesToDBAndResetPreferences()
 {
     $user = new User();
     $user->retrieve('1');
     $userPreference = new UserPreference($user);
     //create a Preference record, save it to DB
     $userPreference->setPreference('test', 'test val', 'test_category');
     $userPreference->savePreferencesToDB();
     //retrieve it back and verify
     $result = $userPreference->retrieve_by_string_fields(array('assigned_user_id' => $user->id, 'category' => 'test_category'));
     $this->assertTrue(isset($result->id));
     //reset the preferences and verify that it is deleted
     $userPreference->resetPreferences();
     $result = $userPreference->retrieve_by_string_fields(array('assigned_user_id' => $user->id, 'category' => 'test_category'));
     $this->assertEquals(null, $result);
 }