You are here

function currency_api_load in Currency 6

Same name and namespace in other branches
  1. 5 currency_api/currency_api.module \currency_api_load()
  2. 7 currency_api/currency_api.module \currency_api_load()

Fetch cached rate for from and to currencies. Retrieve from static array variable, else from database.

Return value

Rate by reference, true if exists otherwise false.

1 call to currency_api_load()
currency_api_convert in currency_api/currency_api.module
Currency exchange rate API function.

File

currency_api/currency_api.module, line 346
This module provides an API for currency conversion.

Code

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_fetch_object(db_query("SELECT * FROM {currencyapi} WHERE currency_from = '%s' AND currency_to = '%s' AND timestamp > %d", $currency_from, $currency_to, time() - variable_get('currency_api_fetch', UPDATE_FREQUENCY)));
    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;
}