diff --git currency.module currency.module index fca86aa..c7dae27 100644 --- currency.module +++ currency.module @@ -52,7 +52,7 @@ function currency_admin_settings() { '#maxlength' => 3, '#description' => t('Three letter symbol for default currency to convert from.'), ); - + $form['currency_default_to'] = array( '#type' => 'textfield', '#title' => t('Default Currency To'), @@ -61,14 +61,14 @@ function currency_admin_settings() { '#maxlength' => 3, '#description' => t('Three letter symbol for default currency to convert to.'), ); - + $form['currency_description'] = array( '#type' => 'textarea', '#title' => t('Currency form text'), '#default_value' => variable_get('currency_description', t('You can use this form to do currency exchange.')), '#description' => t('Text to display on the top of the currency form.'), ); - + return system_settings_form($form); } @@ -82,42 +82,56 @@ function currency_perm() { /** * Currency exchange form. */ -function currency_form($data = array()) { - // Get the saved data from the session, if any - $amount = $_SESSION['currency_amount'] ? $_SESSION['currency_amount'] : 1; - $from = $_SESSION['currency_from'] ? $_SESSION['currency_from'] : variable_get('currency_default_from', 'CAD'); - $to = $_SESSION['currency_to'] ? $_SESSION['currency_to'] : variable_get('currency_default_to', 'USD'); +function currency_form(&$form_state) { + if (isset($form_state['values']['currency_amount']) && isset($form_state['values']['currency_from']) && $form_state['values']['currency_to']) { + // Get the saved data from the previous form submission. + $amount = $form_state['values']['currency_amount']; + $from = $form_state['values']['currency_from']; + $to = $form_state['values']['currency_to']; + + $form['currency_result'] = array( + '#value' => theme('currency_result', $from, $to, $amount), + '#weight' => 5, + ); + } + else { + // Get the saved data from the session, if any. + $amount = $_SESSION['currency_amount'] ? $_SESSION['currency_amount'] : 1; + $from = $_SESSION['currency_from'] ? $_SESSION['currency_from'] : variable_get('currency_default_from', 'CAD'); + $to = $_SESSION['currency_to'] ? $_SESSION['currency_to'] : variable_get('currency_default_to', 'USD'); + } $form['currency_description'] = array( - '#value' => variable_get('currency_description', t('You can use this form to do currency exchange.')), + '#value' => variable_get('currency_description', t('You can use this form to do currency exchange.')), ); $form['currency_amount'] = array( - '#type' => 'textfield', - '#title' => t('Amount'), + '#type' => 'textfield', + '#title' => t('Amount'), '#default_value' => $amount, - '#size' => 9, - '#maxlength' => 9, - '#description' => t('Amount to convert'), + '#size' => 9, + '#maxlength' => 9, + '#description' => t('Amount to convert'), ); $form['currency_from'] = array( - '#type' => 'select', - '#title' => t('From'), + '#type' => 'select', + '#title' => t('From'), '#default_value' => $from, - '#options' => currency_api_get_list(), + '#options' => currency_api_get_list(), ); $form['currency_to'] = array( - '#type' => 'select', - '#title' => t('To'), + '#type' => 'select', + '#title' => t('To'), '#default_value' => $to, - '#options' => currency_api_get_list(), + '#options' => currency_api_get_list(), ); $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Convert'), + '#type' => 'submit', + '#value' => t('Convert'), + '#weight' => 10, ); return $form; @@ -128,11 +142,11 @@ function currency_form($data = array()) { */ function currency_form_validate($form, &$form_state) { if (!$form_state['values']['currency_amount']) { - form_set_error('', t('Amount is required.')); + form_set_error('currency_amount', t('Amount is required.')); } if (!is_numeric($form_state['values']['currency_amount'])) { - form_set_error('', t('Invalid Amount. Please enter a valid numeric amount.')); + form_set_error('currency_amount', t('Invalid Amount. Please enter a valid numeric amount.')); } } @@ -140,34 +154,54 @@ function currency_form_validate($form, &$form_state) { * Submit handler for the currency exchange form. */ function currency_form_submit($form, &$form_state) { - $from = $form_state['values']['currency_from']; - $to = $form_state['values']['currency_to']; - $amount = $form_state['values']['currency_amount']; - $url = 'http://finance.yahoo.com/q?s='. $from . $to .'=X'; + // Save the last used values in the session. + $_SESSION['currency_amount'] = $form_state['values']['currency_amount']; + $_SESSION['currency_from'] = $form_state['values']['currency_from']; + $_SESSION['currency_to'] = $form_state['values']['currency_to']; + + // Rebuild the form to keep the submitted values in $form_state. + $form_state['rebuild'] = TRUE; +} - $ret = currency_api_convert($from, $to, $amount); +/** + * Implementation of hook_theme(). + */ +function currency_theme($existing, $type, $theme, $path) { + return array( + 'currency_result' => array( + 'arguments' => array('currency_from' => NULL, 'currency_to' => NULL, 'amount' => NULL), + ), + ); +} + +/** + * Theme implementation for currency exchange result. + */ +function theme_currency_result($currency_from, $currency_to, $amount) { + $output = ''; + + $url = 'http://finance.yahoo.com/q?s=' . $currency_from . $currency_to . '=X'; + + $ret = currency_api_convert($currency_from, $currency_to, $amount); if ($ret['status'] == FALSE) { - drupal_set_message(t('currency exchange error: ') . $ret['message']); + $output .= t('Currency exchange error: ') . $ret['message']; } else { - $result .= '

'; - $result .= t('@amount @from = @value @to', array( + $output .= '

'; + $output .= t('@amount @from = @value @to', array( '@amount' => $amount, - '@from' => currency_api_get_desc($from), + '@from' => currency_api_get_desc($currency_from), '@value' => $ret['value'], - '@to' => currency_api_get_desc($to))); - $result .= '

'; - $result .= l(t('Detailed history and chart'), $url); - $result .= '

'; + '@to' => currency_api_get_desc($currency_to))); + $output .= '

'; + $output .= '

' . l(t('Detailed history and chart'), $url) . '

'; } - // Save the last used values in the session - $_SESSION['currency_amount'] = $amount; - $_SESSION['currency_from'] = $from; - $_SESSION['currency_to'] = $to; + $output = '
' . $output . '
'; + + return $output; +} - drupal_set_message($result); -} /** * Implementation of hook_views(). diff --git currency_api/currency_api.module currency_api/currency_api.module