You are here

function commerce_multicurrency_conversion_settings_form in Commerce Multicurrency 7

Form to configure conversion settings.

1 string reference to 'commerce_multicurrency_conversion_settings_form'
commerce_multicurrency_menu in ./commerce_multicurrency.module
Implements hook_menu().

File

./commerce_multicurrency.admin.inc, line 10
Administrative UI for commerce currency.

Code

function commerce_multicurrency_conversion_settings_form($form, &$form_state) {
  $enabled_currencies = commerce_currencies(TRUE);
  $default_currency_code = commerce_default_currency();
  $conversion_settings = variable_get('commerce_multicurrency_conversion_settings', array());
  $use_cross_sync = variable_get('commerce_multicurrency_use_cross_conversion', TRUE);
  $sync_providers = array();
  foreach (commerce_multicurrency_commerce_multicurrency_exchange_rate_sync_provider() as $provider_code => $provider) {
    $sync_providers[$provider_code] = $provider['title'];
  }
  $form['sync_provider'] = array(
    '#type' => 'select',
    '#options' => $sync_providers,
    '#default_value' => variable_get('commerce_multicurrency_sync_provider', NULL),
    '#title' => t('Service to fetch exchange rates from:'),
  );
  $form['use_cross_sync'] = array(
    '#type' => 'checkbox',
    '#default_value' => $use_cross_sync,
    '#title' => t('Use cross conversion between non default currencies.'),
    '#description' => t('If enabled only the rates between the default currency and the other currencies have to be managed. The rates between the other currencies is derived from their rates relative to the default currency.'),
  );
  $form['sync_now'] = array(
    '#type' => 'checkbox',
    '#title' => t('Synchronize rates on save.'),
  );
  $form['demo_amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Amount for example conversions:'),
    '#size' => 5,
    '#default_value' => 100,
  );
  $form['conversion_settings'] = array(
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'commerce_multicurrency') . '/commerce_multicurrency.admin.js',
      ),
    ),
    '#tree' => TRUE,
  );
  if (count($enabled_currencies) > 1) {
    foreach ($enabled_currencies as $currency) {
      if ($use_cross_sync && $currency['code'] != $default_currency_code) {
        continue;
      }
      if (!isset($conversion_settings[$currency['code']])) {
        $conversion_settings[$currency['code']] = array();
      }
      $conversion_settings[$currency['code']] += array(
        'sync' => '1',
        'rates' => array(),
      );
      $form['conversion_settings'][$currency['code']] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#title' => $currency['name'],
      );
      if ($currency['code'] == $default_currency_code) {
        $form['conversion_settings'][$currency['code']]['#weight'] = -1;
      }
      $form['conversion_settings'][$currency['code']]['sync'] = array(
        '#type' => 'checkbox',
        '#title' => t('Synchronize all rates of this currency.'),
        '#states' => array(
          'checked' => array(
            '#edit-' . $currency['code'] . ' fieldset input[type="checkbox"]' => array(
              'checked' => TRUE,
            ),
          ),
        ),
        '#default_value' => $conversion_settings[$currency['code']]['sync'],
      );
      foreach ($enabled_currencies as $conversion_currency) {
        if ($conversion_currency == $currency) {
          continue;
        }
        if (!isset($conversion_settings[$currency['code']]['rates'][$conversion_currency['code']])) {
          $conversion_settings[$currency['code']]['rates'][$conversion_currency['code']] = array();
        }
        $conversion_settings[$currency['code']]['rates'][$conversion_currency['code']] += array(
          'sync' => '1',
          'rate' => 0,
        );
        $form['conversion_settings'][$currency['code']]['sync']['#states']['checked']['input[name="conversion_settings[' . $currency['code'] . '][rates][' . $conversion_currency['code'] . '][sync]"]'] = array(
          'checked' => TRUE,
        );
        $form['conversion_settings'][$currency['code']]['rates'][$conversion_currency['code']] = array(
          '#type' => 'fieldset',
          '#attributes' => array(
            'class' => array(
              'conversion-rates',
            ),
          ),
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
          '#title' => $conversion_currency['code'],
        );
        $form['conversion_settings'][$currency['code']]['rates'][$conversion_currency['code']]['sync'] = array(
          '#type' => 'checkbox',
          '#title' => t('Synchronize this conversion rate.'),
          '#default_value' => $conversion_settings[$currency['code']]['rates'][$conversion_currency['code']]['sync'],
        );
        $form['conversion_settings'][$currency['code']]['rates'][$conversion_currency['code']]['rate'] = array(
          '#type' => 'textfield',
          '#title' => t('Exchange rate'),
          '#attributes' => array(
            'class' => array(
              'conversion-rate',
            ),
          ),
          '#description' => t('Exchange rate from @currency_code to @conversion_currency_code.', array(
            '@currency_code' => $currency['name'],
            '@conversion_currency_code' => $conversion_currency['name'],
          )),
          '#states' => array(
            'disabled' => array(
              'input[name="conversion_settings[' . $currency['code'] . '][rates][' . $conversion_currency['code'] . '][sync]"]' => array(
                'checked' => TRUE,
              ),
            ),
          ),
          '#size' => 13,
          '#element_validate' => array(
            'commerce_multicurrency_conversion_settings_form_rate_validate',
          ),
          '#default_value' => $conversion_settings[$currency['code']]['rates'][$conversion_currency['code']]['rate'],
          '#field_suffix' => t('* !demo_amount @currency_symbol = !amount @conversion_currency_symbol', array(
            '!demo_amount' => '<span class="demo-amount">100</span>',
            '@currency_symbol' => $currency['symbol'],
            '@conversion_currency_symbol' => $conversion_currency['symbol'],
            '!amount' => '<span class="demo-amount-converted">' . 100 * $conversion_settings[$currency['code']]['rates'][$conversion_currency['code']]['rate'] . '</span>',
          )),
        );
      }
    }
  }
  else {
    drupal_set_message(t('Please enable the needed currencies to configure first.'), 'warning', FALSE);
  }
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save Settings'),
  );
  return $form;
}