You are here

currency_api.module in Currency 7

This module provides an API for currency conversion.

File

currency_api/currency_api.module
View source
<?php

/**
 * @file
 * This module provides an API for currency conversion.
 */

// Copyright 2005 Khalid Baheyeldin http://2bits.com
// 3600 is once an hour
define('UPDATE_FREQUENCY', 3600);

/**
 * Implements hook_help().
 */
function currency_api_help($path, $arg) {
  switch ($path) {
    case 'admin/help#currency_api':
      return t('This module provides an API for currency conversion.');
  }
}

/**
 * Implements hook_theme().
 */
function currency_api_theme() {
  return array(
    'currency_api_amount' => array(
      'arguments' => array(
        'amount' => NULL,
        'attributes' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function currency_api_menu() {
  $items['admin/config/regional/currency_api'] = array(
    'title' => 'Currency API',
    'description' => 'Settings for currency API.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'currency_api_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Menu callback; module settings form.
 */
function currency_api_admin_settings() {
  $form['currency_api_watchdog'] = array(
    '#type' => 'checkbox',
    '#title' => t('Log all currency exchange requests to watchdog'),
    '#default_value' => variable_get('currency_api_watchdog', 1),
  );
  $period = drupal_map_assoc(array(
    900,
    1800,
    3600,
    10800,
    21600,
    32400,
    43200,
    86400,
  ), 'format_interval');
  $form['currency_api_fetch'] = array(
    '#type' => 'select',
    '#title' => t('Currency data update frequency'),
    '#default_value' => variable_get('currency_api_fetch', UPDATE_FREQUENCY),
    '#options' => $period,
    '#description' => t('How long to keep the currency data from Yahoo! Finance. Default is 1 hour (3600 seconds).'),
  );
  return system_settings_form($form);
}

/**
 * Currency exchange rate API function.
 *
 * This function converts two currencies using exchange rates from Yahoo Finance.
 * The currency codes are standard ISO 3-letter codes, and you can find the details
 * here:
 *  http://www.oanda.com/site/help/iso_code.shtml
 *
 * Here is an example on how to use it:
 *
 *   $from = 'CAD';
 *   $to   = 'USD';
 *   $amt  = 20;
 *   $ret  = currency_api_convert($from, $to, $amt);
 *   if ($ret['status'] == FALSE) {
 *     drupal_set_message(t('An error occured: '). $ret['message']);
 *   }
 *   else {
 *     print $amt . ' ' . $from . ' = ' . $ret['value'] . ' ' . $to;
 *   }
 *
 * @param $currency_from
 *   Currency to convert from.
 * @param $currency_to
 *   Currency to convert to.
 * @param $amount
 *   (optional) Amount to convert. Defaults to 1.
 * @param $decimals
 *   (optional) Number of digits to the right of the decimal point. Leave out this
 *   parameter if you want the actual currency result to proceess it yourself.
 *   Defaults to NULL.
 *
 * @return $result
 *   An associative array that contains the following:
 *    $result['status'] - TRUE or FALSE
 *    $result['message'] - 'success' when status is TRUE, otherwise, contains a
 *                         descriptive error text
 *   The following items are only returned when status is TRUE
 *    $result['value'] - $amount * exchange rate of $currency_from into $currency_to
 *    $result['rate'] - Exchange rate of $currency_from into $currency_to
 *    $result['timestamp'] - Timestamp of the last update to the rates
 *    $result['date'] - Date of the last update to the rates (Format is "m/d/yyyy")
 *    $result['time'] - Time of the last update to the rates (Format is "h:mmpm")
 */
function currency_api_convert($currency_from, $currency_to, $amount = 1, $decimals = NULL) {
  $currency_array = array(
    's' => 'Currencies',
    'l1' => 'Last',
    'd1' => 'Date',
    't1' => 'Time',
  );
  $result = array();
  $result['status'] = FALSE;
  $result['message'] = NULL;
  $result['value'] = 0;
  $result['rate'] = 1.0;
  $result['timestamp'] = NULL;
  $result['date'] = NULL;
  $result['time'] = NULL;
  $from = drupal_strtoupper($currency_from);
  $to = drupal_strtoupper($currency_to);
  if ($from == $to) {
    return array(
      'status' => TRUE,
      'message' => 'success',
      'value' => $amount,
      'rate' => 1.0,
      'timestamp' => REQUEST_TIME,
      'date' => date('n/j/Y'),
      'time' => date('g:ia'),
    );
  }

  // Load cached rate, if exists.
  $record = NULL;
  $cached = currency_api_load($record, $currency_from, $currency_to);
  if (!$record) {

    // Cached rate not found, go get it.
    $url = 'http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=' . currency_api_get_fields($currency_array) . '&s=' . $from . $to . '=X';

    // Validate the passed currency codes, to make sure they are valid.
    if (FALSE == currency_api_get_desc($from)) {
      $msg = t("currency: Invalid currency_from = %from", array(
        '%from' => $from,
      ));
      currency_log($msg, WATCHDOG_ERROR);
      $result['message'] = $msg;
      $result['status'] = FALSE;
    }
    if (FALSE == currency_api_get_desc($to)) {
      $msg = t("currency: Invalid currency_to = %to", array(
        '%to' => $to,
      ));
      currency_log($msg, WATCHDOG_ERROR);
      return FALSE;
      $result['message'] = $msg;
      $result['status'] = FALSE;
    }
    if (!is_numeric($amount)) {
      $msg = t("currency: Invalid amount = %amount", array(
        '%amount' => $amount,
      ));
      currency_log($msg, WATCHDOG_ERROR);
      $result['message'] = $msg;
      $result['status'] = FALSE;
    }
    $http_result = drupal_http_request($url);
    if (isset($http_result->error)) {
      $msg = t('currency: drupal_http_request error: @error', array(
        '@error' => $http_result->error,
      ));
      currency_log($msg, WATCHDOG_ERROR);
      return FALSE;
    }
    if ($http_result->code != 200) {
      $msg = t('currency: drupal_http_request code: @code', array(
        '@code' => $http_result->code,
      ));
      currency_log($msg, WATCHDOG_ERROR);
      return FALSE;
    }
    $record = $http_result->data;
  }
  if (!$record) {
    $msg = t('currency: cannot contact Yahoo Finance host');
    currency_log($msg, WATCHDOG_ERROR);
    $result['status'] = FALSE;
    $result['message'] = $msg;
  }
  else {
    $currency_data = explode(',', $record);
    $rate = $currency_data[1];
    $date = $currency_data[2];
    $time = $currency_data[3];
    $timestamp = strtotime(str_replace('"', '', $date) . ' ' . str_replace('"', '', $time));

    // Calculate the result.
    $value = $amount * $rate;

    // Format the result if $decimals value was specified.
    if ($decimals) {
      if (module_exists('format_number')) {
        $value = format_number($value, $decimals);
      }
      else {
        $value = number_format($value, $decimals);
      }
    }

    // Log it.
    $msg = t("currency: @amount @from = @value @to", array(
      '@amount' => $amount,
      '@from' => $from,
      '@value' => $value,
      '@to' => $to,
    ));
    currency_log($msg, WATCHDOG_NOTICE);

    // Got what we need.
    $result['value'] = $value;
    $result['rate'] = $rate;
    $result['timestamp'] = $timestamp;
    $result['date'] = $date;
    $result['time'] = $time;
    $result['status'] = TRUE;
    $result['message'] = 'success';
    if (!$cached) {

      // Cache rate does not exist, save it.
      currency_api_save($currency_from, $currency_to, $rate);
    }
  }
  return $result;
}

/**
 * Currency exchange API function.
 *
 * This function gets the currency name for a standard ISO 3-letter codes,
 * You can find the details here:
 *  http://www.oanda.com/site/help/iso_code.shtml
 *
 * Here is an example on how to use it:
 *
 *   $ccode = 'CAD';
 *   $ret = currency_get_description($ccode);
 *   if ($ret == FALSE) {
 *     drupal_set_message(t('Could not get description'));
 *   }
 *   else {
 *     print $ccode .' => '. $ret;
 *   }
 *
 * @param string $currency
 *   Currency code (3-letter ISO).
 *
 * @return $result
 *   Contains FALSE if the currency cannot be found, otherwise, it
 *   has the description.
 */
function currency_api_get_desc($currency) {
  $list = currency_api_get_list();
  if (isset($list[$currency])) {
    return $list[$currency];
  }
  return FALSE;
}

/**
 * Returns an array of all currency names.
 */
function currency_api_get_list() {
  static $list;
  if (!isset($list)) {
    $currencies = currency_api_get_currencies();
    $list = array();
    foreach ($currencies as $code => $currency) {
      $list[$code] = $currency['name'];
    }
  }
  return $list;
}

/**
 * Returns an array of all currency symbols.
 */
function currency_api_get_symbols() {
  static $symbols;
  if (!isset($symbols)) {
    $currencies = currency_api_get_currencies();
    $symbols = array();
    foreach ($currencies as $code => $currency) {
      $symbols[$code] = $currency['symbol'];
    }
  }
  return $symbols;
}

/**
 * Returns the symbol for a currency code.
 *
 * @param string $code
 *   Currency code (3-letter ISO).
 *
 * @return $result
 *   Returns the symbol for the provided currency code or NULL if not found.
 */
function currency_api_get_symbol($code) {
  $symbols = currency_api_get_symbols();
  return isset($symbols[$code]) ? $symbols[$code] : NULL;
}

/**
 * Helper function to build yahoo finance api request.
 */
function currency_api_get_fields($array) {
  $field_string = NULL;
  while (list($field, $header) = each($array)) {
    $field_string = $field_string . $field;
  }
  return $field_string;
}

/**
 * Helper function to log messages to watchdog.
 */
function currency_log($msg = '', $severity = WATCHDOG_NOTICE, $type = 'currency') {
  if ($severity == WATCHDOG_ERROR || variable_get('currency_api_watchdog', 1)) {
    watchdog($type, $msg, array(), $severity);
  }
}

/**
 * Fetch cached rate for from and to currencies.
 * Retrieve from static array variable, else from database.
 *
 * @return
 *   Rate by reference, true if exists otherwise false.
 */
function currency_api_load(&$record, $currency_from, $currency_to) {
  static $rate = array();
  $cached = TRUE;
  if (isset($rate[$currency_from][$currency_to])) {

    // retrieve cached rate from static array variable
    $record = $rate[$currency_from][$currency_to];
  }
  else {
    $result = db_query("SELECT * FROM {currencyapi} WHERE currency_from = :currency_from AND currency_to = :currency_to AND timestamp > :timestamp", array(
      ':currency_from' => $currency_from,
      ':currency_to' => $currency_to,
      ':timestamp' => REQUEST_TIME - variable_get('currency_api_fetch', UPDATE_FREQUENCY),
    ))
      ->fetchObject();
    if ($result) {
      $currency = array(
        $currency_from . $currency_to . '=X',
        $result->rate,
        date('n/j/Y', $result->timestamp),
        date('g:ia', $result->timestamp),
      );
      $record = implode(',', $currency);

      // cache rate in static array variable for subsequent queries
      $rate[$currency_from][$currency_to] = $record;
    }
    else {

      // rate does not exist in database cache
      $record = NULL;
      $cached = FALSE;
    }
  }
  return $cached;
}

/**
 * Cache rate for from and to countries delete outdated record, if exists.
 *
 * @param string $currency_from
 * @param string $currency_to
 * @param decimal $rate
 */
function currency_api_save($currency_from, $currency_to, $rate) {

  // Insert or update record.
  db_merge('currencyapi')
    ->key(array(
    'currency_from' => $currency_from,
    'currency_to' => $currency_to,
  ))
    ->fields(array(
    'rate' => $rate,
    'timestamp' => REQUEST_TIME,
  ))
    ->execute();
}

/**
 * Custom theme to display currency amount.
 *
 * Default output example: $200.00 USD
 *
 * @param string $amount
 * @param array $attributes
 *
 * @return string
 *   Formatted string.
 */
function theme_currency_api_amount($amount, $attributes) {
  return t('!symbol!amount !code', array(
    '!symbol' => $attributes['symbol'],
    '!amount' => number_format($amount, $attributes['decimals']),
    '!code' => $attributes['code'],
  ));
}

/**
 * Implements hook_filter_info().
 */
function currency_api_filter_info() {
  $filters['currency_exchange'] = array(
    'title' => t('Currency exchange'),
    'description' => t("Converts currency tokens ([currency:from:to:value:decimals]) to a currency exchange rate. The 'decimals' parameter is optional. Eg: [currency:EUR:USD:100:2]."),
    'process callback' => '_currency_api_filter',
  );
  return $filters;
}

/**
 * Converts currency tokens.
 */
function _currency_api_filter($text) {
  return preg_replace_callback('/\\[currency:(.*?)\\]/i', '_currency_api_filter_process', $text);
}

/**
 * Processes values for the currency api filter.
 */
function _currency_api_filter_process($matches) {
  $return = $matches[1];
  $convert = explode(':', $matches[1]);
  $result = currency_api_convert($convert[0], $convert[1], $convert[2], $convert[3]);
  if ($result['status']) {
    $return = $result['value'];
  }
  return $return;
}

/**
 * Returns an array of all currency properties.
 */
function currency_api_get_currencies() {
  $currencies = array(
    'AED' => array(
      'name' => t('UAE Dirham (AED)'),
      'number' => '784',
      'symbol' => 'د.إ',
      'decimals' => 2,
    ),
    'AFN' => array(
      'name' => t('Afghanistani Afghani (AFN)'),
      'number' => '971',
      'symbol' => '؋',
      'decimals' => 2,
    ),
    'ALL' => array(
      'name' => t('Albanian Lek (ALL)'),
      'number' => '008',
      'symbol' => 'Lek',
      'decimals' => 2,
    ),
    'AMD' => array(
      'decimals' => 2,
      'name' => t('Armenian dram (AMD)'),
      'number' => '051',
    ),
    'ANG' => array(
      'name' => t('Neth Antilles Guilder (ANG)'),
      'number' => '532',
      'symbol' => 'ƒ',
      'decimals' => 2,
    ),
    'AOA' => array(
      'decimals' => 2,
      'name' => t('Angolan kwanza (AOA)'),
      'number' => '973',
    ),
    'ARS' => array(
      'name' => t('Argentine Peso (ARS)'),
      'number' => '032',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'AUD' => array(
      'name' => t('Australian Dollar (AUD)'),
      'number' => '036',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'AWG' => array(
      'name' => t('Aruba Florin (AWG)'),
      'number' => '533',
      'symbol' => 'ƒ',
      'decimals' => 2,
    ),
    'AZN' => array(
      'name' => t('Azerbaijan New Maneat (AZN)'),
      'number' => '944',
      'symbol' => 'ман',
      'decimals' => 2,
    ),
    'BAM' => array(
      'name' => t('Bosnia and Herzegovina Convertible Marka (BAM)'),
      'number' => '977',
      'symbol' => 'KM',
      'decimals' => 2,
    ),
    'BBD' => array(
      'name' => t('Barbadian Dollar (BBD)'),
      'number' => '052',
      'symbol' => 'Bds$',
      'decimals' => 2,
    ),
    'BDT' => array(
      'name' => t('Bangladeshi Taka (BDT)'),
      'number' => '050',
      'symbol' => '৳, ৲',
      'decimals' => 2,
    ),
    'BGN' => array(
      'name' => t('Bulgarian Lev (BGN)'),
      'number' => '975',
      'symbol' => 'лв',
      'decimals' => 2,
    ),
    'BHD' => array(
      'name' => t('Bahraini Dinar (BHD)'),
      'number' => '048',
      'symbol' => '.د.ب',
      'decimals' => 2,
    ),
    'BIF' => array(
      'name' => t('Burundi Franc (BIF)'),
      'number' => '108',
      'symbol' => 'FBu',
      'decimals' => 2,
    ),
    'BMD' => array(
      'name' => t('Bermuda Dollar (BMD)'),
      'number' => '060',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'BND' => array(
      'name' => t('Brunei Dollar (BND)'),
      'number' => '096',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'BOB' => array(
      'name' => t('Bolivian Boliviano (BOB)'),
      'number' => '068',
      'symbol' => '$b',
      'decimals' => 2,
    ),
    'BOV' => array(
      'decimals' => 2,
      'name' => t('Bolivian Mvdol (BOV)'),
      'number' => '984',
    ),
    'BRL' => array(
      'name' => t('Brazilian Real (BRL)'),
      'number' => '986',
      'symbol' => 'R$',
      'decimals' => 2,
    ),
    'BSD' => array(
      'name' => t('Bahamian Dollar (BSD)'),
      'number' => '044',
      'symbol' => 'D',
      'decimals' => 2,
    ),
    'BTN' => array(
      'name' => t('Bhutanese Ngultrum (BTN)'),
      'number' => '064',
      'symbol' => 'Nu.',
      'decimals' => 2,
    ),
    'BWP' => array(
      'name' => t('Botswana Pula (BWP)'),
      'number' => '072',
      'symbol' => 'P',
      'decimals' => 2,
    ),
    'BYR' => array(
      'name' => t('Belarus Ruble (BYR)'),
      'number' => '974',
      'symbol' => 'p.',
      'decimals' => 2,
    ),
    'BZD' => array(
      'name' => t('Belize Dollar (BZD)'),
      'number' => '084',
      'symbol' => 'BZ$',
      'decimals' => 2,
    ),
    'CAD' => array(
      'name' => t('Canadian Dollar (CAD)'),
      'number' => '124',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'CDF' => array(
      'decimals' => 2,
      'name' => t('Congolese franc (CDF)'),
      'number' => '976',
    ),
    'CHE' => array(
      'decimals' => 2,
      'name' => t('WIR Euro (CHE)'),
      'number' => '947',
    ),
    'CHF' => array(
      'name' => t('Swiss Franc (CHF)'),
      'number' => '756',
      'symbol' => 'CHF',
      'decimals' => 2,
    ),
    'CHW' => array(
      'decimals' => 2,
      'name' => t('WIR Franc (CHW)'),
      'number' => '948',
    ),
    'CLF' => array(
      'decimals' => 0,
      'name' => t('Unidad de Fomento (CLF)'),
      'number' => '990',
    ),
    'CLP' => array(
      'name' => t('Chilean Peso (CLP)'),
      'number' => '152',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'CNY' => array(
      'name' => t('Chinese Yuan (CNY)'),
      'number' => '156',
      'symbol' => '元',
      'decimals' => 2,
    ),
    'COP' => array(
      'name' => t('Colombian Peso (COP)'),
      'number' => '170',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'COU' => array(
      'decimals' => 2,
      'name' => t('Unidad de Valor Real (COU)'),
      'number' => '970',
    ),
    'CRC' => array(
      'name' => t('Costa Rica Colon (CRC)'),
      'number' => '188',
      'symbol' => '₡',
      'decimals' => 2,
    ),
    'CUC' => array(
      'decimals' => 2,
      'name' => t('Cuban convertible peso (CUC)'),
      'number' => '931',
    ),
    'CUP' => array(
      'name' => t('Cuban Peso (CUP)'),
      'number' => '192',
      'symbol' => '₱',
      'decimals' => 2,
    ),
    'CVE' => array(
      'name' => t('Cape Verdean Escudo (CVE)'),
      'number' => '132',
      'symbol' => 'Esc',
      'decimals' => 2,
    ),
    'CYP' => array(
      'name' => t('Cyprus Pound (CYP)'),
      'symbol' => '£',
      'number' => '196',
      'decimals' => 2,
    ),
    'CZK' => array(
      'name' => t('Czech Koruna (CZK)'),
      'number' => '203',
      'symbol' => 'Kč',
      'decimals' => 2,
    ),
    'DJF' => array(
      'name' => t('Dijiboutian Franc (DJF)'),
      'number' => '262',
      'symbol' => 'Fdj',
      'decimals' => 2,
    ),
    'DKK' => array(
      'name' => t('Danish Krone (DKK)'),
      'number' => '208',
      'symbol' => 'kr',
      'decimals' => 2,
    ),
    'DOP' => array(
      'name' => t('Dominican Peso (DOP)'),
      'number' => '214',
      'symbol' => 'RD$',
      'decimals' => 2,
    ),
    'DZD' => array(
      'name' => t('Algerian Dinar (DZD)'),
      'number' => '012',
      'symbol' => 'دج',
      'decimals' => 2,
    ),
    'EEK' => array(
      'name' => t('Estonian Kroon (EEK)'),
      'number' => '233',
      'symbol' => 'kr',
      'decimals' => 2,
    ),
    'EGP' => array(
      'name' => t('Egyptian Pound (EGP)'),
      'number' => '818',
      'symbol' => 'LE',
      'decimals' => 2,
    ),
    'ERN' => array(
      'name' => t('Eritrean Nakfa (ERN)'),
      'number' => '232',
      'symbol' => 'Nfk',
      'decimals' => 2,
    ),
    'ETB' => array(
      'name' => t('Ethiopian Birr (ETB)'),
      'number' => '230',
      'symbol' => 'Br',
      'decimals' => 2,
    ),
    'EUR' => array(
      'name' => t('Euro (EUR)'),
      'number' => '978',
      'symbol' => '€',
      'decimals' => 2,
    ),
    'FJD' => array(
      'name' => t('Fiji Dollar (FJD)'),
      'number' => '242',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'FKP' => array(
      'name' => t('Falkland Islands Pound (FKP)'),
      'number' => '238',
      'symbol' => '£',
      'decimals' => 2,
    ),
    'GBP' => array(
      'name' => t('British Pound (GBP)'),
      'number' => '826',
      'symbol' => '£',
      'decimals' => 2,
    ),
    'GEL' => array(
      'decimals' => 2,
      'name' => t('Georgian lari (GEL)'),
      'number' => '981',
    ),
    'GGP' => array(
      'name' => t('Guernsey Pound (GGP)'),
      'number' => '',
      'symbol' => '£',
      'decimals' => 2,
    ),
    'GHC' => array(
      'name' => t('Ghanian Cedi (GHC)'),
      'number' => '288',
      'symbol' => '¢',
      'decimals' => 2,
    ),
    'GHS' => array(
      'decimals' => 2,
      'name' => t('Ghanaian cedi (GHS)'),
      'number' => '936',
    ),
    'GIP' => array(
      'name' => t('Gibraltar Pound (GIP)'),
      'number' => '292',
      'symbol' => '£',
      'decimals' => 2,
    ),
    'GMD' => array(
      'name' => t('Gambian Dalasi (GMD)'),
      'number' => '270',
      'symbol' => 'D',
      'decimals' => 2,
    ),
    'GNF' => array(
      'name' => t('Guinea Franc (GNF)'),
      'number' => '324',
      'symbol' => 'FG',
      'decimals' => 2,
    ),
    'GTQ' => array(
      'name' => t('Guatemala Quetzal (GTQ)'),
      'number' => '320',
      'symbol' => 'Q',
      'decimals' => 2,
    ),
    'GYD' => array(
      'name' => t('Guyana Dollar (GYD)'),
      'number' => '328',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'HKD' => array(
      'name' => t('Hong Kong Dollar (HKD)'),
      'number' => '344',
      'symbol' => 'HK$',
      'decimals' => 2,
    ),
    'HNL' => array(
      'name' => t('Honduras Lempira (HNL)'),
      'number' => '340',
      'symbol' => 'L',
      'decimals' => 2,
    ),
    'HRK' => array(
      'name' => t('Croatian Kuna (HRK)'),
      'number' => '191',
      'symbol' => 'kn',
      'decimals' => 2,
    ),
    'HTG' => array(
      'name' => t('Haiti Gourde (HTG)'),
      'number' => '332',
      'symbol' => 'G',
      'decimals' => 2,
    ),
    'HUF' => array(
      'name' => t('Hungarian Forint (HUF)'),
      'number' => '348',
      'symbol' => 'Ft',
      'decimals' => 0,
    ),
    'IDR' => array(
      'name' => t('Indonesian Rupiah (IDR)'),
      'number' => '360',
      'symbol' => 'Rp',
      'decimals' => 2,
    ),
    'ILS' => array(
      'name' => t('Israeli Shekel (ILS)'),
      'number' => '376',
      'symbol' => '₪',
      'decimals' => 2,
    ),
    'INR' => array(
      'name' => t('Indian Rupee (INR)'),
      'number' => '356',
      'symbol' => '₹',
      'decimals' => 2,
    ),
    'IQD' => array(
      'name' => t('Iraqi Dinar (IQD)'),
      'number' => '368',
      'symbol' => 'ع.د',
      'decimals' => 2,
    ),
    'IRR' => array(
      'name' => t('Iran Rial (IRR)'),
      'number' => '364',
      'symbol' => '﷼',
      'decimals' => 2,
    ),
    'ISK' => array(
      'name' => t('Iceland Krona (ISK)'),
      'number' => '352',
      'symbol' => 'kr',
      'decimals' => 2,
    ),
    'JMD' => array(
      'name' => t('Jamaican Dollar (JMD)'),
      'number' => '388',
      'symbol' => 'J$',
      'decimals' => 2,
    ),
    'JOD' => array(
      'name' => t('Jordanian Dinar (JOD)'),
      'number' => '400',
      'symbol' => 'din.',
      'decimals' => 2,
    ),
    'JPY' => array(
      'name' => t('Japanese Yen (JPY)'),
      'number' => '392',
      'symbol' => '¥',
      'decimals' => 0,
    ),
    'KES' => array(
      'name' => t('Kenyan Shilling (KES)'),
      'number' => '404',
      'symbol' => 'KSh',
      'decimals' => 2,
    ),
    'KGS' => array(
      'name' => t('Kyrgyzstan Som (KGS)'),
      'number' => '417',
      'symbol' => 'лв',
      'decimals' => 2,
    ),
    'KHR' => array(
      'name' => t('Cambodia Riel (KHR)'),
      'number' => '116',
      'symbol' => '៛',
      'decimals' => 2,
    ),
    'KMF' => array(
      'name' => t('Comoros Franc (KMF)'),
      'number' => '174',
      'symbol' => 'F',
      'decimals' => 2,
    ),
    'KPW' => array(
      'name' => t('North Korean Won (KPW)'),
      'number' => '408',
      'symbol' => '₩',
      'decimals' => 2,
    ),
    'KRW' => array(
      'name' => t('Korean Won (KRW)'),
      'number' => '410',
      'symbol' => '₩',
      'decimals' => 2,
    ),
    'KWD' => array(
      'name' => t('Kuwaiti Dinar (KWD)'),
      'number' => '414',
      'symbol' => 'د.ك',
      'decimals' => 2,
    ),
    'KYD' => array(
      'name' => t('Cayman Islands Dollar (KYD)'),
      'number' => '136',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'KZT' => array(
      'name' => t('Kazakhstan Tenge (KZT)'),
      'number' => '398',
      'symbol' => 'лв',
      'decimals' => 2,
    ),
    'LAK' => array(
      'name' => t('Lao Kip (LAK)'),
      'number' => '418',
      'symbol' => '₭',
      'decimals' => 2,
    ),
    'LBP' => array(
      'name' => t('Lebanese Pound (LBP)'),
      'number' => '422',
      'symbol' => '£',
      'decimals' => 2,
    ),
    'LKR' => array(
      'name' => t('Sri Lanka Rupee (LKR)'),
      'number' => '144',
      'symbol' => '₨',
      'decimals' => 2,
    ),
    'LRD' => array(
      'name' => t('Liberian Dollar (LRD)'),
      'number' => '430',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'LSL' => array(
      'name' => t('Lesotho Loti (LSL)'),
      'number' => '426',
      'symbol' => 'M',
      'decimals' => 2,
    ),
    'LTL' => array(
      'name' => t('Lithuanian Lita (LTL)'),
      'number' => '440',
      'symbol' => 'Lt',
      'decimals' => 2,
    ),
    'LVL' => array(
      'name' => t('Latvian Lat (LVL)'),
      'number' => '428',
      'symbol' => 'Ls',
      'decimals' => 2,
    ),
    'LYD' => array(
      'name' => t('Libyan Dinar (LYD)'),
      'number' => '434',
      'symbol' => 'ل.د',
      'decimals' => 2,
    ),
    'MAD' => array(
      'name' => t('Moroccan Dirham (MAD)'),
      'number' => '504',
      'symbol' => 'د.م.',
      'decimals' => 2,
    ),
    'MDL' => array(
      'name' => t('Moldovan Leu (MDL)'),
      'number' => '498',
      'symbol' => 'lei',
      'decimals' => 2,
    ),
    'MGA' => array(
      'name' => t('Malagasy ariary (MGA)'),
      'number' => '969',
      'symbol' => 'Ar',
      'decimals' => 0,
    ),
    'MKD' => array(
      'name' => t('Macedonian Denar (MKD)'),
      'number' => '807',
      'symbol' => 'ден',
      'decimals' => 2,
    ),
    'MMK' => array(
      'name' => t('Myanmar Kyat (MMK)'),
      'number' => '104',
      'symbol' => 'K',
      'decimals' => 2,
    ),
    'MNT' => array(
      'name' => t('Mongolian Tugrik (MNT)'),
      'number' => '496',
      'symbol' => '₮',
      'decimals' => 2,
    ),
    'MOP' => array(
      'name' => t('Macau Pataca (MOP)'),
      'number' => '446',
      'symbol' => 'MOP$',
      'decimals' => 2,
    ),
    'MRO' => array(
      'name' => t('Mauritania Ougulya (MRO)'),
      'number' => '478',
      'symbol' => 'UM',
      'decimals' => 0,
    ),
    'MTL' => array(
      'name' => t('Maltese Lira (MTL)'),
      'number' => '470',
      'symbol' => 'Lm',
      'decimals' => 2,
    ),
    'MUR' => array(
      'name' => t('Mauritius Rupee (MUR)'),
      'number' => '480',
      'symbol' => '₨',
      'decimals' => 2,
    ),
    'MVR' => array(
      'name' => t('Maldives Rufiyaa (MVR)'),
      'number' => '462',
      'symbol' => 'Rf',
      'decimals' => 2,
    ),
    'MWK' => array(
      'name' => t('Malawian Kwacha (MWK)'),
      'number' => '454',
      'symbol' => 'MK',
      'decimals' => 2,
    ),
    'MXN' => array(
      'name' => t('Mexican Peso (MXN)'),
      'number' => '484',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'MXV' => array(
      'decimals' => 2,
      'name' => t('Mexican Unidad de Inversion (MXV)'),
      'number' => '979',
    ),
    'MYR' => array(
      'name' => t('Malaysian Ringgit (MYR)'),
      'number' => '458',
      'symbol' => 'RM',
      'decimals' => 2,
    ),
    'MZM' => array(
      'name' => t('Mozambique Metical (MZM)'),
      'number' => '508',
      'symbol' => 'MT',
      'decimals' => 2,
    ),
    'MZN' => array(
      'decimals' => 2,
      'name' => t('Mozambican metical (MZN)'),
      'number' => '943',
    ),
    'NAD' => array(
      'name' => t('Namibian Dollar (NAD)'),
      'number' => '516',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'NGN' => array(
      'name' => t('Nigerian Naira (NGN)'),
      'number' => '566',
      'symbol' => '₦',
      'decimals' => 2,
    ),
    'NIO' => array(
      'name' => t('Nicaragua Cordoba (NIO)'),
      'number' => '558',
      'symbol' => 'C$',
      'decimals' => 2,
    ),
    'NOK' => array(
      'name' => t('Norwegian Krone (NOK)'),
      'number' => '578',
      'symbol' => 'kr',
      'decimals' => 2,
    ),
    'NPR' => array(
      'name' => t('Nepalese Rupee (NPR)'),
      'number' => '524',
      'symbol' => '₨',
      'decimals' => 2,
    ),
    'NZD' => array(
      'name' => t('New Zealand Dollar (NZD)'),
      'number' => '554',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'OMR' => array(
      'name' => t('Omani Rial (OMR)'),
      'number' => '512',
      'symbol' => '﷼',
      'decimals' => 2,
    ),
    'PAB' => array(
      'name' => t('Panama Balboa (PAB)'),
      'number' => '590',
      'symbol' => 'B/.',
      'decimals' => 2,
    ),
    'PEN' => array(
      'name' => t('Peruvian Nuevo Sol (PEN)'),
      'number' => '604',
      'symbol' => 'S/.',
      'decimals' => 2,
    ),
    'PGK' => array(
      'name' => t('Papua New Guinea Kina (PGK)'),
      'number' => '598',
      'symbol' => 'K',
      'decimals' => 2,
    ),
    'PHP' => array(
      'name' => t('Philippine Peso (PHP)'),
      'number' => '608',
      'symbol' => 'Php',
      'decimals' => 2,
    ),
    'PKR' => array(
      'name' => t('Pakistani Rupee (PKR)'),
      'number' => '586',
      'symbol' => '₨',
      'decimals' => 2,
    ),
    'PLN' => array(
      'name' => t('Polish Zloty (PLN)'),
      'number' => '985',
      'symbol' => 'zł',
      'decimals' => 2,
    ),
    'PYG' => array(
      'name' => t('Paraguayan Guarani (PYG)'),
      'number' => '600',
      'symbol' => 'Gs',
      'decimals' => 2,
    ),
    'QAR' => array(
      'name' => t('Qatar Rial (QAR)'),
      'number' => '634',
      'symbol' => '﷼',
      'decimals' => 2,
    ),
    'RON' => array(
      'name' => t('Romanian New Leu (RON)'),
      'number' => '946',
      'symbol' => 'lei',
      'decimals' => 2,
    ),
    'RSD' => array(
      'name' => t('Serbian Dinar (RSD)'),
      'number' => '941',
      'symbol' => 'Дин.',
      'decimals' => 2,
    ),
    'RUB' => array(
      'name' => t('Russian Rouble (RUB)'),
      'number' => '643',
      'symbol' => 'руб.',
      'decimals' => 2,
    ),
    'RWF' => array(
      'name' => t('Rwandese Franc (RWF)'),
      'number' => '646',
      'symbol' => 'RF',
      'decimals' => 2,
    ),
    'SAR' => array(
      'name' => t('Saudi Arabian Riyal (SAR)'),
      'number' => '682',
      'symbol' => '﷼',
      'decimals' => 2,
    ),
    'SBD' => array(
      'name' => t('Solomon Islands Dollar (SBD)'),
      'number' => '090',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'SCR' => array(
      'name' => t('Seychelles Rupee (SCR)'),
      'number' => '690',
      'symbol' => '₨',
      'decimals' => 2,
    ),
    'SDG' => array(
      'name' => t('Sudanese Pound (SDG)'),
      'number' => '938',
      'symbol' => 'SDG',
      'decimals' => 2,
    ),
    'SEK' => array(
      'name' => t('Swedish Krona (SEK)'),
      'number' => '752',
      'symbol' => 'kr',
      'decimals' => 2,
    ),
    'SGD' => array(
      'name' => t('Singapore Dollar (SGD)'),
      'number' => '702',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'SHP' => array(
      'name' => t('St Helena Pound (SHP)'),
      'number' => '654',
      'symbol' => '£',
      'decimals' => 2,
    ),
    'SKK' => array(
      'name' => t('Slovak Koruna (SKK)'),
      'number' => '703',
      'symbol' => 'SIT',
      'decimals' => 2,
    ),
    'SLL' => array(
      'name' => t('Sierra Leone Leone (SLL)'),
      'number' => '694',
      'symbol' => 'Le',
      'decimals' => 2,
    ),
    'SOS' => array(
      'name' => t('Somali Shilling (SOS)'),
      'number' => '706',
      'symbol' => 'S',
      'decimals' => 2,
    ),
    'SRD' => array(
      'name' => t('Surinam Dollar (SRD)'),
      'number' => '968',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'SSP' => array(
      'decimals' => 2,
      'name' => t('South Sudanese pound (SSP)'),
      'number' => '728',
    ),
    'STD' => array(
      'name' => t('Sao Tome Dobra (STD)'),
      'number' => '678',
      'symbol' => 'Db',
      'decimals' => 2,
    ),
    'SVC' => array(
      'name' => t('El Salvador Colon (SVC)'),
      'number' => '222',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'SYP' => array(
      'name' => t('Syrian Pound (SYP)'),
      'number' => '760',
      'symbol' => '£',
      'decimals' => 2,
    ),
    'SZL' => array(
      'name' => t('Swaziland Lilageni (SZL)'),
      'number' => '748',
      'symbol' => 'E',
      'decimals' => 2,
    ),
    'THB' => array(
      'name' => t('Thai Baht (THB)'),
      'number' => '764',
      'symbol' => '฿',
      'decimals' => 2,
    ),
    'TJS' => array(
      'decimals' => 2,
      'name' => t('Tajikistani somoni (TJS)'),
      'number' => '972',
    ),
    'TMT' => array(
      'decimals' => 2,
      'name' => t('Turkmenistani manat (TMT)'),
      'number' => '934',
    ),
    'TND' => array(
      'name' => t('Tunisian Dinar (TND)'),
      'number' => '788',
      'symbol' => 'د.ت',
      'decimals' => 2,
    ),
    'TOP' => array(
      'name' => t('Tonga Pa\'anga (TOP)'),
      'number' => '776',
      'symbol' => 'T$',
      'decimals' => 2,
    ),
    'TRY' => array(
      'name' => t('Turkish Lira (TRY)'),
      'number' => '949',
      'symbol' => 'TL',
      'decimals' => 2,
    ),
    'TTD' => array(
      'name' => t('Trinidad & Tobago Dollar (TTD)'),
      'number' => '780',
      'symbol' => 'TT$',
      'decimals' => 2,
    ),
    'TWD' => array(
      'name' => t('Taiwan Dollar (TWD)'),
      'number' => '901',
      'symbol' => 'NT$',
      'decimals' => 2,
    ),
    'TZS' => array(
      'name' => t('Tanzanian Shilling (TZS)'),
      'number' => '834',
      'symbol' => 'TZS',
      'decimals' => 2,
    ),
    'UAH' => array(
      'name' => t('Ukraine Hryvnia (UAH)'),
      'number' => '980',
      'symbol' => 'грн.',
      'decimals' => 2,
    ),
    'UGX' => array(
      'name' => t('Ugandan Shilling (UGX)'),
      'number' => '800',
      'symbol' => 'USh',
      'decimals' => 2,
    ),
    'USD' => array(
      'name' => t('U.S. Dollar (USD)'),
      'number' => '840',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'USN' => array(
      'decimals' => 2,
      'name' => t('United States dollar (USN)'),
      'number' => '997',
    ),
    'USS' => array(
      'decimals' => 2,
      'name' => t('United States dollar (same day) (USS)'),
      'number' => '998',
    ),
    'UYI' => array(
      'decimals' => 0,
      'name' => t('Uruguay Peso en Unidades Indexadas (UYI)'),
      'number' => '940',
    ),
    'UYU' => array(
      'name' => t('Uruguayan New Peso (UYU)'),
      'number' => '858',
      'symbol' => '$U',
      'decimals' => 2,
    ),
    'UZS' => array(
      'name' => t('Uzbekistan Sum (UZS)'),
      'number' => '860',
      'symbol' => 'лв',
      'decimals' => 2,
    ),
    'VEB' => array(
      'name' => t('Venezuelan Bolivar (VEB)'),
      'number' => '862',
      'symbol' => 'Bs',
      'decimals' => 2,
    ),
    'VEF' => array(
      'decimals' => 2,
      'name' => t('Venezuelan bolívar fuerte (VEF)'),
      'number' => '937',
    ),
    'VND' => array(
      'name' => t('Vietnam Dong (VND)'),
      'number' => '704',
      'symbol' => '₫',
      'decimals' => 2,
    ),
    'VUV' => array(
      'name' => t('Vanuatu Vatu (VUV)'),
      'number' => '548',
      'symbol' => 'Vt',
      'decimals' => 2,
    ),
    'WST' => array(
      'name' => t('Samoan Tala (WST)'),
      'number' => '882',
      'symbol' => 'WS$',
      'decimals' => 2,
    ),
    'XAF' => array(
      'name' => t('CFA Franc (BEAC) (XAF)'),
      'number' => '950',
      'symbol' => 'F',
      'decimals' => 2,
    ),
    'XAG' => array(
      'name' => t('Silver Ounces (XAG)'),
      'number' => '961',
      'symbol' => 'XAG',
      'decimals' => 2,
    ),
    'XAU' => array(
      'name' => t('Gold Ounces (XAU)'),
      'number' => '959',
      'symbol' => 'XAU',
      'decimals' => 2,
    ),
    'XBA' => array(
      'decimals' => 0,
      'name' => t('European Composite Unit (EURCO) (XBA)'),
      'number' => '955',
    ),
    'XBB' => array(
      'decimals' => 0,
      'name' => t('European Monetary Unit (E.M.U.-6) (XBB)'),
      'number' => '956',
    ),
    'XBC' => array(
      'decimals' => 0,
      'name' => t('European Unit of Account 9 (E.U.A.-9) (XBC)'),
      'number' => '957',
    ),
    'XBD' => array(
      'decimals' => 0,
      'name' => t('European Unit of Account 17 (E.U.A.-17) (XBD)'),
      'number' => '958',
    ),
    'XCD' => array(
      'name' => t('East Caribbean Dollar (XCD)'),
      'number' => '951',
      'symbol' => '$',
      'decimals' => 2,
    ),
    'XDR' => array(
      'decimals' => 0,
      'name' => t('Special drawing rights (XDR)'),
      'number' => '960',
    ),
    'XFU' => array(
      'decimals' => 0,
      'name' => t('UIC franc (XFU)'),
    ),
    'XOF' => array(
      'name' => t('CFA Franc (BCEAO) (XOF)'),
      'number' => '952',
      'symbol' => 'F',
      'decimals' => 2,
    ),
    'XPD' => array(
      'name' => t('Palladium Ounces (XPD)'),
      'number' => '964',
      'symbol' => 'XPD',
      'decimals' => 2,
    ),
    'XPF' => array(
      'name' => t('Pacific Franc (XPF)'),
      'number' => '953',
      'symbol' => 'F',
      'decimals' => 2,
    ),
    'XPT' => array(
      'name' => t('Platinum Ounces (XPT)'),
      'number' => '962',
      'symbol' => 'XPT',
      'decimals' => 2,
    ),
    'XTS' => array(
      'decimals' => 0,
      'name' => t('Code reserved for testing purposes (XTS)'),
      'number' => '963',
    ),
    'XXX' => array(
      'decimals' => 0,
      'name' => t('No currency (XXX)'),
      'number' => '999',
    ),
    'YER' => array(
      'name' => t('Yemen Riyal (YER)'),
      'number' => '886',
      'symbol' => '﷼',
      'decimals' => 2,
    ),
    'YUM' => array(
      'name' => t('Yugoslav Dinar (YUM)'),
      'number' => '891',
      'symbol' => 'дин',
      'decimals' => 2,
    ),
    'ZAR' => array(
      'name' => t('South African Rand (ZAR)'),
      'number' => '710',
      'symbol' => 'R',
      'decimals' => 2,
    ),
    'ZMK' => array(
      'name' => t('Zambian Kwacha (ZMK)'),
      'number' => '894',
      'symbol' => 'ZK',
      'decimals' => 2,
    ),
    'ZMW' => array(
      'name' => t('Zambian Kwacha (ZMW)'),
      'number' => '967',
      'symbol' => 'ZK',
      'decimals' => 2,
    ),
    'ZWD' => array(
      'name' => t('Zimbabwe Dollar (ZWD)'),
      'number' => '716',
      'symbol' => 'Z$',
      'decimals' => 2,
    ),
  );

  // Merge in defaults.
  foreach ($currencies as &$currency) {
    $currency += array(
      'name' => '',
      'number' => '',
      'symbol' => '',
    );
  }
  return $currencies;
}

Functions

Namesort descending Description
currency_api_admin_settings Menu callback; module settings form.
currency_api_convert Currency exchange rate API function.
currency_api_filter_info Implements hook_filter_info().
currency_api_get_currencies Returns an array of all currency properties.
currency_api_get_desc Currency exchange API function.
currency_api_get_fields Helper function to build yahoo finance api request.
currency_api_get_list Returns an array of all currency names.
currency_api_get_symbol Returns the symbol for a currency code.
currency_api_get_symbols Returns an array of all currency symbols.
currency_api_help Implements hook_help().
currency_api_load Fetch cached rate for from and to currencies. Retrieve from static array variable, else from database.
currency_api_menu Implements hook_menu().
currency_api_save Cache rate for from and to countries delete outdated record, if exists.
currency_api_theme Implements hook_theme().
currency_log Helper function to log messages to watchdog.
theme_currency_api_amount Custom theme to display currency amount.
_currency_api_filter Converts currency tokens.
_currency_api_filter_process Processes values for the currency api filter.

Constants

Namesort descending Description
UPDATE_FREQUENCY