From c54d20d32372b52f626d93855495e9c9dcc136de Mon Sep 17 00:00:00 2001 From: abacab Date: Mon, 29 Dec 2008 13:00:08 -0500 Subject: [PATCH] Added automatic currency conversion update. --- modules/currency/currency.inc.php | 58 +++++++++++++++++++++++++++++- modules/task/task_install_data.xml | 18 ++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/modules/currency/currency.inc.php b/modules/currency/currency.inc.php index 3235a2f1..6e99bff7 100644 --- a/modules/currency/currency.inc.php +++ b/modules/currency/currency.inc.php @@ -124,5 +124,61 @@ class currency $db = new CORE_database; $db->search_show($VAR, $this, $type); } + + function task($VAR) + { + $db = &DB(); + + // Fetch all active currencies + $currencies = array(); + $rs = $db->Execute(sqlSelect($db, "currency", "*", "status=1")); + if ($rs && $rs->RecordCount() > 0) + { + while (!$rs->EOF) + { + $currencies[$rs->fields['id']] = $rs->fields; + $rs->MoveNext(); + } + } + $rs->Close(); + + foreach ($currencies as $currFrom) + { + $conversions = array(); + foreach ($currencies as $currTo) + { + // Get currency conversion + if ($currFrom['three_digit'] != $currTo['three_digit']) + { + $ch = curl_init('http://www.xe.net/ucc/convert.cgi?Amount=1&From=' . $currFrom['three_digit'] . '&To=' . $currTo['three_digit']); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); + curl_setopt($ch, CURLOPT_CRLF, true); + $resp = curl_exec ($ch); + curl_close ($ch); + + $m = array(); + preg_match('/[0-9.]+\s*' . $currFrom['three_digit'] . '\s*=\s*([0-9.]+)\s*' . $currTo['three_digit'] . '/', $resp, $m); + } + else + { + // Conversion to/from same currency is always 1. + $m = array(1 => '1'); + } + + if (sizeof($m) > 0) + { + $conversions[$currTo['id']] = array ( + 'rate' => $m[1] + ,'iso' => $currTo['three_digit'] + ); + } + } + + // Update conversions array + $db->Execute('UPDATE ' . AGILE_DB_PREFIX . 'currency SET convert_array = ' . $db->qstr(serialize($conversions)) . ' WHERE id = ' . $db->qstr($currFrom['id']) . ' AND site_id = ' . $db->qstr($currFrom['site_id'])); + } + } } -?> \ No newline at end of file +?> diff --git a/modules/task/task_install_data.xml b/modules/task/task_install_data.xml index 1aebb474..ca7bbb06 100644 --- a/modules/task/task_install_data.xml +++ b/modules/task/task_install_data.xml @@ -285,8 +285,22 @@ 67 + 1 + 0 + 0 + 0 + Currency Conversion Update + Retrieves currency conversions from XE.com and updates. + 0 + currency:task + 0 + 0 + * + * + 2 + 0 - 67 + 68 - \ No newline at end of file +