diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index 08db66dda..57e019888 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -92,7 +92,7 @@ class Coupon extends BaseAction implements EventSubscriberInterface $event->isCumulative(), $event->getMaxUsage(), $event->getRules(), - $event->getLang() + $event->getLocale() ); $event->setCoupon($coupon); diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index 7eea2e005..66c189ec6 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -42,6 +42,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 0058ee5f8..e32865f15 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -52,8 +52,8 @@ Thelia\Controller\Admin\CouponController::createAction - - Thelia\Controller\Admin\CouponController::editAction + + Thelia\Controller\Admin\CouponController::updateAction Thelia\Controller\Admin\CouponController::readAction diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index 7fd9783ba..1e093f89f 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -68,8 +68,6 @@ class CouponController extends BaseAdminController /** * Manage Coupons creation display * - * @param array $args GET arguments - * * @return \Symfony\Component\HttpFoundation\Response */ public function createAction() @@ -80,96 +78,106 @@ class CouponController extends BaseAdminController return $response; } - $message = false; + // Parameters given to the template + $args = array(); - // Create the form from the request - $creationForm = new CouponCreationForm($this->getRequest()); + $i18n = new I18n(); + /** @var Lang $lang */ + $lang = $this->getSession()->get('lang'); + $eventToDispatch = TheliaEvents::COUPON_CREATE; if ($this->getRequest()->isMethod('POST')) { - try { - // Check the form against constraints violations - $form = $this->validateForm($creationForm, 'POST'); - $i18n = new I18n(); - /** @var Lang $lang */ - $lang = $this->getSession()->get('lang'); - - // Get the form field values - $data = $form->getData(); - $couponEvent = new CouponCreateOrUpdateEvent( - $data['code'], - $data['title'], - $data['amount'], - $data['effect'], - $data['shortDescription'], - $data['description'], - $data['isEnabled'], - $i18n->getDateTimeFromForm($lang, $data['expirationDate']), - $data['isAvailableOnSpecialOffers'], - $data['isCumulative'], - $data['isRemovingPostage'], - $data['maxUsage'], - array(), - $data['locale'] - ); - - $this->dispatch( - TheliaEvents::COUPON_CREATE, - $couponEvent - ); - $this->adminLogAppend( - sprintf( - 'Coupon %s (ID ) created', - $couponEvent->getTitle() -// $couponEvent->getCoupon()->getId() - ) - ); - // @todo redirect if successful - } catch (FormValidationException $e) { - // Invalid data entered - $message = 'Please check your input:'; - $this->logError('creation', $message, $e); - - } catch (\Exception $e) { - // Any other error - $message = 'Sorry, an error occurred:'; - $this->logError('creation', $message, $e); - } - - if ($message !== false) { - // Mark the form as with error - $creationForm->setErrorMessage($message); - - // Send the form and the error to the parser - $this->getParserContext() - ->addForm($creationForm) - ->setGeneralError($message); - } + $this->validateCreateOrUpdateForm( + $i18n, + $lang, + $eventToDispatch, + 'created', + 'creation' + ); + } else { + // If no input for expirationDate, now + 2 months + $defaultDate = new \DateTime(); + $args['defaultDate'] = $defaultDate->modify('+2 month') + ->format($lang->getDateFormat()); } - $formAction = 'admin/coupon/create'; + $args['formAction'] = 'admin/coupon/create'; return $this->render( 'coupon-create', - array( - 'formAction' => $formAction - ) + $args ); } /** * Manage Coupons edition display * - * @param array $args GET arguments + * @param int $couponId Coupon id * * @return \Symfony\Component\HttpFoundation\Response */ - public function editAction($couponId) + public function updateAction($couponId) { - $this->checkAuth('ADMIN', 'admin.coupon.edit'); + // Check current user authorization + $response = $this->checkAuth('admin.coupon.update'); + if ($response !== null) { + return $response; + } - $formAction = 'admin/coupon/edit/' . $couponId; + /** @var Coupon $coupon */ + $coupon = CouponQuery::create()->findOneById($couponId); + if (!$coupon) { + $this->pageNotFound(); + } - return $this->render('coupon-edit', array('formAction' => $formAction)); + // Parameters given to the template + $args = array(); + + $i18n = new I18n(); + /** @var Lang $lang */ + $lang = $this->getSession()->get('lang'); + $eventToDispatch = TheliaEvents::COUPON_UPDATE; + + if ($this->getRequest()->isMethod('POST')) { + $this->validateCreateOrUpdateForm( + $i18n, + $lang, + $eventToDispatch, + 'updated', + 'update' + ); + } else { + // Prepare the data that will hydrate the form + $data = array( + 'code' => $coupon->getCode(), + 'title' => $coupon->getTitle(), + 'amount' => $coupon->getAmount(), + 'effect' => $coupon->getType(), + 'shortDescription' => $coupon->getShortDescription(), + 'description' => $coupon->getDescription(), + 'isEnabled' => ($coupon->getIsEnabled() == 1), + 'expirationDate' => $coupon->getExpirationDate($lang->getDateFormat()), + 'isAvailableOnSpecialOffers' => ($coupon->getIsAvailableOnSpecialOffers() == 1), + 'isCumulative' => ($coupon->getIsCumulative() == 1), + 'isRemovingPostage' => ($coupon->getIsRemovingPostage() == 1), + 'maxUsage' => $coupon->getMaxUsage(), + 'rules' => new CouponRuleCollection(array()), + 'locale' => $coupon->getLocale(), + ); + + // Setup the object form + $changeForm = new CouponCreationForm($this->getRequest(), 'form', $data); + + // Pass it to the parser + $this->getParserContext()->addForm($changeForm); + } + + $args['formAction'] = 'admin/coupon/update/' . $couponId; + + return $this->render( + 'coupon-update', + $args + ); } /** @@ -251,4 +259,90 @@ class CouponController extends BaseAdminController return $this; } + /** + * Validate the CreateOrUpdate form + * + * @param string $i18n Local code (fr_FR) + * @param Lang $lang Local variables container + * @param string $eventToDispatch Event which will activate actions + * @param string $log created|edited + * @param string $action creation|edition + * + * @return $this + */ + protected function validateCreateOrUpdateForm($i18n, $lang, $eventToDispatch, $log, $action) + { + // Create the form from the request + $creationForm = new CouponCreationForm($this->getRequest()); + + $message = false; + try { + // Check the form against constraints violations + $form = $this->validateForm($creationForm, 'POST'); + + // Get the form field values + $data = $form->getData(); + $couponEvent = new CouponCreateOrUpdateEvent( + $data['code'], + $data['title'], + $data['amount'], + $data['effect'], + $data['shortDescription'], + $data['description'], + $data['isEnabled'], + $i18n->getDateTimeFromForm($lang, $data['expirationDate']), + $data['isAvailableOnSpecialOffers'], + $data['isCumulative'], + $data['isRemovingPostage'], + $data['maxUsage'], + new CouponRuleCollection(array()), + $data['locale'] + ); + + // Dispatch Event to the Action + $this->dispatch( + $eventToDispatch, + $couponEvent + ); + + $this->adminLogAppend( + sprintf( + 'Coupon %s (ID ) ' . $log, + $couponEvent->getTitle(), + $couponEvent->getCoupon()->getId() + ) + ); + + $this->redirect( + str_replace( + '{id}', + $couponEvent->getCoupon()->getId(), + $creationForm->getSuccessUrl() + ) + ); + + } catch (FormValidationException $e) { + // Invalid data entered + $message = 'Please check your input:'; + $this->logError($action, $message, $e); + + } catch (\Exception $e) { + // Any other error + $message = 'Sorry, an error occurred:'; + $this->logError($action, $message, $e); + } + + if ($message !== false) { + // Mark the form as with error + $creationForm->setErrorMessage($message); + + // Send the form and the error to the parser + $this->getParserContext() + ->addForm($creationForm) + ->setGeneralError($message); + } + + return $this; + } + } diff --git a/core/lib/Thelia/Core/Event/Coupon/CouponCreateOrUpdateEvent.php b/core/lib/Thelia/Core/Event/Coupon/CouponCreateOrUpdateEvent.php index 90c5559d4..796baed08 100644 --- a/core/lib/Thelia/Core/Event/Coupon/CouponCreateOrUpdateEvent.php +++ b/core/lib/Thelia/Core/Event/Coupon/CouponCreateOrUpdateEvent.php @@ -22,7 +22,6 @@ /**********************************************************************************/ namespace Thelia\Core\Event\Coupon; -use Symfony\Component\EventDispatcher\Event; use Thelia\Core\Event\ActionEvent; use Thelia\Coupon\CouponRuleCollection; use Thelia\Model\Coupon; @@ -83,7 +82,7 @@ class CouponCreateOrUpdateEvent extends ActionEvent protected $effect; /** @var string Language code ISO (ex: fr_FR) */ - protected $lang = null; + protected $locale = null; /** * Constructor @@ -101,7 +100,7 @@ class CouponCreateOrUpdateEvent extends ActionEvent * @param boolean $isRemovingPostage Is removing Postage * @param int $maxUsage Coupon quantity * @param CouponRuleCollection $rules CouponRuleInterface to add - * @param string $lang Coupon Language code ISO (ex: fr_FR) + * @param string $locale Coupon Language code ISO (ex: fr_FR) */ function __construct( $code, @@ -117,7 +116,7 @@ class CouponCreateOrUpdateEvent extends ActionEvent $isRemovingPostage, $maxUsage, $rules, - $lang + $locale ) { $this->amount = $amount; $this->code = $code; @@ -132,7 +131,7 @@ class CouponCreateOrUpdateEvent extends ActionEvent $this->shortDescription = $shortDescription; $this->title = $title; $this->effect = $effect; - $this->lang = $lang; + $this->locale = $locale; } /** @@ -214,7 +213,13 @@ class CouponCreateOrUpdateEvent extends ActionEvent */ public function getRules() { - return clone $this->rules; + if ($this->rules === null || !is_object($this->rules)) { + $rules = $this->rules; + } else { + $rules = clone $this->rules; + } + + return $rules; } /** @@ -273,20 +278,28 @@ class CouponCreateOrUpdateEvent extends ActionEvent * * @return string */ - public function getLang() + public function getLocale() { - return $this->lang; + return $this->locale; } /** - * @param \Thelia\Model\Coupon $coupon + * Set Coupon Model + * + * @param \Thelia\Model\Coupon $coupon Coupon Model + * + * @return $this */ public function setCoupon($coupon) { $this->coupon = $coupon; + + return $this; } /** + * Return Coupon Model + * * @return \Thelia\Model\Coupon */ public function getCoupon() diff --git a/core/lib/Thelia/Model/Coupon.php b/core/lib/Thelia/Model/Coupon.php index 36f1ef8e1..03a59b8e9 100755 --- a/core/lib/Thelia/Model/Coupon.php +++ b/core/lib/Thelia/Model/Coupon.php @@ -44,6 +44,9 @@ use Thelia\Model\Map\CouponTableMap; class Coupon extends BaseCoupon { + use \Thelia\Model\Tools\ModelEventDispatcherTrait; + + /** * Constructor * @@ -60,9 +63,9 @@ class Coupon extends BaseCoupon * @param boolean $isRemovingPostage Is removing Postage * @param int $maxUsage Coupon quantity * @param CouponRuleCollection $rules CouponRuleInterface to add - * @param string $lang Coupon Language code ISO (ex: fr_FR) + * @param string $locale Coupon Language code ISO (ex: fr_FR) */ - function createOrUpdate($code, $title, $amount, $effect, $shortDescription, $description, $isEnabled, $expirationDate, $isAvailableOnSpecialOffers, $isCumulative, $maxUsage, $rules, $lang = null) + function createOrUpdate($code, $title, $amount, $effect, $shortDescription, $description, $isEnabled, $expirationDate, $isAvailableOnSpecialOffers, $isCumulative, $maxUsage, $rules, $locale = null) { $this->setCode($code) ->setTitle($title) @@ -79,8 +82,8 @@ class Coupon extends BaseCoupon ->setRules($rules); // Set object language (i18n) - if (!is_null($lang)) { - $this->setLang($lang); + if (!is_null($locale)) { + $this->setLocale($locale); } $con = Propel::getWriteConnection(CouponTableMap::DATABASE_NAME); diff --git a/templates/admin/default/coupon-edit.html b/templates/admin/default/coupon-update.html similarity index 80% rename from templates/admin/default/coupon-edit.html rename to templates/admin/default/coupon-update.html index d881487ec..fed274f14 100755 --- a/templates/admin/default/coupon-edit.html +++ b/templates/admin/default/coupon-update.html @@ -1,6 +1,6 @@ {extends file="admin-layout.tpl"} -{block name="check-permissions"}admin.coupon.edit{/block} -{block name="page-title"}{intl l='Edit coupon'}{/block} +{block name="check-permissions"}admin.coupon.update{/block} +{block name="page-title"}{intl l='Update coupon'}{/block} {block name="main-content"}
@@ -12,10 +12,10 @@ - {form name="thelia.admin.coupon.edit"} + {form name="thelia.admin.coupon.creation"} {include file='coupon/form.html' formAction={url path={$formAction}} form=$form} {/form} diff --git a/templates/admin/default/coupon/form.html b/templates/admin/default/coupon/form.html index b715cd741..a6131922b 100644 --- a/templates/admin/default/coupon/form.html +++ b/templates/admin/default/coupon/form.html @@ -10,6 +10,10 @@ {/form_field} + {form_field form=$form field='success_url'} + + {/form_field} +
@@ -30,9 +34,8 @@
@@ -41,7 +44,7 @@