diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index 331265b73..1958d556b 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -25,10 +25,14 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; 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\Model\Category as CategoryModel; use Thelia\Form\CategoryCreationForm; use Thelia\Core\Event\CategoryEvent; +use Thelia\Model\CouponQuery; use Thelia\Tools\Redirect; use Thelia\Model\CategoryQuery; use Thelia\Model\AdminLog; @@ -40,29 +44,114 @@ use Propel\Runtime\Propel; use Thelia\Model\Map\CategoryTableMap; 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 + * + */ 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 + ); } } diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index a4555bfd2..ab018e245 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -29,6 +29,7 @@ use Thelia\Core\Security\Exception\AuthenticationException; use Thelia\Core\Security\Exception\AuthorizationException; use Thelia\Coupon\CouponRuleCollection; use Thelia\Form\CouponCreationForm; +use Thelia\Form\Exception\FormValidationException; use Thelia\Model\Coupon; /** @@ -79,53 +80,46 @@ class CouponController extends BaseAdminController { $this->checkAuth("ADMIN", "admin.coupon.view"); - 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"); - - $data = $form->getData(); - - $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"]); - - $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); + $this->dispatch( + TheliaEvents::CREATE_COUPON, + $couponCreateEvent + ); + $this->adminLogAppend( + sprintf( + 'Coupon %s (ID %s) created', + $couponBeingCreated->getTitle(), + $couponBeingCreated->getId() + ) + ); + // @todo redirect if successful + } catch (FormValidationException $e) { + $couponCreationForm->setErrorMessage($e->getMessage()); + $this->getParserContext()->setErrorForm($couponCreationForm); + } catch (\Exception $e) { + Tlog::getInstance()->error( + sprintf( + "Failed to create coupon: %s", + $e->getMessage() + ) + ); + $this->getParserContext()->setGeneralError($e->getMessage()); + } } 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 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; + } } diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index ba0d97d2f..8460efda9 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -179,10 +179,15 @@ final class TheliaEvents 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. */ - const BEFORE_CREATE_COUPON = "action.before_create_coupon"; /** @@ -190,6 +195,11 @@ final class TheliaEvents */ 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. */ @@ -200,6 +210,11 @@ final class TheliaEvents */ 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. */ @@ -210,6 +225,11 @@ final class TheliaEvents */ 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. */