From 71fa04a1936d180a76a3e4a8edc026ad64f1b03e Mon Sep 17 00:00:00 2001 From: gmorel Date: Fri, 30 Aug 2013 11:06:33 +0200 Subject: [PATCH] WIP - Update Coupon Controller/Form/Event - create() - Rule serialization/unserialization managed in Model/Coupon now --- .../Controller/Admin/CouponController.php | 52 +++++- .../Core/Event/Coupon/CouponCreateEvent.php | 83 +++++---- .../Core/Event/Coupon/CouponEditEvent.php | 82 +++++++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 52 ++++++ core/lib/Thelia/Form/CouponCreationForm.php | 168 ++++++++++++++++++ core/lib/Thelia/Model/Coupon.php | 70 +++++++- install/sqldb.map | 2 - 7 files changed, 463 insertions(+), 46 deletions(-) create mode 100644 core/lib/Thelia/Core/Event/Coupon/CouponEditEvent.php create mode 100755 core/lib/Thelia/Form/CouponCreationForm.php delete mode 100644 install/sqldb.map diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index b1cf197c8..a4555bfd2 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -23,8 +23,13 @@ namespace Thelia\Controller\Admin; +use Thelia\Core\Event\Coupon\CouponCreateEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\Exception\AuthenticationException; use Thelia\Core\Security\Exception\AuthorizationException; +use Thelia\Coupon\CouponRuleCollection; +use Thelia\Form\CouponCreationForm; +use Thelia\Model\Coupon; /** * Created by JetBrains PhpStorm. @@ -74,6 +79,52 @@ class CouponController extends BaseAdminController { $this->checkAuth("ADMIN", "admin.coupon.view"); + + if ($this->getRequest()->isMethod('POST')) { + + $couponCreationForm = new CouponCreationForm($this->getRequest()); + + $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); + } else { + + } + return $this->render('coupon/edit', $args); } @@ -114,7 +165,6 @@ class CouponController extends BaseAdminController */ public function processAction() { - var_dump($this->getRequest()->attributes); // Get the current action $action = $this->getRequest()->get('action', 'browse'); diff --git a/core/lib/Thelia/Core/Event/Coupon/CouponCreateEvent.php b/core/lib/Thelia/Core/Event/Coupon/CouponCreateEvent.php index c03e21f17..9f0db34df 100644 --- a/core/lib/Thelia/Core/Event/Coupon/CouponCreateEvent.php +++ b/core/lib/Thelia/Core/Event/Coupon/CouponCreateEvent.php @@ -22,61 +22,60 @@ /**********************************************************************************/ namespace Thelia\Core\Event\Coupon; - +use Thelia\Core\Event\ActionEvent; use Thelia\Model\Coupon; +/** + * Created by JetBrains PhpStorm. + * Date: 8/29/13 + * Time: 3:45 PM + * + * Occurring when a Coupon is created + * + * @package Coupon + * @author Guillaume MOREL + * + */ class CouponCreateEvent extends ActionEvent { - protected $title; - protected $parent; - protected $locale; - protected $created_category; + /** + * @var Coupon Coupon being created + */ + protected $createdCoupon; - public function __construct($title, $parent, $locale) + /** + * Constructor + * + * @param Coupon $coupon Coupon being created + */ + public function __construct(Coupon $coupon) { - $this->title = $title; - $this->parent = $parent; - $this->locale = $locale; + $this->createdCoupon = $coupon; } - public function getTitle() + /** + * Modify Coupon being created + * + * @param Coupon $createdCoupon Coupon being created + * + * @return $this + */ + public function setCreatedCoupon(Coupon $createdCoupon) { - return $this->title; + $this->createdCoupon = $createdCoupon; + + return $this; } - public function setTitle($title) + /** + * Get Coupon being created + * + * @return Coupon + */ + public function getCreatedCoupon() { - $this->title = $title; + return clone $this->createdCoupon; } - public function getParent() - { - return $this->parent; - } - public function setParent($parent) - { - $this->parent = $parent; - } - - public function getLocale() - { - return $this->locale; - } - - public function setLocale($locale) - { - $this->locale = $locale; - } - - public function getCreatedCategory() - { - return $this->created_category; - } - - public function setCreatedCategory(Category $created_category) - { - $this->created_category = $created_category; -var_dump($this->created_category); - } } diff --git a/core/lib/Thelia/Core/Event/Coupon/CouponEditEvent.php b/core/lib/Thelia/Core/Event/Coupon/CouponEditEvent.php new file mode 100644 index 000000000..bdde56c40 --- /dev/null +++ b/core/lib/Thelia/Core/Event/Coupon/CouponEditEvent.php @@ -0,0 +1,82 @@ +. */ +/* */ +/**********************************************************************************/ + +namespace Thelia\Core\Event\Coupon; +use Thelia\Core\Event\ActionEvent; +use Thelia\Model\Coupon; + +/** + * Created by JetBrains PhpStorm. + * Date: 8/29/13 + * Time: 3:45 PM + * + * Occurring when a Coupon is edited + * + * @package Coupon + * @author Guillaume MOREL + * + */ +class CouponEditEvent extends ActionEvent +{ + /** @var int Coupon being edited id */ + protected $couponId; + + /** @var Coupon Coupon being created */ + protected $editedCoupon; + + /** + * Constructor + * + * @param Coupon $coupon Coupon being edited + */ + public function __construct(Coupon $coupon) + { + $this->created_coupon = $coupon; + } + + /** + * Modify Coupon being created + * + * @param Coupon $editedCoupon Coupon being created + * + * @return $this + */ + public function setCreatedCoupon(Coupon $editedCoupon) + { + $this->editedCoupon = $editedCoupon; + + return $this; + } + + /** + * Get Coupon being created + * + * @return Coupon + */ + public function getCreatedCoupon() + { + return clone $this->editedCoupon; + } + + +} diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 2dd85905d..ba0d97d2f 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -177,4 +177,56 @@ final class TheliaEvents * Sent on cimage cache clear request */ const IMAGE_CLEAR_CACHE = "action.clearImageCache"; + + + /** + * Sent just before a successful insert of a new Coupon in the database. + */ + + const BEFORE_CREATE_COUPON = "action.before_create_coupon"; + + /** + * Sent just after a successful insert of a new Coupon in the database. + */ + const AFTER_CREATE_COUPON = "action.after_create_coupon"; + + /** + * Sent just before a successful update of a new Coupon in the database. + */ + const BEFORE_EDIT_COUPON = "action.before_edit_coupon"; + + /** + * Sent just after a successful update of a new Coupon in the database. + */ + const AFTER_EDIT_COUPON = "action.after_edit_coupon"; + + /** + * Sent just before a successful disable of a new Coupon in the database. + */ + const BEFORE_DISABLE_COUPON = "action.before_disable_coupon"; + + /** + * Sent just after a successful disable of a new Coupon in the database. + */ + const AFTER_DISABLE_COUPON = "action.after_disable_coupon"; + + /** + * Sent just before a successful enable of a new Coupon in the database. + */ + const BEFORE_ENABLE_COUPON = "action.before_enable_coupon"; + + /** + * Sent just after a successful enable of a new Coupon in the database. + */ + const AFTER_ENABLE_COUPON = "action.after_enable_coupon"; + + /** + * Sent just before an attempt to use a Coupon + */ + const BEFORE_USE_COUPON = "action.before_use_coupon"; + + /** + * Sent just after an attempt to use a Coupon + */ + const AFTER_USE_COUPON = "action.after_use_coupon"; } diff --git a/core/lib/Thelia/Form/CouponCreationForm.php b/core/lib/Thelia/Form/CouponCreationForm.php new file mode 100755 index 000000000..39667cfdb --- /dev/null +++ b/core/lib/Thelia/Form/CouponCreationForm.php @@ -0,0 +1,168 @@ +. */ +/* */ +/**********************************************************************************/ + +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints\NotBlank; + +/** + * Created by JetBrains PhpStorm. + * Date: 8/29/13 + * Time: 3:45 PM + * + * Allow to build a form Coupon + * + * @package Coupon + * @author Guillaume MOREL + * + */ +class CouponCreationForm extends BaseForm +{ + /** + * Build Coupon form + * + * @return void + */ + protected function buildForm() + { + $this->formBuilder + ->add( + "code", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "type", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "title", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "shortDescription", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "description", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "amount", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "isEnabled", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "expirationDate", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "isCumulative", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "isRemovingPostage", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "maxUsage", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ) + ->add( + "isAvailableOnSpecialOffers", + "text", + array( + "constraints" => array( + new NotBlank() + ) + ) + ); + } + + /** + * Get form name + * + * @return string + */ + public function getName() + { + return "thelia_coupon_creation"; + } +} diff --git a/core/lib/Thelia/Model/Coupon.php b/core/lib/Thelia/Model/Coupon.php index d135ccbb0..9718b5e36 100755 --- a/core/lib/Thelia/Model/Coupon.php +++ b/core/lib/Thelia/Model/Coupon.php @@ -1,9 +1,77 @@ . */ +/* */ +/**********************************************************************************/ namespace Thelia\Model; +use Thelia\Coupon\CouponRuleCollection; use Thelia\Model\Base\Coupon as BaseCoupon; -class Coupon extends BaseCoupon { +/** + * Created by JetBrains PhpStorm. + * Date: 8/19/13 + * Time: 3:24 PM + * + * Used to provide an effect (mostly a discount) + * at the end of the Customer checkout tunnel + * It will be usable for a Customer only if it matches the Coupon criteria (Rules) + * + * @package Coupon + * @author Guillaume MOREL + * + */ +class Coupon extends BaseCoupon +{ + /** + * Set the value of [serialized_rules] column. + * + * @param CouponRuleCollection $rules A set of Rules + * + * @return \Thelia\Model\Coupon The current object (for fluent API support) + */ + public function setSerializedRules(CouponRuleCollection $rules) + { + if ($rules !== null) { + $v = (string) base64_encode(serialize($rules)); + } + + if ($this->serialized_rules !== $v) { + $this->serialized_rules = $v; + $this->modifiedColumns[] = CouponTableMap::SERIALIZED_RULES; + } + + + return $this; + } // setSerializedRules() + + + /** + * Get the [serialized_rules] column value. + * + * @return CouponRuleCollection Rules ready to be processed + */ + public function getSerializedRules() + { + return unserialize(base64_decode($this->serialized_rules)); + } } diff --git a/install/sqldb.map b/install/sqldb.map deleted file mode 100644 index 63a93baa8..000000000 --- a/install/sqldb.map +++ /dev/null @@ -1,2 +0,0 @@ -# Sqlfile -> Database map -thelia.sql=thelia