|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* PayPal Donation extension for the phpBB Forum Software package. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright (c) 2015-2024 Skouat |
|
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace skouat\ppde\actions; |
|
12
|
|
|
|
|
13
|
|
|
use phpbb\template\template; |
|
14
|
|
|
use skouat\ppde\entity\currency as currency_entity; |
|
15
|
|
|
use skouat\ppde\operators\currency as currency_operator; |
|
16
|
|
|
|
|
17
|
|
|
class currency |
|
18
|
|
|
{ |
|
19
|
|
|
protected $entity; |
|
20
|
|
|
protected $locale; |
|
21
|
|
|
protected $operator; |
|
22
|
|
|
protected $template; |
|
23
|
|
|
protected $default_currency_data; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* currency constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param currency_entity $entity Currency entity object |
|
29
|
|
|
* @param locale_icu $locale PPDE Locale object |
|
30
|
|
|
* @param currency_operator $operator Currency operator object |
|
31
|
|
|
* @param template $template Template object |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( |
|
34
|
|
|
currency_entity $entity, |
|
35
|
|
|
locale_icu $locale, |
|
36
|
|
|
currency_operator $operator, |
|
37
|
|
|
template $template |
|
38
|
|
|
) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->entity = $entity; |
|
41
|
|
|
$this->locale = $locale; |
|
42
|
|
|
$this->operator = $operator; |
|
43
|
|
|
$this->template = $template; |
|
44
|
|
|
$this->default_currency_data = []; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get currency data based on currency ISO code |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $iso_code The ISO code of the currency |
|
51
|
|
|
* @return array Currency data |
|
52
|
|
|
*/ |
|
53
|
|
|
public function set_currency_data_from_iso_code(string $iso_code): void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->entity->data_exists($this->entity->build_sql_data_exists($iso_code)); |
|
56
|
|
|
$this->set_default_currency_data($this->entity->get_id()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Sets the default currency data. |
|
61
|
|
|
* |
|
62
|
|
|
* @param int $id The ID of the currency (optional). |
|
63
|
|
|
*/ |
|
64
|
|
|
public function set_default_currency_data(int $id): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->default_currency_data = $this->entity->get_data($this->operator->build_sql_data($id, true))[0]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Gets the default currency data. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array The default currency data as an array. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function get_default_currency_data(): array |
|
75
|
|
|
{ |
|
76
|
|
|
return ($this->default_currency_data ?? []); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Formats the given value as currency based on the PHP intl extension, if available. |
|
81
|
|
|
* Otherwise, a basic currency formatter is used. |
|
82
|
|
|
* |
|
83
|
|
|
* @param float $amount The amount to be formatted as currency. |
|
84
|
|
|
* @return string The formatted currency string. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function format_currency(float $amount): string |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->locale->is_locale_configured()) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->locale->numfmt_format_currency($this->locale->numfmt_create(), $amount, $this->default_currency_data['currency_iso_code']); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->format_currency_without_intl($amount, $this->default_currency_data['currency_symbol'], $this->default_currency_data['currency_on_left']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Format a value as a currency string without using the intl extension |
|
98
|
|
|
* |
|
99
|
|
|
* @param float $value The value to format as currency |
|
100
|
|
|
* @param string $currency_symbol The symbol to use as the currency symbol |
|
101
|
|
|
* @param bool $on_left Optional. Determines whether the currency symbol should be placed on the left or |
|
102
|
|
|
* right of the formatted value. Default is true (left side). |
|
103
|
|
|
* @param string $dec_point Optional. The string to use as the decimal separator. Default is '.'. |
|
104
|
|
|
* @param string $thousands_sep Optional. The string to use as the thousands separator. Default is an empty |
|
105
|
|
|
* string. |
|
106
|
|
|
* @return string The formatted value as a currency string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function format_currency_without_intl(float $value, string $currency_symbol, bool $on_left = true, string $dec_point = '.', string $thousands_sep = ''): string |
|
109
|
|
|
{ |
|
110
|
|
|
$formatted_value = number_format(round($value, 2), 2, $dec_point, $thousands_sep); |
|
111
|
|
|
return $on_left ? $currency_symbol . $formatted_value : $formatted_value . $currency_symbol; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Builds a currency select menu. |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $config_value The selected currency value from the configuration (default is 0). |
|
118
|
|
|
*/ |
|
119
|
|
|
public function build_currency_select_menu(int $config_value = 0): void |
|
120
|
|
|
{ |
|
121
|
|
|
// Grab the list of all enabled currencies; 0 is for all data |
|
122
|
|
|
$currency_items = $this->entity->get_data($this->operator->build_sql_data(0, true)); |
|
123
|
|
|
|
|
124
|
|
|
// Process each menu item for pull-down |
|
125
|
|
|
foreach ($currency_items as $currency_item) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->assign_currency_to_template($currency_item, $config_value); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Build pull down menu options of available currency value |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $dropbox_value Comma-separated list of currency values |
|
135
|
|
|
* @param int $default_value Default selected value |
|
136
|
|
|
* @return string HTML options for select menu |
|
137
|
|
|
*/ |
|
138
|
|
|
public function build_currency_value_select_menu(string $dropbox_value, int $default_value): string |
|
139
|
|
|
{ |
|
140
|
|
|
$values = array_filter(array_map('intval', explode(',', $dropbox_value))); |
|
141
|
|
|
$options = ''; |
|
142
|
|
|
|
|
143
|
|
|
foreach ($values as $value) |
|
144
|
|
|
{ |
|
145
|
|
|
$selected = ($value == $default_value) ? ' selected' : ''; |
|
146
|
|
|
$options .= '<option value="' . $value . '"' . $selected . '>' . $value . '</option>'; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $options; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Assign currency information to the template. |
|
154
|
|
|
* |
|
155
|
|
|
* @param array $currency_item The currency item with the following keys: |
|
156
|
|
|
* - currency_id (int): The ID of the currency. |
|
157
|
|
|
* - currency_iso_code (string): The ISO code of the currency. |
|
158
|
|
|
* - currency_name (string): The name of the currency. |
|
159
|
|
|
* - currency_symbol (string): The symbol of the currency. |
|
160
|
|
|
* @param int $config_value The configuration value used to determine the default currency (integer). |
|
161
|
|
|
*/ |
|
162
|
|
|
private function assign_currency_to_template(array $currency_item, int $config_value): void |
|
163
|
|
|
{ |
|
164
|
|
|
$this->template->assign_block_vars('options', [ |
|
165
|
|
|
'CURRENCY_ID' => (int) $currency_item['currency_id'], |
|
166
|
|
|
'CURRENCY_ISO_CODE' => $currency_item['currency_iso_code'], |
|
167
|
|
|
'CURRENCY_NAME' => $currency_item['currency_name'], |
|
168
|
|
|
'CURRENCY_SYMBOL' => $currency_item['currency_symbol'], |
|
169
|
|
|
'S_CURRENCY_DEFAULT' => $config_value === (int) $currency_item['currency_id'], |
|
170
|
|
|
]); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|