refactor modify Article action
This commit is contained in:
@@ -99,6 +99,93 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Delete specify article present into cart
|
||||||
|
*
|
||||||
|
* @param \Thelia\Core\Event\CartEvent $event
|
||||||
|
*/
|
||||||
|
public function deleteArticle(CartEvent $event)
|
||||||
|
{
|
||||||
|
$request = $event->getRequest();
|
||||||
|
|
||||||
|
if (null !== $cartItemId = $request->get('cartItem')) {
|
||||||
|
$cart = $event->getCart();
|
||||||
|
try {
|
||||||
|
$cartItem = CartItemQuery::create()
|
||||||
|
->filterByCartId($cart->getId())
|
||||||
|
->filterById($cartItemId)
|
||||||
|
->delete();
|
||||||
|
} catch (PropelException $e) {
|
||||||
|
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Modify article's quantity
|
||||||
|
*
|
||||||
|
* don't use Form here just test the Request.
|
||||||
|
*
|
||||||
|
* @param \Thelia\Core\Event\CartEvent $event
|
||||||
|
*/
|
||||||
|
public function modifyArticle(CartEvent $event)
|
||||||
|
{
|
||||||
|
$request = $event->getRequest();
|
||||||
|
|
||||||
|
if (null !== $cartItemId = $request->get("cartItem") && null !== $quantity = $request->get("quantity")) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
$cart = $event->getCart($request);
|
||||||
|
|
||||||
|
$cartItem = CartItemQuery::create()
|
||||||
|
->filterByCartId($cart->getId())
|
||||||
|
->filterById($cartItemId)
|
||||||
|
->findOne();
|
||||||
|
|
||||||
|
if ($cartItem) {
|
||||||
|
$this->updateQuantity($cartItem, $quantity);
|
||||||
|
}
|
||||||
|
} catch (PropelException $e) {
|
||||||
|
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during updating cartItem with message : %s", $e->getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of event names this subscriber wants to listen to.
|
||||||
|
*
|
||||||
|
* The array keys are event names and the value can be:
|
||||||
|
*
|
||||||
|
* * The method name to call (priority defaults to 0)
|
||||||
|
* * An array composed of the method name to call and the priority
|
||||||
|
* * An array of arrays composed of the method names to call and respective
|
||||||
|
* priorities, or 0 if unset
|
||||||
|
*
|
||||||
|
* For instance:
|
||||||
|
*
|
||||||
|
* * array('eventName' => 'methodName')
|
||||||
|
* * array('eventName' => array('methodName', $priority))
|
||||||
|
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
|
||||||
|
*
|
||||||
|
* @return array The event names to listen to
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
public static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
"action.addArticle" => array("addArticle", 128),
|
||||||
|
"action.deleteArticle" => array("deleteArticle", 128),
|
||||||
|
"action.modifyArticle" => array("modifyArticle", 128),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* increase the quantity for an existing cartItem
|
* increase the quantity for an existing cartItem
|
||||||
*
|
*
|
||||||
@@ -178,89 +265,4 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
|||||||
return $cartAdd;
|
return $cartAdd;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Delete specify article present into cart
|
|
||||||
*
|
|
||||||
* @param \Thelia\Core\Event\ActionEvent $event
|
|
||||||
*/
|
|
||||||
public function deleteArticle(CartEvent $event)
|
|
||||||
{
|
|
||||||
$request = $event->getRequest();
|
|
||||||
|
|
||||||
if (null !== $cartItemId = $request->get('cartItem')) {
|
|
||||||
$cart = $event->getCart();
|
|
||||||
try {
|
|
||||||
$cartItem = CartItemQuery::create()
|
|
||||||
->filterByCartId($cart->getId())
|
|
||||||
->filterById($cartItemId)
|
|
||||||
->delete();
|
|
||||||
} catch (PropelException $e) {
|
|
||||||
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Modify article's quantity
|
|
||||||
*
|
|
||||||
* don't use Form here just test the Request.
|
|
||||||
*
|
|
||||||
* @param \Thelia\Core\Event\ActionEvent $event
|
|
||||||
*/
|
|
||||||
public function modifyArticle(CartEvent $event)
|
|
||||||
{
|
|
||||||
$request = $event->getRequest();
|
|
||||||
|
|
||||||
if (null !== $cartItemId = $request->get("cartItem") && null !== $quantity = $request->get("quantity")) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
$cart = $event->getCart($request);
|
|
||||||
|
|
||||||
$cartItem = CartItemQuery::create()
|
|
||||||
->filterByCartId($cart->getId())
|
|
||||||
->filterById($cartItemId)
|
|
||||||
->findOne();
|
|
||||||
|
|
||||||
if ($cartItem) {
|
|
||||||
$this->updateQuantity($cartItem, $quantity);
|
|
||||||
}
|
|
||||||
} catch (PropelException $e) {
|
|
||||||
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during updating cartItem with message : %s", $e->getMessage()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an array of event names this subscriber wants to listen to.
|
|
||||||
*
|
|
||||||
* The array keys are event names and the value can be:
|
|
||||||
*
|
|
||||||
* * The method name to call (priority defaults to 0)
|
|
||||||
* * An array composed of the method name to call and the priority
|
|
||||||
* * An array of arrays composed of the method names to call and respective
|
|
||||||
* priorities, or 0 if unset
|
|
||||||
*
|
|
||||||
* For instance:
|
|
||||||
*
|
|
||||||
* * array('eventName' => 'methodName')
|
|
||||||
* * array('eventName' => array('methodName', $priority))
|
|
||||||
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
|
|
||||||
*
|
|
||||||
* @return array The event names to listen to
|
|
||||||
*
|
|
||||||
* @api
|
|
||||||
*/
|
|
||||||
public static function getSubscribedEvents()
|
|
||||||
{
|
|
||||||
return array(
|
|
||||||
"action.addArticle" => array("addArticle", 128),
|
|
||||||
"action.deleteArticle" => array("deleteArticle", 128),
|
|
||||||
"action.modifyArticle" => array("modifyArticle", 128),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,18 +24,31 @@ namespace Thelia\Controller\Front;
|
|||||||
|
|
||||||
|
|
||||||
use Thelia\Core\Event\CartEvent;
|
use Thelia\Core\Event\CartEvent;
|
||||||
|
use Thelia\Core\Event\TheliaEvents;
|
||||||
|
|
||||||
class CartController extends BaseFrontController
|
class CartController extends BaseFrontController
|
||||||
{
|
{
|
||||||
use \Thelia\Cart\CartTrait;
|
use \Thelia\Cart\CartTrait;
|
||||||
|
|
||||||
public function addArticle()
|
public function addArticle()
|
||||||
|
{
|
||||||
|
$cartEvent = $this->getCartEvent();
|
||||||
|
|
||||||
|
$this->dispatch(TheliaEvents::CART_ADD, $cartEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function modifyArticle()
|
||||||
|
{
|
||||||
|
$cartEvent = $this->getCartEvent();
|
||||||
|
|
||||||
|
$this->dispatch(TheliaEvents::CART_MODIFYARTICLE, $cartEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getCartEvent()
|
||||||
{
|
{
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$cart = $this->getCart($request);
|
$cart = $this->getCart($request);
|
||||||
|
|
||||||
$cartEvent = new CartEvent($request, "action.addArticle", $cart);
|
return new CartEvent($request, $cart);
|
||||||
|
|
||||||
$this->dispatch("action.addArticle", $cartEvent);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,11 +43,6 @@ abstract class ActionEvent extends Event
|
|||||||
*/
|
*/
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $action;
|
|
||||||
|
|
||||||
protected $errorForm = null;
|
protected $errorForm = null;
|
||||||
|
|
||||||
@@ -58,10 +53,9 @@ abstract class ActionEvent extends Event
|
|||||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||||
* @param string $action
|
* @param string $action
|
||||||
*/
|
*/
|
||||||
public function __construct(Request $request, $action)
|
public function __construct(Request $request)
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
$this->action = $action;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -79,15 +73,6 @@ abstract class ActionEvent extends Event
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAction()
|
|
||||||
{
|
|
||||||
return $this->action;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return \Symfony\Component\HttpFoundation\Request
|
* @return \Symfony\Component\HttpFoundation\Request
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ class CartEvent extends ActionEvent {
|
|||||||
|
|
||||||
protected $cart;
|
protected $cart;
|
||||||
|
|
||||||
public function __construct(Request $request, $action, Cart $cart)
|
public function __construct(Request $request, Cart $cart)
|
||||||
{
|
{
|
||||||
parent::__construct($request, $action);
|
parent::__construct($request);
|
||||||
$this->cart = $cart;
|
$this->cart = $cart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -121,10 +121,20 @@ final class TheliaEvents
|
|||||||
/**
|
/**
|
||||||
* sent when a new item is added to current cart
|
* sent when a new item is added to current cart
|
||||||
*/
|
*/
|
||||||
const CART_ADDITEM = "cart.addItem";
|
const AFTER_CARTADDITEM = "cart.addItem";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sent when a cart item is modify
|
* sent when a cart item is modify
|
||||||
*/
|
*/
|
||||||
const CART_MODIFYITEM = "cart.modifyItem";
|
const AFTER_CARTMODIFYITEM = "cart.modifyItem";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sent for addArticle action
|
||||||
|
*/
|
||||||
|
const CART_ADDITEM = "action.addArticle";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sent on modify article action
|
||||||
|
*/
|
||||||
|
const CART_MODIFYARTICLE = "action.modifyArticle";
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user