Before merging coupon

This commit is contained in:
franck
2013-09-05 10:00:17 +02:00
parent 58a68b1959
commit 271e6c64f0
8 changed files with 64 additions and 45 deletions

View File

@@ -107,6 +107,7 @@ class Currency extends BaseAction implements EventSubscriberInterface
CurrencyQuery::create()->filterByByDefault(true)->update(array('ByDefault' => false)); CurrencyQuery::create()->filterByByDefault(true)->update(array('ByDefault' => false));
$currency $currency
->setDispatcher($this->getDispatcher())
->setByDefault($event->getIsDefault()) ->setByDefault($event->getIsDefault())
->save() ->save()
; ;
@@ -139,7 +140,7 @@ class Currency extends BaseAction implements EventSubscriberInterface
$rates_url = ConfigQuery::read('currency_rate_update_url', 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml'); $rates_url = ConfigQuery::read('currency_rate_update_url', 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
$rate_data = file_get_contents($rates_url); $rate_data = @file_get_contents($rates_url);
if ($rate_data && $sxe = new \SimpleXMLElement($rate_data)) { if ($rate_data && $sxe = new \SimpleXMLElement($rate_data)) {
@@ -149,12 +150,16 @@ class Currency extends BaseAction implements EventSubscriberInterface
$rate = floatval($last['rate']); $rate = floatval($last['rate']);
if (null !== $currency = CurrencyQuery::create()->findOneByCode($code)) { if (null !== $currency = CurrencyQuery::create()->findOneByCode($code)) {
$currency->setRate($rate)->save(); $currency
->setDispatcher($this->getDispatcher())
->setRate($rate)
->save()
;
} }
} }
} }
else { else {
throw new \RuntimeException(sprintf("Failed to get currency rates data from URL %s", $url)); throw new \RuntimeException(sprintf("Failed to get currency rates data from URL %s", $rates_url));
} }
} }
@@ -165,12 +170,18 @@ class Currency extends BaseAction implements EventSubscriberInterface
*/ */
public function updatePosition(CurrencyUpdatePositionEvent $event) public function updatePosition(CurrencyUpdatePositionEvent $event)
{ {
if (null !== $category = CurrencyQuery::create()->findOneById($event->getObjectId())) { if (null !== $currency = CurrencyQuery::create()->findOneById($event->getObjectId())) {
if ($event->getMode() == BaseChangePositionEvent::POSITION_ABSOLUTE) $currency->setDispatcher($this->getDispatcher());
return $category->changeAbsolutePosition($event->getPosition());
else $mode = $event->getMode();
return $this->exchangePosition($event->getMode());
if ($mode == CurrencyUpdatePositionEvent::POSITION_ABSOLUTE)
return $currency->changeAbsolutePosition($event->getPosition());
else if ($mode == CurrencyUpdatePositionEvent::POSITION_UP)
return $currency->movePositionUp();
else if ($mode == CurrencyUpdatePositionEvent::POSITION_DOWN)
return $currency->movePositionDown();
} }
} }

View File

@@ -316,17 +316,22 @@ class CurrencyController extends BaseAdminController
if (null !== $response = $this->checkAuth("admin.configuration.currencies.update")) return $response; if (null !== $response = $this->checkAuth("admin.configuration.currencies.update")) return $response;
try { try {
$id = $this->getRequest()->get('currency_id', 0);
$mode = $this->getRequest()->get('mode', null); $mode = $this->getRequest()->get('mode', null);
if ($mode == 'up')
$mode = CurrencyUpdatePositionEvent::POSITION_UP;
else if ($mode == 'down')
$mode = CurrencyUpdatePositionEvent::POSITION_DOWN;
else
$mode = CurrencyUpdatePositionEvent::POSITION_ABSOLUTE;
$position = $this->getRequest()->get('position', null); $position = $this->getRequest()->get('position', null);
$event = new CurrencyUpdatePositionEvent(); $event = new CurrencyUpdatePositionEvent(
$this->getRequest()->get('currency_id', null),
$event $mode,
->setObjectId($this->getRequest()->get('currency_id', 0)) $this->getRequest()->get('position', null)
->setPosition($this->getRequest()->get('position', 0)) );
->setMode($mode)
;
$this->dispatch(TheliaEvents::CURRENCY_UPDATE_POSITION, $event); $this->dispatch(TheliaEvents::CURRENCY_UPDATE_POSITION, $event);
} }

View File

@@ -74,14 +74,4 @@ class BaseUpdatePositionEvent extends ActionEvent
$this->object_id = $object_id; $this->object_id = $object_id;
return $this; return $this;
} }
public function getObjectId()
{
return $this->object_id;
}
public function setObjectId($object_id)
{
$this->object_id = $object_id;
}
} }

View File

@@ -23,6 +23,6 @@
namespace Thelia\Core\Event; namespace Thelia\Core\Event;
class CategoryChangePositionEvent extends BaseChangePositionEvent class CurrencyUpdatePositionEvent extends BaseUpdatePositionEvent
{ {
} }

View File

@@ -71,11 +71,11 @@ class AdminUtilities extends AbstractSmartyPlugin
if ($permissions == null || $this->securityContext->isGranted("ADMIN", array($permission))) { if ($permissions == null || $this->securityContext->isGranted("ADMIN", array($permission))) {
return sprintf( return sprintf(
'<a href="%s"><i class="glyphicon glyphicon-arrow-up"></i></a><span class="%s" data-id="%s">%s</span><a href="%s"><i class="glyphicon glyphicon-arrow-down"></i></a>', '<a href="%s"><i class="glyphicon glyphicon-arrow-up"></i></a><span class="%s" data-id="%s">%s</span><a href="%s"><i class="glyphicon glyphicon-arrow-down"></i></a>',
URL::getInstance()->absoluteUrl("$path/positionUp", array($url_parameter => $id)), URL::getInstance()->absoluteUrl($path, array('mode' => 'up', $url_parameter => $id)),
$in_place_edit_class, $in_place_edit_class,
$id, $id,
$position, $position,
URL::getInstance()->absoluteUrl("$path/positionDown", array($url_parameter => $id)) URL::getInstance()->absoluteUrl($path, array('mode' => 'down', $url_parameter => $id))
); );
} }
else { else {

View File

@@ -44,6 +44,11 @@ trait ModelEventDispatcherTrait {
return $this; return $this;
} }
public function getDispatcher()
{
return $this->dispatcher;
}
protected function dispatchEvent($eventName, ActionEvent $event) protected function dispatchEvent($eventName, ActionEvent $event)
{ {
if (!is_null($this->dispatcher)) { if (!is_null($this->dispatcher)) {

View File

@@ -25,6 +25,7 @@ namespace Thelia\Model\Tools;
use Propel\Runtime\ActiveQuery\PropelQuery; use Propel\Runtime\ActiveQuery\PropelQuery;
use Propel\Runtime\ActiveQuery\Criteria; use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Propel;
trait PositionManagementTrait { trait PositionManagementTrait {
@@ -51,10 +52,12 @@ trait PositionManagementTrait {
public function getNextPosition($parent) { public function getNextPosition($parent) {
$last = $this->createQuery() $last = $this->createQuery()
->filterByParent($parent)
->orderByPosition(Criteria::DESC) ->orderByPosition(Criteria::DESC)
->limit(1) ->limit(1);
->findOne()
if ($parent !== null) $last->filterByParent($parent);
$last->findOne()
; ;
return $last != null ? $last->getPosition() + 1 : 1; return $last != null ? $last->getPosition() + 1 : 1;
@@ -63,14 +66,14 @@ trait PositionManagementTrait {
/** /**
* Move up a object * Move up a object
*/ */
protected function movePositionUp() { public function movePositionUp() {
$this->movePositionUpOrDown(true); $this->movePositionUpOrDown(true);
} }
/** /**
* Move down a object * Move down a object
*/ */
protected function movePositionDown() { public function movePositionDown() {
$this->movePositionUpOrDown(false); $this->movePositionUpOrDown(false);
} }
@@ -85,8 +88,9 @@ trait PositionManagementTrait {
$my_position = $this->getPosition(); $my_position = $this->getPosition();
// Find object to exchange position with // Find object to exchange position with
$search = $this->createQuery() $search = $this->createQuery();
->filterByParent($this->getParent());
if (method_exists($this, 'getParent')) $search->filterByParent($this->getParent());
// Up or down ? // Up or down ?
if ($up === true) { if ($up === true) {
@@ -103,18 +107,21 @@ trait PositionManagementTrait {
// If we found the proper object, exchange their positions // If we found the proper object, exchange their positions
if ($result) { if ($result) {
$cnx = Propel::getWriteConnection(CategoryTableMap::DATABASE_NAME); // Find DATABASE_NAME constant
$mapClassName = self::TABLE_MAP;
$database_name = $mapClassName::DATABASE_NAME;
$cnx = Propel::getWriteConnection($database_name);
$cnx->beginTransaction(); $cnx->beginTransaction();
try { try {
$this $this
->setDispatcher($this->getDispatcher())
->setPosition($result->getPosition()) ->setPosition($result->getPosition())
->save() ->save()
; ;
$result->setPosition($my_position)->save(); $result->setDispatcher($this->getDispatcher())->setPosition($my_position)->save();
$cnx->commit(); $cnx->commit();
} catch (Exception $e) { } catch (Exception $e) {
@@ -128,7 +135,7 @@ trait PositionManagementTrait {
* *
* @param newPosition * @param newPosition
*/ */
protected function changeAbsolutePosition($newPosition) public function changeAbsolutePosition($newPosition)
{ {
// The current position // The current position
$current_position = $this->getPosition(); $current_position = $this->getPosition();
@@ -136,7 +143,9 @@ trait PositionManagementTrait {
if ($newPosition != null && $newPosition > 0 && $newPosition != $current_position) { if ($newPosition != null && $newPosition > 0 && $newPosition != $current_position) {
// Find categories to offset // Find categories to offset
$search = $this->createQuery()->filterByParent($this->getParent()); $search = $this->createQuery();
if (method_exists($this, 'getParent')) $search->filterByParent($this->getParent());
if ($newPosition > $current_position) { if ($newPosition > $current_position) {
// The new position is after the current position -> we will offset + 1 all categories located between us and the new position // The new position is after the current position -> we will offset + 1 all categories located between us and the new position
@@ -158,11 +167,10 @@ trait PositionManagementTrait {
try { try {
foreach ($results as $result) { foreach ($results as $result) {
$result->setPosition($result->getPosition() + $delta)->save($cnx); $result->setDispatcher($this->getDispatcher())->setPosition($result->getPosition() + $delta)->save($cnx);
} }
$this $this
->setDispatcher($this->getDispatcher())
->setPosition($newPosition) ->setPosition($newPosition)
->save($cnx) ->save($cnx)
; ;

View File

@@ -138,7 +138,7 @@
<td class="text-center"> <td class="text-center">
{admin_position_block {admin_position_block
permission="admin.currencies.edit" permission="admin.currencies.edit"
path="/admin/configuration/currencies" path="/admin/configuration/currencies/update-position"
url_parameter="currency_id" url_parameter="currency_id"
in_place_edit_class="currencyPositionChange" in_place_edit_class="currencyPositionChange"
position="$POSITION" position="$POSITION"
@@ -371,7 +371,7 @@
placement : 'left', placement : 'left',
success : function(response, newValue) { success : function(response, newValue) {
// The URL template // The URL template
var url = "{url path='/admin/configuration/currencies/updatePosition' currency_id='__ID__' position='__POS__'}"; var url = "{url path='/admin/configuration/currencies/update-position' currency_id='__ID__' position='__POS__'}";
// Perform subtitutions // Perform subtitutions
url = url.replace('__ID__', $(this).data('id')) url = url.replace('__ID__', $(this).data('id'))