1 <?php
2 3 4 5 6 7
8 class PageInformerRates extends Extension {
9
10
11 12 13
14 protected static $valutes = array(
15 'USD' => 'R01235',
16 'EUR' => 'R01239',
17 );
18
19 20 21
22 protected static $expire = 36000;
23
24 25 26
27 protected $rates = false;
28
29 30 31 32 33 34
35 public static function SetExpire($hours) {
36 self::$expire = $hours * 3600;
37 }
38
39 40 41 42 43
44 public static function SetValutes($valutes) {
45 self::$valutes = $valutes;
46 }
47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
63 public function ExchangeRate() {
64 if (!$this->_getRate())
65 return false;
66 return ($this->rates) ? new ArrayData($this->rates) : false;
67 }
68
69 70 71 72 73 74
75 private function _getRate() {
76 if ($this->rates) return true;
77
78 $cache_path = TEMP_FOLDER . '/cache_exchangerate';
79
80
81 if(!isset($_GET['flush']) && @file_exists($cache_path) && @filemtime($cache_path) + self::$expire > time()) {
82 $this->rates = unserialize(file_get_contents($cache_path));
83 if (is_array($this->rates) && count($this->rates) > 0) return true;
84 }
85
86
87
88
89 $day_end = date('d/m/Y');
90 $day_start = date('d/m/Y',(time()-60*60*24*7));
91
92 $this->rates = array(
93 'Date' => DBField::create('Date', date('Y-m-d')),
94 'Rates' => new DataObjectSet(),
95 );
96
97 $all = true;
98 foreach (self::$valutes as $value => $code) {
99 $info = @file_get_contents("http://www.cbr.ru/scripts/XML_dynamic.asp?date_req1={$day_start}&date_req2={$day_end}&VAL_NM_RQ={$code}");
100 if ($info) {
101 $xml = simplexml_load_string($info);
102
103 if ($xml) {
104 $result = array();
105 foreach ($xml as $el) {
106 $result[] = array(
107 'Date' => (string) $el->attributes()->Date,
108 'Value' => floatval(str_replace(',', '.', (string) $el->Value))
109 );
110 }
111
112 $count = count($result);
113 if ($count > 0) {
114 $record = array(
115 'Date' => DBField::create('Date', $result[$count-1]['Date']),
116 'Name' => $value,
117 'Rate' => sprintf('%.2f', $result[$count-1]['Value']),
118 'Delta' => ($count > 1) ? (sprintf('%.4f', $result[$count-1]['Value'] - $result[$count-2]['Value'])) : 0,
119 );
120 $record['DeltaSign'] = ($record['Delta'] > 0) ? 1 : -1;
121 $record['DeltaClass'] = ($record['Delta'] > 0) ? 'up' : 'down';
122
123 $this->rates['Rates']->push(new ArrayData($record));
124 $this->rates['Date'] = $record['Date'];
125 }
126 }
127 }
128 else {
129 $all = false;
130 }
131 }
132
133 if ($this->rates['Rates']->Count() > 0) {
134 if ($all)
135 file_put_contents($cache_path, serialize($this->rates));
136 return true;
137 }
138 $this->rates = false;
139 return false;
140 }
141
142 }
143
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-