You are here

function commerce_ups_settings_form_submit in Commerce UPS 7

Same name and namespace in other branches
  1. 7.2 includes/commerce_ups.admin.inc \commerce_ups_settings_form_submit()

File

./commerce_ups.admin.inc, line 329
Handles admin settings page for Commerce UPS module.

Code

function commerce_ups_settings_form_submit($form, &$form_state) {
  $encrypted = variable_get('commerce_ups_encrypt', FALSE) && function_exists('aes_decrypt');

  // Encrypt the UPS API credentials if available
  if (isset($form_state['input']['commerce_ups_encrypt']) && $form_state['input']['commerce_ups_encrypt']) {
    $form_state['input']['commerce_ups_user_id'] = commerce_ups_encrypt($form_state['input']['commerce_ups_user_id']);
    $form_state['input']['commerce_ups_account_id'] = commerce_ups_encrypt($form_state['input']['commerce_ups_account_id']);
    $form_state['input']['commerce_ups_access_key'] = commerce_ups_encrypt($form_state['input']['commerce_ups_access_key']);
    if (!empty($form_state['input']['commerce_ups_password'])) {
      $form_state['input']['commerce_ups_password'] = commerce_ups_encrypt($form_state['input']['commerce_ups_password']);
    }
    elseif ($encrypted == FALSE) {

      // This is an odd case, if encryption is turned on but the password is already in the db
      // Then we need to pull it from the db, not from the field
      $form_state['input']['commerce_ups_password'] = commerce_ups_encrypt(variable_get('commerce_ups_password', ''));
    }
  }
  if (empty($form_state['input']['commerce_ups_password'])) {
    unset($form_state['input']['commerce_ups_password']);
  }
  if (!isset($form_state['input']['commerce_ups_encrypt'])) {
    $form_state['input']['commerce_ups_encrypt'] = FALSE;
  }
  $fields = array(
    'commerce_ups_account_id',
    'commerce_ups_user_id',
    'commerce_ups_password',
    'commerce_ups_access_key',
    'commerce_ups_encrypt',
    'commerce_ups_services',
    'commerce_ups_company_name',
    'commerce_ups_address_line_1',
    'commerce_ups_address_line_2',
    'commerce_ups_city',
    'commerce_ups_state',
    'commerce_ups_postal_code',
    'commerce_ups_country_code',
    'commerce_ups_packaging',
    'commerce_ups_pick_up_schedule',
    'commerce_ups_show_logo',
    'commerce_ups_default_package_size_length',
    'commerce_ups_default_package_size_width',
    'commerce_ups_default_package_size_height',
    'commerce_ups_shipto_residential',
    'commerce_ups_log',
  );
  $services = variable_get('commerce_ups_services', NULL);
  foreach ($fields as $key) {
    if (array_key_exists($key, $form_state['input'])) {
      $value = $form_state['input'][$key];
      variable_set($key, $value);
    }
  }

  // If the selected services have changed then rebuild caches.
  if ($services !== $form_state['values']['commerce_ups_services']) {
    commerce_shipping_services_reset();
    entity_defaults_rebuild();
    rules_clear_cache(TRUE);
    menu_rebuild();
  }
  drupal_set_message(t('The UPS configuration options have been saved.'));
}