You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
5.4KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Frontend ;
  3. use App\Form\Frontend\OrderProductsType;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\OrderProductInterface;
  7. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  8. use Lc\ShopBundle\Context\OrderUtilsInterface;
  9. use Lc\ShopBundle\Context\ProductFamilyInterface;
  10. use Lc\ShopBundle\Context\ReductionCartInterface;
  11. use Lc\ShopBundle\Model\OrderReductionCart;
  12. use Lc\ShopBundle\Services\UserUtils;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Generator\UrlGenerator;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Security\Core\Security;
  18. class CartController extends BaseController
  19. {
  20. protected $orderUtils ;
  21. protected $userUtils ;
  22. protected $router ;
  23. protected $productFamilyRepository ;
  24. protected $orderProductRepository ;
  25. protected $productFamily ;
  26. protected $orderProducts = [] ;
  27. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils,
  28. UserUtils $userUtils, UrlGeneratorInterface $router)
  29. {
  30. parent::__construct($em, $security, $merchantUtils);
  31. $this->orderUtils = $orderUtils ;
  32. $this->userUtils = $userUtils ;
  33. $this->router = $router ;
  34. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
  35. $this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
  36. }
  37. public function addProductFamily(Request $request)
  38. {
  39. $user = $this->security->getUser() ;
  40. $visitor = $this->userUtils->getVisitorCurrent() ;
  41. $return = [] ;
  42. $data = $request->request->all() ;
  43. if(isset($data['order_products']['id_product_family'])) {
  44. $idProductFamily = $data['order_products']['id_product_family'] ;
  45. $this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;
  46. // alerte si cookies non acceptés
  47. if (!$user && !$visitor) {
  48. $this->addFlash('error', 'Vous devez <a href="'.$this->router->generate('frontend_page', ['devAlias' => 'politique-de-confidentialite']).'">accepter les cookies</a> ou vous <a href="'.$this->router->generate('fos_user_security_login').'">connecter</a> pour ajouter un produit.');
  49. return false ;
  50. }
  51. if($this->productFamily) {
  52. $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
  53. $form->handleRequest($request);
  54. if ($form->isSubmitted() && $form->isValid()) {
  55. $orderShop = $this->orderUtils->getCartCurrent() ;
  56. $data = $form->getData() ;
  57. foreach($data as $orderProduct) {
  58. if($orderProduct instanceof OrderProductInterface) {
  59. $addOrderProduct = $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
  60. if($addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
  61. $this->orderProducts[] = $orderProduct ;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. return new JsonResponse($return) ;
  69. }
  70. public function deleteReductionCart(Request $request)
  71. {
  72. $id = $request->get('id') ;
  73. $orderReductionCartRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCartInterface::class)->getName()) ;
  74. $orderReductionCart = $orderReductionCartRepository->findOneById((int) $id) ;
  75. $orderShop = $this->orderUtils->getCartCurrent() ;
  76. if($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
  77. $this->em->remove($orderReductionCart) ;
  78. $this->em->flush() ;
  79. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.') ;
  80. }
  81. else {
  82. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ;
  83. }
  84. $referer = $request->headers->get('referer');
  85. if($referer) {
  86. return $this->redirect($referer);
  87. }
  88. else {
  89. return $this->redirectToRoute('frontend_order_cart');
  90. }
  91. }
  92. }