Skip to content

Commit

Permalink
:corrlib/Thelange rate, add error message in the currency configurati…
Browse files Browse the repository at this point in the history
…on page when an exchange rate could not be found (related thelia#1751)

	/Controller/Admin/CurrencyController.php
	templates/backOffice/default/I18n/en_US.php
	templates/backOffice/default/I18n/fr_FR.php
  • Loading branch information
roadster31 authored and gillesbourgeat committed Jan 6, 2016
1 parent 4af4b98 commit 5f86a0d
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
# 2.1.8

- Fix for undefined currency exchange rate, add error message in the currency configuration page when an exchange rate could not be found (related #1751)
- Fix the rounding of prices in the order product loop (related to #1753)
- Add EQUAL to product loop filter by min or max (related to #1750)
- Fix output value IS_DEFAULT in the product_sale_elements loop (related to #1745)
Expand Down
32 changes: 21 additions & 11 deletions core/lib/Thelia/Action/Currency.php
Expand Up @@ -12,17 +12,18 @@

namespace Thelia\Action;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Currency\CurrencyCreateEvent;
use Thelia\Core\Event\Currency\CurrencyDeleteEvent;
use Thelia\Core\Event\Currency\CurrencyUpdateEvent;
use Thelia\Core\Event\Currency\CurrencyUpdateRateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Event\UpdatePositionEvent;
use Thelia\Core\Translation\Translator;
use Thelia\CurrencyConverter\CurrencyConverter;
use Thelia\CurrencyConverter\Exception\CurrencyNotFoundException;
use Thelia\Log\Tlog;
use Thelia\Math\Number;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Currency as CurrencyModel;
use Thelia\Model\CurrencyQuery;

Expand Down Expand Up @@ -104,7 +105,9 @@ public function setDefault(CurrencyUpdateEvent $event)

// Update rates when setting a new default currency
if ($event->getIsDefault()) {
$event->getDispatcher()->dispatch(TheliaEvents::CURRENCY_UPDATE_RATES);
$updateRateEvent = new CurrencyUpdateRateEvent();

$event->getDispatcher()->dispatch(TheliaEvents::CURRENCY_UPDATE_RATES, $updateRateEvent);
}

$event->setCurrency($currency);
Expand Down Expand Up @@ -134,7 +137,7 @@ public function delete(CurrencyDeleteEvent $event)
}
}

public function updateRates(Event $event)
public function updateRates(CurrencyUpdateRateEvent $event)
{
if (null === $defaultCurrency = CurrencyQuery::create()->findOneByByDefault(true)) {
throw new \RuntimeException('Unable to find a default currency, please define a default currency.');
Expand All @@ -147,19 +150,26 @@ public function updateRates(Event $event)

/** @var \Thelia\Model\Currency $currency */
foreach ($currencies as $currency) {
$rate = $this->currencyConverter
->from($defaultCurrency->getCode())
->to($currency->getCode())
->convert($baseValue);

$currency->setRate($rate->getNumber(-1))->save();
try {
$rate = $this->currencyConverter
->from($defaultCurrency->getCode())
->to($currency->getCode())
->convert($baseValue);

$currency->setRate($rate->getNumber(-1))->save();
} catch (CurrencyNotFoundException $ex) {
Tlog::getInstance()->addError(
sprintf("Unable to find exchange rate for currency %s, ID %d", $currency->getCode(), $currency->getId())
);
$event->addUndefinedRate($currency->getId());
}
}
}

/**
* Changes position, selecting absolute ou relative change.
*
* @param CategoryChangePositionEvent $event
* @param UpdatePositionEvent $event
*/
public function updatePosition(UpdatePositionEvent $event)
{
Expand Down
13 changes: 11 additions & 2 deletions core/lib/Thelia/Controller/Admin/BaseAdminController.php
Expand Up @@ -88,15 +88,24 @@ protected function pageNotFound()
/**
* Return a general error page
*
* @param string $message a message string, or an exception instance
* @param \Exception|string $message a message string, or an exception instance
* @param int $status the HTTP status (default is 500)
*
* @return \Thelia\Core\HttpFoundation\Response
*/
protected function errorPage($message, $status = 500)
{
if ($message instanceof \Exception) {
$message = $this->getTranslator()->trans("Sorry, an error occured: %msg", array('%msg' => $message->getMessage()));
$strMessage = $this->getTranslator()->trans(
"Sorry, an error occured: %msg",
[ '%msg' => $message->getMessage() ]
);

Tlog::getInstance()->addError($strMessage.": ".$message->getTraceAsString());

$message = $strMessage;
} else {
Tlog::getInstance()->addError($message);
}

return $this->render(
Expand Down
12 changes: 11 additions & 1 deletion core/lib/Thelia/Controller/Admin/CurrencyController.php
Expand Up @@ -14,6 +14,7 @@

use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Core\Event\Currency\CurrencyDeleteEvent;
use Thelia\Core\Event\Currency\CurrencyUpdateRateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Event\Currency\CurrencyUpdateEvent;
use Thelia\Core\Event\Currency\CurrencyCreateEvent;
Expand Down Expand Up @@ -184,7 +185,16 @@ public function updateRatesAction()
}

try {
$this->dispatch(TheliaEvents::CURRENCY_UPDATE_RATES);
$event = new CurrencyUpdateRateEvent();

$this->dispatch(TheliaEvents::CURRENCY_UPDATE_RATES, $event);

if ($event->hasUndefinedRates()) {
return $this->render('currencies', [
'undefined_rates' => $event->getUndefinedRates()
]);
}

} catch (\Exception $ex) {
// Any error
return $this->errorPage($ex);
Expand Down
41 changes: 41 additions & 0 deletions core/lib/Thelia/Core/Event/Currency/CurrencyUpdateRateEvent.php
@@ -0,0 +1,41 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/

namespace Thelia\Core\Event\Currency;

use Thelia\Core\Event\ActionEvent;

class CurrencyUpdateRateEvent extends ActionEvent
{
protected $undefinedRates = [];

/**
* @param int $currencyId
*/
public function addUndefinedRate($currencyId)
{
$this->undefinedRates[] = $currencyId;
}

public function hasUndefinedRates()
{
return ! empty($this->undefinedRates);
}

/**
* @return array of currency objects
*/
public function getUndefinedRates()
{
return $this->undefinedRates;
}
}
6 changes: 6 additions & 0 deletions templates/backOffice/default/I18n/en_US.php
Expand Up @@ -11,6 +11,9 @@
'<strong>Cannot translate all fields.</strong> According to your PHP configuration, forms cannot contains more than %current_max_input_vars input fields, but at least %required_max_input_vars are required. Please change the value of max_input_vars in your PHP configuration of change the translation file by hand.' => '<strong>Cannot translate all fields.</strong> According to your PHP configuration, forms cannot contains more than %current_max_input_vars input fields, but at least %required_max_input_vars are required. Please change the value of max_input_vars in your PHP configuration of change the translation file by hand.',
'<strong>Congratulations</strong>, all text is now translated !' => '<strong>Congratulations</strong>, all text is now translated !',
'<strong>Did not found any text to translate</strong>. It\'s probably normal. If not, please be sure to use Smarty\'s "intl" function in templates, or the Translator::trans() method in PHP files.' => '<strong>Did not found any text to translate</strong>. It\'s probably normal. If not, please be sure to use Smarty\'s "intl" function in templates, or the Translator::trans() method in PHP files.',
'<strong>User</strong> : to redefine the translations for your website' => '<strong>User</strong> : to redefine the translations for your website',
'<strong>Warning</strong>, an exchange rate was not found for at least one currency: %list' => '<strong>Warning</strong>, an exchange rate was not found for at least one currency: %list',
'<strong>Warning</strong>, some of your countries are not included in any shipping zone:' => '<strong>Warning</strong>, some of your countries are not included in any shipping zone:',
'<strong>Warning</strong>, some of your shipping zones are not attached to any delivery module:' => '<strong>Warning</strong>, some of your shipping zones are not attached to any delivery module:',
'A content could be attached to more than one folder. Select here the additional folders for this content.' => 'You can attach more than one folder to a content. Select the additional folders for this content here.',
'A product could be attached to more than one category. Select here the additional categories for this product.' => 'You can attach more than one category to a product. Select the additional categories for this product here.',
Expand Down Expand Up @@ -329,6 +332,7 @@
'Delete this customer address' => 'Delete this customer address',
'Delete this customer and all his orders' => 'Delete this customer and all his orders',
'Delete this feature' => 'Delete this feature',
'Delete this folder' => 'Delete this folder',
'Delete this folder and all its contents' => 'Delete this folder and all its contents',
'Delete this hook' => 'Delete this hook',
'Delete this language' => 'Delete this language',
Expand Down Expand Up @@ -904,13 +908,15 @@
'Select a product and click (+) to add it as an accessory' => 'Select a product and click (+) to add it as an accessory',
'Select a product...' => 'Select a product...',
'Select a tax tule' => 'Select a tax tule',
'Select a template' => 'Select a template',
'Select a value click (+) to add it to the combination' => 'Select a value click (+) to add it to the combination',
'Select an attribute and click (+) to add it to this template' => 'Select an attribute and click (+) to add it to this template',
'Select an attribute and click (+) to view available values' => 'Select an attribute and click (+) to view available values',
'Select an attribute value...' => 'Select an attribute value...',
'Select an attribute...' => 'Select an attribute...',
'Select attribute :' => 'Select attribute :',
'Select attribute values to combine. You may enter a default value for some of the fields of the generated combinations.' => 'Select attribute values to combine. You may enter a default value for some of the fields of the generated combinations.',
'Select here a template for this product' => 'Select here a template for this product',
'Select here the tax applicable to this product' => 'Select here the tax applicable to this product',
'Select offered product :' => 'Select offered product :',
'Select offrered product category :' => 'Select offrered product category :',
Expand Down
8 changes: 8 additions & 0 deletions templates/backOffice/default/I18n/fr_FR.php
Expand Up @@ -11,6 +11,9 @@
'<strong>Cannot translate all fields.</strong> According to your PHP configuration, forms cannot contains more than %current_max_input_vars input fields, but at least %required_max_input_vars are required. Please change the value of max_input_vars in your PHP configuration of change the translation file by hand.' => '<strong>Limitation système bloquante !</strong> Votre configuration PHP limite le nombre de champs d\'un formulaire HTML à %current_max_input_vars champs. Au moins %required_max_input_vars champs sont nécessaires pour effectuer les traductions. Merci de modifier en conséquence la variable max_input_vars de votre configuration PHP. Vous pouvez aussi modifier les fichiers de traduction à la main.',
'<strong>Congratulations</strong>, all text is now translated !' => '<strong>Félicitations</strong>, tous les textes ont été traduits !',
'<strong>Did not found any text to translate</strong>. It\'s probably normal. If not, please be sure to use Smarty\'s "intl" function in templates, or the Translator::trans() method in PHP files.' => '<streong>Aucun texte à traduire</strong>. C\'est probablement normal. Si ce n\'est pas le cas vérifiez que vous utilisez bien la fonction Smarty "intl" ou bien le translator Translator::trans dans un fichier php',
'<strong>User</strong> : to redefine the translations for your website' => '<strong>Utilisateur</strong> : redéfinir les traductions pour votre site Web',
'<strong>Warning</strong>, an exchange rate was not found for at least one currency: %list' => '<strong>Attention</strong>, le taux de change d\'au moins une devise n\'a pas été trouvé: %list',
'<strong>Warning</strong>, some of your countries are not included in any shipping zone:' => '<strong>Attention</strong>, certains des pays ne sont inclus dans aucune zone de livraison',
'<strong>Warning</strong>, some of your shipping zones are not attached to any delivery module:' => '<strong>Attention</strong>, les zones de livraison suivantes ne sont associées à aucun module:',
'A content could be attached to more than one folder. Select here the additional folders for this content.' => 'Un contenu peut être rattaché à plusieurs dossiers. Sélectionnez ici les dossiers dans lesquels ce contenu apparaîtra',
'A product could be attached to more than one category. Select here the additional categories for this product.' => 'Un produit peut être associé à plusieurs rubriques. Sélectionner les rubrique pour lesquels le produit sera associé',
Expand Down Expand Up @@ -329,6 +332,7 @@
'Delete this customer address' => 'Supprimer cette adresse',
'Delete this customer and all his orders' => 'Supprimer ce client et toutes ses commandes',
'Delete this feature' => 'Supprimer cette caractéristique',
'Delete this folder' => 'Supprimer ce dossier',
'Delete this folder and all its contents' => 'Supprimer ce dossier et tout ses contenus',
'Delete this hook' => 'Supprimer ce hook',
'Delete this language' => 'Supprimer cette langue',
Expand Down Expand Up @@ -530,6 +534,7 @@
'Enter here the product name in the default language (%title)' => 'Entrez ici le nom du produit dans la langue par défaut (%title)',
'Enter here the product price in %title' => 'Indiquez ici le prix HT en %title ',
'Enter here the product reference' => 'Entrez ici la nouvelle référence produit',
'Enter here the product stock' => 'Indiez ici le stock du produit',
'Enter here the product tax price in %title' => 'Indiquez ici le prix TTC en %title ',
'Enter here the product weight, in Kilogrammes' => 'Entrez ici le poids du produit, en Kilogrammes',
'Enter here the template name in the default language (%title)' => 'Renseignez le nom du gabarit dans la langue par défaut (%title)',
Expand Down Expand Up @@ -821,6 +826,7 @@
'Product features' => 'Caractéristiques produit',
'Product price' => 'Prix du produit',
'Product price including taxes' => 'Prix du produit taxes incluses',
'Product stock' => 'Stock du produit',
'Product tax price' => 'Prix TTC',
'Product templates' => 'Gabarits de produit',
'Product title' => 'Titre du produit',
Expand Down Expand Up @@ -904,13 +910,15 @@
'Select a product and click (+) to add it as an accessory' => 'Sélectionner un produit et cliquer sur (+) pour l\'ajouter en tant qu\'accessoire',
'Select a product...' => 'Sélectionner un produit...',
'Select a tax tule' => 'Sélectionnez une règle de taxes',
'Select a template' => 'Choisissez un gabarit',
'Select a value click (+) to add it to the combination' => 'Sélectionnez une valeur et cliquez sur (+) pour l\'ajouter à la combinaison',
'Select an attribute and click (+) to add it to this template' => 'Sélectionner une déclinaison et cliquer sur (+) pour l\'ajouter à ce gabarit',
'Select an attribute and click (+) to view available values' => 'Sélectionnez une déclinaison et cliquez sur (+) pour voir les valeurs disponibles',
'Select an attribute value...' => 'Sélectionnez une valeur de déclinaison...',
'Select an attribute...' => 'Sélectionnez une déclinaison...',
'Select attribute :' => 'Choisissez une déclinaison',
'Select attribute values to combine. You may enter a default value for some of the fields of the generated combinations.' => 'Sélectionnez les valeurs de déclinaison à combiner. Vous pouvez entrer une valeur par défaut pour certains champs des combinaisons générées.',
'Select here a template for this product' => 'Choisissez un gabarit pour ce produit',
'Select here the tax applicable to this product' => 'Sélectionnez ici la taxe applicable sur ce produit',
'Select offered product :' => 'Produit offert :',
'Select offrered product category :' => 'Catégorie du produit offert :',
Expand Down
12 changes: 12 additions & 0 deletions templates/backOffice/default/currencies.html
Expand Up @@ -25,6 +25,18 @@
<div class="row">
<div class="col-md-12">

{if ! empty($undefined_rates)}
<div class="alert alert-warning">
{$currencyList = ''}
{loop type="currency" name="err-currencies" id={","|implode:$undefined_rates}}
{$currencyList = "$currencyList, $NAME ($ISOCODE)"}
{/loop}

{intl l="<strong>Warning</strong>, an exchange rate was not found for at least one currency: %list" list=$currencyList|ltrim:', '}

</div>
{/if}

<form action="{url path='/admin/configuration/currencies/update-rates'}" method="post">

<div class="general-block-decorator">
Expand Down

0 comments on commit 5f86a0d

Please sign in to comment.