PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.14
VikBooking Hotel Booking Engine & PMS v1.8.14
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / helpers / widgets / currency_converter.php

currency_converter.php in VikBooking Hotel Booking Engine & PMS 1.8.14, at admin/helpers/widgets/currency_converter.php

345 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage com_vikbooking
5 * @author Alessio Gaggii - E4J srl
6 * @copyright Copyright (C) 2022 E4J srl. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 * @link https://vikwp.com
9 */
10
11 defined('ABSPATH') or die('No script kiddies please!');
12
13 /**
14 * Class handler for admin widget "currency converter".
15 *
16 * @since 1.15.4 (J) - 1.5.10 (WP)
17 */
18 class VikBookingAdminWidgetCurrencyConverter extends VikBookingAdminWidget
19 {
20 /**
21 * The instance counter of this widget.
22 *
23 * @var int
24 */
25 protected static $instance_counter = -1;
26
27 /**
28 * Class constructor will define the widget name and identifier.
29 */
30 public function __construct()
31 {
32 // call parent constructor
33 parent::__construct();
34
35 $this->widgetName = JText::translate('VBO_W_CURRCONV_TITLE');
36 $this->widgetDescr = JText::translate('VBO_W_CURRCONV_DESCR');
37 $this->widgetId = basename(__FILE__, '.php');
38
39 // define widget and icon and style name
40 $this->widgetIcon = '<i class="' . VikBookingIcons::i('funnel-dollar') . '"></i>';
41 $this->widgetStyleName = 'violet';
42
43 // load widget's settings
44 $this->widgetSettings = $this->loadSettings();
45 if (!is_object($this->widgetSettings)) {
46 $this->widgetSettings = new stdClass;
47 }
48 }
49
50 /**
51 * Custom method for this widget only to exchange the rates between currencies.
52 * The method is called by the admin controller through an AJAX request.
53 * The visibility should be public, it should not exit the process, and
54 * any content sent to output will be returned to the AJAX response.
55 * In this case we return an array because this method requires "return":1.
56 *
57 * It's the actual rendering of the widget.
58 */
59 public function exchangeRates()
60 {
61 $wrapper = VikRequest::getString('wrapper', '', 'request');
62
63 $from_currency = VikRequest::getString('from_currency', '', 'request');
64 $to_currency = VikRequest::getString('to_currency', '', 'request');
65 $amount = VikRequest::getFloat('amount', 1, 'request');
66
67 if (empty($from_currency) || empty($to_currency)) {
68 // use the website default currency
69 $from_currency = VikBooking::getCurrencyName($iso = true);
70
71 // use the default to currency
72 $to_currency = !strcasecmp($from_currency, 'EUR') ? 'USD' : 'EUR';
73 }
74
75 if ($amount <= 0) {
76 $amount = 1;
77 }
78
79 // import currency converter class
80 VikBooking::import('currencyconverter');
81
82 // get numbering format data
83 $format = VikBooking::getNumberFormatData();
84
85 // get the instance of the converter object
86 $converter = new VboCurrencyConverter($from_currency, $to_currency, [$amount], explode(':', $format));
87
88 // collect values
89 $converter_error = null;
90 $exchanged = null;
91 $exchange_rate = null;
92
93 // make sure from currency is supported for conversion
94 if (!$converter->currencyExists($from_currency)) {
95 $converter_error = sprintf('Currency %s is not supported', $from_currency);
96 } elseif (!$converter->currencyExists($to_currency)) {
97 $converter_error = sprintf('Currency %s is not supported', $to_currency);
98 }
99
100 if (!$converter_error) {
101 // exchange the given rates
102 $exchanged = $converter->convert($get_floats = true);
103 }
104
105 if (!$converter_error && (!is_array($exchanged) || !count($exchanged))) {
106 // something went wrong during the conversion
107 $converter_error = $converter->getError();
108 $converter_error = empty($converter_error) ? 'Error while converting rates' : $converter_error;
109 } else {
110 // get the conversion rate
111 $exchange_rate = $converter->getConversionRate();
112
113 // update widget's settings with the lastly used currencies
114 $this->widgetSettings->from_currency = $from_currency;
115 $this->widgetSettings->to_currency = $to_currency;
116 $this->updateSettings(json_encode($this->widgetSettings));
117 }
118
119 // start output buffering
120 ob_start();
121
122 ?>
123 <div class="vbo-widget-currconv-result-data">
124 <?php
125 if ($converter_error || empty($exchanged)) {
126 ?>
127 <p class="err"><?php echo $converter_error; ?></p>
128 <?php
129 } else {
130 ?>
131 <p class="info"><?php echo JText::sprintf('VBO_CONV_RES_OTA_CURRENCY_EXC', $from_currency . ' ' . $amount, $to_currency . ' ' . $exchanged[0]); ?></p>
132 <p class="info"><?php echo JText::translate('VBO_CONV_RATE'); ?> <span class="badge badge-primary"><?php echo $exchange_rate; ?></span></p>
133 <?php
134 }
135 ?>
136 </div>
137 <?php
138
139 // get the HTML buffer
140 $html_content = ob_get_contents();
141 ob_end_clean();
142
143 // return an associative array of values
144 return [
145 'html' => $html_content,
146 'exchanged' => $exchanged,
147 'exchange_rate' => $exchange_rate,
148 ];
149 }
150
151 /**
152 * Preload the necessary CSS/JS assets.
153 *
154 * @return void
155 */
156 public function preload()
157 {
158 // load assets
159 $this->vbo_app->loadSelect2();
160 }
161
162 public function render(?VBOMultitaskData $data = null)
163 {
164 // increase widget's instance counter
165 static::$instance_counter++;
166
167 // check whether the widget is being rendered via AJAX when adding it through the customizer
168 $is_ajax = $this->isAjaxRendering();
169
170 // generate a unique ID for the sticky notes wrapper instance
171 $wrapper_instance = !$is_ajax ? static::$instance_counter : rand();
172 $wrapper_id = 'vbo-widget-currconv-' . $wrapper_instance;
173
174 // import currency converter class
175 if (!VikBooking::import('currencyconverter')) {
176 // display nothing
177 return;
178 }
179
180 // the website default currency
181 $def_currency = !empty($this->widgetSettings->from_currency) ? $this->widgetSettings->from_currency : VikBooking::getCurrencyName($iso = true);
182
183 // the default to currency
184 $def_to_currency = !strcasecmp($def_currency, 'EUR') ? 'USD' : 'EUR';
185 if (!empty($this->widgetSettings->to_currency)) {
186 $def_to_currency = $this->widgetSettings->to_currency;
187 }
188
189 // get numbering format data
190 $format = VikBooking::getNumberFormatData();
191
192 // get the instance of the converter object
193 $converter = new VboCurrencyConverter($def_currency, $def_to_currency, [1], explode(':', $format));
194
195 // grab all currency names
196 $all_currencies = $converter->getCurrencyNames();
197
198 ?>
199 <div class="vbo-admin-widget-wrapper">
200 <div class="vbo-admin-widget-head">
201 <h4><?php echo $this->widgetIcon; ?> <span><?php echo $this->widgetName; ?></span></h4>
202 </div>
203 <div id="<?php echo $wrapper_id; ?>" class="vbo-widget-currconv-wrap" data-instance="<?php echo $wrapper_instance; ?>">
204 <div class="vbo-widget-currconv-filters">
205 <div class="vbo-widget-currconv-filters-main">
206 <div class="vbo-widget-currconv-filter vbo-widget-currconv-filter-from">
207 <label for="vbo-widget-currconv-from-<?php echo $wrapper_instance; ?>"><?php echo JText::translate('VBO_CONV_FROM'); ?></label>
208 <select id="vbo-widget-currconv-from-<?php echo $wrapper_instance; ?>" class="vbo-widget-currconv-sel vbo-widget-currconv-from">
209 <?php
210 foreach ($all_currencies as $code => $name) {
211 ?>
212 <option value="<?php echo $code; ?>"<?php echo !strcasecmp($def_currency, $code) ? ' selected="selected"' : ''; ?>><?php echo $name; ?></option>
213 <?php
214 }
215 ?>
216 </select>
217 </div>
218 <div class="vbo-widget-currconv-filter vbo-widget-currconv-filter-amount">
219 <label for="vbo-widget-currconv-amount-<?php echo $wrapper_instance; ?>"><?php echo JText::translate('VBPSHOWSEASONSFOUR'); ?></label>
220 <input type="number" id="vbo-widget-currconv-amount-<?php echo $wrapper_instance; ?>" class="vbo-widget-currconv-amount" min="0.01" step="any" value="1" />
221 </div>
222 </div>
223 <div class="vbo-widget-currconv-filters-secondary">
224 <div class="vbo-widget-currconv-filter vbo-widget-currconv-filter-to">
225 <label for="vbo-widget-currconv-to-<?php echo $wrapper_instance; ?>"><?php echo JText::translate('VBO_CONV_TO'); ?></label>
226 <select id="vbo-widget-currconv-to-<?php echo $wrapper_instance; ?>" class="vbo-widget-currconv-sel vbo-widget-currconv-to">
227 <?php
228 foreach ($all_currencies as $code => $name) {
229 ?>
230 <option value="<?php echo $code; ?>"<?php echo !strcasecmp($def_to_currency, $code) ? ' selected="selected"' : ''; ?>><?php echo $name; ?></option>
231 <?php
232 }
233 ?>
234 </select>
235 </div>
236 </div>
237 <div class="vbo-widget-currconv-filters-submit">
238 <div class="vbo-widget-currconv-filter vbo-widget-currconv-filter-submit">
239 <button type="button" class="btn vbo-config-btn" onclick="vboWidgetCurrConvCalc('<?php echo $wrapper_id; ?>');"><?php VikBookingIcons::e('exchange-alt'); ?><?php echo JText::translate('VBRATESOVWRATESCALCULATORCALC'); ?></button>
240 </div>
241 </div>
242 </div>
243 <div class="vbo-widget-currconv-result"></div>
244 </div>
245 </div>
246 <?php
247
248 if (static::$instance_counter === 0 || $is_ajax) {
249 /**
250 * Print the JS code only once for all instances of this widget.
251 * The real rendering is made through AJAX, not when the page loads.
252 */
253 ?>
254
255 <script type="text/javascript">
256
257 /**
258 * Perform the request to exchange rates.
259 */
260 function vboWidgetCurrConvExchange(wrapper) {
261 var widget_instance = jQuery('#' + wrapper);
262 if (!widget_instance.length) {
263 return false;
264 }
265
266 // get vars for making the request
267 var from_currency = widget_instance.find('.vbo-widget-currconv-from').val();
268 var to_currency = widget_instance.find('.vbo-widget-currconv-to').val();
269 var amount = widget_instance.find('.vbo-widget-currconv-amount').val();
270
271 // the widget method to call
272 var call_method = 'exchangeRates';
273
274 // make a request to load the available room rates
275 VBOCore.doAjax(
276 "<?php echo $this->getExecWidgetAjaxUri(); ?>",
277 {
278 widget_id: "<?php echo $this->getIdentifier(); ?>",
279 call: call_method,
280 return: 1,
281 from_currency: from_currency,
282 to_currency: to_currency,
283 amount: amount,
284 tmpl: "component"
285 },
286 function(response) {
287 try {
288 var obj_res = typeof response === 'string' ? JSON.parse(response) : response;
289 if (!obj_res.hasOwnProperty(call_method) || !obj_res[call_method]) {
290 console.error('Unexpected JSON response', obj_res);
291 return false;
292 }
293
294 // replace HTML with exchange rates
295 widget_instance.find('.vbo-widget-currconv-result').html(obj_res[call_method]['html']);
296 } catch(err) {
297 console.error('could not parse JSON response', err, response);
298 }
299 },
300 function(error) {
301 // empty result container
302 widget_instance.find('.vbo-widget-currconv-result').html('');
303 console.error(error);
304 }
305 );
306 }
307
308 /**
309 * Calculate the exchange rates.
310 */
311 function vboWidgetCurrConvCalc(wrapper) {
312 var widget_instance = jQuery('#' + wrapper);
313 if (!widget_instance.length) {
314 return false;
315 }
316
317 // show loading spinner
318 widget_instance.find('.vbo-widget-currconv-result').html('<p style="text-align: center;"><?php VikBookingIcons::e('circle-notch', 'fa-spin fa-fw'); ?></p>');
319
320 // load data
321 vboWidgetCurrConvExchange(wrapper);
322 }
323
324 </script>
325 <?php
326 }
327 ?>
328
329 <script type="text/javascript">
330
331 jQuery(function() {
332
333 // render select2
334 jQuery('#<?php echo $wrapper_id; ?>').find('.vbo-widget-currconv-sel').select2({
335 width: "200px",
336 });
337
338 });
339
340 </script>
341
342 <?php
343 }
344 }
345