| 1 |
<?php
|
| 2 |
|
| 3 |
//$Id: currency.module,v 1.8 2007/04/08 15:33:35 wafaa Exp $
|
| 4 |
|
| 5 |
// Copyright 2005 Khalid Baheyeldin http://2bits.com
|
| 6 |
|
| 7 |
function currency_help($section) {
|
| 8 |
switch ($section) {
|
| 9 |
case 'admin/help#currency':
|
| 10 |
return t('This module provides currency exchange rates.');
|
| 11 |
}
|
| 12 |
}
|
| 13 |
|
| 14 |
function currency_menu($may_cache) {
|
| 15 |
$items = array();
|
| 16 |
if ($may_cache) {
|
| 17 |
$items[] = array(
|
| 18 |
'path' => 'admin/settings/currency',
|
| 19 |
'title' => t('Currency'),
|
| 20 |
'description' => t('Settings for currency exchange rates.'),
|
| 21 |
'callback' => 'drupal_get_form',
|
| 22 |
'callback arguments' => array('currency_admin_settings'),
|
| 23 |
'access' => user_access('administer site configuration'),
|
| 24 |
'type' => MENU_NORMAL_ITEM, // optional
|
| 25 |
);
|
| 26 |
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'currency',
|
| 29 |
'title' => t('Currency exchange'),
|
| 30 |
'access' => user_access('use currency'),
|
| 31 |
'callback' => 'drupal_get_form',
|
| 32 |
'callback arguments' => array('currency_form'),
|
| 33 |
);
|
| 34 |
}
|
| 35 |
return $items;
|
| 36 |
}
|
| 37 |
|
| 38 |
function currency_admin_settings() {
|
| 39 |
$form['currency_default_from'] = array(
|
| 40 |
'#type' => 'textfield',
|
| 41 |
'#title' => t('Default Currency From'),
|
| 42 |
'#default_value' => variable_get('currency_default_from', 'CAD'),
|
| 43 |
'#size' => 3,
|
| 44 |
'#maxlength' => 3,
|
| 45 |
'#description' => t('Three letter symbol for default currency to convert from.'),
|
| 46 |
);
|
| 47 |
|
| 48 |
$form['currency_default_to'] = array(
|
| 49 |
'#type' => 'textfield',
|
| 50 |
'#title' => t('Default Currency To'),
|
| 51 |
'#default_value' => variable_get('currency_default_to', 'USD'),
|
| 52 |
'#size' => 3,
|
| 53 |
'#maxlength' => 3,
|
| 54 |
'#description' => t('Three letter symbol for default currency to convert to.'),
|
| 55 |
);
|
| 56 |
|
| 57 |
$form['currency_description'] = array(
|
| 58 |
'#type' => 'textarea',
|
| 59 |
'#title' => t('Description'),
|
| 60 |
'#default_value' => variable_get('currency_description', t('This is the currency exchange rate page.')),
|
| 61 |
'#cols' => 70,
|
| 62 |
'#rows' => 7,
|
| 63 |
'#description' => t('This text will be displayed at the top of the currency exchange page'),
|
| 64 |
);
|
| 65 |
|
| 66 |
$form['currency_overview_title'] = array(
|
| 67 |
'#type' => 'textfield',
|
| 68 |
'#title' => t('Navigation link text'),
|
| 69 |
'#default_value' => variable_get('currency_overview_title', t('Currency exchange')),
|
| 70 |
'#size' => 35,
|
| 71 |
'#maxlength' => 255,
|
| 72 |
'#description' => t('The text in the navigation link which points to the currency exchange page.'),
|
| 73 |
);
|
| 74 |
|
| 75 |
return system_settings_form($form);
|
| 76 |
}
|
| 77 |
|
| 78 |
function currency_perm() {
|
| 79 |
return array ('use currency');
|
| 80 |
}
|
| 81 |
|
| 82 |
function currency_form($data = array()) {
|
| 83 |
// Get the saved data from the session, if any
|
| 84 |
$amount = $_SESSION['currency_amount'] ? $_SESSION['currency_amount'] : 1;
|
| 85 |
$from = $_SESSION['currency_from'] ? $_SESSION['currency_from'] : variable_get('currency_default_from', 'CAD');
|
| 86 |
$to = $_SESSION['currency_to'] ? $_SESSION['currency_to'] : variable_get('currency_default_to', 'USD');
|
| 87 |
|
| 88 |
$form['currency_amount'] = array(
|
| 89 |
'#type' => 'textfield',
|
| 90 |
'#title' => t('Amount'),
|
| 91 |
'#default_value' => $amount,
|
| 92 |
'#size' => 9,
|
| 93 |
'#maxlength' => 9,
|
| 94 |
'#description' => t('Amount to convert'),
|
| 95 |
);
|
| 96 |
|
| 97 |
$form['currency_from'] = array(
|
| 98 |
'#type' => 'select',
|
| 99 |
'#title' => t('From'),
|
| 100 |
'#default_value' => $from,
|
| 101 |
'#options' => currency_api_get_list(),
|
| 102 |
);
|
| 103 |
|
| 104 |
$form['currency_to'] = array(
|
| 105 |
'#type' => 'select',
|
| 106 |
'#title' => t('To'),
|
| 107 |
'#default_value' => $to,
|
| 108 |
'#options' => currency_api_get_list(),
|
| 109 |
);
|
| 110 |
|
| 111 |
$form['submit'] = array(
|
| 112 |
'#type' => 'submit',
|
| 113 |
'#value' => t('Convert'),
|
| 114 |
);
|
| 115 |
|
| 116 |
return $form;
|
| 117 |
}
|
| 118 |
|
| 119 |
function currency_form_validate($form_id, $form_values) {
|
| 120 |
if (!$form_values['currency_amount']) {
|
| 121 |
form_set_error('', t('Amount is required.'));
|
| 122 |
}
|
| 123 |
|
| 124 |
if (!is_numeric($form_values['currency_amount'])) {
|
| 125 |
form_set_error('', t('Invalid Amount. Please enter a valid numeric amount.'));
|
| 126 |
}
|
| 127 |
}
|
| 128 |
|
| 129 |
function currency_form_submit($form_id, $form_values) {
|
| 130 |
$from = $form_values['currency_from'];
|
| 131 |
$to = $form_values['currency_to'];
|
| 132 |
$amount = $form_values['currency_amount'];
|
| 133 |
$url = 'http://finance.yahoo.com/q?s=' . $from . $to . '=X';
|
| 134 |
|
| 135 |
$ret = currency_api_convert($from, $to, $amount);
|
| 136 |
if ($ret['status'] == FALSE) {
|
| 137 |
drupal_set_message(t('currency exchange error: ') . $ret['message']);
|
| 138 |
}
|
| 139 |
else {
|
| 140 |
$result .= '<p>';
|
| 141 |
$result .= t('@amount @from = @value @to', array(
|
| 142 |
'@amount' => $amount,
|
| 143 |
'@from' => currency_api_get_desc($from),
|
| 144 |
'@value' => $ret['value'],
|
| 145 |
'@to' => currency_api_get_desc($to)));
|
| 146 |
$result .= '</p><p>';
|
| 147 |
$result .= l(t('Detailed history and chart'), $url);
|
| 148 |
$result .= '</p>';
|
| 149 |
}
|
| 150 |
|
| 151 |
// Save the last used values in the session
|
| 152 |
$_SESSION['currency_amount'] = $amount;
|
| 153 |
$_SESSION['currency_from'] = $from;
|
| 154 |
$_SESSION['currency_to'] = $to;
|
| 155 |
|
| 156 |
drupal_set_message($result);
|
| 157 |
}
|