Inital commit
This commit is contained in:
@@ -12,40 +12,54 @@
|
||||
|
||||
namespace Thelia\Action;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
use Thelia\Core\Event\ActionEvent;
|
||||
use Thelia\Model\CurrencyQuery;
|
||||
use Thelia\Model\Currency as CurrencyModel;
|
||||
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
|
||||
use Thelia\Core\Event\Currency\CurrencyUpdateEvent;
|
||||
use Thelia\Core\Event\Currency\CurrencyCreateEvent;
|
||||
use Thelia\Core\Event\Currency\CurrencyDeleteEvent;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
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\Currency as CurrencyModel;
|
||||
use Thelia\Model\CurrencyQuery;
|
||||
|
||||
class Currency extends BaseAction implements EventSubscriberInterface
|
||||
{
|
||||
/** @var CurrencyConverter */
|
||||
protected $currencyConverter;
|
||||
|
||||
public function __construct(CurrencyConverter $currencyConverter)
|
||||
{
|
||||
$this->currencyConverter = $currencyConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new currencyuration entry
|
||||
*
|
||||
* @param \Thelia\Core\Event\Currency\CurrencyCreateEvent $event
|
||||
* @param $eventName
|
||||
* @param EventDispatcherInterface $dispatcher
|
||||
*/
|
||||
public function create(CurrencyCreateEvent $event)
|
||||
public function create(CurrencyCreateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$currency = new CurrencyModel();
|
||||
|
||||
$currency
|
||||
->setDispatcher($event->getDispatcher())
|
||||
$isDefault = CurrencyQuery::create()->count() === 0;
|
||||
|
||||
$currency
|
||||
->setDispatcher($dispatcher)
|
||||
->setLocale($event->getLocale())
|
||||
->setName($event->getCurrencyName())
|
||||
->setSymbol($event->getSymbol())
|
||||
->setFormat($event->getFormat())
|
||||
->setRate($event->getRate())
|
||||
->setCode(strtoupper($event->getCode()))
|
||||
|
||||
->setByDefault($isDefault)
|
||||
->save()
|
||||
;
|
||||
|
||||
@@ -56,17 +70,19 @@ class Currency extends BaseAction implements EventSubscriberInterface
|
||||
* Change a currency
|
||||
*
|
||||
* @param \Thelia\Core\Event\Currency\CurrencyUpdateEvent $event
|
||||
* @param $eventName
|
||||
* @param EventDispatcherInterface $dispatcher
|
||||
*/
|
||||
public function update(CurrencyUpdateEvent $event)
|
||||
public function update(CurrencyUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
if (null !== $currency = CurrencyQuery::create()->findPk($event->getCurrencyId())) {
|
||||
|
||||
$currency
|
||||
->setDispatcher($event->getDispatcher())
|
||||
->setDispatcher($dispatcher)
|
||||
|
||||
->setLocale($event->getLocale())
|
||||
->setName($event->getCurrencyName())
|
||||
->setSymbol($event->getSymbol())
|
||||
->setFormat($event->getFormat())
|
||||
->setRate($event->getRate())
|
||||
->setCode(strtoupper($event->getCode()))
|
||||
|
||||
@@ -80,36 +96,63 @@ class Currency extends BaseAction implements EventSubscriberInterface
|
||||
* Set the default currency
|
||||
*
|
||||
* @param CurrencyUpdateEvent $event
|
||||
* @param $eventName
|
||||
* @param EventDispatcherInterface $dispatcher
|
||||
*/
|
||||
public function setDefault(CurrencyUpdateEvent $event)
|
||||
public function setDefault(CurrencyUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
|
||||
if (null !== $currency = CurrencyQuery::create()->findPk($event->getCurrencyId())) {
|
||||
// Reset default status
|
||||
CurrencyQuery::create()->filterByByDefault(true)->update(array('ByDefault' => false));
|
||||
|
||||
$currency
|
||||
->setDispatcher($event->getDispatcher())
|
||||
->setDispatcher($dispatcher)
|
||||
->setVisible($event->getVisible())
|
||||
->setByDefault($event->getIsDefault())
|
||||
->save()
|
||||
;
|
||||
|
||||
// Update rates when setting a new default currency
|
||||
if ($event->getIsDefault()) {
|
||||
$updateRateEvent = new CurrencyUpdateRateEvent();
|
||||
|
||||
$dispatcher->dispatch(TheliaEvents::CURRENCY_UPDATE_RATES, $updateRateEvent);
|
||||
}
|
||||
|
||||
$event->setCurrency($currency);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CurrencyUpdateEvent $event
|
||||
*/
|
||||
public function setVisible(CurrencyUpdateEvent $event)
|
||||
{
|
||||
if (null !== $currency = CurrencyQuery::create()->findPk($event->getCurrencyId())) {
|
||||
if (!$currency->getByDefault()) {
|
||||
$currency->setVisible($event->getVisible())->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a currencyuration entry
|
||||
*
|
||||
* @param \Thelia\Core\Event\Currency\CurrencyDeleteEvent $event
|
||||
* @param $eventName
|
||||
* @param EventDispatcherInterface $dispatcher
|
||||
*/
|
||||
public function delete(CurrencyDeleteEvent $event)
|
||||
public function delete(CurrencyDeleteEvent $event, $eventName, EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
|
||||
if (null !== ($currency = CurrencyQuery::create()->findPk($event->getCurrencyId()))) {
|
||||
if ($currency->getByDefault()) {
|
||||
throw new \RuntimeException(
|
||||
Translator::getInstance()->trans('It is not allowed to delete the default currency')
|
||||
);
|
||||
}
|
||||
|
||||
$currency
|
||||
->setDispatcher($event->getDispatcher())
|
||||
->setDispatcher($dispatcher)
|
||||
->delete()
|
||||
;
|
||||
|
||||
@@ -117,39 +160,45 @@ class Currency extends BaseAction implements EventSubscriberInterface
|
||||
}
|
||||
}
|
||||
|
||||
public function updateRates(ActionEvent $event)
|
||||
public function updateRates(CurrencyUpdateRateEvent $event)
|
||||
{
|
||||
$rates_url = ConfigQuery::read('currency_rate_update_url', 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
|
||||
if (null === $defaultCurrency = CurrencyQuery::create()->findOneByByDefault(true)) {
|
||||
throw new \RuntimeException('Unable to find a default currency, please define a default currency.');
|
||||
}
|
||||
|
||||
$rate_data = @file_get_contents($rates_url);
|
||||
$defaultCurrency->setRate(1)->save();
|
||||
|
||||
if ($rate_data && $sxe = new \SimpleXMLElement($rate_data)) {
|
||||
$currencies = CurrencyQuery::create()->filterByByDefault(false);
|
||||
$baseValue = new Number('1');
|
||||
|
||||
foreach ($sxe->Cube[0]->Cube[0]->Cube as $last) {
|
||||
$code = strtoupper($last["currency"]);
|
||||
$rate = floatval($last['rate']);
|
||||
/** @var \Thelia\Model\Currency $currency */
|
||||
foreach ($currencies as $currency) {
|
||||
try {
|
||||
$rate = $this->currencyConverter
|
||||
->from($defaultCurrency->getCode())
|
||||
->to($currency->getCode())
|
||||
->convert($baseValue);
|
||||
|
||||
if (null !== $currency = CurrencyQuery::create()->findOneByCode($code)) {
|
||||
$currency
|
||||
->setDispatcher($event->getDispatcher())
|
||||
->setRate($rate)
|
||||
->save()
|
||||
;
|
||||
}
|
||||
$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());
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException(sprintf("Failed to get currency rates data from URL %s", $rates_url));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes position, selecting absolute ou relative change.
|
||||
*
|
||||
* @param CategoryChangePositionEvent $event
|
||||
* @param UpdatePositionEvent $event
|
||||
* @param string $eventName
|
||||
* @param EventDispatcherInterface $dispatcher
|
||||
*/
|
||||
public function updatePosition(UpdatePositionEvent $event)
|
||||
public function updatePosition(UpdatePositionEvent $event, $eventName, EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->genericUpdatePosition(CurrencyQuery::create(), $event);
|
||||
$this->genericUpdatePosition(CurrencyQuery::create(), $event, $dispatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,6 +211,7 @@ class Currency extends BaseAction implements EventSubscriberInterface
|
||||
TheliaEvents::CURRENCY_UPDATE => array("update", 128),
|
||||
TheliaEvents::CURRENCY_DELETE => array("delete", 128),
|
||||
TheliaEvents::CURRENCY_SET_DEFAULT => array("setDefault", 128),
|
||||
TheliaEvents::CURRENCY_SET_VISIBLE => array("setVisible", 128),
|
||||
TheliaEvents::CURRENCY_UPDATE_RATES => array("updateRates", 128),
|
||||
TheliaEvents::CURRENCY_UPDATE_POSITION => array("updatePosition", 128)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user