UpdateCurrencyRate.php
[Pman.Core] / UpdateCurrencyRate.php
1 <?php
2
3 require_once 'Pman.php';
4
5 class Pman_Core_UpdateCurrencyRate extends Pman
6 {
7     
8     static $cli_desc = "Update Currency Exchange Rate";
9     
10     static $cli_opts = array(
11         'procedures-only' => array(
12             'desc' => 'Only run procedures and return',
13             'short' => 'p',
14             'default' => '',
15             'min' => 1,
16             'max' => 1,
17         ),
18     );
19     
20     var $cli = false; 
21     
22     var $actionUrl = 'http://www.oanda.com/currency/historical-rates-classic';
23     
24     var $mapping = array(
25         'CNY' => 'RMB'
26     );
27     
28     function getAuth() 
29     {
30         $ff = HTML_FlexyFramework::get();
31         if (!empty($ff->cli)) {
32             $this->cli = true;
33             return true;
34         }
35         
36         die("NOT ALLOWED");
37     }
38     
39     function get($args, $opts)
40     {   
41 //        $currency = array();
42 //        
43 //        $response = $this->curl($this->actionUrl, array(), 'GET');
44 //        
45 //        libxml_use_internal_errors (true);
46 //        
47 //        $doc = new DOMDocument();
48 //        $doc->loadHTML($response);
49 //        
50 //        libxml_use_internal_errors (false);
51 //        
52 //        $xpath = new DOMXpath($doc);
53 //        
54 //        $elements = $xpath->query("//select[@name='exch']/option");
55 //        
56 //        foreach($elements as $el) {
57 //            $currency[] = $el->getAttribute('value');
58 //        }
59 //        
60 //        if(empty($currency)){
61 //            die('no any currency');
62 //        }
63 //        
64 //        $fromDate = date('m/d/y', strtotime("-6 MONTH"));
65 //        $toDate = date('m/d/y');
66 //        
67 //        $total = count($currency);
68 //        
69 //        foreach ($currency as $k => $c){
70 //            
71 //            echo "\nProcessing Currency : $c        ($k / $total) \n";
72 //            
73 //            $params = array(
74 //                'lang'          => 'en',
75 //                'result'        => 1,
76 //                'date1'         => $fromDate,
77 //                'date'          => $toDate,
78 //                'date_fmt'      => 'us',
79 //                'exch'          => $c,
80 //                'expr'          => 'USD',
81 //                'margin_fixed'  => 0,
82 //                'format'        => 'HTML'
83 //            );
84 //            
85 //            $response = $this->curl($this->actionUrl, $params, 'POST');
86 //        
87 //            libxml_use_internal_errors (true);
88 //
89 //            $doc = new DOMDocument();
90 //            $doc->loadHTML($response);
91 //
92 //            libxml_use_internal_errors (false);
93 //
94 //            $xpath = new DOMXpath($doc);
95 //
96 //            $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
97 //
98 //            $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
99 //
100 //            $this->processCurrRate($c, $rate, $fromDate, $toDate);
101 //            
102 //            if(array_key_exists($c, $this->mapping)){
103 //                $this->processCurrRate($this->mapping[$c], $rate, $fromDate, $toDate);
104 //            }
105 //        }
106         
107         if(empty($opts['procedures-only'])){
108             $this->jok("DONE");
109         }
110         
111         return;
112     }
113     
114     function processCurrRate($currency, $rate, $from, $to)
115     {
116         $curr = DB_DataObject::factory('core_curr_rate');
117             
118         $curr->curr = $currency;
119
120         $o = false;
121
122         if($curr->find(true)){
123             $o = clone($curr);
124         }
125
126         $curr->setFrom(array(
127             'rate'  => $rate,
128             'from_dt'  => date('Y-m-d H:i:s', strtotime($from)),
129             'to_dt'    => date('Y-m-d H:i:s', strtotime($to))
130         ));
131
132         (empty($o)) ? $curr->insert() : $curr->update($o);
133         
134         return;
135     }
136     
137     function curl($url, $request = array(), $method = 'GET') 
138     {
139          
140         if(is_array($request)){
141             $request = http_build_query($request);
142         }
143         
144         $url = $url . ($method == 'GET' ? "?" . $request : '');  
145         $ch = curl_init($url);
146         
147         if ($method == 'POST') {
148             curl_setopt($ch, CURLOPT_POST, 1);
149             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
150             curl_setopt($ch, CURLOPT_HTTPHEADER,
151                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
152         }
153         
154         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
155         
156         curl_setopt($ch, CURLOPT_HEADER, false);
157         curl_setopt($ch, CURLOPT_VERBOSE, 1);
158         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
159         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
160
161         $response = curl_exec($ch);
162
163         curl_close($ch);
164         
165         return $response;
166     }
167     
168     /*
169     lang:en
170     result:1
171     date1:10/14/14
172     date:10/20/14
173     date_fmt:us
174     exch:USD
175     exch2:
176     expr:EUR
177     expr2:
178     margin_fixed:0
179     format:HTML
180     SUBMIT:Get Table
181     */
182 }