You are here

function currency_form in Currency 7

Same name and namespace in other branches
  1. 5 currency.module \currency_form()
  2. 6 currency.module \currency_form()

Currency exchange form.

1 string reference to 'currency_form'
currency_menu in ./currency.module
Implements hook_menu().

File

./currency.module, line 89
This module provides currency exchange rates.

Code

function currency_form($form, &$form_state) {
  if (isset($form_state['storage']['currency_amount']) && isset($form_state['storage']['currency_from']) && $form_state['storage']['currency_to']) {

    // Get the saved data from the previous form submission.
    $amount = $form_state['storage']['currency_amount'];
    $from = $form_state['storage']['currency_from'];
    $to = $form_state['storage']['currency_to'];
    $form['currency_result'] = array(
      '#markup' => theme('currency_result', array(
        'currency_from' => $from,
        'currency_to' => $to,
        'amount' => $amount,
      )),
      '#weight' => 5,
    );
  }
  else {

    // Get the saved data from the session, if any.
    $amount = isset($_SESSION['currency_amount']) ? $_SESSION['currency_amount'] : 1;
    $from = isset($_SESSION['currency_from']) ? $_SESSION['currency_from'] : variable_get('currency_default_from', 'CAD');
    $to = isset($_SESSION['currency_to']) ? $_SESSION['currency_to'] : variable_get('currency_default_to', 'USD');
  }
  $form['currency_description'] = array(
    '#markup' => variable_get('currency_description', t('You can use this form to do currency exchange.')),
  );
  $form['currency_amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Amount'),
    '#default_value' => $amount,
    '#size' => 9,
    '#maxlength' => 9,
    '#description' => t('Amount to convert'),
  );
  $form['currency_from'] = array(
    '#type' => 'select',
    '#title' => t('From'),
    '#default_value' => $from,
    '#options' => currency_api_get_list(),
  );
  $form['currency_to'] = array(
    '#type' => 'select',
    '#title' => t('To'),
    '#default_value' => $to,
    '#options' => currency_api_get_list(),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Convert'),
    '#weight' => 10,
  );
  return $form;
}