- Coupon create
This commit is contained in:
gmorel
2013-09-03 14:04:05 +02:00
parent 2b337229b8
commit 89edb958e5
6 changed files with 651 additions and 518 deletions

View File

@@ -87,14 +87,35 @@ class CouponController extends BaseAdminController
if ($this->getRequest()->isMethod('POST')) {
try {
$couponCreationForm = new CouponCreationForm($this->getRequest());
$couponBeingCreated = $this->buildCouponFromForm(
$this->validateForm($couponCreationForm, "POST")->getData()
);
// Create the form from the request
$creationForm = new CouponCreationForm(Form($this->getRequest()));
$couponCreateEvent = new CouponCreateEvent(
$couponBeingCreated
// Check the form against constraints violations
$form = $this->validateForm($creationForm, "POST");
// Get the form field values
$data = $form->getData();
var_dump($data);
$couponCreateEvent = new CouponCreateEvent();
$couponCreateEvent->setTitle($data['title']);
$couponCreateEvent->setShortDescription($data['shortDescription']);
$couponCreateEvent->setDescription($data['longDescription']);
$couponCreateEvent->setCode($data['code']);
$couponCreateEvent->setAmount($data['amount']);
$couponCreateEvent->setExpirationDate(
new \DateTime($data['expirationDate'])
);
$couponCreateEvent->setMaxUsage($data['maxUsage']);
$couponCreateEvent->setIsCumulative($data['isCumulative']);
$couponCreateEvent->setIsRemovingPostage($data['isRemovingPostage']);
$couponCreateEvent->setIsAvailableOnSpecialOffers($data['isAvailableOnSpecialOffers']);
$couponCreateEvent->setIsEnabled($data['isEnabled']);
// $couponCreateEvent->setRules($data['rules']);
$this->dispatch(
TheliaEvents::CREATE_COUPON,
@@ -103,14 +124,14 @@ class CouponController extends BaseAdminController
$this->adminLogAppend(
sprintf(
'Coupon %s (ID %s) created',
$couponBeingCreated->getTitle(),
$couponBeingCreated->getId()
$couponCreateEvent->getTitle(),
$couponCreateEvent->getId()
)
);
// @todo redirect if successful
} catch (FormValidationException $e) {
$couponCreationForm->setErrorMessage($e->getMessage());
$this->getParserContext()->setErrorForm($couponCreationForm);
$creationForm->setErrorMessage($e->getMessage());
$this->getParserContext()->setErrorForm($creationForm);
} catch (\Exception $e) {
Tlog::getInstance()->error(
sprintf(
@@ -124,7 +145,7 @@ class CouponController extends BaseAdminController
}
return $this->render('coupon/edit', array());
return $this->render('coupon/create', array('action' => 'create'));
}
/**
@@ -138,7 +159,7 @@ class CouponController extends BaseAdminController
{
$this->checkAuth("ADMIN", "admin.coupon.view");
return $this->render('coupon/edit', $args);
return $this->render('coupon/update', $args);
}
/**

View File

@@ -24,7 +24,6 @@
namespace Thelia\Core\Event\Coupon;
use Thelia\Core\Event\ActionEvent;
use Thelia\Coupon\CouponRuleCollection;
use Thelia\Model\Coupon;
/**
* Created by JetBrains PhpStorm.
@@ -42,198 +41,6 @@ class CouponCreateEvent extends ActionEvent
/** @var CouponRuleCollection Array of CouponRuleInterface */
protected $rules = null;
/**
* @param float $amount
*/
public function setAmount($amount)
{
$this->amount = $amount;
}
/**
* @return float
*/
public function getAmount()
{
return $this->amount;
}
/**
* @param string $code
*/
public function setCode($code)
{
$this->code = $code;
}
/**
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param \DateTime $expirationDate
*/
public function setExpirationDate($expirationDate)
{
$this->expirationDate = $expirationDate;
}
/**
* @return \DateTime
*/
public function getExpirationDate()
{
return $this->expirationDate;
}
/**
* @param boolean $isAvailableOnSpecialOffers
*/
public function setIsAvailableOnSpecialOffers($isAvailableOnSpecialOffers)
{
$this->isAvailableOnSpecialOffers = $isAvailableOnSpecialOffers;
}
/**
* @return boolean
*/
public function getIsAvailableOnSpecialOffers()
{
return $this->isAvailableOnSpecialOffers;
}
/**
* @param boolean $isCumulative
*/
public function setIsCumulative($isCumulative)
{
$this->isCumulative = $isCumulative;
}
/**
* @return boolean
*/
public function getIsCumulative()
{
return $this->isCumulative;
}
/**
* @param boolean $isEnabled
*/
public function setIsEnabled($isEnabled)
{
$this->isEnabled = $isEnabled;
}
/**
* @return boolean
*/
public function getIsEnabled()
{
return $this->isEnabled;
}
/**
* @param boolean $isRemovingPostage
*/
public function setIsRemovingPostage($isRemovingPostage)
{
$this->isRemovingPostage = $isRemovingPostage;
}
/**
* @return boolean
*/
public function getIsRemovingPostage()
{
return $this->isRemovingPostage;
}
/**
* @param int $maxUsage
*/
public function setMaxUsage($maxUsage)
{
$this->maxUsage = $maxUsage;
}
/**
* @return int
*/
public function getMaxUsage()
{
return $this->maxUsage;
}
/**
* @param \Thelia\Coupon\CouponRuleCollection $rules
*/
public function setRules($rules)
{
$this->rules = $rules;
}
/**
* @return \Thelia\Coupon\CouponRuleCollection
*/
public function getRules()
{
return $this->rules;
}
/**
* @param string $shortDescription
*/
public function setShortDescription($shortDescription)
{
$this->shortDescription = $shortDescription;
}
/**
* @return string
*/
public function getShortDescription()
{
return $this->shortDescription;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/** @var string Coupon code (ex: XMAS) */
protected $code = null;
@@ -267,4 +74,301 @@ class CouponCreateEvent extends ActionEvent
/** @var bool if Coupon is available for Products already on special offers */
protected $isAvailableOnSpecialOffers = false;
/**
* Return Coupon code (ex: XMAS)
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Return Coupon title (ex: Coupon for XMAS)
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Return Coupon short description
*
* @return string
*/
public function getShortDescription()
{
return $this->shortDescription;
}
/**
* Return Coupon description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* If Coupon is cumulative or prevent any accumulation
* If is cumulative you can sum Coupon effects
* If not cancel all other Coupon and take the last given
*
* @return bool
*/
public function isCumulative()
{
return $this->isCumulative;
}
/**
* If Coupon is removing Checkout Postage
*
* @return bool
*/
public function isRemovingPostage()
{
return $this->isRemovingPostage;
}
/**
* Return effects generated by the coupon
*
* @return float Amount removed from the Total Checkout
*/
public function getAmount()
{
return $this->amount;
}
/**
* Return condition to validate the Coupon or not
*
* @return CouponRuleCollection
*/
public function getRules()
{
return clone $this->rules;
}
/**
* Set effects generated by the coupon
*
* @param float $amount Amount removed from the Total Checkout
*
* @return $this
*/
public function setAmount($amount)
{
$this->amount = $amount;
return $this;
}
/**
* Set Coupon Code
*
* @param string $code Coupon Code
*
* @return $this
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Set Coupon description
*
* @param string $description Coupon description
*
* @return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Set Coupon expiration date (date given considered as expired)
*
* @param \DateTime $expirationDate Coupon expiration date
*
* @return $this
*/
public function setExpirationDate($expirationDate)
{
$this->expirationDate = $expirationDate;
return $this;
}
/**
* Return Coupon expiration date
*
* @return \DateTime
*/
public function getExpirationDate()
{
return clone $this->expirationDate;
}
/**
* Set if Coupon is available on special offers
*
* @param boolean $isAvailableOnSpecialOffers is available on special offers
*
* @return $this
*/
public function setIsAvailableOnSpecialOffers($isAvailableOnSpecialOffers)
{
$this->isAvailableOnSpecialOffers = $isAvailableOnSpecialOffers;
return $this;
}
/**
* If Coupon is available on special offers
*
* @return boolean
*/
public function getIsAvailableOnSpecialOffers()
{
return $this->isAvailableOnSpecialOffers;
}
/**
* Set if the Coupon is cumulative with other Coupons or not
*
* @param boolean $isCumulative is cumulative
*
* @return $this
*/
public function setIsCumulative($isCumulative)
{
$this->isCumulative = $isCumulative;
return $this;
}
/**
* Enable/Disable the Coupon
*
* @param boolean $isEnabled Enable/Disable
*
* @return $this
*/
public function setIsEnabled($isEnabled)
{
$this->isEnabled = $isEnabled;
return $this;
}
/**
* Get if Coupon is enabled or not
*
* @return boolean
*/
public function isEnabled()
{
return $this->isEnabled;
}
/**
* Set if Coupon is removing Postage
*
* @param boolean $isRemovingPostage is removing Postage
*
* @return $this
*/
public function setIsRemovingPostage($isRemovingPostage)
{
$this->isRemovingPostage = $isRemovingPostage;
return $this;
}
/**
* Set how many time a coupon can be used (-1 : unlimited)
*
* @param int $maxUsage Coupon quantity
*
* @return $this
*/
public function setMaxUsage($maxUsage)
{
$this->maxUsage = $maxUsage;
return $this;
}
/**
* Return how many time the Coupon can be used again
* Ex : -1 unlimited
*
* @return int
*/
public function getMaxUsage()
{
return $this->maxUsage;
}
/**
* Replace the existing Rules by those given in parameter
* If one Rule is badly implemented, no Rule will be added
*
* @param CouponRuleCollection $rules CouponRuleInterface to add
*
* @return $this
* @throws \Thelia\Exception\InvalidRuleException
*/
public function setRules(CouponRuleCollection $rules)
{
$this->rules = $rules;
$this->constraintManager = new ConstraintManager(
$this->adapter,
$this->rules
);
return $this;
}
/**
* Set Coupon short description
*
* @param string $shortDescription Coupon short description
*
* @return $this
*/
public function setShortDescription($shortDescription)
{
$this->shortDescription = $shortDescription;
return $this;
}
/**
* Set Coupon title
*
* @param string $title Coupon title
*
* @return $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
}