src/Controller/ProductController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Classe\Search;
  4. use App\Entity\Product;
  5. use App\Entity\Comments;
  6. use App\Form\SearchType;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. class ProductController extends AbstractController
  13. {
  14.      private $entityManager;
  15.      public function __construct(EntityManagerInterface $entityManager){
  16.         $this->entityManager $entityManager;
  17.      }
  18.     #[Route('/Nos-activites'name'app_products')]
  19.     public function index(Request $request): Response
  20.     {
  21.         $search = new Search();
  22.         $form $this->createForm(SearchType::class , $search);
  23.         $activity $this->entityManager->getRepository(Product::class)->findAll();
  24.         
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()){
  27.         $products $this->entityManager->getRepository(Product::class)->findWithSearch($search);
  28.         }else{
  29.             $products $this->entityManager->getRepository(Product::class)->findAll();
  30.         }
  31.         return $this->render('product/index.html.twig', [
  32.             'products' => $products,
  33.             'form' => $form->CreateView(),
  34.             'activity' => $activity,
  35.         ]);
  36.     }
  37.     #[Route('/Activite/{slug}'name'app_product')]
  38.     public function show($slug): Response
  39.     {
  40.         $comments $this->entityManager->getRepository(Comments::class)->findAll();
  41.         $product $this->entityManager->getRepository(Product::class)->findOneBySlug($slug);
  42.         $products $this->entityManager->getRepository(Product::class)->findByIsBest(1);
  43.         $activity $this->entityManager->getRepository(Product::class)->findAll();
  44.         $commentsByProduct = array();
  45.         if(!$product){
  46.             return $this->redirectToRoute('app_products');
  47.         }
  48.         foreach ($activity as $acti) {
  49.             $idProduct $acti->getId();
  50.             $commentsId = array();
  51.             $i 1;
  52.             foreach($comments as $comment) {
  53.                 if($comment->getActivity()->getId() == $idProduct && $comment->isIsValid()) {
  54.                     array_push($commentsId, [$i++, $comment]);
  55.                 }
  56.             }
  57.             array_push($commentsByProduct, [$idProduct$commentsId1]);
  58.         }
  59.         return $this->render('product/show.html.twig', [
  60.             'product' => $product,
  61.             'products' => $products,
  62.             'activity' => $activity,
  63.             'commentsByProduct' => $commentsByProduct
  64.         ]);
  65.     }
  66. }