custom/plugins/PixupWishlistSW6/src/Controller/PixupWishlistController.php line 302

Open in your IDE?
  1. <?php
  2. namespace Pixup\Wishlist\Controller;
  3. use Pixup\Wishlist\Core\Boot;
  4. use Pixup\Wishlist\Entitys\Model\WishlistModel;
  5. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  11. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Storefront\Controller\StorefrontController;
  15. use Shopware\Storefront\Page\Product\Configurator\ProductPageConfiguratorLoader;
  16. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoader;
  17. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoader;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. /**
  21.  * @RouteScope(scopes={"storefront"})
  22.  */
  23. class PixupWishlistController extends StorefrontController
  24. {
  25.     /**
  26.      * @var Boot
  27.      */
  28.     private $boot;
  29.     /**
  30.      * @var array
  31.      */
  32.     private $config;
  33.     /**
  34.      * @var ProductListingLoader $productLoader
  35.      */
  36.     private $productLoader;
  37.     /**
  38.      * @var ProductPageConfiguratorLoader
  39.      */
  40.     private $productConfiguratorServiceLoader;
  41.     private $salesChannelId;
  42.     /**
  43.      * @var PixupWishlistAjaxController
  44.      */
  45.     private $ajaxController;
  46.     /**
  47.      * @var HeaderPageletLoader
  48.      */
  49.     private $headerPageletLoader;
  50.     /**
  51.      * @var FooterPageletLoader
  52.      */
  53.     private $footerPageletLoader;
  54.     use PixupWishlistControllerTrait;
  55.     public function __construct(
  56.         Boot $boot,
  57.         ProductListingLoader $productLoader,
  58.         ProductPageConfiguratorLoader $productConfiguratorServiceLoader,
  59.         PixupWishlistAjaxController $ajaxController,
  60.         HeaderPageletLoader $headerPageletLoader,
  61.         FooterPageletLoader $footerPageletLoader
  62.     )
  63.     {
  64.         $this->boot $boot;
  65.         $this->productLoader $productLoader;
  66.         $this->productConfiguratorServiceLoader $productConfiguratorServiceLoader;
  67.         $this->headerPageletLoader $headerPageletLoader;
  68.         $this->footerPageletLoader $footerPageletLoader;
  69.         $this->wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  70.         $this->wishListCookieHandler $this->boot->getFacade()->getCookieHandler();
  71.         $this->ajaxController $ajaxController;
  72.     }
  73.     /**
  74.      * @Route(
  75.      *     "/widgets/pixup/wishlist-products/{returnProducts}",
  76.      *     name="frontend.pixup.wishlist.products",
  77.      *     options={"seo"="false"},
  78.      *     methods={"GET"},
  79.      *     defaults={"_format"="json"}
  80.      * )
  81.      * */
  82.     public function ajax_wishlistProducts(SalesChannelContext $contextRequest $requestbool $returnProducts false){
  83.         return $this->ajaxController->createAjaxResponse([
  84.             'wishlists'=>$this->getWishlists($context'','',$returnProducts)
  85.         ]);
  86.     }
  87.     /**
  88.      * @Route(
  89.      *     "/pixup/wishlist/ajax/add-product/{productId}/{wishlistId?null}",
  90.      *     name="frontend.pixup.wishlist.ajax.add_product",
  91.      *     options={"seo"="false"},
  92.      *     methods={"GET"},
  93.      *     defaults={"_format"="json"}
  94.      * )
  95.      *\\description: if wishlistId = -1 a new wishlist will be added
  96.      */
  97.     public function ajax_addProduct(string $productId,string $wishlistIdSalesChannelContext $contextRequest $request)
  98.     {
  99.         return $this->ajaxController->addProduct($productId,$wishlistId,$context,$request);
  100.     }
  101.     /**
  102.      * @Route(
  103.      *     "/pixup/wishlist/ajax/remove-product/{productId}/{wishlistId?null}",
  104.      *     name="frontend.pixup.wishlist.ajax.remove_product",
  105.      *     options={"seo"="false"},
  106.      *     methods={"GET"},
  107.      *     defaults={"_format"="json"}
  108.      * )
  109.      ** description: if wishlistId = -1 a new wishlist will be added
  110.      */
  111.     public function ajax_removeProduct(string $productId,string $wishlistIdSalesChannelContext $contextRequest $request){
  112.         return $this->ajaxController->removeProduct($productId,$wishlistId,$context,$request);
  113.     }
  114.     /**
  115.      * @Route(
  116.      *     "/pixup/wishlist/ajax/get-product-state/{$productId}",
  117.      *     name="frontend.pixup.wishlist.ajax.get_product_state",
  118.      *     options={"seo"="false"},
  119.      *     methods={"GET"},
  120.      *     defaults={"_format"="json"}
  121.      * )
  122.      */
  123.     public function ajax_getProductState(string $productId,SalesChannelContext $contextRequest $request){
  124.         return $this->ajaxController->getProductState($productId,$context,$request);
  125.     }
  126.     /**
  127.      * @Route(
  128.      *     "/pixup/wishlist/ajax/get-product-variant/{parentId}/{options}/{wishedGroupId}",
  129.      *     name="frontend.pixup.wishlist.ajax.get_product_variant",
  130.      *     methods={"GET"},
  131.      *     defaults={"XmlHttpRequest": true}
  132.      * )
  133.      ** description: takes the parentId and the options array to return the productId of an Variant
  134.      ** options = [groupID1=>optionID1,groupID2=>optionID2]
  135.      */
  136.     public function ajax_WishlistSwitchVariant(
  137.         string $parentId,
  138.         string $options,
  139.         string $wishedGroupId,
  140.         Request $request,
  141.         SalesChannelContext $salesChannelContext
  142.     ){
  143.         return $this->ajaxController->switchVariant($parentId,$options,$wishedGroupId,$request,$salesChannelContext);
  144.     }
  145.     /**
  146.      * @Route(
  147.      *     "pixup/wishlist/ajax/replace-from-wishlist/{oldId}/{newId}/{wishListId}",
  148.      *     name="frontend.pixup.wishlist.ajax.replace_from_wishlist",
  149.      *     options={"seo"="false"},
  150.      *     methods={"GET"},
  151.      *     defaults={"_format"="html"}
  152.      * )
  153.      ** replaces the oldProductId with the newProductId ( based on the $wishlistId )
  154.      */
  155.     public function ajax_WishlistReplaceProduct(
  156.         string $oldId,
  157.         string $newId,
  158.         string $wishListId,
  159.         Request $request,
  160.         SalesChannelContext $context
  161.     ){
  162.         return $this->ajaxController->replaceProduct($oldId,$newId,$wishListId,$request,$context);
  163.     }
  164.     /**
  165.      * @Route(
  166.      *     "/pixup/wishlist/ajax/get-wishlist/{productId?null}/{wishlistId?null}/{returnView?null}/{returnProducts?false}",
  167.      *     name="frontend.pixup.wishlist.ajax.get_wishlists",
  168.      *     options={"seo"="false"},
  169.      *     methods={"GET"},
  170.      *     defaults={"_format"="json"}
  171.      * )
  172.      ** description returns all wishlists which includes the givin productId/wishlistId ( if productId/wishlist is not set it will return any )
  173.      ** param string $productId // null or a product ID
  174.      ** param string $wishlistId // null or wishlist ID
  175.      *  param returnView // null or bool -- will return a view from the product-collection ( all products from a wishlist )
  176.      ** param SalesChannelContext $context
  177.      ** param Request $request
  178.      */
  179.     public function ajax_getWishlist(string $productId,string $wishlistId,string $returnView,string $returnProductsSalesChannelContext $contextRequest $request){
  180.         $productId = ($productId==='null')?"":$productId;
  181.         $wishlistId = ($wishlistId==='null')?"":$wishlistId;
  182.         $returnView = ($returnView=='null' || $returnView=='false')?false:true;
  183.         $returnProducts = ($returnProducts == "true")?true:false;
  184.         if($returnView){
  185.             $wishlists $this->getWishlists($context,$productId,$wishlistId);
  186.             $wishlistsPayload $this->ajaxController->addAdvancedProductInformationToWishlist($wishlists,$context,true);
  187.             return $this->ajaxController->createAjaxResponse([
  188.                 'wishlists'=>$wishlists,
  189.                 'view'=>$this->renderStorefront('@Storefront/wishlist/box/product-collection-view.html.twig',[
  190.                     'wishlists'=>$wishlistsPayload
  191.                 ])->getContent(),
  192.             ]);
  193.         }
  194.         return $this->ajaxController->createAjaxResponse([
  195.             'wishlists'=>$this->getWishlists($context,$productId,$wishlistId,$returnProducts)
  196.         ]);
  197.     }
  198.     /**
  199.      * @Route(
  200.      *     "/pixup/wishlist/ajax/create-wishlist/{name}/{products?null}/{private?string}/{editable?string}/{birthday?string}/{password?null}/{wishListId?null}",
  201.      *     name="frontend.pixup.wishlist.ajax.create_wishlist",
  202.      *     options={"seo"="false"},
  203.      *     methods={"GET"},
  204.      *     defaults={"_format"="json"}
  205.      * )
  206.      */
  207.     public function ajax_createWishlist(string $name,string $products,string $private,
  208.                                         string $editable,string $birthday,string $password,string $wishListId,
  209.                                         SalesChannelContext $context,Request $request
  210.     ){
  211.         return $this->ajaxController->createWishlist($name,$products,$private,$editable,$birthday,$password,$wishListId,$context,$request);
  212.     }
  213.     /**
  214.      * @Route(
  215.      *     "/pixup/wishlist/ajax/edit-wishlist/{name}/{wishListId}/{private?string}/{editable?string}/{birthday?string}/{password?null}",
  216.      *     name="frontend.pixup.wishlist.ajax.edit_wishlist",
  217.      *     options={"seo"="false"},
  218.      *     methods={"GET"},
  219.      *     defaults={"_format"="json"}
  220.      * )
  221.      */
  222.     public function ajax_editWishlist(string $name,string $wishListId,string $private,
  223.                                       string $editable,string $birthday,string $password,
  224.                                       SalesChannelContext $context,Request $request){
  225.         return $this->ajaxController->editWIshlist($name,$wishListId,$private,$editable,$birthday,$password,$context,$request);
  226.     }
  227.     /**
  228.      * @Route(
  229.      *     "/pixup/wishlist/ajax/delete-wishlist/{wishlistId}",
  230.      *     name="frontend.pixup.wishlist.ajax.delete_wishlist",
  231.      *     options={"seo"="false"},
  232.      *     methods={"GET"},
  233.      *     defaults={"_format"="json"}
  234.      * )
  235.      */
  236.     public function ajax_removeWishlist(string $wishlistIdSalesChannelContext $context,Request $request){
  237.        return $this->ajaxController->removeWishlist($wishlistId,$context,$request);
  238.     }
  239.     /**
  240.      **param string $productId
  241.      **param SalesChannelContext $context
  242.      **description returns all wishlists which includes the givin productId ( if productId is not set it will return any )
  243.      * Based on the SalesChannelId and customerID (SalesChannelContext)
  244.      **return array
  245.      */
  246.     private function getWishlists(SalesChannelContext $context,string $productId "",string $wishlistId="",bool $returnProducts false) :array{
  247.         $this->prePatch($context);
  248.         if($this->customerId == null)
  249.             return [];
  250.         $this->ajaxController->customerId $this->customerId;
  251.         $formatedWishlistResponse =  $this->ajaxController->formatWishlistResponseArray(
  252.             $this->wishListEntityHandler->getWishlists(
  253.             $this->salesChannelId,
  254.             $this->customerId,
  255.             (!empty($productId))?$productId:'',
  256.             '',
  257.             $wishlistId,
  258.             true,
  259.             !$returnProducts
  260.             )
  261.         );
  262.         if($returnProducts) {
  263.             $formatedWishlistResponse $this->ajaxController->addInfoToProduct($formatedWishlistResponse$context,true);
  264.         }
  265.         return $formatedWishlistResponse;
  266.     }
  267.     /**
  268.      * @Route(
  269.      *     "/pixup/wishlist/{wishlistId?null}/{password?null}",
  270.      *     name="frontend.pixup.wishlist.overview",
  271.      *     options={"seo"="false"},
  272.      *     methods={"GET"},
  273.      *     defaults={"_format"="html"}
  274.      * )
  275.      */
  276.     public function WishlistOverview(string $wishlistId,string $password,SalesChannelContext $context,Request $request){
  277.         $wishlistId = ($wishlistId=='null')?null:$wishlistId;
  278.         $this->prePatch($context);
  279.         $message "";
  280.         $password = ($password=='null')?null:$password;
  281.         $subscribedToWishlist false;
  282.         //check if passed wishlistId is own wishlistId
  283.         if($wishlistId !== null) {
  284.             if($this->customerId == null){
  285.                 $res $this->subscribeToWishlist($wishlistId,$context,$password);
  286.             }else {
  287.                 $ownWishList $this->wishListEntityHandler->checkForOwnWishlist($wishlistId$this->customerId,true);
  288.                 if(!$ownWishList)
  289.                     $res $this->subscribeToWishlist($wishlistId,$context,$password);
  290.             }
  291.             if(isset($res))
  292.                 $message $res['message'];
  293.             else
  294.                 $res = [];
  295.             if(isset($res['success']) && $res['success'] == true) {
  296.                 $subscribedToWishlist true;
  297.             }
  298.         }
  299.         $wishlists $this->getWishlists($context,'');
  300.         $isLoggedIn = ($context->getCustomer()==null)?false:true;
  301.         if(!$this->boot->getConfig($context->getSalesChannelId())['fullAjaxMode']) {
  302.             $wishlists $this->ajaxController->addAdvancedProductInformationToWishlist($wishlists$contexttrue);
  303.         }else{
  304.             if(empty($wishlistId)){
  305.                 if(isset($wishlists[0]))
  306.                     $wishlistId $wishlists[0]['id'];
  307.             }
  308.         }
  309.         if($wishlistId !== null) {
  310.             $publicWishlists $this->wishListEntityHandler->getPublicWishlistById($wishlistId$context->getSalesChannel()->getId(), $this->customerId);
  311.             if ($publicWishlists->first() !== null) {
  312.                 //check for password
  313.                 /**
  314.                  * @var WishlistModel $pWishlist
  315.                  */
  316.                 $pWishlist $publicWishlists->first();
  317.                 if ($pWishlist->getPassword() !== null)
  318.                     if ($pWishlist->getPassword() !== $password)
  319.                         return $this->renderStorefront('@Storefront/storefront/page/wishlist/enterPassword.html.twig',[
  320.                             'wishlistId'=>$wishlistId,
  321.                             "page" => [
  322.                                 "header"=>$this->headerPageletLoader->load($request,$context),
  323.                                 'footer'=>$this->footerPageletLoader->load($request,$context)
  324.                             ]
  325.                         ]);
  326.                 $publicWishlists $this->ajaxController->formatWishlistResponseArray($publicWishliststrue);
  327.                 $publicWishlists $this->ajaxController->addAdvancedProductInformationToWishlist($publicWishlists$context,true);
  328.             }
  329.         }
  330.         if(empty($publicWishlists))
  331.             $publicWishlists = [];
  332.         return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig',[
  333.             "wishlists"=>$wishlists,
  334.             'isLoggedIn' => $isLoggedIn,
  335.             'wishlistCanBePublic' => $this->boot->getConfig($context->getSalesChannelId())['wishlistCanBePublic'],
  336.             'wishlistBirthday'=> $this->boot->getConfig($context->getSalesChannelId())['wishlistCanBeBirthday'],
  337.             'wishlistCanBePrinted'=> $this->boot->getConfig($context->getSalesChannelId())['printWishlist'],
  338.             'wishlistId' => $wishlistId,
  339.             'message' => $message,
  340.             'subscribedToWishlist' => $subscribedToWishlist,
  341.             'publicWishlist' => $publicWishlists,
  342.             'fullAjaxMode' => $this->boot->getConfig($context->getSalesChannelId())['fullAjaxMode'],
  343.             "page" => [
  344.                 "header"=>$this->headerPageletLoader->load($request,$context),
  345.                 "footer"=>$this->footerPageletLoader->load($request,$context),
  346.                 "metaInformation"=>[
  347.                     "metaTitle"=>"Wishlist",
  348.                     "metaDescription"=>"Wishlist",
  349.                     "robots"=>"noindex,nofollow"
  350.                 ]
  351.             ]
  352.         ]);
  353.     }
  354.     private function subscribeToWishlist(string $wishlistId,SalesChannelContext $context,$password null):array{
  355.         $isLoggedIn = ($context->getCustomer()==null)?false:true;
  356.         $responseArr = ['success'=>true,'message'=>''];
  357.         if(!$isLoggedIn){
  358.             if(!$this->boot->getConfig($context->getSalesChannelId())['cookieUserCanSubscribe']) {
  359.                 $responseArr['success'] = false;
  360.                 $responseArr['message'] = 8;
  361.                 return $responseArr;
  362.             }
  363.         }
  364.         $this->prePatch($context);
  365.         return $this->wishListEntityHandler->subscribeToWishlist($wishlistId,$context->getSalesChannel()->getId(),$this->customerId,$password);
  366.     }
  367.     /**
  368.      * @Route(
  369.      *     "/wishlist/productBox/{productId}/{index}/{wishlistId}",
  370.      *     name="frontend.pixup.wishlist.product_box",
  371.      *     options={"seo"="false"},
  372.      *     methods={"GET"},
  373.      *     defaults={"_format"="html"}
  374.      * )
  375.      ** this returns a productBox to load it dynamic inside of the wishlist
  376.      */
  377.     public function WishlistLoadProductBox(
  378.         string $productId,
  379.         string $index,
  380.         string $wishlistId,
  381.         Request $request,
  382.         SalesChannelContext $context
  383.     ){
  384.         $product $this->productLoader->load((new Criteria([$productId]))// add options.id filter so the productloader loads the product even if the display is bound to specific variant
  385.             ->addPostFilter(new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter('options.id',Uuid::randomHex())]))
  386.             ,$context)->first();
  387.         $configuratorSettings $this->productConfiguratorServiceLoader->load($product,$context);
  388.         /**
  389.          * @var EntityCollection $wishlist
  390.          */
  391.         $wishlist $this->wishListEntityHandler->getWishlistById($wishlistId);
  392.         $wishlist $this->ajaxController->formatWishlistResponseArray($wishlist)[0];
  393.         if($wishlist['isOwnWishlist'] && !$wishlist['isOwnWishlist'] && !$wishlist['editable']){
  394.             $delete false;
  395.             $move false;
  396.         }else{
  397.             $delete true;
  398.             $move true;
  399.         }
  400.         return $this->renderStorefront('@Storefront/wishlist/box/product-prev.html.twig',[
  401.                     'product'=>$product,
  402.                     'configuratorSettings' => $configuratorSettings,
  403.                     'index' => $index,
  404.                     'wishlistId' => $wishlistId,
  405.                     'external' => false,
  406.                     'delete'=>$delete,
  407.                     'move'=>$move,
  408.         ]);
  409.     }
  410. }