You are here

function currency_api_load in Currency 7

Same name and namespace in other branches
  1. 5 currency_api/currency_api.module \currency_api_load()
  2. 6 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 347
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_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;
}