Merge pull request #157 from gmorel/coupon

Coupon : Fix bug preventing to update Coupon
This commit is contained in:
Manuel Raynaud
2014-01-02 00:19:48 -08:00

View File

@@ -35,6 +35,7 @@ use Thelia\Core\Security\AccessManager;
use Thelia\Coupon\CouponFactory; use Thelia\Coupon\CouponFactory;
use Thelia\Coupon\CouponManager; use Thelia\Coupon\CouponManager;
use Thelia\Condition\ConditionCollection; use Thelia\Condition\ConditionCollection;
use Thelia\Coupon\Type\CouponAbstract;
use Thelia\Coupon\Type\CouponInterface; use Thelia\Coupon\Type\CouponInterface;
use Thelia\Coupon\Type\RemoveXPercent; use Thelia\Coupon\Type\RemoveXPercent;
use Thelia\Form\CouponCreationForm; use Thelia\Form\CouponCreationForm;
@@ -205,7 +206,8 @@ class CouponController extends BaseAdminController
$lang, $lang,
$eventToDispatch, $eventToDispatch,
'updated', 'updated',
'update' 'update',
$coupon
); );
} else { } else {
// Display // Display
@@ -348,7 +350,7 @@ class CouponController extends BaseAdminController
$coupon->getCode(), $coupon->getCode(),
$coupon->getType(), $coupon->getType(),
$coupon->getTitle(), $coupon->getTitle(),
array('quantity' => $coupon->getAmount()), $coupon->getEffects(),
$coupon->getShortDescription(), $coupon->getShortDescription(),
$coupon->getDescription(), $coupon->getDescription(),
$coupon->getIsEnabled(), $coupon->getIsEnabled(),
@@ -456,10 +458,11 @@ class CouponController extends BaseAdminController
* @param string $eventToDispatch Event which will activate actions * @param string $eventToDispatch Event which will activate actions
* @param string $log created|edited * @param string $log created|edited
* @param string $action creation|edition * @param string $action creation|edition
* @param Coupon $model Model if in update mode
* *
* @return $this * @return $this
*/ */
protected function validateCreateOrUpdateForm(I18n $i18n, Lang $lang, $eventToDispatch, $log, $action) protected function validateCreateOrUpdateForm(I18n $i18n, Lang $lang, $eventToDispatch, $log, $action, Coupon $model = null)
{ {
// Create the form from the request // Create the form from the request
$creationForm = new CouponCreationForm($this->getRequest()); $creationForm = new CouponCreationForm($this->getRequest());
@@ -469,7 +472,7 @@ class CouponController extends BaseAdminController
// Check the form against conditions violations // Check the form against conditions violations
$form = $this->validateForm($creationForm, 'POST'); $form = $this->validateForm($creationForm, 'POST');
$couponEvent = $this->feedCouponCreateOrUpdateEvent($form); $couponEvent = $this->feedCouponCreateOrUpdateEvent($form, $model);
// Dispatch Event to the Action // Dispatch Event to the Action
$this->dispatch( $this->dispatch(
@@ -635,29 +638,40 @@ class CouponController extends BaseAdminController
/** /**
* Feed the Coupon Create or Update event with the User inputs * Feed the Coupon Create or Update event with the User inputs
* *
* @param Form $form * @param Form $form Form containing user data
* @param Coupon $model Model if in update mode
* *
* @return CouponCreateOrUpdateEvent * @return CouponCreateOrUpdateEvent
*/ */
protected function feedCouponCreateOrUpdateEvent(Form $form) protected function feedCouponCreateOrUpdateEvent(Form $form, Coupon $model = null)
{ {
// Get the form field values // Get the form field values
$data = $form->getData(); $data = $form->getData();
$serviceId = $data['type']; $serviceId = $data['type'];
/** @var CouponInterface $couponManager */ /** @var CouponInterface $couponManager */
$couponManager = $this->container->get($serviceId); $couponManager = $this->container->get($serviceId);
$effects = array('amount' => $data['amount']); $effects = array(CouponAbstract::INPUT_AMOUNT_NAME => $data[CouponAbstract::INPUT_AMOUNT_NAME]);
$effects = $this->addExtendedLogic($effects, $couponManager->getExtendedInputs()); $effects = $this->addExtendedLogic($effects, $couponManager->getExtendedInputs());
$couponEvent = new CouponCreateOrUpdateEvent( $couponEvent = new CouponCreateOrUpdateEvent(
$data['code'], $serviceId, $data['title'], $effects, $data['shortDescription'], $data['description'], $data['isEnabled'], \DateTime::createFromFormat('Y-m-d', $data['expirationDate']), $data['isAvailableOnSpecialOffers'], $data['isCumulative'], $data['isRemovingPostage'], $data['maxUsage'], $data['locale'] $data['code'],
$serviceId,
$data['title'],
$effects,
$data['shortDescription'],
$data['description'],
$data['isEnabled'],
\DateTime::createFromFormat('Y-m-d', $data['expirationDate']),
$data['isAvailableOnSpecialOffers'],
$data['isCumulative'],
$data['isRemovingPostage'],
$data['maxUsage'],
$data['locale']
); );
// If Update mode // If Update mode
$couponQuery = new CouponQuery(); if (isset($model)) {
$coupon = $couponQuery->findOneByCode($data['code']); $couponEvent->setCouponModel($model);
if (isset($coupon)) {
$couponEvent->setCouponModel($coupon);
} }
return $couponEvent; return $couponEvent;