i’ve got the snippet working okay but there are two things I need some help with.
<?php
class CurrencyConverter {
var $xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
var $exchange_rates = array();
function CurrencyConverter() {
// I can't get the 'exchanger' value from the snippet call inside the function, which means manually entering the table name here, rather than in the snippet call //
$this->mysql_table = 'exchanger';
$this->checkLastUpdated();
$res = mysql_query('SELECT * FROM `'.$this->mysql_table.'`');
while($row = mysql_fetch_array($res)) {
$this->exchange_rates[$row['currency']] = $row['rate'];
}
}
function convert($amount=1,$from="GBP",$to="USD",$decimals=2) {
return(number_format(($amount/$this->exchange_rates[$from])*$this->exchange_rates[$to],$decimals));
}
function checkLastUpdated() {
$rts = mysql_query('SHOW TABLE STATUS FROM `dev` LIKE \''.$this->mysql_table.'\'');
if(mysql_num_rows($rts) == 0 ) {
$this->createTable();
} else {
$row = mysql_fetch_array($rts);
if(time() > (strtotime($row["Update_time"])+(12*60*60)) ) {
$this->downloadExchangeRates();
}
}
}
function downloadExchangeRates() {
$currency_domain = substr($this->xml_file,0,strpos($this->xml_file,"/"));
$currency_file = substr($this->xml_file,strpos($this->xml_file,"/"));
$fp = @fsockopen($currency_domain, 80, $errno, $errstr, 10);
if($fp) {
$out = "GET ".$currency_file." HTTP/1.1\r\n";
$out .= "Host: ".$currency_domain."\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$buffer .= fgets($fp, 128);
}
fclose($fp);
$pattern = "{<Cube\s*currency='(\w*)'\s*rate='([\d\.]*)'/>}is";
preg_match_all($pattern,$buffer,$xml_rates);
array_shift($xml_rates);
for($i=0;$i<count($xml_rates[0]);$i++) {
$exchange_rate[$xml_rates[0][$i]] = $xml_rates[1][$i];
}
foreach($exchange_rate as $currency=>$rate) {
if((is_numeric($rate)) && ($rate != 0)) {
$rs = mysql_query('SELECT * FROM `'.$this->mysql_table.'` WHERE currency="'.$currency.'"') or die(mysql_error());
if(mysql_num_rows($rs) > 0) {
$sql = mysql_query('UPDATE `'.$this->mysql_table.'` SET rate="'.$rate.'" WHERE currency="'.$currency.'"') or die(mysql_error());
} else {
$sql = mysql_query('INSERT INTO `'.$this->mysql_table.'` VALUES("'.$currency.'",'.$rate.')') or die(mysql_error());
}
}
}
}
}
function createTable() {
$rs = mysql_query('CREATE TABLE `'.$this->mysql_table.'` ( currency char(3) NOT NULL default \'\', rate float NOT NULL default \'0\', PRIMARY KEY(currency) ) ENGINE=MyISAM') or die(mysql_error());
$rs = mysql_query('INSERT INTO `'.$this->mysql_table.'` VALUES(\'EUR\',1)') or die(mysql_error());
$this->downloadExchangeRates();
}
}
$output = '';
// I can get the 'dev' value from the snippet call but it's only the DB name and should be an optional parameter. I tried using $modx->dbConfig['dbase'] but no go //
$curdb = isset($curdb) ? $curdb : 'dev';
$ctable = isset($ctable) ? $ctable : 'exchanger';
$tvname = isset($tvname) ? $tvname : 'price';
$cur = isset($cur) ? $cur : 'GBP';
$tpl = isset($tpl) ? $tpl : 'currency_report';
if( $TVarray = $modx->getTemplateVarOutput(array($tvname)) )
$val = $TVarray[$tvname];
$sql = mysql_query('SELECT * FROM `'.$ctable.'`');
$rsArray = $modx->db->makeArray( $sql );
foreach($rsArray as $item)
{
$params['currency']=$item['currency'];
$conv = new CurrencyConverter();
$params['conv']= $conv->convert($val,$cur,''.$item['currency'].'');
$params['rate']=$item['rate'];
$params['val']=$val;
$output.=$modx->parseChunk(''.$tpl.'', $params, '[+', '+]');
}
$result = mysql_query('SHOW TABLE STATUS FROM `'.$curdb.'` LIKE \''.$ctable.'\'');
while($row = mysql_fetch_array($result)){
$updated = strftime('%d-%m-%Y @ %H:%M:%S',strtotime($row['Update_time']));
}
$modx->setPlaceholder('updated',''.$updated.'');
$output.= '<h3>[+updated+]</h3>';
return $output;
?>
The two issues are commented in the code.
Anyone help me finish it ready for release?
David