src/Ecommerce/Controller/WishListController.php line 117

Open in your IDE?
  1. <?php
  2. namespace App\Ecommerce\Controller;
  3. use App\Service\WishListService;
  4. use CoreShop\Bundle\FrontendBundle\Controller\FrontendController;
  5. use CoreShop\Bundle\FrontendBundle\TemplateConfigurator\TemplateConfiguratorInterface;
  6. use Exception;
  7. use Pimcore\Model\DataObject\CoreShopCustomer;
  8. use Pimcore\Model\DataObject\CoreShopProduct;
  9. use Pimcore\Twig\Extension\Templating\Placeholder;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class WishListController extends FrontendController
  15. {
  16.     protected TemplateConfiguratorInterface $templateConfigurator;
  17.     private function errorResponse(): JsonResponse
  18.     {
  19.         return new JsonResponse(['success' => false]);
  20.     }
  21.     public function setTemplateConfigurator(TemplateConfiguratorInterface $templateConfigurator): void
  22.     {
  23.         $this->templateConfigurator $templateConfigurator;
  24.     }
  25.     /**
  26.      * @param Request $request
  27.      * @param WishListService $wishListService
  28.      * @return Response
  29.      * @Route("/add_wishlist_item", name="wishlist_add_item", methods={"POST"})
  30.      */
  31.     public function addToWishlistAction(Request $requestWishListService $wishListService): Response
  32.     {
  33.         $this->denyAccessUnlessGranted('CORESHOP_WISHLIST');
  34.         $this->denyAccessUnlessGranted('CORESHOP_WISHLIST_ADD_ITEM');
  35.         $product CoreShopProduct::getById($request->get('id'));
  36.         if (!$product instanceof CoreShopProduct) {
  37.             return $this->errorResponse();
  38.         }
  39.         if ($wishlisted = !$wishListService->hasItem($product)) {
  40.             try {
  41.                 $wishListService->addItem($product);
  42.             } catch (Exception) {
  43.                 return $this->errorResponse();
  44.             }
  45.             $icon '_wishlisted';
  46.         } else {
  47.             try {
  48.                 $wishListService->removeItem($product);
  49.             } catch (Exception $e) {
  50.                 return $this->errorResponse();
  51.             }
  52.             $icon '_addToWishlist';
  53.         }
  54.         return new JsonResponse([
  55.             'success' => true,
  56.             'icon' => $this->renderView($this->templateConfigurator->findTemplate("Wishlist/$icon.html")),
  57.             'wishlisted' => $wishlisted,
  58.             'widget' => $this->wishlistWidgetAction($wishListService)->getContent(),
  59.         ]);
  60.     }
  61.     /**
  62.      * @param Request $request
  63.      * @return Response
  64.      * @Route("/remove_wishlist_item", name="wishlist_remove_item", methods={"POST"})
  65.      */
  66.     public function removeFromWishlistAction(Request $requestWishListService $wishListService): Response
  67.     {
  68.         $this->denyAccessUnlessGranted('CORESHOP_WISHLIST');
  69.         $this->denyAccessUnlessGranted('CORESHOP_WISHLIST_REMOVE_ITEM');
  70.         $product CoreShopProduct::getById($request->get('id'));
  71.         if (!$product instanceof CoreShopProduct) {
  72.             return $this->errorResponse();
  73.         }
  74.         if ($wishListService->hasItem($product)) {
  75.             try {
  76.                 $wishListService->removeItem($product);
  77.             } catch (Exception) {
  78.                 return $this->errorResponse();
  79.             }
  80.         }
  81.         return new JsonResponse([
  82.             'success' => true,
  83.             'items' => $this->wishListItemsAction($wishListService),
  84.             'message' => 'Sikeresen eltávolítva!',
  85.             'widget' => $this->wishlistWidgetAction($wishListService)->getContent()
  86.         ]);
  87.     }
  88.     public function wishlistWidgetAction(WishListService $wishListService): Response
  89.     {
  90.         return $this->render($this->templateConfigurator->findTemplate('Wishlist/_widget.html'), [
  91.             'count' => $wishListService->count()
  92.         ]);
  93.     }
  94.     public function wishListAction(Request $requestPlaceholder $placeholderWishListService $wishListService): Response
  95.     {
  96.         $this->denyAccessUnlessGranted('CORESHOP_WISHLIST');
  97.         $this->denyAccessUnlessGranted('CORESHOP_WISHLIST_SUMMARY');
  98.         $bc = [['title' => 'Főoldal''path' => '/'], ['title' => 'Kedvencek''path' => null]];
  99.         $placeholder('breadcrumbNews')->set($bc);
  100.         return $this->render($this->templateConfigurator->findTemplate('Wishlist/wishlist.html'), [
  101.             'wishlist' => $this->wishListItemsAction($wishListService)
  102.         ]);
  103.     }
  104.     public function wishListItemsAction(WishListService $wishListService): string
  105.     {
  106.         return $this->renderView($this->templateConfigurator->findTemplate('Wishlist/_wishlist_items.html'), [
  107.             'items' => $wishListService->count() ? $wishListService->getList() : []
  108.         ]);
  109.     }
  110. }