Working : coupon creation : add default rule (thelia.constraint.rule.available_for_everyone)

This commit is contained in:
gmorel
2013-09-23 23:04:00 +02:00
parent e4658d2fa2
commit d0f20cca1d
12 changed files with 154 additions and 156 deletions

View File

@@ -32,6 +32,7 @@ use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Coupon\CouponFactory;
use Thelia\Coupon\CouponManager;
use Thelia\Coupon\CouponRuleCollection;
use Thelia\Coupon\Type\CouponInterface;
use Thelia\Model\Coupon as CouponModel;
@@ -137,11 +138,21 @@ class Coupon extends BaseAction implements EventSubscriberInterface
{
$coupon->setDispatcher($this->getDispatcher());
// Set default rule if none found
$noConditionRule = $this->container->get('thelia.constraint.rule.available_for_everyone');
$constraintFactory = $this->container->get('thelia.constraint.factory');
$couponRuleCollection = new CouponRuleCollection();
$couponRuleCollection->add($noConditionRule);
$defaultSerializedRule = $constraintFactory->serializeCouponRuleCollection(
$couponRuleCollection
);
$coupon->createOrUpdate(
$event->getCode(),
$event->getTitle(),
$event->getAmount(),
$event->getEffect(),
$event->getType(),
$event->isRemovingPostage(),
$event->getShortDescription(),
$event->getDescription(),
@@ -150,9 +161,12 @@ class Coupon extends BaseAction implements EventSubscriberInterface
$event->isAvailableOnSpecialOffers(),
$event->isCumulative(),
$event->getMaxUsage(),
$defaultSerializedRule,
$event->getLocale()
);
$event->setCoupon($coupon);
}

View File

@@ -229,7 +229,7 @@ class Image extends BaseCachedFile implements EventSubscriberInterface
// Compute the image URL
$processed_image_url = $this->getCacheFileURL($subdir, basename($cacheFilePath));
// compute the full resulution image path in cache
// compute the full resolution image path in cache
$original_image_url = $this->getCacheFileURL($subdir, basename($originalImagePathInCache));
// Update the event with file path and file URL

View File

@@ -78,13 +78,13 @@ class CouponController extends BaseAdminController
$args['urlReadCoupon'] = $this->getRoute(
'admin.coupon.read',
array('couponId' => 'couponId'),
array('couponId' => 0),
Router::ABSOLUTE_URL
);
$args['urlEditCoupon'] = $this->getRoute(
'admin.coupon.update',
array('couponId' => 'couponId'),
array('couponId' => 0),
Router::ABSOLUTE_URL
);
@@ -164,7 +164,7 @@ class CouponController extends BaseAdminController
$args['dateFormat'] = $this->getSession()->getLang()->getDateFormat();
$args['availableCoupons'] = $this->getAvailableCoupons();
$args['formAction'] = 'admin/coupon/create/';
$args['formAction'] = 'admin/coupon/create';
return $this->render(
'coupon-create',
@@ -223,7 +223,7 @@ class CouponController extends BaseAdminController
'code' => $coupon->getCode(),
'title' => $coupon->getTitle(),
'amount' => $coupon->getAmount(),
'effect' => $coupon->getType(),
'type' => $coupon->getType(),
'shortDescription' => $coupon->getShortDescription(),
'description' => $coupon->getDescription(),
'isEnabled' => ($coupon->getIsEnabled() == 1),
@@ -271,7 +271,7 @@ class CouponController extends BaseAdminController
Router::ABSOLUTE_URL
);
$args['formAction'] = 'admin/coupon/update/' . $couponId;
$args['formAction'] = 'admin/coupon/update' . $couponId;
return $this->render('coupon-update', $args);
}
@@ -347,20 +347,7 @@ class CouponController extends BaseAdminController
);
$couponEvent = new CouponCreateOrUpdateEvent(
$coupon->getCode(),
$coupon->getTitle(),
$coupon->getAmount(),
$coupon->getType(),
$coupon->getShortDescription(),
$coupon->getDescription(),
$coupon->getIsEnabled(),
$coupon->getExpirationDate(),
$coupon->getIsAvailableOnSpecialOffers(),
$coupon->getIsCumulative(),
$coupon->getIsRemovingPostage(),
$coupon->getMaxUsage(),
$rules,
$coupon->getLocale()
$coupon->getCode(), $coupon->getTitle(), $coupon->getAmount(), $coupon->getType(), $coupon->getShortDescription(), $coupon->getDescription(), $coupon->getIsEnabled(), $coupon->getExpirationDate(), $coupon->getIsAvailableOnSpecialOffers(), $coupon->getIsCumulative(), $coupon->getIsRemovingPostage(), $coupon->getMaxUsage(), $coupon->getLocale()
);
$couponEvent->setCoupon($coupon);
@@ -497,20 +484,7 @@ class CouponController extends BaseAdminController
$data = $form->getData();
$couponEvent = new CouponCreateOrUpdateEvent(
$data['code'],
$data['title'],
$data['amount'],
$data['type'],
$data['shortDescription'],
$data['description'],
$data['isEnabled'],
\DateTime::createFromFormat('Y-m-d', $data['expirationDate']),
$data['isAvailableOnSpecialOffers'],
$data['isCumulative'],
$data['isRemovingPostage'],
$data['maxUsage'],
new CouponRuleCollection(array()),
$data['locale']
$data['code'], $data['title'], $data['amount'], $data['type'], $data['shortDescription'], $data['description'], $data['isEnabled'], \DateTime::createFromFormat('Y-m-d', $data['expirationDate']), $data['isAvailableOnSpecialOffers'], $data['isCumulative'], $data['isRemovingPostage'], $data['maxUsage'], $data['locale']
);
// Dispatch Event to the Action
@@ -538,7 +512,6 @@ class CouponController extends BaseAdminController
} catch (FormValidationException $e) {
// Invalid data entered
$message = 'Please check your input:';
$this->logError($action, $message, $e);
} catch (\Exception $e) {
// Any other error

View File

@@ -78,8 +78,8 @@ class CouponCreateOrUpdateEvent extends ActionEvent
/** @var Coupon Coupon model */
protected $coupon = null;
/** @var string Coupon effect */
protected $effect;
/** @var string Coupon type */
protected $type;
/** @var string Language code ISO (ex: fr_FR) */
protected $locale = null;
@@ -87,36 +87,22 @@ class CouponCreateOrUpdateEvent extends ActionEvent
/**
* Constructor
*
* @param string $code Coupon Code
* @param string $title Coupon title
* @param float $amount Amount removed from the Total Checkout
* @param string $type Coupon type
* @param string $shortDescription Coupon short description
* @param string $description Coupon description
* @param boolean $isEnabled Enable/Disable
* @param \DateTime $expirationDate Coupon expiration date
* @param boolean $isAvailableOnSpecialOffers Is available on special offers
* @param boolean $isCumulative Is cumulative
* @param boolean $isRemovingPostage Is removing Postage
* @param int $maxUsage Coupon quantity
* @param CouponRuleCollection $rules CouponRuleInterface to add
* @param string $locale Coupon Language code ISO (ex: fr_FR)
* @param string $code Coupon Code
* @param string $title Coupon title
* @param float $amount Amount removed from the Total Checkout
* @param string $type Coupon type
* @param string $shortDescription Coupon short description
* @param string $description Coupon description
* @param boolean $isEnabled Enable/Disable
* @param \DateTime $expirationDate Coupon expiration date
* @param boolean $isAvailableOnSpecialOffers Is available on special offers
* @param boolean $isCumulative Is cumulative
* @param boolean $isRemovingPostage Is removing Postage
* @param int $maxUsage Coupon quantity
* @param string $locale Coupon Language code ISO (ex: fr_FR)
*/
public function __construct(
$code,
$title,
$amount,
$type,
$shortDescription,
$description,
$isEnabled,
\DateTime $expirationDate,
$isAvailableOnSpecialOffers,
$isCumulative,
$isRemovingPostage,
$maxUsage,
$rules,
$locale
$code, $title, $amount, $type, $shortDescription, $description, $isEnabled, \DateTime $expirationDate, $isAvailableOnSpecialOffers, $isCumulative, $isRemovingPostage, $maxUsage, $locale
) {
$this->amount = $amount;
$this->code = $code;
@@ -127,7 +113,6 @@ class CouponCreateOrUpdateEvent extends ActionEvent
$this->isEnabled = $isEnabled;
$this->isRemovingPostage = $isRemovingPostage;
$this->maxUsage = $maxUsage;
$this->rules = $rules;
$this->shortDescription = $shortDescription;
$this->title = $title;
$this->type = $type;
@@ -206,22 +191,6 @@ class CouponCreateOrUpdateEvent extends ActionEvent
return $this->amount;
}
/**
* Return condition to validate the Coupon or not
*
* @return CouponRuleCollection
*/
public function getRules()
{
if ($this->rules === null || !is_object($this->rules)) {
$rules = $this->rules;
} else {
$rules = clone $this->rules;
}
return $rules;
}
/**
* Return Coupon expiration date
*

View File

@@ -63,11 +63,12 @@ class Coupon extends BaseCoupon
* @param boolean $isAvailableOnSpecialOffers Is available on special offers
* @param boolean $isCumulative Is cumulative
* @param int $maxUsage Coupon quantity
* @param string $defaultSerializedRule Serialized default rule added if none found
* @param string $locale Coupon Language code ISO (ex: fr_FR)
*
* @throws \Exception
*/
function createOrUpdate($code, $title, $amount, $type, $isRemovingPostage, $shortDescription, $description, $isEnabled, $expirationDate, $isAvailableOnSpecialOffers, $isCumulative, $maxUsage, $locale = null)
function createOrUpdate($code, $title, $amount, $type, $isRemovingPostage, $shortDescription, $description, $isEnabled, $expirationDate, $isAvailableOnSpecialOffers, $isCumulative, $maxUsage, $defaultSerializedRule, $locale = null)
{
$this->setCode($code)
->setTitle($title)
@@ -82,6 +83,11 @@ class Coupon extends BaseCoupon
->setIsCumulative($isCumulative)
->setMaxUsage($maxUsage);
// If no rule given, set default rule
if (null === $this->getSerializedRules()) {
$this->setSerializedRules($defaultSerializedRule);
}
// Set object language (i18n)
if (!is_null($locale)) {
$this->setLocale($locale);