1<?php
2/*
3* 2007-2015 PrestaShop
4*
5* NOTICE OF LICENSE
6*
7* This source file is subject to the Academic Free License (AFL 3.0)
8* that is bundled with this package in the file LICENSE.txt.
9* It is also available through the world-wide-web at this URL:
10* http://opensource.org/licenses/afl-3.0.php
11* If you did not receive a copy of the license and are unable to
12* obtain it through the world-wide-web, please send an email
13* to license@prestashop.com so we can send you a copy immediately.
14*
15* DISCLAIMER
16*
17* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18* versions in the future. If you wish to customize PrestaShop for your
19* needs please refer to http://www.prestashop.com for more information.
20*
21*  @author PrestaShop SA <contact@prestashop.com>
22*  @copyright  2007-2015 PrestaShop SA
23*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
24*  International Registered Trademark & Property of PrestaShop SA
25*/
26
27use PrestaShop\PrestaShop\Adapter\ObjectPresenter;
28use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
29
30if (!defined('_PS_VERSION_')) {
31    exit;
32}
33
34class Ps_Currencyselector extends Module implements WidgetInterface
35{
36    private $templateFile;
37
38    public function __construct()
39    {
40        $this->name = 'ps_currencyselector';
41        $this->author = 'PrestaShop';
42        $this->version = '2.0.1';
43        $this->need_instance = 0;
44
45        parent::__construct();
46
47        $this->displayName = $this->trans('Currency block', [], 'Modules.Currencyselector.Admin');
48        $this->description = $this->trans('Adds a block allowing customers to choose their preferred shopping currency.', [], 'Modules.Currencyselector.Admin');
49
50        $this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_];
51
52        $this->templateFile = 'module:ps_currencyselector/ps_currencyselector.tpl';
53    }
54
55    public function install()
56    {
57        return parent::install()
58            && $this->registerHook('actionAdminCurrenciesControllerSaveAfter');
59    }
60
61    public function hookActionAdminCurrenciesControllerSaveAfter($params)
62    {
63        return parent::_clearCache($this->templateFile);
64    }
65
66    public function getWidgetVariables($hookName, array $configuration)
67    {
68        $current_currency = null;
69        $serializer = new ObjectPresenter();
70        $currencies = array_map(
71            function ($currency) use ($serializer, &$current_currency) {
72                $currencyArray = $serializer->present($currency);
73
74                // serializer doesn't see 'sign' because it is not a regular
75                // ObjectModel field.
76                $currencyArray['sign'] = $currency->sign;
77
78                $url = $this->context->link->getLanguageLink($this->context->language->id);
79
80                $parsedUrl = parse_url($url);
81                $urlParams = [];
82                if (isset($parsedUrl['query'])) {
83                    parse_str($parsedUrl['query'], $urlParams);
84                }
85                $newParams = array_merge(
86                    $urlParams,
87                    [
88                        'SubmitCurrency' => 1,
89                        'id_currency' => $currency->id,
90                    ]
91                );
92                $newUrl = sprintf('%s://%s%s%s?%s',
93                    $parsedUrl['scheme'],
94                    $parsedUrl['host'],
95                    isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : '',
96                    $parsedUrl['path'],
97                    http_build_query($newParams)
98                );
99
100                $currencyArray['url'] = $newUrl;
101
102                if ($currency->id === $this->context->currency->id) {
103                    $currencyArray['current'] = true;
104                    $current_currency = $currencyArray;
105                } else {
106                    $currencyArray['current'] = false;
107                }
108
109                return $currencyArray;
110            },
111            Currency::getCurrencies(true, true)
112        );
113
114        return [
115            'currencies' => $currencies,
116            'current_currency' => $current_currency,
117        ];
118    }
119
120    public function renderWidget($hookName, array $configuration)
121    {
122        if (Configuration::isCatalogMode() || !Currency::isMultiCurrencyActivated()) {
123            return false;
124        }
125
126        $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
127
128        return $this->fetch($this->templateFile);
129    }
130}
131