* @copyright 2007-2015 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ use PrestaShop\PrestaShop\Adapter\Presenter\Object\ObjectPresenter; use PrestaShop\PrestaShop\Core\Module\WidgetInterface; if (!defined('_PS_VERSION_')) { exit; } class Ps_Currencyselector extends Module implements WidgetInterface { private $templateFile; public function __construct() { $this->name = 'ps_currencyselector'; $this->tab = 'front_office_features'; $this->author = 'PrestaShop'; $this->version = '2.1.1'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->trans('Currency block', [], 'Modules.Currencyselector.Admin'); $this->description = $this->trans('Offer your customers the possibility to buy matching items when on a product page.', [], 'Modules.Currencyselector.Admin'); $this->ps_versions_compliancy = ['min' => '1.7.5.0', 'max' => _PS_VERSION_]; $this->templateFile = 'module:ps_currencyselector/ps_currencyselector.tpl'; } public function install() { return parent::install() && $this->registerHook('actionAdminCurrenciesControllerSaveAfter'); } public function hookActionAdminCurrenciesControllerSaveAfter() { return parent::_clearCache($this->templateFile); } public function getWidgetVariables($hookName, array $configuration) { $current_currency = null; $serializer = new ObjectPresenter(); $currencies = array_map( function ($currency) use ($serializer, &$current_currency) { $currencyArray = $serializer->present($currency); // serializer doesn't see 'sign' because it is not a regular // ObjectModel field. $currencyArray['sign'] = $currency->sign; $url = $this->context->link->getLanguageLink($this->context->language->id); $parsedUrl = parse_url($url); $urlParams = []; if (isset($parsedUrl['query'])) { parse_str($parsedUrl['query'], $urlParams); } $newParams = array_merge( $urlParams, [ 'SubmitCurrency' => 1, 'id_currency' => $currency->id, ] ); $newUrl = sprintf('%s://%s%s%s?%s', $parsedUrl['scheme'], $parsedUrl['host'], isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : '', $parsedUrl['path'], http_build_query($newParams) ); $currencyArray['url'] = $newUrl; if ($currency->id === $this->context->currency->id) { $currencyArray['current'] = true; $current_currency = $currencyArray; } else { $currencyArray['current'] = false; } return $currencyArray; }, Currency::getCurrencies(true, true) ); return [ 'currencies' => $currencies, 'current_currency' => $current_currency, ]; } public function renderWidget($hookName, array $configuration) { if (Configuration::isCatalogMode() || !Currency::isMultiCurrencyActivated()) { return false; } $this->smarty->assign($this->getWidgetVariables($hookName, $configuration)); return $this->fetch($this->templateFile); } }