-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency_converter.module
291 lines (253 loc) · 10.4 KB
/
currency_converter.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
// $Id$
/**
* Implementation of hook_menu()
*/
function currency_converter_menu() {
$items['admin/config/currency_converter'] = array(
'title' => 'Currency converter',
'page callback' => 'drupal_get_form',
'page arguments' => array('currency_converter_admin_settings', NULL),
'access arguments' => array('administer content'),
'type' => MENU_NORMAL_ITEM,
'file' => 'currency_converter.admin.inc',
);
return $items;
}
/**
* Implementation of hook_block_info()
*/
function currency_converter_block_info() {
$blocks['currency_converter'] = array(
'info' => t('Currency converter'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
/**
* Implementation of hook_block_view()
*/
function currency_converter_block_view($delta = '') {
$block = array();
$block['subject'] = t('Converter');
$block['content'] = drupal_get_form('currency_converter_block_form');
return $block;
}
/**
* Implementation of hook_init()
*/
function currency_converter_init() {
$use_css = variable_get('currency_converter_use_css', 1);
if ($use_css) {
drupal_add_css(drupal_get_path('module', 'currency_converter') .'/css/currency_converter.css');
}
$use_bg = variable_get('currency_converter_use_bg', 1);
if ($use_bg) {
drupal_add_css(drupal_get_path('module', 'currency_converter') .'/css/currency_converter_bg.css');
}
drupal_add_js(drupal_get_path('module', 'currency_converter') .'/currency_converter.js');
}
/**
* Implementation of hook_cron()
*/
function currency_converter_cron() {
$time = time();
//Belorussian bank
$xmlstr = file_get_contents('http://www.priorbank.by/CurratesExportXml.axd?channel=1' );
if ($xmlstr) {
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->LIST_R_DATE->R_DATE->LIST_E_CHANNEL->E_CHANNEL->LIST_RATE->RATE as $rate) {
if ($rate->ISO == 'RUB') {
$rub = number_format((float)$rate->BUY, 2, '.', '');
}
if ($rate->ISO == 'EUR') {
$eur = number_format((float)$rate->BUY, 2, '.', '');
}
if ($rate->ISO == 'USD') {
$usd = number_format((float)$rate->BUY, 2, '.', '');
}
if ($rate->ISO == 'CAD') {
$cad = number_format((float)$rate->BUY, 2, '.', '');
}
if ($rate->ISO == 'PLN') {
$pln = number_format((float)$rate->BUY, 2, '.', '');
}
if ($rate->ISO == 'UAH') {
$uah = number_format((float)$rate->BUY, 2, '.', '');
}
if ($rate->ISO == 'CNY') {
$cny = number_format((float)$rate->BUY, 2, '.', '');
}
if ($rate->ISO == 'LTL') {
$ltl = number_format((float)$rate->BUY, 2, '.', '');
}
if ($rate->ISO == 'LVL') {
$lvl = number_format((float)$rate->BUY, 2, '.', '');
}
}
$check = db_select('currency_converter', 'c')->fields('c')->condition('conv_type', 0, '=')->execute()->fetchAssoc();
if ($check) {
$fields = array('rub' => $rub, 'eur' => $eur, 'usd' => $usd, 'cad' => $cad, 'pln' => $pln, 'uah' => $uah, 'cny' => $cny, 'ltl' => $ltl, 'lvl' => $lvl, 'timestamp' => $time);
db_update('currency_converter')->condition('conv_type', 0)->fields($fields)->execute();
}
else {
$signature = array(
'создание сайта', 'продвижение сайта', 'создание сайтов', 'продвижение сайтов', 'создание полезных виджетов в интернете',
'seo', 'раскрутка сайтов', 'раскрутка сайта', 'реклама в интернете'
);
$signature_selected = $signature[rand(0, 8)];
$fields = array('conv_type' => 0, 'byr' => 0, 'rub' => $rub, 'eur' => $eur, 'usd' => $usd, 'cad' => $cad, 'pln' => $pln, 'uah' => $uah, 'cny' => $cny, 'ltl' => $ltl, 'lvl' => $lvl, 'timestamp' => $time, 'signature' => $signature_selected);
db_insert('currency_converter')->fields($fields)->execute();
}
}
//Russian bank
$xmlstr=file_get_contents('http://www.cbr.ru/scripts/XML_daily.asp' );
if ($xmlstr) {
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->Valute as $rate) {
if ($rate->CharCode == 'BYR') {
$byr = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
if ($rate->CharCode == 'EUR') {
$eur = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
if ($rate->CharCode == 'USD') {
$usd = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
if ($rate->CharCode == 'CAD') {
$cad = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
if ($rate->CharCode == 'PLN') {
$pln = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
if ($rate->CharCode == 'UAH') {
$uah = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
if ($rate->CharCode == 'CNY') {
$cny = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
if ($rate->CharCode == 'LTL') {
$ltl = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
if ($rate->CharCode == 'LVL') {
$lvl = number_format(str_replace(',' , '.', $rate->Value ) / $rate->Nominal , 4, '.', '');
}
}
$check = db_select('currency_converter', 'c')->fields('c')->condition('conv_type', 1, '=')->execute()->fetchAssoc();
if ($check) {
$fields = array('byr' => $byr, 'eur' => $eur, 'usd' => $usd, 'cad' => $cad, 'pln' => $pln, 'uah' => $uah, 'cny' => $cny, 'ltl' => $ltl, 'lvl' => $lvl, 'timestamp' => $time);
db_update('currency_converter')->condition('conv_type', 1)->fields($fields)->execute();
}
else {
$signature = array(
'создание сайта', 'продвижение сайта', 'создание сайтов', 'продвижение сайтов', 'создание полезных виджетов в интернете',
'seo', 'раскрутка сайтов', 'раскрутка сайта', 'реклама в интернете'
);
$signature_selected = $signature[rand(0, 8)];
$fields = array('conv_type' => 1, 'byr' => $byr, 'rub' => 0, 'eur' => $eur, 'usd' => $usd, 'cad' => $cad, 'pln' => $pln, 'uah' => $uah, 'cny' => $cny, 'ltl' => $ltl, 'lvl' => $lvl, 'timestamp' => $time, 'signature' => $signature_selected);
db_insert('currency_converter')->fields($fields)->execute();
}
}
}
/**
* Implementation of hook_theme().
*/
function currency_converter_theme($existing, $type, $theme, $path) {
return array(
'currency_converter_block_form' => array(
'render element' => 'form',
'template' => 'currency_converter',
),
'currency_converter_admin_settings' => array(
'render element' => 'form',
),
);
}
/**
* Process block form for currency converter
*/
function currency_converter_block_form() {
$form = array();
$currencies = array('BYR', 'RUB', 'EUR', 'USD', 'CAD', 'PLN', 'UAH', 'CNY', 'LTL', 'LVL');
$default_currency = variable_get('currency_converter_main_currency', 'BYR');
$currency_types_enabled = variable_get('currency_converter_currency_types', array('BYR', 'RUB', 'EUR', 'USD', 'CAD', 'PLN', 'UAH', 'CNY', 'LTL', 'LVL'));
$currency_weight = variable_get('currency_converter_weight', array('BYR' => 0, 'RUB' => 0, 'EUR' => 0, 'USD' => 0, 'CAD' => 0, 'PLN' => 0, 'UAH' => 0, 'CNY' => 0, 'LTL' => 0, 'LVL' => 0));
$bank = variable_get('currency_converter_bank_default', 0);
$currencies_values = db_select('currency_converter', 'c')
->fields('c')
->condition('conv_type', $bank, '=')
->execute()
->fetchAssoc();
if ($currencies_values) {
$default_value = $currencies_values[strtolower($default_currency)] ? $currencies_values[strtolower($default_currency)] : '1';
$form[$default_currency] = array(
'#weight' => -11,
'#prefix' => '<div class = "'. strtolower($default_currency) .'">',
'#suffix' => '</div>',
);
$form[$default_currency]['value'] = array(
'#type' => 'item',
'#title' => $default_currency,
'#markup' => '<span></span>',
);
$form[$default_currency]['input'] = array(
'#type' => 'textfield',
'#size' => 10,
'#attributes' => array('class' => array('active')),
);
foreach ($currency_types_enabled as $currency) {
if ($currency != $default_currency) {
$value = $currencies_values[strtolower($currency)] ? $currencies_values[strtolower($currency)] : '1';
$value = $value / $default_value;
$value = (int)$value >= 100 ? number_format($value, 2, '.', '') : number_format($value, 4, '.', '');
$weight = $currency_weight[$currency];
$form[$currency] = array(
'#weight' => $weight,
'#prefix' => '<div class = "'. strtolower($currency) .'">',
'#suffix' => '</div>',
);
$form[$currency]['value'] = array(
'#type' => 'item',
'#title' => $currency,
'#markup' => '<span>'. $value .'</span>',
);
$form[$currency]['input'] = array(
'#type' => 'textfield',
'#size' => 10,
);
}
}
}
else {
drupal_set_message(t('Please, run cron to enable the Currency converter module'), 'warning');
}
return $form;
}
/**
* Process variables for currency_converter.tpl.php.
*
* The $variables array contains the following arguments:
* - $form
*
* @see currency_converter.tpl.php
*/
function template_preprocess_currency_converter_block_form(&$variables, $hook) {
$val = db_select('currency_converter', 'c')->fields('c', array('timestamp', 'signature'))->execute()->fetchObject();
if ($val) {
$bank = variable_get('currency_converter_bank_default', '0');
if ($bank == '0') {
$from = t('From NBB');
}
else {
$from = t('From RCB');
}
$variables['time'] = $from .' '. date('d.m.Y', $val->timestamp);
$variables['body'] = drupal_render_children($variables['form']);
$variables['developers'] = '<a href = "http://www.internet-marketing.by/" alt = "'. $val->signature .'" class = "dev" ></a>';
}
else {
$variables['time'] = '';
$variables['body'] = '';
$variables['developers'] = '';
}
}