currency.module in Currency 6
Same filename and directory in other branches
This module provides currency exchange rates.
File
currency.moduleView source
<?php
/**
* @file
* This module provides currency exchange rates.
*/
// Copyright 2005 Khalid Baheyeldin http://2bits.com
/**
* Implementation of hook_help().
*/
function currency_help($path, $arg) {
switch ($path) {
case 'admin/help#currency':
return t('This module provides currency exchange rates.');
}
}
/**
* Implementation of hook_menu().
*/
function currency_menu() {
$items['admin/settings/currency'] = array(
'title' => 'Currency',
'description' => 'Settings for currency exchange rates.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'currency_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
);
$items['currency'] = array(
'title' => 'Currency exchange',
'access arguments' => array(
'use currency',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'currency_form',
),
);
return $items;
}
/**
* Menu callback; module settings form.
*/
function currency_admin_settings() {
$form['currency_default_from'] = array(
'#type' => 'textfield',
'#title' => t('Default Currency From'),
'#default_value' => variable_get('currency_default_from', 'CAD'),
'#size' => 3,
'#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'),
'#default_value' => variable_get('currency_default_to', 'USD'),
'#size' => 3,
'#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);
}
/**
* Implementation of hook_perm().
*/
function currency_perm() {
return array(
'use currency',
);
}
/**
* Currency exchange form.
*/
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.')),
);
$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;
}
/**
* Validate handler for the currency exchange form.
*/
function currency_form_validate($form, &$form_state) {
if (!$form_state['values']['currency_amount']) {
form_set_error('currency_amount', t('Amount is required.'));
}
if (!is_numeric($form_state['values']['currency_amount'])) {
form_set_error('currency_amount', t('Invalid Amount. Please enter a valid numeric amount.'));
}
}
/**
* Submit handler for the currency exchange form.
*/
function currency_form_submit($form, &$form_state) {
// 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;
}
/**
* 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) {
$output .= t('Currency exchange error: ') . $ret['message'];
}
else {
$output .= '<p class="result">';
$output .= t('@amount @from = @value @to', array(
'@amount' => $amount,
'@from' => currency_api_get_desc($currency_from),
'@value' => $ret['value'],
'@to' => currency_api_get_desc($currency_to),
));
$output .= '</p>';
$output .= '<p class="detailed-history">' . l(t('Detailed history and chart'), $url) . '</p>';
}
$output = '<div class="currency-result">' . $output . '</div>';
return $output;
}
/**
* Implementation of hook_views().
*/
function currency_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'currency') . '/views',
);
}
Functions
Name | Description |
---|---|
currency_admin_settings | Menu callback; module settings form. |
currency_form | Currency exchange form. |
currency_form_submit | Submit handler for the currency exchange form. |
currency_form_validate | Validate handler for the currency exchange form. |
currency_help | Implementation of hook_help(). |
currency_menu | Implementation of hook_menu(). |
currency_perm | Implementation of hook_perm(). |
currency_theme | Implementation of hook_theme(). |
currency_views_api | Implementation of hook_views(). |
theme_currency_result | Theme implementation for currency exchange result. |