You are here

function commerce_avatax_credentials_settings_form in Drupal Commerce Connector for AvaTax 7.5

Builds the AvaTax credentials settings form.

1 string reference to 'commerce_avatax_credentials_settings_form'
commerce_avatax_menu in ./commerce_avatax.module
Implements hook_menu().

File

includes/commerce_avatax.admin.inc, line 10
Administrative callbacks for the Commerce Avatax module.

Code

function commerce_avatax_credentials_settings_form($form, &$form_state) {
  form_load_include($form_state, 'inc', 'commerce_avatax', 'includes/commerce_avatax.admin');
  $var_prefix = COMMERCE_AVATAX_VAR_PREFIX;
  $modes = array(
    COMMERCE_AVATAX_DEVELOPMENT_MODE => t('Development'),
    COMMERCE_AVATAX_PRODUCTION_MODE => t('Production'),
  );
  $api_mode = commerce_avatax_api_mode();
  if (!empty($form_state['values'][$var_prefix . 'api_mode'])) {
    $api_mode = $form_state['values'][$var_prefix . 'api_mode'];
  }
  $form['credentials'] = array(
    '#title' => t('API Credentials'),
    '#type' => 'fieldset',
    '#attributes' => array(
      'id' => 'commerce-avatax-credentials-wrapper',
    ),
    '#description' => t('Please note that only the account number and the license key are validated.'),
  );
  $form['credentials'][$var_prefix . 'api_mode'] = array(
    '#title' => t('API Mode'),
    '#description' => t('Only select Production after completing the GO LIVE process with Avatax'),
    '#type' => 'select',
    '#options' => $modes,
    '#default_value' => $api_mode,
    '#ajax' => array(
      'callback' => 'commerce_avatax_ajax_mode_credentials',
      'wrapper' => 'commerce-avatax-credentials-wrapper',
    ),
  );

  // Allow to set different credentials for each mode (dev|prod).
  $fields = array(
    COMMERCE_AVATAX_COMPANY_CODE => array(
      'title' => t('@mode company code', array(
        '@mode' => $modes[$api_mode],
      )),
      'description' => t('The Avatax company code.'),
      'required' => FALSE,
    ),
    COMMERCE_AVATAX_ACCOUNT_NUMBER => array(
      'title' => t('@mode account number', array(
        '@mode' => $modes[$api_mode],
      )),
      'description' => t('The Avatax account number that is used to authenticate against the API.'),
      'required' => TRUE,
    ),
    COMMERCE_AVATAX_LICENSE_KEY => array(
      'title' => t('@mode license key', array(
        '@mode' => $modes[$api_mode],
      )),
      'description' => t('The Avatax license key that is used to authenticate against the API.'),
      'required' => TRUE,
    ),
  );
  $display_warning = FALSE;
  foreach ($fields as $key => $conf) {
    $variable_name = $var_prefix . $api_mode . '_' . $key;
    $variable_value = variable_get($variable_name, '');

    // Determine if the settings are in the database; if they are not but have
    // values set, we disable the form fields so the values will not be saved to
    // the database on submission.
    $variable_in_db = unserialize(db_query("SELECT value FROM {variable} WHERE name = :name", array(
      ':name' => $variable_name,
    ))
      ->fetchField());
    if (!empty($variable_value) && empty($variable_in_db)) {
      $form['credentials'][$variable_name] = array(
        '#type' => 'item',
        '#title' => $conf['title'],
        '#description' => $conf['description'],
        '#markup' => check_plain($variable_value),
      );
    }
    else {
      $form['credentials'][$variable_name] = array(
        '#type' => 'textfield',
        '#title' => $conf['title'],
        '#description' => $conf['description'],
        '#default_value' => $variable_value,
        '#required' => $conf['required'],
      );
    }

    // Show a message informing the user of best practices to not store API
    // credentials in the database if necessary.
    if (empty($variable_value) || !empty($variable_value) && $variable_in_db) {
      $display_warning = TRUE;
    }
  }

  // Show a message informing the user of best practices to not store API
  // credentials in the database if necessary.
  // Avoid to repeat the message during when the credentials are validated.
  if ($display_warning && !isset($form_state['triggering_element'])) {
    $description = t('It is best practice to store API credentials outside of the database and your source code repository.');
    $description .= ' ' . t('Consider setting the credential variables as server environment variables and bringing them into your Drupal configuration via the $conf array in your settings.php file instead.');
    drupal_set_message($description, 'warning');
  }

  // Add a "Validate credentials" button.
  $form['credentials']['validate'] = array(
    '#value' => t('Validate credentials'),
    '#type' => 'submit',
    '#validate' => array(
      'commerce_avatax_credentials_settings_validate',
    ),
    '#ajax' => array(
      'callback' => 'commerce_avatax_ajax_mode_credentials',
      'wrapper' => 'commerce-avatax-credentials-wrapper',
    ),
  );
  $form = system_settings_form($form);
  return $form;
}