src/Controller/Front/CategoryController.php line 239

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Category;
  4. use App\Entity\Declination;
  5. use App\Entity\Produit;
  6. use App\Entity\ProduitDeclinationValue;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  13. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  14. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  15. use Symfony\Component\Serializer\Serializer;
  16. use Symfony\Component\Serializer\SerializerInterface;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. class CategoryController extends AbstractController
  19. {
  20.     use ImageTrait;
  21.     /** @var EntityManagerInterface */
  22.     private $em;
  23.     /**
  24.      * @var Serializer
  25.      */
  26.     private $serializer;
  27.     public function __construct(EntityManagerInterface $managerSerializerInterface $serializer)
  28.     {
  29.         $this->em $manager;
  30.         $this->serializer $serializer;
  31.     }
  32.     /**
  33.      * @Route("/api/product-list-by-category", name="product_list_by_category", options={"expose"=true}, methods={"GET"})
  34.      */
  35.     public function getProductsAPI(Request $request): Response
  36.     {
  37.         $category_id $request->query->get('id');
  38.         $page $request->query->get('page');
  39.         $orderBy $request->query->get('orderBy');
  40.         $categories=[(int)$category_id];
  41.         $category $this->em->getRepository(Category::class)->find($category_id);
  42.         if (!$category) {
  43.             throw $this->createNotFoundException('Category Not Found');
  44.         }
  45.         foreach ($category->getSubCategories() as $child) {
  46.             $categories[] = $child->getId();
  47.             foreach ($child->getSubCategories() as $sub_child) {
  48.                 $categories[] = $sub_child->getId();
  49.             }
  50.         }
  51.         //Les Filtres de recherche
  52.         $tailles preg_split('@,@'$request->query->get('tailles'), NULLPREG_SPLIT_NO_EMPTY);
  53.         $couleurs preg_split('@,@'$request->query->get('couleurs'), NULLPREG_SPLIT_NO_EMPTY);
  54.         $maxPrice floatval($request->query->get('maxPrice'));
  55.         $minPrice floatval($request->query->get('minPrice'));
  56.         // get the product repository
  57.         $produits $this->em->getRepository(Produit::class);
  58.         // build the query for the doctrine paginator
  59.         $query $produits->createQueryBuilder('p');
  60.         $query->where('p.categories IN (:cat)')
  61.             ->setParameter('cat'$categories)
  62.             ->andWhere('p.deletedAt is null')
  63.             ->andWhere('p.showInWebSite = 1')
  64.             ->andWhere('p.price_ttc between :minPrice and :maxPrice')
  65.             ->setParameter('maxPrice'$maxPrice)
  66.             ->setParameter('minPrice'$minPrice);
  67.         // Appliquer les filtres de tailles si ils sont appliqués
  68.         if( $tailles && !$couleurs) {
  69.             $declination $this->em->getRepository(Declination::class)->find(1);
  70.             $query->innerJoin('App\Entity\ProduitDeclinationValue''d''with''d.produit=p')
  71.                 ->innerJoin('App\Entity\GroupDeclinationValue''g''with''g.produitDeclination= d')
  72.                 ->innerJoin('App\Entity\ValueDeclination''v''with''v= g.value')
  73.                 ->andWhere("v.name in (:tailles)")
  74.                 ->andWhere('v.declination = :declination')
  75.                 ->setParameter('declination'$declination)
  76.                 ->setParameter('tailles'$tailles);
  77.         }
  78.         // Appliquer les filtres de couleurs si ils sont appliqués seulement
  79.         if( $couleurs && !$tailles) {
  80.             $declination $this->em->getRepository(Declination::class)->find(2);
  81.             $query->innerJoin('App\Entity\ProduitDeclinationValue''d''with''d.produit=p')
  82.                 ->innerJoin('App\Entity\GroupDeclinationValue''g''with''g.produitDeclination= d')
  83.                 ->innerJoin('App\Entity\ValueDeclination''v''with''v= g.value')
  84.                 ->andWhere("v.id in (:couleurs) or v.parent in (:couleurs)")
  85.                 ->andWhere('v.declination = :declination')
  86.                 ->setParameter('declination'$declination)
  87.                 ->setParameter('couleurs'$couleurs);
  88.         }
  89.         // Appliquer les filtres de tailles et couleurs appliqués les deux
  90.         if( $tailles && $couleurs) {
  91.             $declinationTaille $this->em->getRepository(Declination::class)->find(1);
  92.             $declinationCouleur $this->em->getRepository(Declination::class)->find(2);
  93.             // Fusionner les deux filtres en un seul
  94.             $filtres array_merge($tailles$couleurs);
  95.             $query->innerJoin('App\Entity\ProduitDeclinationValue''d''with''d.produit=p')
  96.                 ->innerJoin('App\Entity\GroupDeclinationValue''g''with''g.produitDeclination= d')
  97.                 ->innerJoin('App\Entity\ValueDeclination''v''with''v= g.value')
  98.                 ->andWhere("v.name in (:tailles) or v.id in (:couleurs) or v.parent in (:couleurs)")
  99.                 /* ->andWhere('v.declination = :declinationTaille OR v.declination = :declinationCouleur')
  100.                  ->setParameter('declinationTaille', $declinationTaille)*/
  101.                 ->setParameter('couleurs'$couleurs)
  102.                 ->setParameter('tailles'$tailles);
  103.         }
  104.         // Order by
  105.         switch ($orderBy) {
  106.             case 1:
  107.                 $query->orderBy('p.name''ASC');
  108.                 break;
  109.             case 2:
  110.                 $query->orderBy('p.name''DESC');
  111.                 break;
  112.             case 3:
  113.                 $query->orderBy('p.price_ttc''ASC');
  114.                 break;
  115.             case 4:
  116.                 $query->orderBy('p.price_ttc''DESC');
  117.                 break;
  118.             case 5:
  119.                 $query->orderBy('p.createdAt''ASC');
  120.                 break;
  121.             case 6:
  122.                 $query->orderBy('p.createdAt''DESC');
  123.                 break;
  124.         }
  125.         // set page size
  126.         $pageSize $request->query->get('pageSize');;
  127.         // load doctrine Paginator
  128.         $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
  129.         // you can get total items
  130.         $totalItems count($paginator);
  131.         // get total pages
  132.         $pagesCount ceil($totalItems $pageSize);
  133.         // now get one page's items:
  134.         $paginator
  135.             ->getQuery()
  136.             ->setFirstResult($pageSize * ($page-1)) // set the offset
  137.             ->setMaxResults($pageSize); // set the limit
  138.         $data= array();
  139.         foreach ($paginator as $pageItem) {
  140.             // do stuff with results...
  141.             array_push($data,$pageItem);
  142.         }
  143.         foreach ($data as $product){
  144.             $product->image =  $this->getDefaultImage$product);
  145.         }
  146.         // Les nombres de pages
  147.         $pages = array();
  148.         for ($i max($page 31); $i <= min($page 3$pagesCount); $i++) {
  149.             array_push($pages$i);
  150.         }
  151.         /* // build the query for the doctrine paginator
  152.          $query = $produits->createQueryBuilder('p')
  153.              ->where('p.name like :search OR p.description like :search OR p.reference like :search')
  154.              ->setParameter('search', '%'.$search.'%')
  155.              ->andWhere('p.deletedAt is null');*/
  156.         $response = [
  157.             'res' => 'OK',
  158.             'data' => $data,
  159.             //'taillesFilter' => $taillesFilter,
  160.             'pagesCount' => $pagesCount,
  161.             'total' => $totalItems,
  162.             'pages' => $pages,
  163.             'message' => 'Produits récupérés avec succès.',
  164.         ];
  165.         return new jsonResponse($response);
  166.     }
  167.     /**
  168.      * @Route("/category/{id}/{name?}", options={"expose"=true}, name="category_new",requirements={"id"="\d+"})
  169.      */
  170.     public function indexNew(Request $request$id): Response
  171.     {
  172.         $category $this->em->getRepository(Category::class)->find($id);
  173.         if (!$category) {
  174.             throw $this->createNotFoundException('Category Not Found');
  175.         }
  176.         // Récupérer les catégories fils
  177.         $categories = [];
  178.         array_push($categories,$category);
  179.         foreach ($category->getSubCategories() as $child) {
  180.             array_push($categories,$child);
  181.             foreach ($child->getSubCategories() as $sub) {
  182.                 array_push($categories,$sub);
  183.             }
  184.         }
  185.         return $this->render('front/category/index.html.twig', [
  186.             'category_id' => $id,
  187.             'categories' => $categories,
  188.             'categorie' => $category $category null,
  189.         ]);
  190.     }
  191.     /**
  192.      * @Route("/category-dec/{id}/{name?}", options={"expose"=true}, name="category_dec",requirements={"id"="\d+"})
  193.      */
  194.     public function indexDec(Request $request$id): Response
  195.     {
  196.         $category $this->em->getRepository(Category::class)->find($id);
  197.         if (!$category) {
  198.             throw $this->createNotFoundException('Category Not Found');
  199.         }
  200.         // Récupérer les catégories fils
  201.         $categories = [];
  202.         array_push($categories,$category);
  203.         foreach ($category->getSubCategories() as $child) {
  204.             array_push($categories,$child);
  205.             foreach ($child->getSubCategories() as $sub) {
  206.                 array_push($categories,$sub);
  207.             }
  208.         }
  209.         return $this->render('front/dec/index.html.twig', [
  210.             'category_id' => $id,
  211.             'categories' => $categories,
  212.             'categorie' => $category $category null,
  213.         ]);
  214.     }
  215.     /**
  216.      * @Route("/api/dec-list-by-category", name="dec_list_by_category", options={"expose"=true}, methods={"GET"})
  217.      */
  218.     public function getProductsDecAPI(Request $request): Response
  219.     {
  220.         $category_id $request->query->get('id');
  221.         $page $request->query->get('page');
  222.         $orderBy $request->query->get('orderBy');
  223.         $category $this->em->getRepository(Category::class)->find($category_id);
  224.         // Récupérer les catégories fils
  225.         $categories = [];
  226.         array_push($categories,$category);
  227.         foreach ($category->getSubCategories() as $child) {
  228.             array_push($categories,$child);
  229.             foreach ($child->getSubCategories() as $sub) {
  230.                 array_push($categories,$sub);
  231.             }
  232.         }
  233.         //Les Filtres de recherche
  234.         $tailles preg_split('@,@'$request->query->get('tailles'), NULLPREG_SPLIT_NO_EMPTY);
  235.         $couleurs preg_split('@,@'$request->query->get('couleurs'), NULLPREG_SPLIT_NO_EMPTY);
  236.         $maxPrice floatval($request->query->get('maxPrice'));
  237.         $minPrice floatval($request->query->get('minPrice'));
  238.         // get the product dec repository
  239.         $produits $this->em->getRepository(ProduitDeclinationValue::class);
  240.         // build the query for the doctrine paginator
  241.         $query $produits->createQueryBuilder('d');
  242.         $query->innerJoin('App\Entity\Produit''p''with''d.produit=p')
  243.             ->where('p.categories in (:cat)')
  244.             ->setParameter('cat'$categories)
  245.             ->andWhere('p.deletedAt is null')
  246.             ->andWhere('p.showInWebSite = 1')
  247.             ->andWhere('p.price_ttc between :minPrice and :maxPrice')
  248.             ->setParameter('maxPrice'$maxPrice)
  249.             ->setParameter('minPrice'$minPrice)
  250.             ->groupBy('p.id,v.id');
  251.         // Appliquer les filtres de tailles si ils sont ne pas appliqués
  252.         if( !$tailles && !$couleurs) {
  253.             $declination $this->em->getRepository(Declination::class)->find(1);
  254.             $query->innerJoin('App\Entity\GroupDeclinationValue''g''with''g.produitDeclination= d')
  255.                 ->innerJoin('App\Entity\ValueDeclination''v''with''v= g.value')
  256.                 ->andWhere('v.declination=2');
  257.         }
  258.         // Appliquer les filtres de tailles si ils sont appliqués
  259.         if( $tailles && !$couleurs) {
  260.             $declination $this->em->getRepository(Declination::class)->find(1);
  261.             $query->innerJoin('App\Entity\GroupDeclinationValue''g''with''g.produitDeclination= d')
  262.                 ->innerJoin('App\Entity\ValueDeclination''v''with''v= g.value')
  263.                 ->andWhere("v.name in (:tailles)")
  264.                 ->setParameter('tailles'$tailles);
  265.         }
  266.         // Appliquer les filtres de couleurs si ils sont appliqués seulement
  267.         if( $couleurs && !$tailles) {
  268.             $declination $this->em->getRepository(Declination::class)->find(2);
  269.             $query->innerJoin('App\Entity\GroupDeclinationValue''g''with''g.produitDeclination= d')
  270.                 ->innerJoin('App\Entity\ValueDeclination''v''with''v= g.value')
  271.                 ->andWhere("v.id in (:couleurs) or v.parent in (:couleurs)")
  272.                 ->setParameter('couleurs'$couleurs);
  273.         }
  274.         // Appliquer les filtres de tailles et couleurs appliqués les deux
  275.         if( $tailles && $couleurs) {
  276.             $declinationTaille $this->em->getRepository(Declination::class)->find(1);
  277.             $declinationCouleur $this->em->getRepository(Declination::class)->find(2);
  278.             // Fusionner les deux filtres en un seul
  279.             $filtres array_merge($tailles$couleurs);
  280.             $query->innerJoin('App\Entity\GroupDeclinationValue''g''with''g.produitDeclination= d')
  281.                 ->innerJoin('App\Entity\ValueDeclination''v''with''v= g.value')
  282.                 ->andWhere("v.name in (:tailles) or v.id in (:couleurs) or v.parent in (:couleurs)")
  283.                 ->setParameter('couleurs'$couleurs)
  284.                 ->setParameter('tailles'$tailles)
  285.                 ->groupBy('v.declination')
  286.                 ->having('v.declination=2');
  287.         }
  288.         // Order by
  289.         switch ($orderBy) {
  290.             case 1:
  291.                 $query->orderBy('p.name''ASC');
  292.                 break;
  293.             case 2:
  294.                 $query->orderBy('p.name''DESC');
  295.                 break;
  296.             case 3:
  297.                 $query->orderBy('p.price_ttc''ASC');
  298.                 break;
  299.             case 4:
  300.                 $query->orderBy('p.price_ttc''DESC');
  301.                 break;
  302.             case 5:
  303.                 $query->orderBy('p.createdAt''ASC');
  304.                 break;
  305.             case 6:
  306.                 $query->orderBy('p.createdAt''DESC');
  307.                 break;
  308.         }
  309.         // set page size
  310.         $pageSize $request->query->get('pageSize');;
  311.         // load doctrine Paginator
  312.         $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
  313.         // you can get total items
  314.         $totalItems count($paginator);
  315.         // get total pages
  316.         $pagesCount ceil($totalItems $pageSize);
  317.         // now get one page's items:
  318.         $paginator
  319.             ->getQuery()
  320.             ->setFirstResult($pageSize * ($page-1)) // set the offset
  321.             ->setMaxResults($pageSize); // set the limit
  322.         $data= array();
  323.         foreach ($paginator as $pageItem) {
  324.             // do stuff with results...
  325.             $pageItem->setImage($this->getDefaultImage(null$pageItem));
  326.             array_push($data,$pageItem);
  327.         }
  328.         // Les nombres de pages
  329.         $pages = array();
  330.         for ($i max($page 31); $i <= min($page 3$pagesCount); $i++) {
  331.             array_push($pages$i);
  332.         }
  333.         $response = [
  334.             'res' => 'OK',
  335.             'data' => $data,
  336.             'pagesCount' => $pagesCount,
  337.             'total' => $totalItems,
  338.             'pages' => $pages,
  339.             'message' => 'Produits récupérés avec succès.',
  340.         ];
  341.         return new jsonResponse($response);
  342.     }
  343.     // Non utilisé
  344.     /*public function categoriesDropdownList(): Response {
  345.         $sql = 'select * from category limit 100';
  346.         $stmt = $this->em->getConnection()->prepare($sql);
  347.         $categories = $stmt->executeQuery()->fetchAllAssociative();
  348.         return $this->render('front/category/_categoriesDropdownList.html.twig', [
  349.             'categories' => $categories
  350.         ]);
  351.     }*/
  352. }