src/Controller/Front/ProductController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Category;
  4. use App\Entity\File;
  5. use App\Entity\Produit;
  6. use App\Entity\ProduitDeclinationValue;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use phpDocumentor\Reflection\Types\Array_;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Serializer\Serializer;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Doctrine\ORM\Tools\Pagination\Paginator;
  17. class ProductController extends AbstractController
  18. {
  19.     use ImageTrait;
  20.     /** @var EntityManagerInterface */
  21.     private $em;
  22.     public function __construct(EntityManagerInterface $manager)
  23.     {
  24.         $this->em $manager;
  25.     }
  26.     /**
  27.      * @Route("api/v0/product/{id}", name="product")
  28.      */
  29.     public function index(Produit $produitRequest $request): Response
  30.     {
  31.         return $this->json( [
  32.             'product' => $produit
  33.         ]);
  34.     }
  35.     /**
  36.      * @Route("/product/{id}/{name?}", name="product_new", options={"expose"=true}, methods={"GET"})
  37.      */
  38.     public function indexNew(Request $request,$id): Response
  39.     {
  40.         //$categories = $this->em->getRepository(Category::class)->findAll();
  41.         // Recupérer la catégorie du produit
  42.         $produit $this->em->getRepository(Produit::class)->find($id);
  43.         if (!$produit) {
  44.             throw $this->createNotFoundException('Product Not Found');
  45.         }
  46.         $relatedProduct $this->em->getRepository(Produit::class)->findBy(['categories'=>$produit->getCategories(),'showInWebSite'=>1,'deletedAt' => null],null,4);
  47.         $categorie $this->em->getRepository(Category::class)->find($produit->getCategories()->getId());
  48.         foreach ($relatedProduct as $product) {
  49.             $product->image $this->getDefaultImage($product);
  50.         };
  51.         return $this->render('front/product/index.html.twig', [
  52.             'id' => $id,
  53.             'categorie' => $categorie,
  54.             'product' => $produit,
  55.             'relatedProduct' =>$relatedProduct,
  56.             'productName' => $produit->getName(),
  57.             'productImg' => $this->getDefaultImage($produitnull)
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/product-dec/{id}/{idDec}/{name?}", name="product_new_dec", options={"expose"=true}, methods={"GET"})
  62.      */
  63.     public function indexNewDec(Request $request,$id,$idDec): Response
  64.     {
  65.         //$categories = $this->em->getRepository(Category::class)->findAll();
  66.         // Recupérer la catégorie du produit
  67.         $produit $this->em->getRepository(Produit::class)->find($id);
  68.         $relatedProduct $this->em->getRepository(Produit::class)->findBy(['categories'=>$produit->getCategories(),'showInWebSite'=>1,'deletedAt' => null],null,4);
  69.         $categorie $this->em->getRepository(Category::class)->find($produit->getCategories()->getId());
  70.         foreach ($relatedProduct as $product) {
  71.             $product->image $this->getDefaultImage($product);
  72.         };
  73.         return $this->render('front/product/indexDec.html.twig', [
  74.             'id' => $id,
  75.             'idDec' => $idDec,
  76.             'categorie' => $categorie,
  77.             'product' => $produit,
  78.             'relatedProduct' =>$relatedProduct,
  79.             'productName' => $produit->getName(),
  80.             'productImg' => $this->getDefaultImage($produitnull)
  81.         ]);
  82.     }
  83.     /**
  84.      * @Route("/productByDec/{id}", name="product_by_dec", options={"expose"=true}, methods={"GET"})
  85.      */
  86.     public function productByDec(Request $request$id): Response
  87.     {
  88.         // Recupérer le produit par id declinaison
  89.         $declinaison $this->em->getRepository(ProduitDeclinationValue::class)->find($id);
  90.         return $this->redirectToRoute('product_new', ['id' => $declinaison->getProduit()->getId()]);
  91.     }
  92.     /**
  93.      * @Route("/api/product-get", name="product_get", options={"expose"=true}, methods={"GET"})
  94.      */
  95.     public function getAPI(Request $request): Response
  96.     {
  97.         $id $request->query->get('id');
  98.         $product $this->em->getRepository(Produit::class)->findOneById($id);
  99.         $response = [
  100.             'res' => 'OK',
  101.             'data' => $product,
  102.             'message' => 'Produit récupéré avec succès.',
  103.         ];
  104.         return new jsonResponse($response);
  105.     }
  106.     /**
  107.      * @Route("api/v0/productByCategory/{id}", name="productByCategory")
  108.      */
  109.     public function productByCategory(Category $categoryRequest $request): Response
  110.     {
  111.         $products $this->em->getRepository(Produit::class)->findBy(array('categories' => $category), [], 20);
  112.          return $this->json([
  113.             'products' => $products
  114.         ]);
  115.     }
  116.     /**
  117.      * @Route("api/v0/productByDeclinaison",  options={"expose"=true},name="productDeclinaison")
  118.      */
  119.     public function productDeclinaison(Request $request): Response
  120.     {
  121.         $size=$request->request->get('size');
  122.         $product=$request->request->get('product');
  123.         $produitDeclinaisonValue  $this->em->getRepository(ProduitDeclinationValue::class)->findDeclinationValueWithDeclination($size,$product);
  124.        return $this->json([
  125.            'produitDeclinaisonValue' => $produitDeclinaisonValue
  126.        ]);
  127.     }
  128.     /**
  129.      * @Route("api/v0/productImage",  options={"expose"=true},name="productImage")
  130.      */
  131.     public function productImage(Request $request): Response
  132.     {
  133.         $imageId $request->request->get('image');
  134.         $image $this->em->getRepository(File::class)->find($imageId);
  135.         return $this->json([
  136.             'image' => $image
  137.         ]);
  138.     }
  139.     private function comparator($object1$object2)
  140.     {
  141.         return $object2->getId() > $object1->getId();
  142.     }
  143.     /**
  144.      * @Route("/api/product/{idProduit}/declinaison-value", name="product-declinaison-value", options={"expose"=true}, methods={"GET"})
  145.      */
  146.     public function getdeclinaisonValuePerProduct($idProduitRequest $request): Response
  147.     {
  148.         $product $this->em->getRepository(Produit::class)->findOneById($idProduit);
  149.         $dec = [];
  150.         $output = [];
  151.         foreach ($product->getProduitDeclinationValues() as $entity){
  152.             foreach ($entity->getGroupDeclinationValues() as $group) {
  153.                 $key strtolower($group->getDeclination()->getName());
  154.                 $value = ($key == "couleur") ? $group->getValue() : $group->getValue()->getName();
  155.                 $data[$key] = $value;
  156.                 $dec[$key][] = $value;
  157.             }
  158.             $quantity 0;
  159.             foreach ($entity->getStocks() as $stock)
  160.                 $quantity $quantity + ($stock->getQtStock() - $stock->getQtReserved());
  161.             $priceTtc $entity->getProduit()->getPriceTtc();
  162.             $in_promo=false;
  163.             if( $entity->getProduit()->getPromotion()) {
  164.                 $promotion $entity->getProduit()->getPromotion();
  165.                 $date = new \DateTime('now');
  166.                 if( $promotion->getStartAt() <= $date && $promotion->getEndAt() >= $date){
  167.                     $priceTtc =($promotion->getDiscountType() == 'percent')?
  168.                         $entity->getProduit()->getPriceTtc() - ((($entity->getProduit()->getPriceTtc() / 100) * $promotion->getDiscountValue()))
  169.                         : $entity->getProduit()->getPriceTtc() - $promotion->getDiscountValue();
  170.                     $in_promo=true;
  171.                 }
  172.             }
  173.             $output[] = array_merge([
  174.                 'id' => $entity->getId(),
  175.                 "qty" => $quantity,
  176.                 "price_ttc" => number_format($priceTtc3),
  177.                 "in_promo" => $in_promo,
  178.                 'price_ht' => number_format($entity->getPriceHt(), 3),
  179.                 "picture" => $this->getDefaultImage(null$entity),
  180.                 "ref" => $entity->getReference(),
  181.                 "name" => $entity->getName()
  182.             ], $data);
  183.         }
  184.         // Récuérer les tags reliés
  185.         $Tags $product->getTags();
  186.         $produitsTags = [];
  187.         // Récupérer les produit reliés au tag
  188.         foreach ($Tags as $tag) {
  189.             foreach ($tag->getProduits() as $prod) {
  190.                 /*if(sizeof($produitsTags) == 4  ){
  191.                     break;
  192.                 }*/
  193.                 if( $prod->getId() != $product->getId()) {
  194.                     array_push($produitsTags$prod);
  195.                 }
  196.             }
  197.             /*if(sizeof($produitsTags) == 4  ){
  198.                 break;
  199.             }*/
  200.         }
  201.         // Trier par id décroissant et récupérer les 4 dernier
  202.         usort($produitsTags, array($this"comparator"));
  203.         $tags = [];
  204.         foreach ($produitsTags as $prod) {
  205.             if( sizeof($tags) == 4) break;
  206.             $prod->image =  $this->getDefaultImage$prod);
  207.             array_push($tags$prod);
  208.         }
  209.         foreach ($dec as $k=>$v)
  210.             $dec[$k] = array_values(array_unique($dec[$k], SORT_REGULAR));
  211.         $response = [
  212.             'res' => 'OK',
  213.             'data' => ["produit" => $product"declinaisons_produit" => $output"declinaisons" => $dec'tags' => $tags],
  214.             'message' => 'Produit Dec récupéré avec succès.',
  215.         ];
  216.         return new jsonResponse($response);
  217.     }
  218.     /**
  219.      * @Route("/api/product/{idProduit}/{idDec}/declinaison-value-new", name="product-declinaison-value_new", options={"expose"=true}, methods={"GET"})
  220.      */
  221.     public function getdeclinaisonValuePerProductNew($idProduit,$idDecRequest $request): Response
  222.     {
  223.         $product $this->em->getRepository(Produit::class)->findOneById($idProduit);
  224.         $dec = [];
  225.         $output = [];
  226.         foreach ($product->getProduitDeclinationValues() as $entity){
  227.             foreach ($entity->getGroupDeclinationValues() as $group) {
  228.                 $key strtolower($group->getDeclination()->getName());
  229.                 $value = ($key == "couleur") ? $group->getValue() : $group->getValue()->getName();
  230.                 $data[$key] = $value;
  231.                 $dec[$key][] = $value;
  232.             }
  233.             $quantity 0;
  234.             foreach ($entity->getStocks() as $stock)
  235.                 $quantity $quantity + ($stock->getQtStock() - $stock->getQtReserved());
  236.             $priceTtc $entity->getProduit()->getPriceTtc();
  237.             $in_promo=false;
  238.             if( $entity->getProduit()->getPromotion()) {
  239.                 $promotion $entity->getProduit()->getPromotion();
  240.                 $date = new \DateTime('now');
  241.                 if( $promotion->getStartAt() <= $date && $promotion->getEndAt() >= $date){
  242.                     $priceTtc =($promotion->getDiscountType() == 'percent')?
  243.                         $entity->getProduit()->getPriceTtc() - ((($entity->getProduit()->getPriceTtc() / 100) * $promotion->getDiscountValue()))
  244.                         : $entity->getProduit()->getPriceTtc() - $promotion->getDiscountValue();
  245.                     $in_promo=true;
  246.                 }
  247.             }
  248.             // Mettre la declinaison choisi dans la première position
  249.             if ($entity->getId() == $idDec){
  250.                 array_unshift($outputarray_merge([
  251.                     'id' => $entity->getId(),
  252.                     "qty" => $quantity,
  253.                     "price_ttc" => number_format($priceTtc3),
  254.                     "in_promo" => $in_promo,
  255.                     'price_ht' => number_format($entity->getPriceHt(), 3),
  256.                     "picture" => $this->getDefaultImage(null$entity,true),
  257.                     "ref" => $entity->getReference(),
  258.                     "name" => $entity->getName()
  259.                 ], $data));
  260.             }else {
  261.                 $output[] = array_merge([
  262.                     'id' => $entity->getId(),
  263.                     "qty" => $quantity,
  264.                     "price_ttc" => number_format($priceTtc3),
  265.                     "in_promo" => $in_promo,
  266.                     'price_ht' => number_format($entity->getPriceHt(), 3),
  267.                     "picture" => $this->getDefaultImage(null$entity,true),
  268.                     "ref" => $entity->getReference(),
  269.                     "name" => $entity->getName()
  270.                 ], $data);
  271.             }
  272.         }
  273.         // Récuérer les tags reliés
  274.         $Tags $product->getTags();
  275.         $produitsTags = [];
  276.         // Récupérer les produit reliés au tag
  277.         foreach ($Tags as $tag) {
  278.             foreach ($tag->getProduits() as $prod) {
  279.                 /*if(sizeof($produitsTags) == 4  ){
  280.                     break;
  281.                 }*/
  282.                 if( $prod->getId() != $product->getId()) {
  283.                     array_push($produitsTags$prod);
  284.                 }
  285.             }
  286.             /*if(sizeof($produitsTags) == 4  ){
  287.                 break;
  288.             }*/
  289.         }
  290.         // Trier par id décroissant et récupérer les 4 dernier
  291.         usort($produitsTags, array($this"comparator"));
  292.         $tags = [];
  293.         foreach ($produitsTags as $prod) {
  294.             if( sizeof($tags) == 4) break;
  295.             $prod->image =  $this->getDefaultImage$prod);
  296.             array_push($tags$prod);
  297.         }
  298.         foreach ($dec as $k=>$v)
  299.             $dec[$k] = array_values(array_unique($dec[$k], SORT_REGULAR));
  300.         $response = [
  301.             'res' => 'OK',
  302.             'data' => ["produit" => $product"declinaisons_produit" => $output"declinaisons" => $dec'tags' => $tags],
  303.             'message' => 'Produit Dec récupéré avec succès.',
  304.         ];
  305.         return new jsonResponse($response);
  306.     }
  307. }