/
CurrencyConverterWidget.php
509 lines (459 loc) · 13.7 KB
/
CurrencyConverterWidget.php
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<?php
/*
$page->defaultFromCurrencyCode = "NZD";
$page->Defaultto_currency_code = "EUR";
$page->DefaultAmount = "100";
*/
class CurrencyConverterWidget extends Widget {
private static $title = 'Currency Converter';
private static $cmsTitle = 'Currency Converter';
private static $description = 'Allows users to convert any amount FROM one TO another currency.';
private static $db = array(
"DefaultFromCurrency" => "Varchar(3)",
"DefaultToCurrency" => "Varchar(3)",
"DefaultAmount" => "Currency"
);
private static $defaults = array(
"DefaultFromCurrency" => "NZD",
"DefaultToCurrency" => "EUR",
"DefaultAmount" => "1"
);
static function set_default_from_currency($v) {
self::$defaults["DefaultFromCurrency"] = $v;
}
static function set_default_to_currency($v) {
self::$defaults["DefaultToCurrency"] = $v;
}
static function set_default_amount($v) {
self::$defaults["DefaultAmount"] = $v;
}
static function add_to_array_of_currencies_to_show($arrayOfCodes) {
if(is_array($arrayOfCodes)) {
foreach($arrayOfCodes as $code) {
if(3 == strlen($code)) {
self::$array_of_currencies_to_show[] = $code;
}
}
}
elseif(3 == strlen($arrayOfCodes)) {
self::$array_of_currencies_to_show[] = $arrayOfCodes;
}
else{
debug::show("add_to_array_of_currencies_to_show is not provided with right argument");
}
}
private static $array_of_currencies_to_show = array();
private static $currency_list = array(
"-0-" => "-- Select Currency --",
"usd"=>"us dollar",
"afn"=>"afghanistan afghani",
"all"=>"albanian lek",
"dzd"=>"algerian dinar",
"adf"=>"andorran franc",
"adp"=>"andorran peseta",
"aoa"=>"angolan kwanza",
"aon"=>"angolan new kwanza",
"ars"=>"argentine peso",
"amd"=>"armenian dram",
"awg"=>"aruban florin",
"aud"=>"australian dollar",
"ats"=>"austrian schilling",
"azm"=>"azerbaijan manat",
"azn"=>"azerbaijan new manat",
"bsd"=>"bahamian dollar",
"bhd"=>"bahraini dinar",
"bdt"=>"bangladeshi taka",
"bbd"=>"barbados dollar",
"byr"=>"belarusian ruble",
"bef"=>"belgian franc",
"bzd"=>"belize dollar",
"bmd"=>"bermudian dollar",
"btn"=>"bhutan ngultrum",
"bob"=>"bolivian boliviano",
"bam"=>"bosnian mark",
"bwp"=>"botswana pula",
"brl"=>"brazilian real",
"gbp"=>"british pound",
"bnd"=>"brunei dollar",
"bgn"=>"bulgarian lev",
"bif"=>"burundi franc",
"xof"=>"cfa franc bceao",
"xaf"=>"cfa franc beac",
"xpf"=>"cfp franc",
"khr"=>"cambodian riel",
"cad"=>"canadian dollar",
"cve"=>"cape verde escudo",
"kyd"=>"cayman islands dollar",
"clp"=>"chilean peso",
"cny"=>"chinese yuan renminbi",
"cop"=>"colombian peso",
"kmf"=>"comoros franc",
"cdf"=>"congolese franc",
"crc"=>"costa rican colon",
"hrk"=>"croatian kuna",
"cuc"=>"cuban convertible peso",
"cup"=>"cuban peso",
"cyp"=>"cyprus pound",
"czk"=>"czech koruna",
"dkk"=>"danish krone",
"djf"=>"djibouti franc",
"dop"=>"dominican r. peso",
"nlg"=>"dutch guilder",
"xeu"=>"ecu",
"xcd"=>"east caribbean dollar",
"ecs"=>"ecuador sucre",
"egp"=>"egyptian pound",
"svc"=>"el salvador colon",
"eek"=>"estonian kroon",
"etb"=>"ethiopian birr",
"eur"=>"European Union euro",
"fkp"=>"falkland islands pound",
"fjd"=>"fiji dollar",
"fim"=>"finnish markka",
"frf"=>"french franc",
"gmd"=>"gambian dalasi",
"gel"=>"georgian lari",
"dem"=>"german mark",
"ghc"=>"ghanaian cedi",
"ghs"=>"ghanaian new cedi",
"gip"=>"gibraltar pound",
"xau"=>"gold (oz.)",
"grd"=>"greek drachma",
"gtq"=>"guatemalan quetzal",
"gnf"=>"guinea franc",
"gyd"=>"guyanese dollar",
"htg"=>"haitian gourde",
"hnl"=>"honduran lempira",
"hkd"=>"hong kong dollar",
"huf"=>"hungarian forint",
"isk"=>"iceland krona",
"inr"=>"indian rupee",
"idr"=>"indonesian rupiah",
"irr"=>"iranian rial",
"iqd"=>"iraqi dinar",
"iep"=>"irish punt",
"ils"=>"israeli new shekel",
"itl"=>"italian lira",
"jmd"=>"jamaican dollar",
"jpy"=>"japanese yen",
"jod"=>"jordanian dinar",
"kzt"=>"kazakhstan tenge",
"kes"=>"kenyan shilling",
"kwd"=>"kuwaiti dinar",
"kgs"=>"kyrgyzstanian som",
"lak"=>"lao kip",
"lvl"=>"latvian lats",
"lbp"=>"lebanese pound",
"lsl"=>"lesotho loti",
"lrd"=>"liberian dollar",
"lyd"=>"libyan dinar",
"ltl"=>"lithuanian litas",
"luf"=>"luxembourg franc",
"mop"=>"macau pataca",
"mkd"=>"macedonian denar",
"mga"=>"malagasy ariary",
"mgf"=>"malagasy franc",
"mwk"=>"malawi kwacha",
"myr"=>"malaysian ringgit",
"mvr"=>"maldive rufiyaa",
"mtl"=>"maltese lira",
"mro"=>"mauritanian ouguiya",
"mur"=>"mauritius rupee",
"mxn"=>"mexican peso",
"mdl"=>"moldovan leu",
"mnt"=>"mongolian tugrik",
"mad"=>"moroccan dirham",
"mzm"=>"mozambique metical",
"mzn"=>"mozambique new metical",
"mmk"=>"myanmar kyat",
"ang"=>"nl antillian guilder",
"nad"=>"namibia dollar",
"npr"=>"nepalese rupee",
"nzd"=>"new zealand dollar",
"nio"=>"nicaraguan cordoba oro",
"ngn"=>"nigerian naira",
"kpw"=>"north korean won",
"nok"=>"norwegian kroner",
"omr"=>"omani rial",
"pkr"=>"pakistan rupee",
"xpd"=>"palladium (oz.)",
"pab"=>"panamanian balboa",
"pgk"=>"papua new guinea kina",
"pyg"=>"paraguay guarani",
"pen"=>"peruvian nuevo sol",
"php"=>"philippine peso",
"xpt"=>"platinum (oz.)",
"pln"=>"polish zloty",
"pte"=>"portuguese escudo",
"qar"=>"qatari rial",
"rol"=>"romanian lei",
"ron"=>"romanian new lei",
"rub"=>"russian rouble",
"rwf"=>"rwandan franc",
"wst"=>"samoan tala",
"std"=>"sao tome/principe dobra",
"sar"=>"saudi riyal",
"rsd"=>"serbian dinar",
"scr"=>"seychelles rupee",
"sll"=>"sierra leone leone",
"xag"=>"silver (oz.)",
"sgd"=>"singapore dollar",
"skk"=>"slovak koruna",
"sit"=>"slovenian tolar",
"sbd"=>"solomon islands dollar",
"sos"=>"somali shilling",
"zar"=>"south african rand",
"krw"=>"south-korean won",
"esp"=>"spanish peseta",
"lkr"=>"sri lanka rupee",
"shp"=>"st. helena pound",
"sdd"=>"sudanese dinar",
"sdp"=>"sudanese old pound",
"sdg"=>"sudanese pound",
"srd"=>"suriname dollar",
"srg"=>"suriname guilder",
"szl"=>"swaziland lilangeni",
"sek"=>"swedish krona",
"chf"=>"swiss franc",
"syp"=>"syrian pound",
"twd"=>"taiwan dollar",
"tjs"=>"tajikistani somoni",
"tzs"=>"tanzanian shilling",
"thb"=>"thai baht",
"top"=>"tonga pa'anga",
"ttd"=>"trinidad/tobago dollar",
"tnd"=>"tunisian dinar",
"try"=>"turkish lira",
"trl"=>"turkish old lira",
"tmm"=>"turkmenistan manat",
"tmt"=>"turkmenistan new manat",
"ugx"=>"uganda shilling",
"uah"=>"ukraine hryvnia",
"uyu"=>"uruguayan peso",
"aed"=>"utd. arab emir. dirham",
"uzs"=>"uzbekistan som",
"vuv"=>"vanuatu vatu",
"veb"=>"venezuelan bolivar",
"vef"=>"venezuelan bolivar fuerte",
"vnd"=>"vietnamese dong",
"yer"=>"yemeni rial",
"yun"=>"yugoslav dinar",
"zmk"=>"zambian kwacha",
"zwd"=>"zimbabwe dollar"
);
private static $rates = array();
private static $from_currency_code = '';
private static $to_currency_code = '';
private static $debug = false;
protected $amount = 0;
// set once....
static function set_debug_mode($trueOrFalse) {
self::$debug = $trueOrFalse;
}
static function get_currency_list() {
if(count(self::$array_of_currencies_to_show)) {
foreach(self::$array_of_currencies_to_show as $code) {
$array[$code] = self::$currency_list[$code];
}
}
else {
$array = self::$currency_list;
}
return $array;
}
static function set_from_currency_code($v) {
if(self::currency_exists($v)) {
self::$from_currency_code = $v;
Session::set("CurrencyConverter.from_currency_code", $v);
}
}
static function set_to_currency_code($v) {
if(self::currency_exists($v)) {
self::$to_currency_code = $v;
Session::set("CurrencyConverter.to_currency_code", $v);
}
}
static function get_from_currency_code() {
self::retrieve_defaults();
return self::$from_currency_code;
}
static function get_to_currency_code() {
self::retrieve_defaults();
return self::$to_currency_code;
}
static function from_equals_to() {
self::retrieve_defaults();
return (strtolower(self::$to_currency_code) == strtolower(self::$from_currency_code));
}
static function has_from_and_to_currencies() {
self::retrieve_defaults();
if(self::$to_currency_code && self::$from_currency_code) {
return true;
}
}
static function get_rate() {
//$url = http://finance.yahoo.com/currency/convert?amt=1&from=NZD&to=USD&submit=Convert
if(self::has_from_and_to_currencies() && !self::from_equals_to()) {
if(isset(self::$rates[self::$from_currency_code.".".self::$to_currency_code]) && self::$rates[self::$from_currency_code.".".self::$to_currency_code] > 0) {
return self::$rates[self::$from_currency_code.".".self::$to_currency_code]+0;
}
else {
$url = 'http://download.finance.yahoo.com/d/quotes.csv?s='.self::$from_currency_code.self::$to_currency_code.'=X&f=sl1d1t1ba&e=.csv';
if (($ch = @curl_init())) {
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, "$url");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$record = curl_exec($ch);
if(self::$debug) {echo "-- CURL:"; print_r($record);}
curl_close($ch);
}
if(!$record) {
$record = file_get_contents($url);
//if(self::$debug) {echo "-- FILE_GET_CONTENTS:"; print_r($record);}
}
if ($record) {
$currency_data = explode(',', $record);
self::$rates[self::$from_currency_code.".".self::$to_currency_code] = $currency_data[1];
if(!isset(self::$rates[self::$from_currency_code.".".self::$to_currency_code]) || !self::$rates[self::$from_currency_code.".".self::$to_currency_code]) {
self::$rates[self::$from_currency_code.".".self::$to_currency_code] = $currency_data[2];
}
}
else {
if(self::$debug) {echo "-- could not retrieve data";}
}
}
}
elseif(self::from_equals_to()) {
self::$rates[self::$from_currency_code.".".self::$to_currency_code] = 1;
return self::$rates[self::$from_currency_code.".".self::$to_currency_code]+0;
}
else {
if(self::$debug) {echo "-- could not find from and to values!";}
}
if(isset(self::$rates[self::$from_currency_code.".".self::$to_currency_code])) {
return self::$rates[self::$from_currency_code.".".self::$to_currency_code]+0;
}
else {
return 0;
}
}
static function get_exchanged_value($amount = 0, $digits = 2) {
self::retrieve_defaults();
if(self::$debug) {echo "-- from ".self::$from_currency_code;}
if(self::$debug) {echo "-- to ".self::$to_currency_code;}
if(self::$debug) {echo "-- amount ".$amount;}
$rate = self::get_rate();
if(self::$debug) {echo "-- rate ".$rate;}
return strtoupper(self::$to_currency_code).' '.round(floatval($rate * $amount), $digits);
}
private static function retrieve_defaults() {
if(!self::$from_currency_code) {
if(self::$from_currency_code = Session::get("CurrencyConverter.from_currency_code")) {
}
else {
self::$from_currency_code = self::$defaults["DefaultFromCurrency"];
}
if(!self::$from_currency_code && self::$to_currency_code) {
self::$from_currency_code = self::$to_currency_code;
}
}
if(!self::$to_currency_code) {
if(self::$to_currency_code = Session::get("CurrencyConverter.to_currency_code")) {
}
else {
self::$to_currency_code = self::$defaults["DefaultToCurrency"];
}
if(self::$from_currency_code && !self::$to_currency_code) {
self::$to_currency_code = self::$from_currency_code;
}
}
}
private static function currency_exists($v) {
$outcome = array_key_exists($v, self::$currency_list);
if(!$outcome && self::$debug) {
die( "$v currency could not be found!");
}
return $outcome;
}
//CMS ...
public function getCMSFields() {
return new FieldList(
new TextField("defaultFromCurrencyCode", _t('CurrencyConverterWidget.defaultFromCurrencyCode', "Default From Currency Code")),
new TextField("Defaultto_currency_code", _t('CurrencyConverterWidget.Defaultto_currency_code', "Default To Currency Code")),
new CurrencyField("DefaultAmount", _t('CurrencyConverterWidget.DefaultAmount', "Default Amount to be Converted"))
);
}
//set for every transaction....
public function setAmount($amount = 0) {
$this->amount = floatval($amount);
Session::set("CurrencyConverter.Amount", $amount);
}
// get for every transaction ...
public function getExchangedAmount($amount = 0) {
$this->getValues();
if($amount) {
$this->amount = $amount;
}
$this->amount = floatval($this->amount);
return self::get_exchanged_value($this->amount);
}
// for templates ...
public function CurrencyConverter() {
$this->getValues();
$convertedAmount = $this->getExchangedAmount();
$output = new ArrayList();
$output->push(
new ArrayData(
array(
"from_currency_code" => self::$from_currency_code,
"to_currency_code" => self::$to_currency_code,
"amount" => $this->amount,
"rate" => floatval(self::$rates[self::$from_currency_code.".".self::$to_currency_code]+0),
"convertedAmount" => $convertedAmount
)
)
);
return $output;
}
public function Currencies() {
$this->getValues();
$currencies = new ArrayList;
foreach(self::$currency_list as $key => $value) {
$from = ($key == self::$from_currency_code);
$to = ($key == self::$to_currency_code);
$item = new ArrayData(
Array(
"code" => $key,
"name" => $value,
"currentFrom" => $from,
"currentTo" => $to
)
);
$currencies->push($item);
}
return $currencies;
}
private function retrieveGetValues() {
if(isset($_GET["f"]) ) {
self::set_from_currency_code(strtolower(substr($_GET["f"], 0, 3)));
}
if( isset($_GET["t"]) ) {
self::set_to_currency_code(strtolower(substr($_GET["t"], 0, 3)));
}
if(isset($_GET["a"]) ) {
$this->amount = floatval($_GET["a"]);
$this->setAmount($this->amount);
}
}
}
class CurrencyConverterWidget_Controller extends ContentController {
function __construct($dataRecord = Null) {
parent::__construct($dataRecord);
}
function getRate() {
$CurrencyConverterWidgetObject = new CurrencyConverterWidget();
//$CurrencyConverterWidgetObject->debug = true;
echo $CurrencyConverterWidgetObject->getExchangedAmount();
}
}