lib/composer/OrderBundle/src/VisualMedia/OrderBundle/Controller/CartController.php line 80

Open in your IDE?
  1. <?php
  2. /**
  3. * Cart Controller
  4. *
  5. * @author Bertin van den Ham <b.vandenham@visualmedia.nl>
  6. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  7. */
  8. namespace VisualMedia\OrderBundle\Controller;
  9. use InvalidArgumentException;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use VisualMedia\LisaBundle\Component\ManagerData;
  14. use VisualMedia\LisaBundle\Controller\BaseController;
  15. use VisualMedia\LisaBundle\Helper\EntityHelper;
  16. use VisualMedia\OrderBundle\Entity\BaseOrderItem;
  17. use VisualMedia\OrderBundle\Form\Type\CartType;
  18. use VisualMedia\OrderBundle\Service\Cart;
  19. /**
  20. * Cart Controller
  21. */
  22. class CartController extends BaseController
  23. {
  24. /**
  25. * Index Action
  26. *
  27. * @param Request $request
  28. *
  29. * @return Response
  30. */
  31. public function indexAction(Request $request): Response
  32. {
  33. $translator = $this->get('translator');
  34. $cart = $this->get(Cart::class);
  35. if (true === $clear = (bool)$request->query->get('clear')) {
  36. $cart->clear();
  37. return $this->redirectToRoute('cart');
  38. }
  39. $form = $this->createForm(CartType::class, $cart->getCart());
  40. $form->handleRequest($request);
  41. if ($form->isSubmitted() && $form->isValid() && $form->get('submit')->isClicked()) {
  42. return $this->redirectToRoute('order_checkout');
  43. }
  44. $data = array(
  45. 'cart' => $cart,
  46. 'form' => $form->createView()
  47. );
  48. if ($request->isXmlHttpRequest()) {
  49. return new JsonResponse(array(
  50. 'content' => $this->renderView('@VisualMediaOrder/Cart/cart.html.twig', $data),
  51. 'cart_amount' => $cart->getAmount(),
  52. ));
  53. }
  54. return $this->render('@VisualMediaOrder/Cart/index.html.twig', array_merge($data, array(
  55. 'title' => $translator->trans('cart.title', array(), 'frontend'),
  56. )));
  57. }
  58. /**
  59. * Add Action
  60. *
  61. * @param Request $request
  62. * @param string $type
  63. * @param int $id
  64. *
  65. * @return Response
  66. */
  67. public function addAction(Request $request, string $type, int $id): Response
  68. {
  69. $cart = $this->get(Cart::class);
  70. $translator = $this->get('translator');
  71. if ($this->has($alias = sprintf('manager.%s', $type)) === false) {
  72. throw new InvalidArgumentException($translator->trans('cart.alias_manager_not_found', array(
  73. '%alias%' => $alias,
  74. ), 'exception'));
  75. }
  76. $manager = $this->get($alias);
  77. if (null === $entity = $manager->getFirst(new ManagerData(array('id' => $id)))) {
  78. throw $this->createNotFoundException($translator->trans($type . '.not_found', [], 'exception'));
  79. }
  80. $item = new BaseOrderItem();
  81. $item->setEntity($entity);
  82. $item->setReference(EntityHelper::entityToKey($entity));
  83. $item->setReferenceData(array(
  84. 'type' => $type,
  85. ));
  86. if (method_exists($entity, 'getCascadedPrice')) {
  87. $item->setPrice($entity->getCascadedPrice());
  88. }
  89. else {
  90. $item->setPrice($entity->getPrice());
  91. }
  92. $cart->add($item);
  93. $cart->save();
  94. return $this->redirectToRoute('cart');
  95. }
  96. /**
  97. * Change Amount Action
  98. *
  99. * @param Request $request
  100. *
  101. * @return Response
  102. */
  103. public function changeAmountAction(Request $request): Response
  104. {
  105. $cart = $this->get(Cart::class);
  106. $hash = $request->request->get('hash');
  107. $amount = $request->request->get('amount');
  108. $cart->updateItemAmount($hash, $amount);
  109. return $this->redirectToRoute('cart');
  110. }
  111. /**
  112. * Remove Action
  113. *
  114. * @param Request $request
  115. * @param string $hash
  116. *
  117. * @return Response
  118. */
  119. public function removeAction(Request $request, string $hash)
  120. {
  121. $cart = $this->get(Cart::class);
  122. $cart->removeItem($hash);
  123. return $this->redirectToRoute('cart');
  124. }
  125. }