src/Flexy/FrontBundle/Controller/SearchController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Flexy\FrontBundle\Controller;
  3. use App\Flexy\FrontBundle\Repository\ProductFrontRepository;
  4. use App\Flexy\ProductBundle\Repository\ProductRepository;
  5. use App\Flexy\ShopBundle\Entity\Product\Attribut;
  6. use App\Flexy\ShopBundle\Entity\Product\Product;
  7. use App\Flexy\ShopBundle\Entity\Brand;
  8. use App\Flexy\ShopBundle\Repository\Product\AttributRepository;
  9. use App\Repository\Flexy\ShopBundle\Entity\BrandRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. #[Route('/shop/search')]
  17. class SearchController extends AbstractController
  18. {
  19.     #[Route('/'name"front_search")]
  20.     public function search(Request $request,ProductFrontRepository $productFrontRepository): Response
  21.     {
  22.         $keyword $request->query->get("keyword");
  23.         $products $productFrontRepository->findAll();
  24.         if(count($productFrontRepository->findByKeyWord($keyword)) > 0){
  25.             $products $productFrontRepository->findByKeyWord($keyword);
  26.         }
  27.         return $this->render('@Flexy\FrontBundle/templates/search/search.html.twig',[
  28.             "products"=>$products
  29.         ]);
  30.     }
  31.     #[Route('/sideBarFilter'name"front_sideBarFilter")]
  32.     public function sideBarFilter(
  33.         Request $request,
  34.         AttributRepository $attributRepository,
  35.         BrandRepository $brandRepository,
  36.     ): Response
  37.     
  38.         {
  39.             
  40.             return $this->render('@Flexy\FrontBundle/templates/search/_sideBarFilter.html.twig',[
  41.                 "Attributes"=>$attributRepository->findAll(),
  42.                 "brands"=>$brandRepository->findBy(
  43.                     array(),
  44.                     array('name' => 'ASC')
  45.                 )
  46.             ]);
  47.         }
  48.         
  49.     
  50.     #[Route('/filter'name"front_filter")]
  51.     public function filter(
  52.         Request $request,
  53.         EntityManagerInterface $em,
  54.         PaginatorInterface $paginator
  55.     ): Response
  56.     {
  57.         $filterValues=[];
  58.         $filters=$request->query->get("filter");
  59.         
  60.         foreach ( (array)$filters as $key => $value) {
  61.             $filterValues[]=$key;
  62.         }
  63.         
  64.         $dql   "SELECT product FROM App\Flexy\ShopBundle\Entity\Product\Product product";
  65.         foreach ( (array)$filters as $key => $value) {
  66.             if($key == "price"){
  67.                 
  68.                 continue;
  69.             }
  70.             $dql.= ' LEFT JOIN product.'.$key.' '.$key.' ';
  71.         }
  72.         
  73.         
  74.        
  75.         $counter 0;
  76.         $startAdditionalFilters false;
  77.         foreach ((array)$filters as $key => $value) {
  78.             $filterValues = [];
  79.             foreach((array)$value as  $filterKey=>$filterValue){
  80.                $filterValues[]=  $filterKey;
  81.             }
  82.         
  83.               
  84.             
  85.             if(isset($filters["brand"]) && $filters["brand"]!= "" /*$key == "brand"*/){
  86.             
  87.                 $brand $this->getDoctrine()->getRepository(Brand::class)->findBy(
  88.                     array(
  89.                       'name' => $filters["brand"]
  90.                     )                       
  91.                   );
  92.                   if(count($brand) > 0){
  93.                   //dd($brand[0]->getId());
  94.               
  95.                   array_push($filterValues$brand[0]->getId());
  96.                 }
  97.                 
  98.             }
  99.            
  100.             $sqlfilterValues '(' join(','$filterValues) . ')';
  101.             if($counter == 0){
  102.                 $dql.= ' WHERE 1 = 1';// Condition principale
  103.             }
  104.            
  105.             if($key == "price"){
  106.                 $dql.= ' AND ';
  107.                 $dql.= ' (product.price >=  '.($filters["price"]["min"] * 100 ).' AND  product.price <= '.($filters["price"]["max"] * 100 ).')';
  108.                 if(count((array)$filters) > 1){
  109.                     $dql.= ' AND (';// Fin de condition principale, et comencant les autres filtres avec 'OR' ***Ouvrir la parentaise***
  110.                     
  111.                     $counter $counter 1;
  112.                     
  113.                 }
  114.                 
  115.                 
  116.                 continue;
  117.             }
  118.             
  119.             
  120.             if($startAdditionalFilters){
  121.                 $dql.= ' OR ';
  122.             }
  123.             
  124.         
  125.             $dql.= '  '.$key.'.id IN  '.$sqlfilterValues;
  126.             $startAdditionalFilters true;
  127.             $counter $counter 1;
  128.         }
  129.         if(count((array)$filters) > 1){
  130.         $dql.= ' ) ';//***fermer la parentaise***
  131.         }
  132.         // Add filter price
  133.         
  134.         //dd($dql);
  135.        
  136.         $query $em->createQuery($dql);
  137.         
  138.         
  139.         $pagination $paginator->paginate(
  140.             $query/* query NOT result */
  141.             $request->query->getInt('page'1), /*page number*/
  142.             10 /*limit per page*/
  143.         );
  144.         return $this->render('@Flexy\FrontBundle/templates/search/search.html.twig',[
  145.      
  146.             "products"=>$pagination
  147.         ]);
  148.     }
  149. }