PluginProbe
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / modules / mod_vikappointments_currencyconverter / libraries / helper.php
helper.php
121 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage mod_vikappointments_currencyconverter
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2024 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access to this file
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 VAPLoader::import('libraries.helpers.module');
15
16 /**
17 * Helper class used by the Currency Converter module.
18 *
19 * @since 1.0
20 */
21 class VikAppointmentsCurrencyConverterHelper
22 {
23 /**
24 * Use methods defined by modules trait for a better reusability.
25 *
26 * @see VAPModuleHelper
27 */
28 use VAPModuleHelper;
29
30 /**
31 * Returns the currency selected by the user.
32 *
33 * @return string The ISO 4217 of the preferred currency.
34 */
35 public static function getUserCurrency()
36 {
37 return JFactory::getApplication()->getUserState(
38 'vikappointments.user.currency',
39 VAPFactory::getConfig()->get('currencyname')
40 );
41 }
42
43 /**
44 * Returns a list of currencies according to the configured ones.
45 *
46 * @param Registry $params The module configuration.
47 *
48 * @return object[] The select options.
49 */
50 public static function getCurrencies($params)
51 {
52 $config = VAPFactory::getConfig();
53
54 // take the list containing all the selected currencies
55 $value = $params->get('currencies', []);
56
57 // take the option text template
58 $template = $params->get('format', '{name} ({symbol})');
59
60 // take only the selected currencies
61 $currencies = array_filter(VAPCurrencyHelperEnum::getCurrencies(), function($code) use ($value) {
62 return !$value || in_array($code, $value);
63 }, ARRAY_FILTER_USE_KEY);
64
65 $options = [];
66
67 // get system currency
68 $currencyName = $config->get('currencyname');
69 $currencySymb = $config->get('currencysymb');
70
71 if (!isset($currencies[$currencyName]))
72 {
73 // currency not supported, manually register it
74 $options[] = [
75 'currency' => $currencyName,
76 'code' => $currencyName,
77 'symbol' => $currencySymb,
78 ];
79 }
80 else
81 {
82 // always move the default currency on top
83 $options[] = array_merge($currencies[$currencyName], ['code' => $currencyName]);
84 }
85
86 // iterate all the supported currencies
87 foreach ($currencies as $code => $data)
88 {
89 if ($code === $currencyName)
90 {
91 // skip default currency
92 continue;
93 }
94
95 $options[] = array_merge($data, ['code' => $code]);
96 }
97
98 // format currencies accordingly
99 return array_map(function($currency) use ($template) {
100 return JHtml::fetch('select.option', $currency['code'], static::formatCurrency($template, $currency));
101 }, $options);
102 }
103
104 /**
105 * Formats the currency option displayed in the dropdown.
106 *
107 * @param string $template The option text template.
108 * @param array $metadata The currency metadata
109 *
110 * @return string The resulting option text.
111 */
112 protected static function formatCurrency(string $template, array $metadata)
113 {
114 $template = preg_replace("/{name}/i", $metadata['currency'] ?? '', $template);
115 $template = preg_replace("/{symbol}/i", $metadata['symbol'] ?? '', $template);
116 $template = preg_replace("/{code}/i", $metadata['code'] ?? '', $template);
117
118 return $template;
119 }
120 }
121