<?php
namespace App\Ecommerce\Controller;
use App\Service\WishListService;
use CoreShop\Bundle\FrontendBundle\Controller\FrontendController;
use CoreShop\Bundle\FrontendBundle\TemplateConfigurator\TemplateConfiguratorInterface;
use Exception;
use Pimcore\Model\DataObject\CoreShopCustomer;
use Pimcore\Model\DataObject\CoreShopProduct;
use Pimcore\Twig\Extension\Templating\Placeholder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class WishListController extends FrontendController
{
protected TemplateConfiguratorInterface $templateConfigurator;
private function errorResponse(): JsonResponse
{
return new JsonResponse(['success' => false]);
}
public function setTemplateConfigurator(TemplateConfiguratorInterface $templateConfigurator): void
{
$this->templateConfigurator = $templateConfigurator;
}
/**
* @param Request $request
* @param WishListService $wishListService
* @return Response
* @Route("/add_wishlist_item", name="wishlist_add_item", methods={"POST"})
*/
public function addToWishlistAction(Request $request, WishListService $wishListService): Response
{
$this->denyAccessUnlessGranted('CORESHOP_WISHLIST');
$this->denyAccessUnlessGranted('CORESHOP_WISHLIST_ADD_ITEM');
$product = CoreShopProduct::getById($request->get('id'));
if (!$product instanceof CoreShopProduct) {
return $this->errorResponse();
}
if ($wishlisted = !$wishListService->hasItem($product)) {
try {
$wishListService->addItem($product);
} catch (Exception) {
return $this->errorResponse();
}
$icon = '_wishlisted';
} else {
try {
$wishListService->removeItem($product);
} catch (Exception $e) {
return $this->errorResponse();
}
$icon = '_addToWishlist';
}
return new JsonResponse([
'success' => true,
'icon' => $this->renderView($this->templateConfigurator->findTemplate("Wishlist/$icon.html")),
'wishlisted' => $wishlisted,
'widget' => $this->wishlistWidgetAction($wishListService)->getContent(),
]);
}
/**
* @param Request $request
* @return Response
* @Route("/remove_wishlist_item", name="wishlist_remove_item", methods={"POST"})
*/
public function removeFromWishlistAction(Request $request, WishListService $wishListService): Response
{
$this->denyAccessUnlessGranted('CORESHOP_WISHLIST');
$this->denyAccessUnlessGranted('CORESHOP_WISHLIST_REMOVE_ITEM');
$product = CoreShopProduct::getById($request->get('id'));
if (!$product instanceof CoreShopProduct) {
return $this->errorResponse();
}
if ($wishListService->hasItem($product)) {
try {
$wishListService->removeItem($product);
} catch (Exception) {
return $this->errorResponse();
}
}
return new JsonResponse([
'success' => true,
'items' => $this->wishListItemsAction($wishListService),
'message' => 'Sikeresen eltávolítva!',
'widget' => $this->wishlistWidgetAction($wishListService)->getContent()
]);
}
public function wishlistWidgetAction(WishListService $wishListService): Response
{
return $this->render($this->templateConfigurator->findTemplate('Wishlist/_widget.html'), [
'count' => $wishListService->count()
]);
}
public function wishListAction(Request $request, Placeholder $placeholder, WishListService $wishListService): Response
{
$this->denyAccessUnlessGranted('CORESHOP_WISHLIST');
$this->denyAccessUnlessGranted('CORESHOP_WISHLIST_SUMMARY');
$bc = [['title' => 'Főoldal', 'path' => '/'], ['title' => 'Kedvencek', 'path' => null]];
$placeholder('breadcrumbNews')->set($bc);
return $this->render($this->templateConfigurator->findTemplate('Wishlist/wishlist.html'), [
'wishlist' => $this->wishListItemsAction($wishListService)
]);
}
public function wishListItemsAction(WishListService $wishListService): string
{
return $this->renderView($this->templateConfigurator->findTemplate('Wishlist/_wishlist_items.html'), [
'items' => $wishListService->count() ? $wishListService->getList() : []
]);
}
}