We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4310
    • 2,310 Posts
    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
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Wouldn’t the $modx object be outside of the scope of your CurrencyConverter object (and especially of its functions)? Wouldn’t you need to have a "global $modx" in there somewhere?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        As sottwell suggests, global $modx; should be at the head of any function that uses the $modx object.

        You can make the exchanger an argument to the contstructor.

        function CurrencyConverter($exch) {
        
              $this->mysql_table = $exch;
        
        } 

        Then pass that argument in the new statement.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 18940
          • 152 Posts
          Nice site smiley Pretty cool the calender is clickable too!

          Question: Which LightBox snippet did you use? I’m looking for the exact same one for a site I’m working on now smiley
            Quality doesn&#39;t need a big signature.
            • 4310
            • 2,310 Posts
            I used a jQuery based one from here.
            It’s not really any different to most of the others using mootools etc.
              • 18940
              • 152 Posts
              Thnx for your fast reply smiley I’m going to add it today grin
                Quality doesn&#39;t need a big signature.
                • 18940
                • 152 Posts
                Okay can tell me exactly what to do please?

                I tried:
                lightbox.zip (original)
                krewenki-jquery-lightbox.zip
                jquery-lightbox-0.5.zip
                jquery_lightbox_v1.3.5-final.zip
                lightbox-gone-wild

                and some more and they all fail sad
                  Quality doesn&#39;t need a big signature.
                  • 4310
                  • 2,310 Posts
                  jquery_lightbox_v1.3.5-final.zip
                  has everything you need, look at the index,html file to see it working.
                  Then add the links to the lightbox and jQuery script files to your template <head>.
                    • 18940
                    • 152 Posts
                    Okay thnx that’s working too now laugh

                    Do you happen to know how I remove this hovering text: "Hover on interact" (it’s displayed when hovering Click to close and it looks really ugly on blending so I want it out smiley )
                      Quality doesn&#39;t need a big signature.