1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14function smarty_function_currency($params, $smarty)
15{
16	extract($params);
17
18	if (! isset($amount)) {
19		return tra('Parameter amount is not specified.');
20	}
21
22	if (! isset($sourceCurrency)) {
23		return tra('Parameter sourceCurrency is not specified.');
24	}
25
26	$trk = TikiLib::lib('trk');
27	$smarty = TikiLib::lib('smarty');
28
29	if (is_numeric($params['date'])) {
30		$date = date('Y-m-d', $params['date']);
31	} elseif (! empty($params['date'])) {
32		$date = date('Y-m-d', strtotime($params['date']));
33	} else {
34		$date = date('Y-m-d');
35	}
36
37	$conversions = [];
38	if (! empty($exchangeRatesTrackerId)) {
39		$rates = $trk->exchange_rates($exchangeRatesTrackerId, $date);
40
41		if (empty($defaultCurrency)) {
42			$defaultCurrency = 'USD';
43		}
44
45		// convert amount to default currency before converting to other currencies
46		$defaultAmount = $amount;
47		if ($sourceCurrency != $defaultCurrency && !empty($rates[$sourceCurrency])) {
48			$defaultAmount = (float)$defaultAmount / (float)$rates[$sourceCurrency];
49			$conversions[$defaultCurrency] = $defaultAmount;
50		}
51		foreach ($rates as $currency => $rate) {
52			if ($currency != $sourceCurrency) {
53				$conversions[$currency] = (float)$rate * (float)$defaultAmount;
54			}
55		}
56	}
57
58	foreach ($params as $key => $val) {
59		$smarty->assign($key, $val);
60	}
61
62	$smarty->assign('amount', $amount);
63	$smarty->assign('currency', $sourceCurrency);
64	$smarty->assign('conversions', $conversions);
65	$smarty->assign('id', uniqid());
66
67	return $smarty->fetch('currency_output.tpl');
68}
69