src/Controller/ProductController.php line 355

Open in your IDE?
  1. <?php
  2. /* -- !!!!! LA CLASSE "Marchandise" CORRESPOND AUX PRODUIT ET LA   !!!!! -- */
  3. /* -- !!!!! CLASSE "Product" EST LA SUPERCLASSE DES PRODUITS ET    !!!!! -- */
  4. /* -- !!!!! DES SERVICES. CE CONTROLEUR NE CORRESPOND QUE A LA     !!!!! -- */
  5. /* -- !!!!! GESTION DES PRODUITS (classe "Marchandise") ET NON     !!!!! -- */
  6. /* -- !!!!! A LA GESTION DES SERVICES (sauf pour productShow et    !!!!! -- */
  7. /* -- !!!!! productSearch qui s'occupent des produits et des       !!!!! -- */
  8. /* -- !!!!! services)                                              !!!!! -- */
  9. namespace App\Controller;
  10. use App\Entity\Company;
  11. use App\Entity\Product;
  12. use App\Entity\ProductClick;
  13. use App\Entity\Marchandise;
  14. use App\Entity\ProductFav;
  15. use App\Entity\Service;
  16. use App\Entity\Subcategory;
  17. use App\Entity\User;
  18. use App\Entity\Visite;
  19. use App\Form\ProductType;
  20. use App\Repository\CategoryRepository;
  21. use App\Repository\CompanyRepository;
  22. use App\Repository\ProductRepository;
  23. use App\Repository\SubcategoryRepository;
  24. use App\Repository\UserRepository;
  25. use App\Service\FileUploader;
  26. use Doctrine\Common\Collections\ArrayCollection;
  27. use Symfony\Bundle\FrameworkBundle\Controller;
  28. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  29. use Symfony\Component\HttpFoundation\JsonResponse;
  30. use Symfony\Component\HttpFoundation\RedirectResponse;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Cocur\Slugify\Slugify;
  33. use JMS\Serializer\SerializerInterface;
  34. use Symfony\Component\HttpFoundation\File\File;
  35. use Symfony\Component\HttpFoundation\Response;
  36. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  37. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  38. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  39. use Symfony\Component\Serializer\Serializer;
  40. use Symfony\Component\Serializer\SerializerInterface as SerializerSerializerInterface;
  41. use Symfony\Component\Validator\Constraints\Json;
  42. use Symfony\Flex\ComposerRepository;
  43. class ProductController extends Controller\AbstractController
  44. {
  45.     /**
  46.      * @Route("/{_locale<en|fr|de|it|es>}/produit/ajout", name="productAdd")
  47.      * @param Request $request
  48.      * @return RedirectResponse|Response
  49.      */
  50.     public function add(Request $requestFileUploader $fileUploaderCompanyRepository $companyRepositoryProductRepository $productRepositoryUserRepository $userRepository)
  51.     {
  52.         // dd($companyRepository->find($id));
  53.         // die();
  54.         $em $this->getDoctrine()->getManager();
  55.         $contenu $request->request->get('product');
  56.         // $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  57.         $marchandise = new Marchandise();
  58.         $id $_SESSION['_sf2_attributes']['gaeaUserId'];
  59.         $user $userRepository->findOneByGaeaId($id);
  60.         // dump($user);
  61.         $form $this->createForm(ProductType::class, $marchandise, array(
  62.             'user' => $user
  63.         ));
  64.         $form->handleRequest($request);
  65.         
  66.         if ($form->isSubmitted() && $form->isValid()) {
  67.             $marchandise->setUpdateddate(null);
  68.             $marchandise->setCreationdate(new \DateTime());
  69.             $slugify = new Slugify();
  70.             $slug=$slugify->slugify($marchandise->getName());
  71.             $marchandise->setGaearecommanded(false);
  72.             $marchandise->setSlug($slug);
  73.             $marchandise->setCompany($user);
  74.             $marchandise->setType('marchandise');
  75.             $file $marchandise->getImage();
  76.             if($marchandise->isWantevaluation()){
  77.                 $marchandise->setNiveau('N.1');
  78.             } else{
  79.                 $marchandise->setNiveau('N.0');
  80.             }
  81.             //$product->setImage($imageName);
  82.             
  83.             if ($file) {
  84.                 /*$fileName = uniqid() . '.' . $file->guessExtension();
  85.                 $file->move($this->getParameter('products'), $fileName);*/
  86.                
  87.                 $fileName $fileUploader->upload($file$this->getParameter('products'));
  88.                 
  89.                 $marchandise->setImage($fileName);
  90.             }
  91.             
  92.            $subcategories $marchandise->getSubCategories();
  93.             foreach ($subcategories as $subcategory)
  94.             {
  95.                 $subcategory->addProduct($marchandise);
  96.             }
  97.             $em->persist($marchandise);
  98.             $em->flush();
  99.             
  100.             return $this->redirectToRoute('productManage');
  101.         }
  102.         $products $productRepository->findAllProductsByComp($user);
  103.         
  104.         return $this->render('product/add.html.twig', [
  105.             'form' => $form->createView(),
  106.             'user' => $user,
  107.             'products' => $products,
  108.         ]);
  109.     }
  110.     /**
  111.      * @Route("/sousCategories", name="subCategory",methods={"GET"})
  112.     */
  113.     public function subCategorySelect(SubcategoryRepository $subCategoryRepository):Response
  114.     {
  115.         $sousCategories $subCategoryRepository->findAll();
  116.         // dump($sousCategories);
  117.         $encoder = new JsonEncoder();
  118.         // $defaultContext = [
  119.         //     AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
  120.         //         return $object->getId();
  121.         //     },
  122.         // ];
  123.         // $normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext);
  124.         // $serializer = new Serializer([$normalizer], [$encoder]);
  125.         $serializationSousCategories $serializer->serialize($sousCategories'json', ['groups' => 'category']);
  126.         $response = new Response($serializationSousCategories200, [
  127.              'Content-Type' => "application/json"
  128.         ]); 
  129.         return $response;
  130.     }
  131. // fonction pour modifier les marchandises uniquement //
  132.     /**
  133.      * @Route("/{_locale<en|fr|de|it|es>}/produit/modifier/{id}", name="productEdit")
  134.      * @param $id
  135.      * @param Request $request
  136.      * @return RedirectResponse|Response
  137.      */
  138.     public function edit($id,Request $requestFileUploader $fileUploaderProductRepository $productRepositoryUserRepository $userRepository)
  139.     {
  140.        // $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  141.         //Récupère la marchandise avec l'id $id
  142.         $em $this->getDoctrine()->getManager();
  143.         $marchandise $em->getRepository(Marchandise::class)->findOneBy(['id' => $id]);
  144.         //Si la catégorie n'existe pas on throw une 404
  145.         if (!$marchandise) {
  146.             throw $this->createNotFoundException('produit non trouvé');
  147.         }
  148.         $id $_SESSION['_sf2_attributes']['gaeaUserId'];
  149.         $user $userRepository->findOneByGaeaId($id);
  150.         $fileName null;
  151.         $oldImage $marchandise->getImage();
  152.         if($marchandise->getImage() != null){
  153.             // Le formulaire a besoin du fichier image et non pas du nom de l'image
  154.             $marchandise->setImage(
  155.                 new File($this->getParameter('products').'/'.$marchandise->getImage())
  156.             );
  157.             
  158.             $fileName $marchandise->getImage()->getFileName();
  159.             //dd($fileName);
  160.         }
  161.         $form $this->createForm(ProductType::class, $marchandise, array(
  162.             'user' => $user
  163.         ));
  164.         $subcategories $marchandise->getSubCategories();
  165.         foreach ($subcategories as $subcategory) {
  166.             //$marchandise->removeSubcategory($subcategory);
  167.             $subcategory->removeProduct($marchandise);
  168.         }
  169.         //dd($subcategories);
  170.         $form->handleRequest($request);
  171.         if ($form->isSubmitted() && $form->isValid()) {
  172.             $marchandise->setUpdateddate(new \DateTime());
  173.             $slugify = new Slugify();
  174.             $slug=$slugify->slugify($marchandise->getName());
  175.             $marchandise->setSlug($slug);
  176.             if($marchandise->isWantevaluation()){
  177.                 $marchandise->setNiveau('N.1');
  178.             } else{
  179.                 $marchandise->setNiveau('N.0');
  180.             }
  181.             //$image=$marchandise->getImage();
  182.             //$marchandise->setImage($image);
  183.             if ($form->get('image')->getData() !== null) {
  184.                 $file $marchandise->getImage();
  185.                 //if ($file) {
  186.                 /*$fileName = uniqid() . '.' . $file->guessExtension();
  187.                 $file->move($this->getParameter('products'), $fileName);*/
  188.                 $fileName $fileUploader->upload($file$this->getParameter('products'));
  189.                 $marchandise->setImage($fileName);
  190.                 //}
  191.             } else {
  192.                 // On doit reset manuellement l'image à l'ancienne valeur si pas de modif car le formulaire le met à null automatiquement
  193.                 $marchandise->setImage($oldImage);
  194.             }
  195.             $subcategories $marchandise->getSubCategories();
  196.             //dd($subcategories);
  197.             //$marchandise->setSubCategories($subcategories);
  198.             foreach ($subcategories as $subcategory) {
  199.                 $subcategory->addProduct($marchandise);
  200.             }
  201.             $em->persist($marchandise);
  202.             $em->flush();
  203.             return $this->redirectToRoute('productManage');
  204.         }
  205.         /*if($form->isSubmitted() && !$form->isValid()) {
  206.             $marchandise->setImage(null);
  207.             $em->persist($marchandise);
  208.             $em->flush();
  209.         }*/
  210.         //dump($form);
  211.         $products $productRepository->findAllProductsByComp($user);
  212.         return $this->render('product/edit.html.twig', [
  213.             'form' => $form->createView(),
  214.             'file_name' => $fileName,
  215.             'user' => $user,
  216.             'product' => $marchandise,
  217.             'products' => $products,
  218.         ]);
  219.     }
  220.     public function deleteOldImages(){
  221.         // $images=glob("web/css/img/product/*.{jpg,jpeg,png,gif}");
  222.         $em $this->getDoctrine()->getManager();
  223.         $marchandises $em->getRepository('App\Entity\Marchandise')->findAll();
  224.         //dump(glob("web/css/img/product/*.png"));
  225.         foreach (glob("web/css/img/product/*.png") as $filename) {
  226.             echo "$filename occupe " filesize($filename) . "\n";
  227.             //dump($filename);
  228.         }
  229.         /*
  230.         foreach(glob("web/css/img/product/*.png") as $imageValue){
  231.             //dump('boucle 1');
  232.             foreach($marchandises as $productValue){
  233.                 //dump('boucle 2');
  234.                 $path=$productValue->getImage();
  235.                 if($images[$imageValue]==$path){
  236.                     //dump('condition if');
  237.                     break 2;
  238.                 }else{
  239.                     unlink($imageValue);
  240.                 }
  241.             }
  242.         }
  243.         */
  244.     }
  245.     /**
  246.      * @Route("/{_locale<en|fr|de|it|es>}/produit/{id}", name="productShow")
  247.      * @param $id
  248.      * @return Response
  249.      */
  250.     public function show($id): Response
  251.     {
  252.         $n=0;
  253.         if($this->isGranted('IS_AUTHENTICATED_FULLY'))
  254.         {
  255.             $em $this->getDoctrine()->getManager();
  256.             $product $em->getRepository(Product::class)->findOneBy( array('id' => $id ) );
  257.             $user=$this->getUser();
  258.             $productFav $em->getRepository(ProductFav::class)->findOneBy( array('user' => $user,'product' => $product ) );
  259.             if($productFav == null)
  260.         {
  261.             $n=0;
  262.         } else {
  263.             $n=1;
  264.         }
  265.         } else {
  266.             $n=3;
  267.         }
  268.         $em $this->getDoctrine()->getManager();
  269.         $product $em->getRepository(Product::class)->findOneBy(['id' => $id]);
  270.         // dump($product);
  271.         /*$date=(new \DateTime());
  272.         $clicks = $em->getRepository(ProductClick::class)->findOneBy( array('product' => $product, 'date' => $date) );
  273.         if ($clicks==null):
  274.             $click = new ProductClick();
  275.             $click->setDate($date);
  276.             $click->setProduct($product);
  277.             $click->setCompany($product->getCompany());
  278.             $click->setNumber(1);
  279.             $em->persist($click);
  280.             $em->flush();
  281.         else :
  282.             $clicks->setNumber($clicks->getNumber()+1);
  283.             $em->persist($clicks);
  284.             $em->flush();
  285.         endif;*/
  286.         // RECUPERATION DES COMMENTAIRES/AVIS
  287.         $avis = new ArrayCollection();
  288.         //  on separe en 2 pour users et company
  289.         $tableaux_avis_user =[];
  290.         $tableaux_avis_company =[];
  291.             $count1 0;
  292.             $count2 0;
  293.             $note1 0;
  294.             $note2 0;
  295.             foreach ($product->getProductComments() as &$comment) {
  296.                 $avis->add($comment);
  297.                 $id_product $product->getId();
  298.                 if ($comment->getCreator() instanceof Company)
  299.                 {
  300.                     // dump('company bro for the article',$id_product);
  301.                     $count1 ++;
  302.                     $note1 += random_int(0,5);
  303.                     $moyenne1 intval(ceil($note1 $count1));
  304.                     $tableaux_avis_company['avis_'.$id_product]  = $count1;
  305.                     $tableaux_avis_company['note_'.$id_product]  = $moyenne1;
  306.                 }
  307.                 else{
  308.                     //dump('user bro for the article',$id_product);
  309.                     $count2 ++;
  310.                     $note2 += random_int(0,5);
  311.                     $moyenne2 intval(ceil($note2 $count2));
  312.                     $tableaux_avis_user['avis_'.$id_product]  = $count2;
  313.                     $tableaux_avis_user['note_'.$id_product]  = $moyenne2;
  314.                 }
  315.             }
  316.         if (!$product) {  
  317.             throw $this->createNotFoundException('produit/service non trouvé');
  318.         }
  319.         foreach ($product->getProductFavProducts() as $fav
  320.         {
  321.             $product->addProductFavProduct($fav);
  322.         }
  323.     // dd($product);
  324.     // dd($product->getSizes());
  325.         if ($product instanceof Marchandise
  326.         {
  327.             $marchandise $em->getRepository(Marchandise::class)->findOneBy(['id' => $id]);
  328.            // dd('hello marchandises');
  329.             return $this->render('product/ficheProduit.html.twig', [
  330.                 'marchandise' => $marchandise,
  331.                 'product'=>$product,
  332.                 'avis_users'=>$tableaux_avis_user,
  333.                 'avis_companies'=>$tableaux_avis_company,
  334.                 'n'=>$n,
  335.                 'sizes' => $product->getSizes()
  336.             ]);
  337.         }
  338.         elseif ($product instanceof Service) {
  339.             $service $em->getRepository(Service::class)->findOneBy(['id' => $id]);
  340.             // RECUPERATION DES COMMENTAIRES/AVIS
  341.             $avis = new ArrayCollection();
  342.             //  on separe en 2 pour users et company
  343.             $tableaux_avis_user =[];
  344.             $tableaux_avis_company =[];
  345.             $count1 0;
  346.             $count2 0;
  347.             $note1 0;
  348.             $note2 0;
  349.             foreach ($service->getProductComments() as &$comment) {
  350.                 $avis->add($comment);
  351.                 $id_product $service->getId();
  352.                 if ($comment->getCreator() instanceof Company)
  353.                 {
  354.                     // dump('company bro for the article',$id_product);
  355.                     $count1 ++;
  356.                     $note1 += random_int(0,5);
  357.                     $moyenne1 intval(ceil($note1 $count1));
  358.                     $tableaux_avis_company['avis_'.$id_product]  = $count1;
  359.                     $tableaux_avis_company['note_'.$id_product]  = $moyenne1;
  360.                 }
  361.                 else{
  362.                     //dump('user bro for the article',$id_product);
  363.                     $count2 ++;
  364.                     $note2 += random_int(0,5);
  365.                     $moyenne2 intval(ceil($note2 $count2));
  366.                     $tableaux_avis_user['avis_'.$id_product]  = $count2;
  367.                     $tableaux_avis_user['note_'.$id_product]  = $moyenne2;
  368.                 }
  369.             }
  370.             //dd('hello services');
  371.             return $this->render('services/FicheService.html.twig', [
  372.                 'service' => $service,
  373.                 'product'=>$product,
  374.                 'avis_users'=>$tableaux_avis_user,
  375.                 'avis_companies'=>$tableaux_avis_company,
  376.                 'n'=>$n
  377.             ]);
  378.         }
  379.     }
  380.     /**
  381.      * @Route("/{_locale<en|fr|de|it|es>}/produits", name="productManage")
  382.      */
  383.     public function manage(CategoryRepository $categoryRepositoryUserRepository $userRepository): Response
  384.     {
  385.        // $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  386.         
  387.         // $marchandises = $this->getDoctrine()
  388.         //     ->getRepository('App:Marchandise')
  389.         //     ->createQueryBuilder('p')
  390.         //     ->andWhere('p.company = :id')
  391.         //     ->setParameter('id', $this->getUser()->getId())
  392.         //     ->getQuery()
  393.         //     ->getResult();
  394.         $id $_SESSION['_sf2_attributes']['gaeaUserId'];
  395.         $company $userRepository->findOneByGaeaId($id);
  396.         $user $userRepository->findOneByGaeaId($id);
  397.         // si on veut recuperer et les marchandises et les services
  398.         $products $this->getDoctrine()
  399.             ->getRepository('App\Entity\Product')
  400.             ->createQueryBuilder('p')
  401.             ->andWhere('p.company = :id')
  402.             ->setParameter('id'$user->getId())
  403.             ->getQuery()
  404.             ->getResult();
  405.         // RECUPERATION DES COMMENTAIRES/AVIS
  406.         $avis = new ArrayCollection();
  407.         //  on separe en 2 pour users et company
  408.         $tableaux_avis_company =[
  409.         ];
  410.         $tableaux_avis_user =[
  411.         ];
  412.         foreach ($products as $product)
  413.         {
  414.             $count1 0;
  415.             $count2 0;
  416.             $note1 0;
  417.             $note2 0;
  418.             foreach ($product->getProductComments() as &$comment) {
  419.                 $avis->add($comment);
  420.                 $id_product $product->getId();
  421.                 if ($comment->getCreator() instanceof Company)
  422.                 {
  423.                    // dump('company bro for the article',$id_product);
  424.                     $count1 ++;
  425.                     $note1 += random_int(0,5);
  426.                     $moyenne1 intval(ceil($note1 $count1));
  427.                     $tableaux_avis_company['avis_'.$id_product]  = $count1;
  428.                     $tableaux_avis_company['note_'.$id_product]  = $moyenne1;
  429.                 }
  430.                 else{
  431.                     //dump('user bro for the article',$id_product);
  432.                     $count2 ++;
  433.                     $note2 += random_int(0,5);
  434.                     $moyenne2 intval(ceil($note2 $count2));
  435.                     $tableaux_avis_user['avis_'.$id_product]  = $count2;
  436.                     $tableaux_avis_user['note_'.$id_product]  = $moyenne2;
  437.                 }
  438.             }
  439.         }
  440.         $em $this->getDoctrine()->getManager();
  441.         $subcategories $em->getRepository(Subcategory::class)->findByCompany($company->getId());
  442.         $this->deleteOldImages();
  443.         return $this->render('product/manage.html.twig', [
  444.             // 'marchandises' => $marchandises,
  445.             'products' => $products,
  446.             'avis_users'=>$tableaux_avis_user,
  447.             'avis_companies'=>$tableaux_avis_company,
  448.             'company' => $company,
  449.             'subcategories' => $subcategories
  450.         ]);
  451.     }
  452.     /**
  453.      * @Route("/{_locale<en|fr|de|it|es>}/produit/recherche/{param}", name="productSearch")
  454.      * @param $param
  455.      * @return Response
  456.      */
  457.     public function search($param): Response
  458.     {
  459.         $company $this->getUser();
  460.         $idcompany $company->getId();
  461.         //dd($idcompany);
  462.         $slugify = new Slugify();
  463.         $slug_param=$slugify->slugify($param);
  464.         //dd($slug_param);
  465.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  466.         $products $this->getDoctrine()
  467.             ->getRepository(Product::class)
  468.             ->createQueryBuilder('p')
  469.             ->where('p.owner = :idcompany')
  470.             ->setParameter('idcompany'$idcompany)
  471.             ->andWhere('p.slug LIKE :slug')
  472.             ->setParameter('slug'"%" $slug_param "%")
  473.             ->getQuery()
  474.             ->getResult();
  475.         //dd($products[0]->getName());
  476.         // RECUPERATION DES COMMENTAIRES/AVIS
  477.         $avis = new ArrayCollection();
  478.         //  on separe en 2 pour users et company
  479.         $tableaux_avis_company = [
  480.         ];
  481.         $tableaux_avis_user = [
  482.         ];
  483.         foreach ($products as $product) {
  484.             $count1 0;
  485.             $count2 0;
  486.             $note1 0;
  487.             $note2 0;
  488.             foreach ($product->getProductComments() as &$comment) {
  489.                 $avis->add($comment);
  490.                 $id_product $product->getId();
  491.                 if ($comment->getCreator() instanceof Company) {
  492.                     // dump('company bro for the article',$id_product);
  493.                     $count1++;
  494.                     $note1 += random_int(05);
  495.                     $moyenne1 intval(ceil($note1 $count1));
  496.                     $tableaux_avis_company['avis_' $id_product] = $count1;
  497.                     $tableaux_avis_company['note_' $id_product] = $moyenne1;
  498.                 } else {
  499.                     //dump('user bro for the article',$id_product);
  500.                     $count2++;
  501.                     $note2 += random_int(05);
  502.                     $moyenne2 intval(ceil($note2 $count2));
  503.                     $tableaux_avis_user['avis_' $id_product] = $count2;
  504.                     $tableaux_avis_user['note_' $id_product] = $moyenne2;
  505.                 }
  506.             }
  507.         }
  508.         foreach ($products as $res)
  509.         {
  510.             $produits[] = $res->getId();
  511.         }
  512.         //dd($produits);
  513.         return new JsonResponse([
  514.             'produits'=>$produits,
  515.         ]);
  516.        /* return new JsonResponse([
  517.             'utilisateurs'=>json_encode($tableaux_avis_user),
  518.             'entreprises'=>json_encode($tableaux_avis_user),
  519.         ]); */
  520.     }
  521. // fonction pour supprimer les marchandises uniquement //
  522.     /**
  523.      * @Route("/{_locale<en|fr|de|it|es>}/produit/supprimer/{id}", name="productDelete")
  524.      * @param $id
  525.      * @return Response
  526.      */
  527.     public function delete($id): Response
  528.     {
  529.         // $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  530.         $em $this->getDoctrine()->getManager();
  531.         $marchandise $em->getRepository('App\Entity\Marchandise')->findOneBy(array('id' => $id));
  532.         $em->remove($marchandise);
  533.         $em->flush();
  534.         $path $marchandise->getImage();
  535.         /*
  536.         if ($this->getCompany() == $product->getCompany()) {
  537.             //DELETE
  538.             $em->remove($marchandise);
  539.             $em->flush();
  540.         }
  541.         */
  542.         /*
  543.         if ($this->getUser()->getRole() == "ROLE_COMPANY" and $this->getUser() == $product->getCompany()) {
  544.             //DELETE
  545.             $em->remove($marchandise);
  546.             $em->flush();
  547.         }
  548.         */
  549.         return $this->redirectToRoute('productManage');
  550.         //return $this->render('product/manage.html.twig', $path);
  551.     }
  552.     /**
  553.      * @Route("/{_locale<en|fr|de|it|es>}/productsFav", name="productsFav")
  554.      * @return
  555.      */
  556.     public function productsFav()
  557.     {
  558.         $em $this->getDoctrine()->getManager();
  559.         $productsFav $em->getRepository(ProductFav::class)->findBy(['user' => $this->getUser()]);
  560.         $products=new ArrayCollection();
  561.         foreach($productsFav as $comp)
  562.         {
  563.             $products[]=$comp->getProductx();
  564.         }
  565.         return $this->render('product/productsFav.html.twig', [
  566.             'products' => $products,
  567.             'productsFav'=>$productsFav
  568.         ]);
  569.     }
  570.          // function show Productdetails
  571.                      
  572.      // @Route("product/show/{id}", name="ShowProductDetails" , requirements={"id"="\d+"})
  573.      
  574.      
  575.     // public function showDetails($id){
  576.      
  577.     //     $em = $this->getDoctrine()->getManager();
  578.     //     $product = $em->getRepository(Product::class)->findOneBy(['id' => $id]);
  579.     //     // $marchandise =$em->getRepository(Marchandise::class)->findAll();
  580.     //     // $product =$em->getRepository(Product::class)->findAll();
  581.         
  582.     //     return $this->render('product/showProductDetails.html.twig', [
  583.     //         'product' => $product
  584.     //         // 'marchandise' => $marchandise
  585.     //     ]); 
  586.     // }
  587.   
  588.     /**
  589.      * @Route("/{_locale<en|fr|de|it|es>}/produits/{categoryId}/{subcategoryId}/{companyId}", name="ShowProductDetails" , requirements={"id"="\d+"})
  590.      * 
  591.      */
  592.     public function productdetails(CategoryRepository $categoryRepository$categoryId$subcategoryId,$companyId): Response
  593.     {   
  594.         
  595.         $em $this->getDoctrine()->getManager();
  596.         $company $em->getRepository(Company::class)->findOneBy(['id' => $companyId]);
  597.         $subcategory =$em->getRepository(Subcategory::class)->findOneBy(['id' => $subcategoryId]);
  598.         $subcategoryName$subcategory->getName();
  599.         $subcategories $em->getRepository(Subcategory::class)->findByCompany($company->getId());
  600.       
  601.         $marchandises $this->getDoctrine()
  602.             ->getRepository('App:Marchandise')
  603.             ->createQueryBuilder('p')
  604.             ->andWhere('p.company = :id')
  605.             ->setParameter('id'$company->getId())
  606.             ->getQuery()
  607.             ->getResult();
  608.             // si on veut recuperer et les marchandises et les services
  609.         $products $this->getDoctrine()
  610.             ->getRepository('App:Product')
  611.             ->createQueryBuilder('p')
  612.             ->andWhere('p.company = :id')
  613.             ->setParameter('id'$company->getId())
  614.             ->getQuery()
  615.             ->getResult();
  616.         // RECUPERATION DES COMMENTAIRES/AVIS
  617.         $avis = new ArrayCollection();
  618.         //  on separe en 2 pour users et company
  619.         $tableaux_avis_company =[
  620.         ];
  621.         $tableaux_avis_user =[
  622.         ];
  623.         foreach ($products as $product)
  624.         {
  625.             $count1 0;
  626.             $count2 0;
  627.             $note1 0;
  628.             $note2 0;
  629.             foreach ($product->getProductComments() as &$comment) {
  630.                 $avis->add($comment);
  631.                 $id_product $product->getId();
  632.                 if ($comment->getCreator() instanceof Company)
  633.                 {
  634.                    // dump('company bro for the article',$id_product);
  635.                     $count1 ++;
  636.                     $note1 += random_int(0,5);
  637.                     $moyenne1 intval(ceil($note1 $count1));
  638.                     $tableaux_avis_company['avis_'.$id_product]  = $count1;
  639.                     $tableaux_avis_company['note_'.$id_product]  = $moyenne1;
  640.                 }
  641.                 else{
  642.                     //dump('user bro for the article',$id_product);
  643.                     $count2 ++;
  644.                     $note2 += random_int(0,5);
  645.                     $moyenne2 intval(ceil($note2 $count2));
  646.                     $tableaux_avis_user['avis_'.$id_product]  = $count2;
  647.                     $tableaux_avis_user['note_'.$id_product]  = $moyenne2;
  648.                 }
  649.           
  650.             }
  651.         }
  652.         $categories $categoryRepository->findAll();
  653.        
  654.         $this->deleteOldImages();
  655.         return $this->render('product/showProductDetails.html.twig', [
  656.             'marchandises' => $marchandises,
  657.             'products' => $products,
  658.             'avis_users'=>$tableaux_avis_user,
  659.             'avis_companies'=>$tableaux_avis_company,
  660.             'company' => $company,
  661.             'categories' => $categories,
  662.             'categoryId' => $categoryId
  663.             'subcategoryId' => $subcategoryId,
  664.             'subcategoryName'=>$subcategoryName,
  665.             'subcategories' => $subcategories
  666.         ]);
  667.    
  668. }
  669.  /**
  670.      * @Route("/{_locale<en|fr|de|it|es>}/ficheProduitNonConnecte/{categoryId}/{subcategoryId}/{companyId}/{productId}", name="ficheProduitNonConnecte" , requirements={"id"="\d+"})
  671.      * 
  672.      */
  673.     public function productdetailsNonConnecte(CategoryRepository $categoryRepository$companyId$productId,$categoryId,$subcategoryId): Response
  674.     {
  675.           
  676.         
  677.         
  678.             $em $this->getDoctrine()->getManager();
  679.             $product $em->getRepository(Product::class)->findOneBy( array('id' => $productId ) );
  680.             $user$em->getRepository(Company::class)->findOneBy(['id' => $companyId]);
  681.             $productFav $em->getRepository(ProductFav::class)->findOneBy( array('user' => $user,'product' => $product ) );
  682.           
  683.         
  684.         // $em = $this->getDoctrine()->getManager();
  685.         // $product = $em->getRepository(Product::class)->findOneBy(['id' => $id]);
  686.         $date=(new \DateTime());
  687.         $clicks $em->getRepository(ProductClick::class)->findOneBy( array('product' => $product'date' => $date) );
  688.         if ($clicks==null):
  689.             $click = new ProductClick();
  690.             $click->setDate($date);
  691.             $click->setProduct($product);
  692.             $click->setCompany($product->getCompany());
  693.             $click->setNumber(1);
  694.             $em->persist($click);
  695.             $em->flush();
  696.         else :
  697.             $clicks->setNumber($clicks->getNumber()+1);
  698.             $em->persist($clicks);
  699.             $em->flush();
  700.         endif;
  701.         // RECUPERATION DES COMMENTAIRES/AVIS
  702.         $avis = new ArrayCollection();
  703.         //  on separe en 2 pour users et company
  704.         $tableaux_avis_user =[
  705.         ];
  706.         $tableaux_avis_company =[
  707.         ];
  708.             $count1 0;
  709.             $count2 0;
  710.             $note1 0;
  711.             $note2 0;
  712.             foreach ($product->getProductComments() as &$comment) {
  713.                 $avis->add($comment);
  714.                 $id_product $product->getId();
  715.                 if ($comment->getCreator() instanceof Company)
  716.                 {
  717.                     // dump('company bro for the article',$id_product);
  718.                     $count1 ++;
  719.                     $note1 += random_int(0,5);
  720.                     $moyenne1 intval(ceil($note1 $count1));
  721.                     $tableaux_avis_company['avis_'.$id_product]  = $count1;
  722.                     $tableaux_avis_company['note_'.$id_product]  = $moyenne1;
  723.                 }
  724.                 else{
  725.                     //dump('user bro for the article',$id_product);
  726.                     $count2 ++;
  727.                     $note2 += random_int(0,5);
  728.                     $moyenne2 intval(ceil($note2 $count2));
  729.                     $tableaux_avis_user['avis_'.$id_product]  = $count2;
  730.                     $tableaux_avis_user['note_'.$id_product]  = $moyenne2;
  731.                 }
  732.             }
  733.         if (!$product) {  
  734.             throw $this->createNotFoundException('produit/service non trouvé');
  735.         }
  736.         foreach ($product->getProductFavProducts() as $fav) {
  737.             $product->addProductFavProduct($fav);
  738.         }
  739.     //dd($product);
  740.         if ($product instanceof Marchandise) {
  741.             $marchandise $em->getRepository(Marchandise::class)->findOneBy(['id' => $productId]);
  742.         // dd('hello marchandises');
  743.             return $this->render('product/ficheProduitNonConnecte.html.twig', [
  744.                 'marchandise' => $marchandise,
  745.                 'product'=>$product,
  746.                 'company'=>$user,
  747.                 'avis_users'=>$tableaux_avis_user,
  748.                 'avis_companies'=>$tableaux_avis_company,
  749.                 'subcategoryId'=>$subcategoryId,
  750.                 'categoryId'=>$categoryId
  751.                 // 'n'=>$n
  752.             ]);
  753.         }
  754.         elseif ($product instanceof Service) {
  755.             $service $em->getRepository(Service::class)->findOneBy(['id' => $productId]);
  756.             // RECUPERATION DES COMMENTAIRES/AVIS
  757.             $avis = new ArrayCollection();
  758.             //  on separe en 2 pour users et company
  759.             $tableaux_avis_user =[
  760.             ];
  761.             $tableaux_avis_company =[
  762.             ];
  763.             $count1 0;
  764.             $count2 0;
  765.             $note1 0;
  766.             $note2 0;
  767.             foreach ($service->getProductComments() as &$comment) {
  768.                 $avis->add($comment);
  769.                 $id_product $service->getId();
  770.                 if ($comment->getCreator() instanceof Company)
  771.                 {
  772.                     // dump('company bro for the article',$id_product);
  773.                     $count1 ++;
  774.                     $note1 += random_int(0,5);
  775.                     $moyenne1 intval(ceil($note1 $count1));
  776.                     $tableaux_avis_company['avis_'.$id_product]  = $count1;
  777.                     $tableaux_avis_company['note_'.$id_product]  = $moyenne1;
  778.                 }
  779.                 else{
  780.                     //dump('user bro for the article',$id_product);
  781.                     $count2 ++;
  782.                     $note2 += random_int(0,5);
  783.                     $moyenne2 intval(ceil($note2 $count2));
  784.                     $tableaux_avis_user['avis_'.$id_product]  = $count2;
  785.                     $tableaux_avis_user['note_'.$id_product]  = $moyenne2;
  786.                 }
  787.             }
  788.             //dd('hello services');
  789.             return $this->render('services/FicheServiceNonConnecte.html.twig', [
  790.                 'service' => $service,
  791.                 'product'=>$product,
  792.                 'avis_users'=>$tableaux_avis_user,
  793.                 'avis_companies'=>$tableaux_avis_company,
  794.                 'subcategoryId'=>$subcategoryId,
  795.                 'categoryId'=>$categoryId,
  796.                 'companyId'=>$companyId
  797.                 // 'n'=>$n
  798.             ]);
  799.         }
  800.     }
  801.     /**
  802.      *@Route("/{_locale<en|fr|de|it|es>}/ajouter_un_produit", name="add_product", methods={"POST"})
  803.      */
  804.     public function ajouter(
  805.     ProductRepository $repositoryProduct
  806.     SerializerSerializerInterface $serializer
  807.     Request $request){
  808.             
  809.         $dataProduct json_decode($request->getContent());
  810.         
  811.         $product = new Product();
  812.         $product->setName($dataProduct->name);
  813.         $product->setPrice($dataProduct->price);
  814.         $product->setDescription($dataProduct->description);
  815.         $product->setCurrency($dataProduct->currency);
  816.         $product->setWantevaluation($dataProduct->wantevaluation);
  817.         //$product->setCompany($dataProduct->company);
  818.         
  819.         $repositoryProduct->save($producttrue);
  820.         $encoder = new JsonEncoder();
  821.         $defaultContext = [
  822.             AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object$format$context) {
  823.                 return $object->getId();
  824.             },
  825.         ];
  826.         $normalizer = new ObjectNormalizer(nullnullnullnullnullnull$defaultContext);
  827.         $serializer = new Serializer([$normalizer], [$encoder]);
  828.         
  829.         $refreshUser $repositoryProduct->findAll();
  830.         $serializationAjoutProduit $serializer->serialize($refreshUser'json', []);
  831.         $response = new Response($serializationAjoutProduit200, [
  832.                 'Content-Type' => "application/json"
  833.         ]);
  834.         return $response;
  835.     }
  836.        
  837. }