custom/plugins/GbmedShoppingList/src/Controller/ShoppingListController.php line 76

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedShoppingList
  12.  * @copyright      Copyright (c) 2020, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. namespace Gbmed\ShoppingList\Controller;
  18. use Gbmed\ShoppingList\Storefront\Page\Account\Order\AccountShoppingListPage;
  19. use Gbmed\ShoppingList\Service\ShoppingList;
  20. use Shopware\Core\Checkout\Cart\Cart;
  21. use Shopware\Core\Checkout\Cart\Error\Error;
  22. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  23. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  24. use Shopware\Core\Content\Product\Exception\ProductNotFoundException;
  25. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  26. use Shopware\Core\Framework\Uuid\Uuid;
  27. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  28. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  29. use Shopware\Storefront\Controller\StorefrontController;
  30. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\Response;
  33. use Symfony\Component\Routing\Annotation\Route;
  34. use Symfony\Component\HttpFoundation\JsonResponse;
  35. /**
  36.  * @Route(defaults={"_routeScope"={"storefront"}})
  37.  */
  38. class ShoppingListController extends StorefrontController
  39. {
  40.     private GenericPageLoaderInterface $genericLoader;
  41.     private ShoppingList $shoppingListService;
  42.     private CartService $cartService;
  43.     /**
  44.      * ShoppingListController constructor.
  45.      * @param GenericPageLoaderInterface $genericLoader
  46.      * @param ShoppingList $shoppingListService
  47.      * @param CartService $cartService
  48.      */
  49.     public function __construct(
  50.         GenericPageLoaderInterface $genericLoader,
  51.         ShoppingList $shoppingListService,
  52.         CartService $cartService
  53.     ) {
  54.         $this->genericLoader $genericLoader;
  55.         $this->shoppingListService $shoppingListService;
  56.         $this->cartService $cartService;
  57.     }
  58.     /**
  59.      * @Route(
  60.      *     "/account/shoppinglist",
  61.      *     name="frontend.shopping_list.overview.page",
  62.      *     methods={"GET"},
  63.      *     options={"seo"="false"},
  64.      *     defaults={"_loginRequired"=true}
  65.      * )
  66.      * @param Request $request
  67.      * @param SalesChannelContext $salesChannelContext
  68.      * @return Response
  69.      * @throws \Exception
  70.      */
  71.     public function shoppingLists(Request $requestSalesChannelContext $salesChannelContext): Response
  72.     {
  73.         $page $this->genericLoader->load($request$salesChannelContext);
  74.         $page AccountShoppingListPage::createFrom($page);
  75.         try {
  76.             $page->setShoppingLists($this->shoppingListService->getCustomerShoppingLists($salesChannelContext));
  77.         } catch (\Exception $e) {
  78.             throw $e;
  79.         }
  80.         return $this->renderStorefront('@Storefront/storefront/page/account/shopping_list/index.html.twig',
  81.             ['page' => $page]);
  82.     }
  83.     /**
  84.      * @Route(
  85.      *     "/shoppinglist/rename",
  86.      *     name="gbmed.shopping_list.rename",
  87.      *     methods={"POST"},
  88.      *     defaults={"XmlHttpRequest"=true,"csrf_protected"=true, "_loginRequired"=true},
  89.      *     options={"seo"="false"}
  90.      * )
  91.      * @param Request $request
  92.      * @param SalesChannelContext $salesChannelContext
  93.      * @return JsonResponse
  94.      * @throws \Exception
  95.      */
  96.     public function renameShoppingList(Request $requestSalesChannelContext $salesChannelContext): JsonResponse
  97.     {
  98.         $name $request->get('name');
  99.         $data = [
  100.             'id' => $request->get('id'),
  101.             'name' => strlen(trim($name)) ? $name 'Shopping-List'
  102.         ];
  103.         try {
  104.             $this->shoppingListService->renameCustomerShoppingList(
  105.                 $data['id'],
  106.                 [$data],
  107.                 $salesChannelContext
  108.             );
  109.         } catch (\Exception $e) {
  110.             throw $e;
  111.         }
  112.         return new JsonResponse(['success' => true]);
  113.     }
  114.     /**
  115.      * @Route(
  116.      *     "/shoppinglist/remove",
  117.      *     name="gbmed.shopping_list.remove",
  118.      *     methods={"POST"},
  119.      *     defaults={"XmlHttpRequest"=true,"csrf_protected"=true, "_loginRequired"=true},
  120.      *     options={"seo"="false"}
  121.      * )
  122.      * @param Request $request
  123.      * @param SalesChannelContext $salesChannelContext
  124.      * @return JsonResponse
  125.      * @throws \Exception
  126.      */
  127.     public function removeShoppingList(Request $requestSalesChannelContext $salesChannelContext): JsonResponse
  128.     {
  129.         $data = [
  130.             'id' => $request->get('id')
  131.         ];
  132.         try {
  133.             $this->shoppingListService->removeCustomerShoppingList(
  134.                 $data['id'],
  135.                 [$data],
  136.                 $salesChannelContext
  137.             );
  138.         } catch (\Exception $e) {
  139.             throw $e;
  140.         }
  141.         return new JsonResponse(['success' => true]);
  142.     }
  143.     /**
  144.      * @Route(
  145.      *     "/shoppinglist/remove-product",
  146.      *     name="gbmed.shopping_list.remove.product",
  147.      *     methods={"POST"},
  148.      *     defaults={"XmlHttpRequest"=true,"csrf_protected"=true, "_loginRequired"=true},
  149.      *     options={"seo"="false"}
  150.      * )
  151.      * @param Request $request
  152.      * @param SalesChannelContext $salesChannelContext
  153.      * @return JsonResponse
  154.      * @throws \Exception
  155.      */
  156.     public function removeShoppingListProduct(Request $requestSalesChannelContext $salesChannelContext): JsonResponse
  157.     {
  158.         $id $request->get('id');
  159.         try {
  160.             $this->shoppingListService->deleteCustomerProductItem($id$salesChannelContext);
  161.         } catch (\Exception $e) {
  162.             throw $e;
  163.         }
  164.         return new JsonResponse(['success' => true]);
  165.     }
  166.     /**
  167.      * @Route(
  168.      *     "/shoppinglist/push-product",
  169.      *     name="gbmed.shopping_list.push.product",
  170.      *     methods={"POST"},
  171.      *     defaults={"XmlHttpRequest"=true,"csrf_protected"=true, "_loginRequired"=true},
  172.      *     options={"seo"="false"}
  173.      * )
  174.      * @param Request $request
  175.      * @param SalesChannelContext $salesChannelContext
  176.      * @return JsonResponse
  177.      * @throws \Exception
  178.      */
  179.     public function pushShoppingListProduct(Request $requestSalesChannelContext $salesChannelContext): JsonResponse
  180.     {
  181.         $productId $request->get('productId');
  182.         $quantity = (int)$request->get('quantity'1);
  183.         $existList $request->get('existList');
  184.         $newList $request->get('newList');
  185.         try {
  186.             if ($existList && $productId) {
  187.                 $data = [
  188.                     'gbmedShoppingListId' => $existList,
  189.                     'gbmedShoppingListProductId' => Uuid::randomHex(),
  190.                     'productId' => $productId,
  191.                     'quantity' => $quantity
  192.                 ];
  193.                 $this->shoppingListService->createCustomerProductItem($data$salesChannelContext);
  194.             } else {
  195.                 if (trim($newList) && $productId) {
  196.                     $data = [
  197.                         'gbmedShoppingListId' => Uuid::randomHex(),
  198.                         'customerId' => $salesChannelContext->getCustomer()->getId(),
  199.                         'name' => $newList,
  200.                         'position' => 0,
  201.                         'gbmedShoppingListProductId' => Uuid::randomHex(),
  202.                         'productId' => $productId,
  203.                         'quantity' => $quantity
  204.                     ];
  205.                     $this->shoppingListService->createCustomerProductItem($data$salesChannelContext);
  206.                 }
  207.             }
  208.         } catch (\Exception $e) {
  209.             throw $e;
  210.         }
  211.         return new JsonResponse(['success' => true]);
  212.     }
  213.     /**
  214.      * @Route(
  215.      *     "/shoppinglist/push-cart",
  216.      *     name="gbmed.shopping_list.push.cart",
  217.      *     methods={"POST"},
  218.      *     defaults={"XmlHttpRequest"=true,"csrf_protected"=true, "_loginRequired"=true},
  219.      *     options={"seo"="false"}
  220.      * )
  221.      * @param Request $request
  222.      * @param SalesChannelContext $salesChannelContext
  223.      * @return JsonResponse
  224.      * @throws \Exception
  225.      */
  226.     public function pushShoppingListCart(Request $requestSalesChannelContext $salesChannelContext): JsonResponse
  227.     {
  228.         $cart $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
  229.         $existList $request->get('existList');
  230.         $newList $request->get('newList');
  231.         $data = [
  232.             'gbmedShoppingListId' => $existList,
  233.             'customerId' => $salesChannelContext->getCustomer()->getId(),
  234.             'position' => 0,
  235.         ];
  236.         if (trim($newList)) {
  237.             $data array_merge($data, [
  238.                 'gbmedShoppingListId' => Uuid::randomHex(),
  239.                 'name' => $newList,
  240.             ]);
  241.         }
  242.         try {
  243.             foreach ($cart->getLineItems() as $lineItem) {
  244.                 $data['gbmedShoppingListProductId'] = Uuid::randomHex();
  245.                 $data['productId'] = $lineItem->getId();
  246.                 $data['quantity'] = $lineItem->getQuantity();
  247.                 $this->shoppingListService->createCustomerProductItem($data$salesChannelContext);
  248.             }
  249.         } catch (\Exception $e) {
  250.             throw $e;
  251.         }
  252.         return new JsonResponse(['success' => true]);
  253.     }
  254.     /**
  255.      * @Route("/shoppinglist/line-item/add",
  256.      *     name="gbmed.shopping_list.line-item.add",
  257.      *     methods={"POST"},
  258.      *     defaults={"XmlHttpRequest"=true,"csrf_protected"=true, "_loginRequired"=true},
  259.      *     options={"seo"="false"}
  260.      * )
  261.      */
  262.     public function addLineItems(Cart $cartRequestDataBag $requestDataBagRequest $requestSalesChannelContext $context): Response
  263.     {
  264.         /** @var RequestDataBag|null $lineItems */
  265.         $lineItems $requestDataBag->get('lineItems');
  266.         if ($lineItems) {
  267.             try {
  268.                 $items = [];
  269.                 /** @var RequestDataBag $lineItemData */
  270.                 foreach ($lineItems as $lineItemData) {
  271.                     if($lineItemData->getInt('quantity'0) < 1){
  272.                         continue;
  273.                     }
  274.                     $lineItem = new LineItem(
  275.                         $lineItemData->getAlnum('id'),
  276.                         $lineItemData->getAlnum('type'),
  277.                         $lineItemData->get('referencedId'),
  278.                         $lineItemData->getInt('quantity'1)
  279.                     );
  280.                     $lineItem->setStackable($lineItemData->getBoolean('stackable'true));
  281.                     $lineItem->setRemovable($lineItemData->getBoolean('removable'true));
  282.                     $items[] = $lineItem;
  283.                 }
  284.                 $cart $this->cartService->add($cart$items$context);
  285.             } catch (ProductNotFoundException $exception) {
  286.                 throw $exception;
  287.             }
  288.         }
  289.         return $this->createActionResponse($request);
  290.     }
  291.     private function traceErrors(Cart $cart): bool
  292.     {
  293.         if ($cart->getErrors()->count() <= 0) {
  294.             return false;
  295.         }
  296.         $this->addCartErrors($cart, function (Error $error) {
  297.             return $error->isPersistent();
  298.         });
  299.         return true;
  300.     }
  301. }