You are here

function currency_api_load in Currency 5

Same name and namespace in other branches
  1. 6 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 countries retrieve from static array variable, else from database returns: 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 610

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 > UNIX_TIMESTAMP( ) - %d", $currency_from, $currency_to, 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;
}