src/Controller/Main/ArticleController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Main;
  3. use App\Entity\Article;
  4. use App\Entity\ArticleFiltre;
  5. use App\Entity\ArticleSearch;
  6. use App\Entity\Comment;
  7. use App\Form\ArticleFiltreType;
  8. use App\Form\ArticleSearchType;
  9. use App\Form\CommentType;
  10. use App\Repository\ArticleBuyRepository;
  11. use App\Repository\ArticleRepository;
  12. use App\Repository\BrandRepository;
  13. use App\Repository\CategoryRepository;
  14. use App\Repository\CommentRepository;
  15. use App\Repository\ParentCategoryRepository;
  16. use Cocur\Slugify\Slugify;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. class ArticleController extends AbstractController
  24. {
  25.     /**
  26.      * @Route("article/{category}/{slug}/{id}", name="articles_show", requirements={"slug": "[a-z0-9\-]*"} )
  27.      */
  28.     public function showPaginatorInterface $paginatorInterfaceCommentRepository $commentRepositoryArticleBuyRepository $articleBuyRepositoryArticle $article,string $categorystring $slugRequest $requestArticleRepository $articleRepository): Response
  29.     {
  30.         if (!$article) {
  31.             throw $this->createNotFoundException(
  32.                 'No product found for id '
  33.             );
  34.         }
  35.         if($slug !== $article->getSlug() || $category !== $article->getCategory()->getSlug() ){
  36.             return $this->redirectToRoute('articles_show',
  37.                 [
  38.                     'category'=>$article->getCategory()->getSlug(),
  39.                     'slug'=>$article->getSlug(),
  40.                     'id'=>$article->getId()
  41.                 ],301);
  42.         }
  43.         $search = new ArticleSearch();
  44.         $form $this->createForm(ArticleSearchType::class,$search);
  45.         
  46.         $comment = new Comment();
  47.         $comment->setArticle($article);
  48.         
  49.         $formComment $this->createForm(CommentType::class, $comment);
  50.         $formComment->handleRequest($request);
  51.         
  52.         if ($formComment->isSubmitted() && $formComment->isValid()) {
  53.             if(!empty($this->getUser())){
  54.                 $comment->setUser($this->getUser());
  55.             }else{
  56.                 return $this->redirectToRoute('app_login');
  57.             }
  58.             $ratting $request->request->get('rating');
  59.             $comment->setRating($ratting);
  60.             $entityManager $this->getDoctrine()->getManager();
  61.             $entityManager->persist($comment);
  62.             $entityManager->flush();
  63.             $this->addFlash('success','Le commentaire a été enregistré');
  64.             return $this->redirectToRoute('articles_show',
  65.                 [
  66.                     'category'=>$article->getCategory()->getSlug(),
  67.                     'slug'=>$article->getSlug(),
  68.                     'id'=>$article->getId()
  69.                 ],Response::HTTP_SEE_OTHER);
  70.         }
  71.         $user $this->getUser();
  72.         $isBuy $isComment false;
  73.         if($user){
  74.             $client $user->getClient();
  75.             if($client)
  76.             {               
  77.                 $isBuy $articleBuyRepository->isBuy($client,$article);
  78.             }
  79.             $isComment $commentRepository->isComment($user,$article);
  80.         }
  81.         $pagination $paginatorInterface->paginate(
  82.             $articleRepository->showPagination(),
  83.             $request->query->getInt('page',1),
  84.             1
  85.         );
  86.         return $this->renderForm($this->getParameter('template').'/shop/show.html.twig', [
  87.             'article'=>$article,
  88.             'articles'=>$articleRepository->findBy(['enabled'=>true,'etat'=>'top'],null,12),
  89.             'form' => $form,
  90.             'formComment' => $formComment,
  91.             'is_buy'=>$isBuy,
  92.             'is_comment'=>$isComment,
  93.             'propositions'=>$articleRepository->findAll(),
  94.             'article_rand'=>$articleRepository->findRand(20,$article)
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/boutique", name="articles")
  99.      * @Route("/boutique/{category3}", name="articles_category3")
  100.      * //@Route("/boutique/{category3_slug}/{category2}", name="articles_category2")
  101.      * @Route("/boutique/{category3_slug}/{category2_slug}/{category}", name="articles_category")
  102.      */
  103.     public function index(
  104.         string $parent null,
  105.         string $category null,
  106.         string $category3 null,
  107.         string $category2 null,
  108.         SessionInterface $sessionInterface,
  109.         Request $request,
  110.         PaginatorInterface $paginator,
  111.         ArticleRepository $articleRepository,
  112.         CategoryRepository $categoryRepository,
  113.         BrandRepository $brandRepository): Response
  114.     {
  115.         
  116.         $search = new ArticleSearch();
  117.         $route_name $request->attributes->get('_route');
  118.         if ($route_name == 'articles_category') {
  119.             $search->setCategory($category);
  120.         } elseif ($route_name == 'articles_category3') {
  121.             $search->setCategory3($category3);
  122.         } elseif ($route_name == 'articles_category2') {
  123.             $search->setCategory2($category2);
  124.         }
  125.         
  126.         $sort_valide = [
  127.             'created_at''ASC''DESC''price'
  128.         ];
  129.         $sort_by = ($sessionInterface->get('sort_by'))? $sessionInterface->get('sort_by'): [];
  130.         if (!is_array($sort_by)) {
  131.             $sort_by explode(':'$sort_by);
  132.             if (!in_array($sort_by[0], $sort_valide)) {
  133.                 $sort_by = [];
  134.             }
  135.         }else{
  136.             $sort_by = [];
  137.         }
  138.         
  139.         $form $this->createForm(ArticleSearchType::class,$search)->handleRequest($request);
  140.     
  141.         $pagination $paginator->paginate(
  142.             $articleRepository->search(
  143.                 $search->getMots(),
  144.                 $search->getCategory3(),
  145.                 $search->getCategory2(),
  146.                 $search->getCategory(),
  147.                 $search->getMinPrice(),
  148.                 $search->getMaxPrice(),
  149.                 $search->getBrand(),
  150.                 $search->getEtat(),
  151.                 $sort_by
  152.             ),
  153.             $request->query->getInt('page',1),
  154.             24
  155.         );
  156.         return $this->renderForm($this->getParameter('template').'/shop/index.html.twig', [
  157.             'articles' => $pagination,
  158.             'form'=>$form,
  159.             'brands'=> $brandRepository->findAll(),
  160.             'breadcrumb'=>[
  161.                 'parent'=>ucfirst($parent),
  162.                 'category'=>ucfirst($category)
  163.             ],
  164.             'categorie'=>$categoryRepository->findOneBy([
  165.                 'title'=>$category
  166.             ]),
  167.             'category'=>$categoryRepository->findAll(),
  168.             'top_articles'=>$articleRepository->findBy(['enabled'=>true,'etat'=>'top'],null,12),
  169.         ]);
  170.     }
  171.     /**
  172.      * @Route("/suggestion/", name="suggestions")
  173.      */
  174.     public function suggestion($motsArticleRepository $articleRepository)
  175.     {
  176.        $search $articleRepository->searchJson($mots);
  177.         // dd($search);
  178.         return $this->json(
  179.             [
  180.                 'suggests'=>[ $search ]
  181.             ]
  182.         );
  183.     }
  184. }