src/Controller/ExposantController.php line 104

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Category;
  4. use App\Entity\Entreprise;
  5. use App\Entity\Produits;
  6. use App\Entity\Visiteur;
  7. use App\Form\ProfileUserType;
  8. use App\Repository\CategoryRepository;
  9. use App\Repository\EntrepriseRepository;
  10. use App\Repository\ProduitsRepository;
  11. use App\Repository\VisiteurRepository;
  12. use DateTime;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Knp\Component\Pager\PaginatorInterface;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class ExposantController extends AbstractController
  20. {
  21.     private $entityManager;
  22.     private $visiteurRepo;
  23.     private $entrepriseRepository;
  24.     public function __construct(EntityManagerInterface $entityManagerVisiteurRepository $visiteurRepositoryEntrepriseRepository $entrepriseRepository)
  25.     {
  26.         $this->entityManager $entityManager;
  27.         $this->visiteurRepo $visiteurRepository;
  28.         $this->entrepriseRepository $entrepriseRepository;
  29.     }
  30.     /**
  31.      * @Route("/tous-les-exposants", name="app_exposant")
  32.      */
  33.     public function exposants(Request $requestCategoryRepository $categoryRepositoryPaginatorInterface $paginator)
  34.     {
  35.         $query $this->entrepriseRepository->findAll();
  36.         $exposants $paginator->paginate(
  37.             $query,
  38.             $request->query->getInt('page'1),
  39.             8
  40.         );
  41.         return $this->render('exposant/index.html.twig', [
  42.             'exposants' => $exposants,
  43.             'categories' => $categoryRepository->findAll(),
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/exposant/{slug}", name="app_show_exposant")
  48.      */
  49.     public function index(Entreprise $entrepriseCategoryRepository $categoryRepository): Response
  50.     {
  51.         return $this->render('exposant/show.html.twig', [
  52.             'exposant' => $entreprise,
  53.             'categories' => $categoryRepository->findAll(),
  54.         ]);
  55.     }
  56.     /**
  57.      * @Route("/produit/{slug}", name="app_product_detail")
  58.      */
  59.     public function produit(Request $requestProduits $produitCategoryRepository $categoryRepositoryProduitsRepository $produitrepository)
  60.     {
  61.         $cat_id $produit->getCategories()->getId();
  62.         $similarProducts $produitrepository->findBy(
  63.             ['categories' => $cat_id ], 
  64.             ['createdAt' =>'DESC'],
  65.             5
  66.         );
  67.         $request Request::createFromGlobals();
  68.         $clientIp $request->getClientIp();
  69.         $visiteur = new Visiteur();
  70.         $ipAdress $this->visiteurRepo->findOneBy(['adressIp' => $clientIp]);
  71.         if(!$ipAdress){
  72.             $visiteur->setAdressIp($clientIp);
  73.             $visiteur->setConnectedAt(new DateTime('now'));
  74.             $produit->setVue($produit->getVue()+1);
  75.             $this->entityManager->persist($visiteur);
  76.             $this->entityManager->persist($produit);
  77.         }
  78.         $this->entityManager->flush();
  79.         return $this->render('exposant/show_product.html.twig', [
  80.             'produit' => $produit,
  81.             'categories' => $categoryRepository->findAll(),
  82.             'similarProducts' => $similarProducts
  83.         ]);
  84.     }
  85.     /**
  86.      * @Route("/categorie/{slug}", name="app_category")
  87.      */
  88.     public function showProductByCategory(Request $request,Category $category,CategoryRepository $categoryRepository
  89.     ProduitsRepository $produitrepositoryPaginatorInterface $paginator)
  90.     {
  91.         $query $produitrepository->findBy(
  92.             ['categories' => $category],
  93.             ['createdAt' => 'ASC']
  94.         );
  95.         $produits $paginator->paginate(
  96.             $query,
  97.             $request->query->getInt('page'1),
  98.             15
  99.         );
  100.         return $this->render('exposant/product_by_category.html.twig', [
  101.             'produits' => $produits,
  102.             'category' => $category,
  103.             'categories' => $categoryRepository->findAll(),
  104.         ]);
  105.     }
  106.     /**
  107.      * @Route("/me/profile", name="app_profile", methods={"GET", "POST"})
  108.      */
  109.     public function profile(Request $request)
  110.     {
  111.         $user $this->getUser();
  112.         $form $this->createForm(ProfileUserType::class, $user);
  113.         $form->handleRequest($request);
  114.         if($form->isSubmitted() && $form->isValid()){
  115.             $em $this->getDoctrine()->getManager();
  116.             $em->persist($user);
  117.             $em->flush();
  118.             $this->addFlash('success''Profil mis à jour');
  119.             return $this->redirectToRoute('app_profile');
  120.         }
  121.         return $this->render('admin/exposant/profile.html.twig', [
  122.             'form' => $form->createView(),
  123.         ]);
  124.     }
  125. }