- Update Coupon Controller/Form/Event - create()
This commit is contained in:
gmorel
2013-08-30 12:23:49 +02:00
parent df57c8fccd
commit fd63115c97
3 changed files with 189 additions and 51 deletions

View File

@@ -25,10 +25,14 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\ActionEvent; use Thelia\Core\Event\ActionEvent;
use Thelia\Core\Event\Coupon\CouponCreateEvent;
use Thelia\Core\Event\Coupon\CouponDisableEvent;
use Thelia\Core\Event\Coupon\CouponEnableEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Category as CategoryModel; use Thelia\Model\Category as CategoryModel;
use Thelia\Form\CategoryCreationForm; use Thelia\Form\CategoryCreationForm;
use Thelia\Core\Event\CategoryEvent; use Thelia\Core\Event\CategoryEvent;
use Thelia\Model\CouponQuery;
use Thelia\Tools\Redirect; use Thelia\Tools\Redirect;
use Thelia\Model\CategoryQuery; use Thelia\Model\CategoryQuery;
use Thelia\Model\AdminLog; use Thelia\Model\AdminLog;
@@ -40,29 +44,114 @@ use Propel\Runtime\Propel;
use Thelia\Model\Map\CategoryTableMap; use Thelia\Model\Map\CategoryTableMap;
use Propel\Runtime\Exception\PropelException; use Propel\Runtime\Exception\PropelException;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Process Coupon Events
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class Coupon extends BaseAction implements EventSubscriberInterface class Coupon extends BaseAction implements EventSubscriberInterface
{ {
/**
* Create a Coupon if a Coupon creation attempt is found
*
* @param CouponCreateEvent $event Coupon creation Event
*/
public function create(CouponCreateEvent $event)
{
$this->checkAuth("ADMIN", "admin.coupon.create");
$this->dispatch(
TheliaEvents::BEFORE_CREATE_COUPON,
$event
);
$event->getCreatedCoupon()->save();
$this->dispatch(
TheliaEvents::AFTER_CREATE_COUPON,
$event
);
}
/** /**
* Disable a Coupon * Edit a Coupon if a Coupon edition attempt is found
* *
* @param ActionEvent $event * @param CouponEditEvent $event Coupon edition Event
*/ */
public function delete(CategoryDeleteEvent $event) public function edit(CouponEditEvent $event)
{ {
$this->checkAuth("ADMIN", "admin.category.delete"); $this->checkAuth("ADMIN", "admin.coupon.edit");
$category = CategoryQuery::create()->findPk($event->getId()); $this->dispatch(
TheliaEvents::BEFORE_EDIT_COUPON,
$event
);
if ($category !== null) { $couponToUpdate = CouponQuery::create()->findPk($event->getId());
$event->setDeletedCategory($category); if ($couponToUpdate !== null) {
$event->getCreatedCoupon()->save();
}
$event->getDispatcher()->dispatch(TheliaEvents::BEFORE_DELETECATEGORY, $event); $this->dispatch(
TheliaEvents::AFTER_EDIT_COUPON,
$event
);
}
$category->delete(); /**
* Disable a Coupon if a Coupon disable attempt is found
*
* @param CouponDisableEvent $event Coupon disable Event
*/
public function disable(CouponDisableEvent $event)
{
$this->checkAuth("ADMIN", "admin.coupon.disable");
$event->getDispatcher()->dispatch(TheliaEvents::AFTER_DELETECATEGORY, $event); $couponToUpdate = CouponQuery::create()->findPk($event->getId());
if ($couponToUpdate !== null) {
$couponToUpdate->setIsEnabled(0);
$event->getDispatcher()->dispatch(
TheliaEvents::BEFORE_DISABLE_COUPON, $event
);
$couponToUpdate->save();
$event->getDispatcher()->dispatch(
TheliaEvents::AFTER_DISABLE_COUPON, $event
);
}
}
/**
* Enable a Coupon if a Coupon enable attempt is found
*
* @param CouponEnableEvent $event Coupon enable Event
*/
public function enable(CouponEnableEvent $event)
{
$this->checkAuth("ADMIN", "admin.coupon.enable");
$couponToUpdate = CouponQuery::create()->findPk($event->getId());
if ($couponToUpdate !== null) {
$couponToUpdate->setIsEnabled(1);
$event->getDispatcher()->dispatch(
TheliaEvents::BEFORE_ENABLE_COUPON, $event
);
$couponToUpdate->save();
$event->getDispatcher()->dispatch(
TheliaEvents::AFTER_ENABLE_COUPON, $event
);
} }
} }

View File

@@ -29,6 +29,7 @@ use Thelia\Core\Security\Exception\AuthenticationException;
use Thelia\Core\Security\Exception\AuthorizationException; use Thelia\Core\Security\Exception\AuthorizationException;
use Thelia\Coupon\CouponRuleCollection; use Thelia\Coupon\CouponRuleCollection;
use Thelia\Form\CouponCreationForm; use Thelia\Form\CouponCreationForm;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\Coupon; use Thelia\Model\Coupon;
/** /**
@@ -79,53 +80,46 @@ class CouponController extends BaseAdminController
{ {
$this->checkAuth("ADMIN", "admin.coupon.view"); $this->checkAuth("ADMIN", "admin.coupon.view");
if ($this->getRequest()->isMethod('POST')) { if ($this->getRequest()->isMethod('POST')) {
try {
$couponCreationForm = new CouponCreationForm($this->getRequest());
$couponBeingCreated = $this->buildCouponFromForm(
$this->validateForm($couponCreationForm, "POST")->getData()
);
$couponCreationForm = new CouponCreationForm($this->getRequest()); $couponCreateEvent = new CouponCreateEvent(
$couponBeingCreated
);
$form = $this->validateForm($couponCreationForm, "POST"); $this->dispatch(
TheliaEvents::CREATE_COUPON,
$data = $form->getData(); $couponCreateEvent
);
$couponBeingCreated = new Coupon(); $this->adminLogAppend(
$couponBeingCreated->setCode($data["code"]); sprintf(
$couponBeingCreated->setType($data["type"]); 'Coupon %s (ID %s) created',
$couponBeingCreated->setTitle($data["title"]); $couponBeingCreated->getTitle(),
$couponBeingCreated->setShortDescription($data["shortDescription"]); $couponBeingCreated->getId()
$couponBeingCreated->setDescription($data["description"]); )
$couponBeingCreated->setAmount($data["amount"]); );
$couponBeingCreated->setIsEnabled($data["isEnabled"]); // @todo redirect if successful
$couponBeingCreated->setExpirationDate($data["expirationDate"]); } catch (FormValidationException $e) {
$couponBeingCreated->setSerializedRules( $couponCreationForm->setErrorMessage($e->getMessage());
new CouponRuleCollection( $this->getParserContext()->setErrorForm($couponCreationForm);
array() } catch (\Exception $e) {
) Tlog::getInstance()->error(
); sprintf(
$couponBeingCreated->setIsCumulative($data["isCumulative"]); "Failed to create coupon: %s",
$couponBeingCreated->setIsRemovingPostage($data["isRemovingPostage"]); $e->getMessage()
$couponBeingCreated->setMaxUsage($data["maxUsage"]); )
$couponBeingCreated->setIsAvailableOnSpecialOffers($data["isAvailableOnSpecialOffers"]); );
$this->getParserContext()->setGeneralError($e->getMessage());
$couponCreateEvent = new CouponCreateEvent( }
$couponBeingCreated
);
$this->dispatch(TheliaEvents::BEFORE_CREATE_COUPON, $couponCreateEvent);
// @todo Save
$this->adminLogAppend(
sprintf(
'Coupon %s (ID %s) created',
$couponBeingCreated->getTitle(),
$couponBeingCreated->getId()
)
);
$this->dispatch(TheliaEvents::AFTER_CREATE_COUPON, $couponCreateEvent);
} else { } else {
} }
return $this->render('coupon/edit', $args); return $this->render('coupon/edit', array());
} }
/** /**
@@ -196,4 +190,39 @@ class CouponController extends BaseAdminController
// We did not recognized the action -> return a 404 page // We did not recognized the action -> return a 404 page
return $this->pageNotFound(); return $this->pageNotFound();
} }
/**
* Build a Coupon from its form
*
* @param array $data Form data
*
* @return Coupon
*/
protected function buildCouponFromForm(array $data)
{
$couponBeingCreated = new Coupon();
$couponBeingCreated->setCode($data["code"]);
$couponBeingCreated->setType($data["type"]);
$couponBeingCreated->setTitle($data["title"]);
$couponBeingCreated->setShortDescription($data["shortDescription"]);
$couponBeingCreated->setDescription($data["description"]);
$couponBeingCreated->setAmount($data["amount"]);
$couponBeingCreated->setIsEnabled($data["isEnabled"]);
$couponBeingCreated->setExpirationDate($data["expirationDate"]);
$couponBeingCreated->setSerializedRules(
new CouponRuleCollection(
array()
)
);
$couponBeingCreated->setIsCumulative($data["isCumulative"]);
$couponBeingCreated->setIsRemovingPostage(
$data["isRemovingPostage"]
);
$couponBeingCreated->setMaxUsage($data["maxUsage"]);
$couponBeingCreated->setIsAvailableOnSpecialOffers(
$data["isAvailableOnSpecialOffers"]
);
return $couponBeingCreated;
}
} }

View File

@@ -179,10 +179,15 @@ final class TheliaEvents
const IMAGE_CLEAR_CACHE = "action.clearImageCache"; const IMAGE_CLEAR_CACHE = "action.clearImageCache";
/**
* Sent when creating a Coupon
*/
const COUPON_CREATE = "action.create_coupon";
/** /**
* Sent just before a successful insert of a new Coupon in the database. * Sent just before a successful insert of a new Coupon in the database.
*/ */
const BEFORE_CREATE_COUPON = "action.before_create_coupon"; const BEFORE_CREATE_COUPON = "action.before_create_coupon";
/** /**
@@ -190,6 +195,11 @@ final class TheliaEvents
*/ */
const AFTER_CREATE_COUPON = "action.after_create_coupon"; const AFTER_CREATE_COUPON = "action.after_create_coupon";
/**
* Sent when editing a Coupon
*/
const COUPON_EDIT = "action.edit_coupon";
/** /**
* Sent just before a successful update of a new Coupon in the database. * Sent just before a successful update of a new Coupon in the database.
*/ */
@@ -200,6 +210,11 @@ final class TheliaEvents
*/ */
const AFTER_EDIT_COUPON = "action.after_edit_coupon"; const AFTER_EDIT_COUPON = "action.after_edit_coupon";
/**
* Sent when disabling a Coupon
*/
const COUPON_DISABLE = "action.disable_coupon";
/** /**
* Sent just before a successful disable of a new Coupon in the database. * Sent just before a successful disable of a new Coupon in the database.
*/ */
@@ -210,6 +225,11 @@ final class TheliaEvents
*/ */
const AFTER_DISABLE_COUPON = "action.after_disable_coupon"; const AFTER_DISABLE_COUPON = "action.after_disable_coupon";
/**
* Sent when enabling a Coupon
*/
const COUPON_ENABLE = "action.enable_coupon";
/** /**
* Sent just before a successful enable of a new Coupon in the database. * Sent just before a successful enable of a new Coupon in the database.
*/ */