Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Money utilties | |
38 | */ | |
39 | class CRM_Utils_Money { | |
40 | static $_currencySymbols = NULL; | |
41 | ||
42 | /** | |
43 | * format a monetary string | |
44 | * | |
45 | * Format a monetary string basing on the amount provided, | |
46 | * ISO currency code provided and a format string consisting of: | |
47 | * | |
48 | * %a - the formatted amount | |
49 | * %C - the currency ISO code (e.g., 'USD') if provided | |
50 | * %c - the currency symbol (e.g., '$') if available | |
51 | * | |
52 | * @param float $amount the monetary amount to display (1234.56) | |
53 | * @param string $currency the three-letter ISO currency code ('USD') | |
54 | * @param string $format the desired currency format | |
55 | * | |
56 | * @return string formatted monetary string | |
57 | * | |
58 | * @static | |
59 | */ | |
60 | static function format($amount, $currency = NULL, $format = NULL, $onlyNumber = FALSE) { | |
61 | ||
62 | if (CRM_Utils_System::isNull($amount)) { | |
63 | return ''; | |
64 | } | |
65 | ||
66 | $config = CRM_Core_Config::singleton(); | |
67 | ||
68 | if (!$format) { | |
69 | $format = $config->moneyformat; | |
70 | } | |
71 | ||
72 | if ($onlyNumber) { | |
73 | // money_format() exists only in certain PHP install (CRM-650) | |
74 | if (is_numeric($amount) and function_exists('money_format')) { | |
75 | $amount = money_format($config->moneyvalueformat, $amount); | |
76 | } | |
77 | return $amount; | |
78 | } | |
79 | ||
80 | if (!self::$_currencySymbols) { | |
81 | $currencySymbolName = CRM_Core_PseudoConstant::currencySymbols('name'); | |
82 | $currencySymbol = CRM_Core_PseudoConstant::currencySymbols(); | |
83 | ||
84 | self::$_currencySymbols = array_combine($currencySymbolName, $currencySymbol); | |
85 | } | |
86 | ||
87 | if (!$currency) { | |
88 | $currency = $config->defaultCurrency; | |
89 | } | |
90 | ||
91 | if (!$format) { | |
92 | $format = $config->moneyformat; | |
93 | } | |
94 | ||
95 | // money_format() exists only in certain PHP install (CRM-650) | |
96 | // setlocale() affects native gettext (CRM-11054, CRM-9976) | |
97 | if (is_numeric($amount) && function_exists('money_format')) { | |
98 | $lc = setlocale(LC_MONETARY, 0); | |
99 | setlocale(LC_MONETARY, 'en_US.utf8', 'en_US', 'en_US.utf8', 'en_US', 'C'); | |
100 | $amount = money_format($config->moneyvalueformat, $amount); | |
101 | setlocale(LC_MONETARY, $lc); | |
102 | } | |
103 | ||
104 | $rep = array( | |
105 | ',' => $config->monetaryThousandSeparator, | |
106 | '.' => $config->monetaryDecimalPoint, | |
107 | ); | |
108 | ||
109 | // If it contains tags, means that HTML was passed and the | |
110 | // amount is already converted properly, | |
111 | // so don't mess with it again. | |
112 | if (strpos($amount, '<') === FALSE) { | |
113 | $amount = strtr($amount, $rep); | |
114 | } | |
115 | ||
116 | $replacements = array( | |
117 | '%a' => $amount, | |
118 | '%C' => $currency, | |
119 | '%c' => CRM_Utils_Array::value($currency, self::$_currencySymbols, $currency), | |
120 | ); | |
121 | return strtr($format, $replacements); | |
122 | } | |
123 | } | |
124 |