DataObjects/Core_curr_rate.php
[Pman.Core] / DataObjects / Core_curr_rate.php
1 <?php
2 /**
3  * Table Definition for core_curr_rate
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_curr_rate extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11   
12     public $__table = 'core_curr_rate';    // table name
13     public $id;
14     public $curr;
15     public $rate;
16     public $from_dt;
17     public $to_dt;
18
19     /* the code above is auto generated do not remove the tag below */
20     ###END_AUTOCODE
21     
22     function applyFilters($q, $au, $roo)
23     {
24         
25     }
26     
27     /**
28      * current rate fetching
29      *
30      * - we used to do historical rates... (but sources keep disappearing for that..)
31      *
32      * this just get's the current rates from the ecb..
33      * 
34      * 
35      */
36     
37     var $rates = array();
38     function loadRates()
39     {
40         
41         // how often do we need to know this..?
42         // let's assume we do it once a week..
43         $x = DB_DataObject::Factory('core_curr_rate');
44         $x->whereAdd('to_date > NOW()');
45         if ($x->count()) {
46             // got some data for beyond today..
47             return;
48         }
49         
50         
51         
52         
53         
54         if (!empty($this->rates)) {
55             return true;
56         }
57         // load our default rates to start with..
58         $dom = simplexml_load_file(dirname(__FILE__).'/eurofxref-daily.xml');
59         $this->rates['EUR'] = 1.0;
60         $this->rates['TWD'] = 46.7008412;
61         $this->rates['VND'] = 26405.3;
62        
63        
64         foreach($dom->Cube->Cube->Cube as $c) {
65            //echo '<PRE>';print_r($c );
66             $this->rates[(string)$c['currency']] = (string)$c['rate'];
67         }
68         $this->rates['RMB'] = $this->rates['CNY'] ;
69         // now try loading from latest..
70         $target = ini_get('session.save_path').'/eurofxref-daily.xml';
71         
72         if (!file_exists($target) || filemtime($target) < (time() - 60*60*24)) {
73             // this may fail...
74             $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
75             if (!strlen($f)) {
76                 return;
77             } 
78             file_put_contents($target,$f);
79         
80         } 
81         $dom = simplexml_load_file($target);
82         $this->rates['EUR'] = 1.0;
83         $this->rates['TWD'] = 46.7008412;
84         $this->rates['VND'] = 26405.3;
85        
86         foreach($dom->Cube->Cube->Cube as $c) {
87            //echo '<PRE>';print_r($c );
88             $this->rates[(string)$c['currency']] = (string)$c['rate'];
89         }
90         $this->rates['RMB'] = $this->rates['CNY'] ;
91     }
92 }