src/Controller/Products/categoryController.php line 146

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Products;
  3. use Pimcore\Controller\FrontendController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Pimcore\Localization\LocaleServiceInterface;
  8. use Pimcore\Model\DataObject\ClassDefinition\Service;
  9. use Pimcore\Model\DataObject\Category;
  10. use Pimcore\Model\DataObject\Products;
  11. use Pimcore\Model\DataObject\Brands;
  12. use App\Model\Product\AbstractProduct;
  13. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  14. use Pimcore\Model\DataObject\Userloyaltyprogram;
  15. use Pimcore\Model\DataObject\Slider;
  16. use Pimcore\Model\DataObject\Recipe;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use Pimcore\Model\DataObject\FilterDefinition;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  20. use Pimcore\Config;
  21. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
  22. use Knp\Component\Pager\PaginatorInterface;
  23. class categoryController extends FrontendController
  24. {
  25.     /**
  26.         * @Route("/{locale}/products/{url?}", name="products")
  27.     */
  28.     public function categoryProductAction(Request $request TranslatorInterface $translator,Factory $ecommerceFactory,ListHelper $listHelperPaginatorInterface $paginator){
  29.                 
  30.         $get_param $request->query->all();
  31.         $params array_merge($request->query->all(), $request->attributes->all());
  32.        
  33.         $category_ids   = isset($params["categories"])?$params["categories"]:"";
  34.         $brand_ids      = isset($params["brands"])?$params["brands"]:"";
  35.         $subCategoryIds      = isset($params["subCategory"])?$params["subCategory"]:"";
  36.         $query          = isset($params["query"])?$params["query"]:"";
  37.         $cuisine_id     = isset($params["cuisine_id"])?$params["cuisine_id"]:"";
  38.         $meal_type   = isset($params["meal_type"])?$params["meal_type"]:"";
  39.         $childList = array();
  40.         $brand null;
  41.         $url $request->get('url');
  42.         if(!empty($request->get('url')) && empty($get_param)){
  43.             $brand = (string)$this->getBrandId($request->get('url'));
  44.         }
  45.         $catId null;
  46.         if(!empty($request->get('url')) && empty($get_param)){
  47.             $catId = (string)$this->getCategoryId($request->get('url'));
  48.         }
  49.         $indexService $ecommerceFactory->getIndexService();
  50.         $productListing $indexService->getProductListForCurrentTenant();  
  51.         $productListing->addCondition("o_parentId != 74");    
  52.         if($query){
  53.             $productListing->addCondition("name LIKE '%{$query}%'"); 
  54.         }        
  55.         if(!empty($request->get('url')) && !empty($brand)){
  56.             $brand_ids explode(",",$brand);
  57.             $params["brands"] = $brand_ids;
  58.         }
  59.         if(!empty($request->get('url')) && !empty($catId)){      
  60.             $category_ids explode(",",$catId);
  61.             $params["categories"] = $category_ids;
  62.         }
  63.         $productListing->setOrderKey('name');
  64.         $productListing->setOrder('asc');
  65.         $page $request->get('page');
  66.         if($page){
  67.             $page $page+1;
  68.         }else{
  69.             $page 1;
  70.         }          
  71.         if ($request->get('filterdefinition') instanceof FilterDefinition) {
  72.             $filterDefinition $request->get('filterdefinition');
  73.         }
  74.         if (empty($filterDefinition)) {
  75.             $filterDefinition Config::getWebsiteConfig()->get('fallbackFilterdefinition');
  76.         }        
  77.         $filterService $ecommerceFactory->getFilterService();
  78. (new \Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper)->setupProductList($filterDefinition$productListing$params$filterServicetruetrue);
  79.        
  80.         $paginator $paginator->paginate(
  81.             $productListing,
  82.             $page,
  83.             24
  84.         ); 
  85.         
  86.        // p_r();
  87.                  
  88.         foreach ($paginator as $product) {            
  89.             if($product->getChildren() ){
  90.                 $parent_id $product->getParentId();
  91.                 if($parent_id 74){
  92.                     $childList[] = $product->getChildren();
  93.                 }
  94.             }else{
  95.                 $childList[] = $product;
  96.             }            
  97.         }
  98.         
  99.         $categories $this->getCategories();
  100.         $brands $this->getBrands();
  101.         // filters
  102.         // dd($brand_ids);
  103.         $filters = array(
  104.             'category_ids'      => $category_ids,
  105.             'brand_ids'         => $brand_ids,
  106.             'subCategory'         => $subCategoryIds,
  107.             'query'             => $query,
  108.             'cuisine_id'        => $cuisine_id,
  109.             'meal_type'         => $meal_type,            
  110.         );       
  111.         $currentFilter = [            
  112.             'categoryIds' => $category_ids,
  113.             "categories" => $category_ids,
  114.             'subCategory'         => $subCategoryIds,
  115.             'brands' => $brand_ids,
  116.             'meal_type' => $meal_type
  117.         ];
  118.         // Category Sliders
  119.         $category_slider = new Slider\Listing();
  120.         $category_slider->setCondition("o_id = 205");
  121.         $category_slider->load();
  122.         if(empty($category_slider->getdata()[0]->getsliders())){
  123.             $category_slider = array();
  124.         }else{
  125.             $category_slider $category_slider->getdata()[0]->getsliders();
  126.         }
  127.         if(!empty($request->get('url'))){
  128.             $getCategoryData    $this->getCategoryDescription($request->get('url'));
  129.             $getBrandData       $this->getBrandDescription($request->get('url'));
  130.             if(!empty($getCategoryData)){
  131.                 $products_head = array(
  132.                     'id'         => $getCategoryData->getId(),
  133.                     'name'         => $getCategoryData->getName(),
  134.                     'description'  => $getCategoryData->getDescription(),
  135.                     'Detail_desc'  => $getCategoryData->getDetail_desc(),
  136.                     'breadcrumb'   => $getCategoryData->getName(),
  137.                     'slides'       => $getCategoryData->getCategory_slides(),
  138.                     'brand_logo'   => '',
  139.                     'meta_title'       => $getCategoryData->getMeta_title(),
  140.                     'meta_description'   => $getCategoryData->getMeta_description(),
  141.                 );
  142.             }else if(!empty($getBrandData)){
  143.                 $products_head = array(
  144.                     'id'           => $getBrandData->getId(),
  145.                     'name'         => $getBrandData->getName(),
  146.                     'description'  => $getBrandData->getDescription(),
  147.                     'breadcrumb'   => $getBrandData->getName(),
  148.                     'slides'       => $getBrandData->getCategory_slides(),
  149.                     'brand_logo'   => $getBrandData->getImage(),
  150.                     'meta_title'       => $getBrandData->getMeta_title(),
  151.                     'meta_description'   => $getBrandData->getMeta_description(),
  152.                 );
  153.             }
  154.             else{
  155.                 $products_head = array();
  156.             }
  157.         }else{
  158.             $products_head = array();
  159.         }
  160.         // Category Mid Banner
  161.         $mid_banner = new Slider\Listing();
  162.         $mid_banner->setCondition("o_id = 206");
  163.         $mid_banner->load();
  164.         if(empty($mid_banner->getdata()[0]->getsliders())){
  165.             $mid_banner = array();
  166.         }else{
  167.             $mid_banner $mid_banner->getdata()[0]->getsliders();
  168.         }   
  169.         // dd($childList);
  170.         if ($request->isXmlHttpRequest()) {
  171.             return $this->loadMoreProduct($childList$request ,$translator);
  172.         }
  173.         // Featured Recipes
  174.         $featured_recipes = new Recipe\Listing();
  175.         $featured_recipes->setCondition('featured IN (?)''1');
  176.         $featured_recipes->setLimit('10');
  177.         $featured_recipes->load();
  178.         //dd($childList);
  179.         $ObjectIds = [];
  180.         foreach($childList as $ChildList){
  181.             $ObjectIds[] =  $ChildList->getId();
  182.         }
  183.        // dd(count($ObjectIds));
  184.        // echo $ObjectIds;
  185.         $uid $this->get('session')->get('loginUID');
  186.         //$rid = $recipedata->getId();
  187.         $Liked = new Userloyaltyprogram\Listing();
  188.        //for($i=0; $i<count($ObjectIds); $i++){
  189.         $Liked->setCondition("op_name = ? and u_id = ? and obj_mod_name = ? and obj_data__id IN (?)",['Like',$uid,'Product',$ObjectIds]);
  190.     
  191.        
  192.       //}
  193.      
  194.       $Liked->load();
  195.       $LikedId = [];
  196.       foreach($Liked as $liked){
  197.         $LikedId[]=$liked->getObj_id();
  198.       }  
  199.      
  200.         return $this->render('Products/category.html.twig', [
  201.                 'products'          => $childList,
  202.                 'categories'        => $categories,
  203.                 'brands'            => $brands,
  204.                 'filters'           => $filters,
  205.                 'category_slider'   => $category_slider,
  206.                 'featured_recipes'  => $featured_recipes,
  207.                 'mid_banner'        => $mid_banner,
  208.                 'products_head'     => $products_head,
  209.                 'Liked'     => $Liked,
  210.                 'LikedId'     => $LikedId,
  211.                 'filterService'=> $filterService,
  212.                 'filterDefinition'=> $filterDefinition,            
  213.                 'productListing'=> $productListing,
  214.                 'currentFilter' => $currentFilter
  215.             ]);
  216.     }
  217.     public function getCategories($category_id 0){
  218.         $categories = new Category\Listing();
  219.         $categories->load();
  220.         return $categories;
  221.     }
  222.     public function getCategoryId($url ''){
  223.         $categories = new Category\Listing();
  224.         $categories->setCondition("url IN (?)", [[$url]]);
  225.         $categories->load();
  226.         foreach($categories as $category){
  227.             $category_id $category->getId();
  228.             break;
  229.         }
  230.         if(isset($category_id)){
  231.             return $category_id;
  232.         }
  233.         return '';
  234.     }
  235.     public function getBrands($brand_id 0){
  236.         $brands = new Brands\Listing();
  237.         $brands->load();
  238.         return $brands;
  239.     }
  240.     
  241.     public function getBrandId($url ''){
  242.         $brands = new Brands\Listing();
  243.         $brands->setCondition("url IN (?)", [[$url]]);
  244.         $brands->load();
  245.         foreach($brands as $brand){
  246.             $brand_id $brand->getId();
  247.             break;
  248.         }
  249.         if(isset($brand_id)){
  250.             return $brand_id;
  251.         }
  252.         return '';
  253.     }
  254.     // Get Category Description
  255.     public function getCategoryDescription($url ''){
  256.         $categories = new Category\Listing();
  257.         $categories->setCondition("url IN (?)", [[$url]]);
  258.         $categories->load();
  259.         foreach($categories as $category_data){
  260.             break;
  261.         }
  262.         if(isset($category_data)){
  263.             return $category_data;
  264.         }
  265.         return '';
  266.     }
  267.     // Get Brands
  268.     public function getBrandDescription($url ''){
  269.         $brands = new Brands\Listing();
  270.         $brands->setCondition("url IN (?)", [[$url]]);
  271.         $brands->load();
  272.         foreach($brands as $brand){
  273.             break;
  274.         }
  275.         if(isset($brand)){
  276.             return $brand;
  277.         }
  278.         return '';
  279.     }
  280.     
  281.     /**
  282.      * @Route("products/{}/loadmore", name="loadmore")
  283.     */
  284.     public function loadMoreProduct($childList$request ,$translator){
  285.         $more_product '';
  286.         if(!empty($childList)){
  287.             $total_products sizeof($childList);
  288.         }else{
  289.             $total_products '';
  290.         }
  291.             foreach($childList as $product){
  292.                 $uid $this->get('session')->get('loginUID');
  293.                 $LikedId = [];
  294.                 if($uid){
  295.                     $Liked = new Userloyaltyprogram\Listing();
  296.                     $Liked->setCondition("op_name = ? and u_id = ? and obj_mod_name = ? and obj_id IN (?)",['Like',$uid,'Product',$product->getId()]);         
  297.                     $Liked->load();
  298.                     foreach($Liked as $liked){
  299.                         $LikedId[]=$liked->getObj_id();        
  300.                     }
  301.                     
  302.                 }       
  303.                 if($product->getBrands()->getId()){
  304.                     if($product->getBrands()->getId() == 263 ){
  305.                         $class "slide-img-bg-brown";
  306.                     }elseif($product->getBrands()->getId() == 262){
  307.                         $class "slide-img-bg-blue";
  308.                     }elseif($product->getBrands()->getId() == 413){
  309.                         $class "slide-img-bg-purple";
  310.                     }elseif($product->getBrands()->getId() == 260){
  311.                         $class "slide-img-bg-green";
  312.                     }else{
  313.                         $class "slide-img-bg-green";
  314.                     }
  315.                 }
  316.                 $more_product .= '<div class="col-12 col-md-6 col-lg-4 product-result">';
  317.                 $more_product .=  $this->renderView('/includes/product-card.html.twig', [
  318.                     'featured_product' => $product,
  319.                     'class' => $class,
  320.                     'LikedId' => $LikedId 
  321.                 ]);
  322.                 $more_product .= '</div>';
  323.             }
  324.             return $this->json([
  325.                 'success' => $more_product,
  326.                 'total_products' => $total_products
  327.             ]);        
  328.     }
  329.     /**
  330.   * @Route("/{locale}/brand", name="brand")
  331.  */
  332.     public function redirectBrandAction(Request $request)
  333.     {
  334.         $weblink $request->get('locale');
  335.         return $this->redirect('/'.$weblink.'/products/',301);
  336.     }
  337.      /**
  338.   * @Route("/{locale}/product", name="product")
  339.  */
  340. public function redirectProductDetailAction(Request $request)
  341. {
  342.     $weblink $request->get('locale');
  343.     return $this->redirect('/'.$weblink.'/products/',301);
  344. }
  345. //     /**
  346. //   * @Route("/{locale}/products", name="productslash")
  347. //  */
  348. // public function redirectProductslashRemove(Request $request)
  349. // {
  350. //     $weblink = $request->get('locale');
  351. //     return $this->json(['name'=>'name']);
  352. // }
  353. /**
  354.         * @Route("/{locale}/products2/{url?}", name="products2")
  355.     */
  356.     public function categoryProduct2Action(Request $request TranslatorInterface $translator){
  357.         
  358.         $products = new Products\Listing();
  359.         $products->load();        
  360.         $childList = array();
  361.         foreach ($products as $product) {            
  362.             if($product->getChildren()){
  363.                 $parent_id $product->getParentId();
  364.                 if($parent_id 74){
  365.                     $childList[] = $product->getChildren();
  366.                 }
  367.             }else{
  368.                 $childList[] = $product;
  369.             }            
  370.         }
  371.         
  372.         $categories $this->getCategories();
  373.         $brands $this->getBrands();
  374.        
  375.         // Category Sliders
  376.         $category_slider = new Slider\Listing();
  377.         $category_slider->setCondition("o_id = 205");
  378.         $category_slider->load();
  379.         if(empty($category_slider->getdata()[0]->getsliders())){
  380.             $category_slider = array();
  381.         }else{
  382.             $category_slider $category_slider->getdata()[0]->getsliders();
  383.         }
  384.         if(!empty($request->get('url'))){
  385.             $getCategoryData    $this->getCategoryDescription($request->get('url'));
  386.             $getBrandData       $this->getBrandDescription($request->get('url'));
  387.             if(!empty($getCategoryData)){
  388.                 $products_head = array(
  389.                     'id'         => $getCategoryData->getId(),
  390.                     'name'         => $getCategoryData->getName(),
  391.                     'description'  => $getCategoryData->getDescription(),
  392.                     'Detail_desc'  => $getCategoryData->getDetail_desc(),
  393.                     'breadcrumb'   => $getCategoryData->getName(),
  394.                     'slides'       => $getCategoryData->getCategory_slides(),
  395.                     'brand_logo'   => '',
  396.                     'meta_title'       => $getCategoryData->getMeta_title(),
  397.                     'meta_description'   => $getCategoryData->getMeta_description(),
  398.                 );
  399.             }else if(!empty($getBrandData)){
  400.                 $products_head = array(
  401.                     'id'           => $getBrandData->getId(),
  402.                     'name'         => $getBrandData->getName(),
  403.                     'description'  => $getBrandData->getDescription(),
  404.                     'breadcrumb'   => $getBrandData->getName(),
  405.                     'slides'       => $getBrandData->getCategory_slides(),
  406.                     'brand_logo'   => $getBrandData->getImage(),
  407.                     'meta_title'       => $getBrandData->getMeta_title(),
  408.                     'meta_description'   => $getBrandData->getMeta_description(),
  409.                 );
  410.             }
  411.             else{
  412.                 $products_head = array();
  413.             }
  414.         }else{
  415.             $products_head = array();
  416.         }
  417.         // Category Mid Banner
  418.         $mid_banner = new Slider\Listing();
  419.         $mid_banner->setCondition("o_id = 206");
  420.         $mid_banner->load();
  421.         if(empty($mid_banner->getdata()[0]->getsliders())){
  422.             $mid_banner = array();
  423.         }else{
  424.             $mid_banner $mid_banner->getdata()[0]->getsliders();
  425.         }   
  426.         // Featured Recipes
  427.         $featured_recipes = new Recipe\Listing();
  428.         $featured_recipes->setCondition('featured IN (?)''1');
  429.         $featured_recipes->setLimit('10');
  430.         $featured_recipes->load();
  431.         //dd($childList);
  432.        
  433.        // dd(count($ObjectIds));
  434.        // echo $ObjectIds;
  435.         $uid $this->get('session')->get('loginUID');
  436.         //$rid = $recipedata->getId();
  437.         $Liked = new Userloyaltyprogram\Listing();
  438.        //for($i=0; $i<count($ObjectIds); $i++){
  439.         // $Liked->setCondition("op_name = ? and u_id = ? and obj_mod_name = ? and obj_data__id IN (?)",['Like',$uid,'Product',$ObjectIds]);
  440.     
  441.        
  442.       //}
  443.      
  444.       $Liked->load();
  445.       $LikedId = [];
  446.       foreach($Liked as $liked){
  447.         $LikedId[]=$liked->getObj_id();
  448.       }
  449.       //dd($LikedId);
  450.        
  451.          
  452.         return $this->render('Products/category2.html.twig', [
  453.                 'products'          => $childList,
  454.                 'categories'        => $categories,
  455.                 'brands'            => $brands,
  456.                 'category_slider'   => $category_slider,
  457.                 'featured_recipes'  => $featured_recipes,
  458.                 'mid_banner'        => $mid_banner,
  459.                 'products_head'     => $products_head,
  460.                 'Liked'     => $Liked,
  461.                 'LikedId'     => $LikedId,
  462.             ]);
  463.     }
  464.  
  465.    
  466.   
  467. }
  468. ?>