- Coupon create
This commit is contained in:
gmorel
2013-09-04 15:43:05 +02:00
parent 6cba95f4ee
commit 62c1a59f68
3 changed files with 111 additions and 44 deletions

View File

@@ -25,8 +25,10 @@ namespace Thelia\Controller\Admin;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Core\Event\Coupon\CouponCreateEvent; use Thelia\Core\Event\Coupon\CouponCreateEvent;
use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent;
use Thelia\Core\Event\Coupon\CouponEvent; use Thelia\Core\Event\Coupon\CouponEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Core\Security\Exception\AuthenticationException; 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;
@@ -35,6 +37,8 @@ use Thelia\Form\Exception\FormValidationException;
use Thelia\Log\Tlog; use Thelia\Log\Tlog;
use Thelia\Model\Coupon; use Thelia\Model\Coupon;
use Thelia\Model\CouponQuery; use Thelia\Model\CouponQuery;
use Thelia\Model\Lang;
use Thelia\Tools\I18n;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
@@ -56,7 +60,7 @@ class CouponController extends BaseAdminController
*/ */
public function browseAction() public function browseAction()
{ {
$this->checkAuth("ADMIN", "admin.coupon.view"); $this->checkAuth('ADMIN', 'admin.coupon.view');
return $this->render('coupon-list'); return $this->render('coupon-list');
} }
@@ -71,7 +75,7 @@ class CouponController extends BaseAdminController
public function createAction() public function createAction()
{ {
// Check current user authorization // Check current user authorization
$response = $this->checkAuth("admin.coupon.create"); $response = $this->checkAuth('admin.coupon.create');
if ($response !== null) { if ($response !== null) {
return $response; return $response;
} }
@@ -84,11 +88,14 @@ class CouponController extends BaseAdminController
if ($this->getRequest()->isMethod('POST')) { if ($this->getRequest()->isMethod('POST')) {
try { try {
// Check the form against constraints violations // Check the form against constraints violations
$form = $this->validateForm($creationForm, "POST"); $form = $this->validateForm($creationForm, 'POST');
$i18n = new I18n();
/** @var Lang $lang */
$lang = $this->getSession()->get('lang');
// Get the form field values // Get the form field values
$data = $form->getData(); $data = $form->getData();
$couponEvent = new CouponEvent( $couponEvent = new CouponCreateOrUpdateEvent(
$data['code'], $data['code'],
$data['title'], $data['title'],
$data['amount'], $data['amount'],
@@ -96,12 +103,13 @@ class CouponController extends BaseAdminController
$data['shortDescription'], $data['shortDescription'],
$data['description'], $data['description'],
$data['isEnabled'], $data['isEnabled'],
new \DateTime($data['expirationDate']), $i18n->getDateTimeFromForm($lang, $data['expirationDate']),
$data['isAvailableOnSpecialOffers'], $data['isAvailableOnSpecialOffers'],
$data['isCumulative'], $data['isCumulative'],
$data['isRemovingPostage'], $data['isRemovingPostage'],
$data['maxUsage'], $data['maxUsage'],
array() array(),
$data['locale']
); );
$this->dispatch( $this->dispatch(
@@ -112,39 +120,34 @@ class CouponController extends BaseAdminController
sprintf( sprintf(
'Coupon %s (ID %s) created', 'Coupon %s (ID %s) created',
$couponEvent->getTitle(), $couponEvent->getTitle(),
$couponEvent->getId() $couponEvent->getCoupon()->getId()
) )
); );
// @todo redirect if successful // @todo redirect if successful
} catch (FormValidationException $e) { } catch (FormValidationException $e) {
// Invalid data entered // Invalid data entered
$message = 'Please check your input:'; $message = 'Please check your input:';
$this->logError('creation', $message, $e);
} catch (\Exception $e) { } catch (\Exception $e) {
// Any other error // Any other error
$message = 'Sorry, an error occured:'; $message = 'Sorry, an error occurred:';
$this->logError('creation', $message, $e);
} }
if ($message !== false) { if ($message !== false) {
// Log error message // Mark the form as with error
Tlog::getInstance()->error(
sprintf(
"Error during variable modification process : %s. Exception was %s",
$message, $e->getMessage()
)
);
// Mark the form as errored
$creationForm->setErrorMessage($message); $creationForm->setErrorMessage($message);
// Pas the form and the error to the parser // Send the form and the error to the parser
$this->getParserContext() $this->getParserContext()
->addForm($creationForm) ->addForm($creationForm)
->setGeneralError($message) ->setGeneralError($message);
;
} }
} }
$formAction = 'admin/coupon/create'; $formAction = 'admin/coupon/create';
return $this->render( return $this->render(
'coupon-create', 'coupon-create',
array( array(
@@ -162,7 +165,7 @@ class CouponController extends BaseAdminController
*/ */
public function editAction($couponId) public function editAction($couponId)
{ {
$this->checkAuth("ADMIN", "admin.coupon.edit"); $this->checkAuth('ADMIN', 'admin.coupon.edit');
$formAction = 'admin/coupon/edit/' . $couponId; $formAction = 'admin/coupon/edit/' . $couponId;
@@ -178,7 +181,7 @@ class CouponController extends BaseAdminController
*/ */
public function readAction($couponId) public function readAction($couponId)
{ {
$this->checkAuth("ADMIN", "admin.coupon.read"); $this->checkAuth('ADMIN', 'admin.coupon.read');
// Database request repeated in the loop but cached // Database request repeated in the loop but cached
$search = CouponQuery::create(); $search = CouponQuery::create();
@@ -201,28 +204,51 @@ class CouponController extends BaseAdminController
protected function buildCouponFromForm(array $data) protected function buildCouponFromForm(array $data)
{ {
$couponBeingCreated = new Coupon(); $couponBeingCreated = new Coupon();
$couponBeingCreated->setCode($data["code"]); $couponBeingCreated->setCode($data['code']);
$couponBeingCreated->setType($data["type"]); $couponBeingCreated->setType($data['type']);
$couponBeingCreated->setTitle($data["title"]); $couponBeingCreated->setTitle($data['title']);
$couponBeingCreated->setShortDescription($data["shortDescription"]); $couponBeingCreated->setShortDescription($data['shortDescription']);
$couponBeingCreated->setDescription($data["description"]); $couponBeingCreated->setDescription($data['description']);
$couponBeingCreated->setAmount($data["amount"]); $couponBeingCreated->setAmount($data['amount']);
$couponBeingCreated->setIsEnabled($data["isEnabled"]); $couponBeingCreated->setIsEnabled($data['isEnabled']);
$couponBeingCreated->setExpirationDate($data["expirationDate"]); $couponBeingCreated->setExpirationDate($data['expirationDate']);
$couponBeingCreated->setSerializedRules( $couponBeingCreated->setSerializedRules(
new CouponRuleCollection( new CouponRuleCollection(
array() array()
) )
); );
$couponBeingCreated->setIsCumulative($data["isCumulative"]); $couponBeingCreated->setIsCumulative($data['isCumulative']);
$couponBeingCreated->setIsRemovingPostage( $couponBeingCreated->setIsRemovingPostage(
$data["isRemovingPostage"] $data['isRemovingPostage']
); );
$couponBeingCreated->setMaxUsage($data["maxUsage"]); $couponBeingCreated->setMaxUsage($data['maxUsage']);
$couponBeingCreated->setIsAvailableOnSpecialOffers( $couponBeingCreated->setIsAvailableOnSpecialOffers(
$data["isAvailableOnSpecialOffers"] $data['isAvailableOnSpecialOffers']
); );
return $couponBeingCreated; return $couponBeingCreated;
} }
/**
* Log error message
*
* @param string $action Creation|Update|Delete
* @param string $message Message to log
* @param \Exception $e Exception to log
*
* @return $this
*/
protected function logError($action, $message, $e)
{
Tlog::getInstance()->error(
sprintf(
'Error during Coupon ' . $action . ' process : %s. Exception was %s',
$message,
$e->getMessage()
)
);
return $this;
}
} }

View File

@@ -207,17 +207,17 @@ final class TheliaEvents
/** /**
* Sent when editing a Coupon * Sent when editing a Coupon
*/ */
const COUPON_EDIT = "action.edit_coupon"; const COUPON_UPDATE = "action.update_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.
*/ */
const BEFORE_EDIT_COUPON = "action.before_edit_coupon"; const BEFORE_UPDATE_COUPON = "action.before_update_coupon";
/** /**
* Sent just after a successful update of a new Coupon in the database. * Sent just after a successful update of a new Coupon in the database.
*/ */
const AFTER_EDIT_COUPON = "action.after_edit_coupon"; const AFTER_UPDATE_COUPON = "action.after_update_coupon";
/** /**
* Sent when disabling a Coupon * Sent when disabling a Coupon

View File

@@ -1,16 +1,57 @@
<?php <?php
/** /**********************************************************************************/
* Created by JetBrains PhpStorm. /* */
* Date: 9/4/13 /* Thelia */
* Time: 2:55 PM /* */
* /* Copyright (c) OpenStudio */
* @author Guillaume MOREL <gmorel@openstudio.fr> /* email : info@thelia.net */
*/ /* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/**********************************************************************************/
namespace Thelia\Tools; namespace Thelia\Tools;
use Thelia\Model\Lang;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Helper for translations
*
* @package I18n
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class I18n class I18n
{ {
/**
* Create a \DateTime from a date picker form input
* The date format is the same as the one from the current User Session
* Ex : $lang = $session->getLang()
*
* @param Lang $lang Object containing date format
* @param string $date String to convert
*
* @return \DateTime
*/
public function getDateTimeFromForm(Lang $lang, $date)
{
$currentDateFormat = $lang->getDateFormat();
return \DateTime::createFromFormat($currentDateFormat, $date);
}
} }