<?php
namespace App\Dashboard\Widget; class CurrencyCalculator extends \Engine\Dashboard\Widgets\WidgetAbstract { protected function getDefaultOptions() { return []; } protected function getData() { $calledClass = get_called_class(); global $CFG, $_LANG; $formValues = []; $formValues['initialCurrency']=$CFG['currencies']['workCurrency']; $formValues['finalCurrency']=$CFG['currencies']['defaultCurrency']; $formValues['currencyDate']=TODAY_ISO; $formValues['currencyType']=$CFG['currencies']['defaultCurrencyType']; $id = $this->getId(); $form = new \Engine\Form\General(); $form->setId('formCurrencyCalculator'); $form->addContainer(\Engine\Form\Container\Table::create()); $form->setStyle('width', 150); $fld=new \App\Form\Input\Price($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['initialPrice'], "price_{$id}", ['value'=>$formValues['initialPrice'], 'currency'=>$formValues['initialCurrency']]); $fld->setMandatory(); $form->addElement($fld); $fld=new \App\Form\Input\Select_Currency($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['finalCurr'], "finalCurrency_{$id}", $formValues['finalCurrency']); $fld->setMandatory(); $form->addElement($fld); $fld=new \Engine\Form\Input\Date($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['currDate'], "currencyDate_{$id}", $formValues['currencyDate']); $form->addElement($fld); $fld=new \App\Form\Input\Select_CurrencyType($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['currType'], "currencyType_{$id}", $formValues['currencyType']); $form->addElement($fld); $fld = new \Engine\Form\Input\Text($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['response'], "response_{$id}"); $fld->setStyle('CSSClass', 'response'); $fld->setDisabled(true); $form->addElement($fld); $fldButton = new \Engine\Form\Button\Button($_LANG['dashboard']['widgetsList'][$calledClass]['formFields']['submitBtn']); $dashboard = $this->getDashboard(); $idEntity = ''; if ($dashboard->getOption('idCorporate')) { $idEntity = $dashboard->getOption('idCorporate'); } elseif ($dashboard->getOption('idCustomer')) { $idEntity = $dashboard->getOption('idCustomer'); } $fldButton->addEvent('onClick', "window.dashboardController.dashboard.request(MVC.appUrl(MVC.CURRENT_MODULE, 'editDashboardElement', { 'idEntity' : '$idEntity' } ),
				{
					operation: 'methodCall',
					method: 'convert',
					src: '$id',
					price: \$F('price_{$id}_value_'),
					currency: \$F('price_{$id}_currency_'),
					currencyDate: \$F('currencyDate_{$id}'),
					currencyType: \$F('currencyType_{$id}'),
					finalCurrency: \$F('finalCurrency_{$id}')
				},

				window.currencyCalculator_callMethod_callMethod
			);
		"); $form->addButton($fldButton); return ['form'=>$form]; } protected function populateLocalOptionsForm(\Engine\Form\Container\GeneralAbstract $form) { return $form; } protected function extractFormOptions($formData) { } public function __callMethod($method, $params, $JSONResponse) { if ($method=='convert') { return $this->convert($params, $JSONResponse); } return parent::__callMethod($method, $params, $JSONResponse); } private function convert($params, $JSONResponse) { $JSONResponse->status = 'OK'; $JSONResponse->response = \App\CurrencyExchange\Rates::getInstance($params['currencyDate'], $params['currencyType'])->convert($params['price'], $params['currency'], $params['finalCurrency']); } }