src/Controller/NiveauController.php line 108

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contenu;
  4. use App\Entity\FavorisNiveau;
  5. use App\Entity\Formation;
  6. use App\Entity\Niveau;
  7. use App\Entity\Produit;
  8. use App\Entity\Profil;
  9. use App\Entity\UserLog;
  10. use App\Repository\ContenuRepository;
  11. use App\Repository\UserRepository;
  12. use App\Service\Access;
  13. use App\Service\NiveauService;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Knp\Component\Pager\PaginatorInterface;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. class NiveauController extends AbstractController
  23. {
  24.     /**
  25.      * @Route("/load/niveau/articles/{niveau}/{page}", name="load_article_niveau", defaults={"page" : 1},options={"expose" : true})
  26.      * @param Request $request
  27.      * @param SessionInterface $session
  28.      * @param ContenuRepository $contenuRepository
  29.      * @param Niveau $niveau
  30.      * @param int $page
  31.      * @return Response
  32.      */
  33.     public function load(Request $requestSessionInterface $sessionContenuRepository $contenuRepositoryNiveau $niveau$page 1): Response
  34.     {
  35.         $contenus $contenuRepository->getByProfilNiveau($this->getUser(), $niveau->getId(), 2020 * ($page 1));
  36.         $nextContenus $contenuRepository->getByProfilNiveau($this->getUser(), $niveau->getId(), 2020 $page);
  37.         $hasMore count($nextContenus) > 0;
  38.         return $this->render('niveau/next_contenus.html.twig', [
  39.             'contenus' => $contenus,
  40.             'niveau' => $niveau,
  41.             'page' => $page,
  42.             'nombre' => 20,
  43.             'sum' => $request->get('sum'0),
  44.             'has_more' => $hasMore,
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/niveau/favorite/{id}", name="niveau_favorite")
  49.      * @param Request $request
  50.      * @param Access $access
  51.      * @param Niveau $niveau
  52.      * @return RedirectResponse|Response
  53.      */
  54.     public function favorite(Request $requestAccess $accessNiveau $niveau)
  55.     {
  56.         $this->denyAccessUnlessGranted('favoris_niveau'true);
  57.         $em $this->getDoctrine()->getManager();
  58.         $user $this->getUser();
  59.         $fav $user->hasFavorisNiveau($niveau);
  60.         if (!$fav) {
  61.             $favoris $em->getRepository(FavorisNiveau::class)->getByUser($user->getId());
  62.             foreach ($favoris as $f) {
  63.                 $f->setPosition($f->getPosition() + 1);
  64.                 $em->persist($f);
  65.             }
  66.             $fav = new FavorisNiveau();
  67.             $fav->setUser($user);
  68.             $fav->setNiveau($niveau);
  69.             $fav->setPosition(1);
  70.             $em->persist($fav);
  71.         } else {
  72.             $em->remove($fav);
  73.             $favoris $em->getRepository(FavorisNiveau::class)->getByUserFromPosition($user->getId(), $fav->getPosition());
  74.             foreach ($favoris as $f) {
  75.                 $f->setPosition($f->getPosition() - 1);
  76.                 $em->persist($f);
  77.             }
  78.         }
  79.         $em->flush();
  80.         if ($request->isXmlHttpRequest()) {
  81.             return new Response();
  82.         } else {
  83.             $referer $request->headers->get('referer');
  84.             return $this->redirect($referer);
  85.         }
  86.     }
  87.     /**
  88.      * @Route("/{slug}", name="niveau", requirements={"slug"="^[^_].*"})
  89.      * @param Request $request
  90.      * @param SessionInterface $session
  91.      * @param Niveau $niveau
  92.      * @param PaginatorInterface $paginator
  93.      * @return RedirectResponse|Response
  94.      */
  95.     public function show(Request                $request,
  96.                          SessionInterface       $session,
  97.                          Niveau                 $niveau,
  98.                          PaginatorInterface     $paginator,
  99.                          UserRepository         $userRepository,
  100.                          EntityManagerInterface $entityManager
  101.     )
  102.     {
  103.         $liens '';
  104.         $em $this->getDoctrine()->getManager();
  105.         $isxhr $request->get('isxhr'false);
  106.         $user $userRepository->findUserById($this->getUser()->getId());
  107.         $univers $user->getUnivers() ?? $user->getPreferedUnivers();
  108.         $hierarchy = [];
  109.         /** @param Profil $profil */
  110.         $profil $session->get('profil'$this->getUser()->getProfil());
  111. //        if (!Access::isAllowedNiveau($niveau, $profil)) {
  112. //            if (!$isxhr) {
  113. //                $this->addFlash('danger', 'flash.permission_denied');
  114. //                return $this->redirectToRoute('home');
  115. //            } else {
  116. //                return new Response('');
  117. //            }
  118. //        }
  119.         $userLog = new UserLog($user, ['niveau' => $niveau]);
  120.         $em->persist($userLog);
  121.         $em->flush();
  122.         if (!$isxhr) {
  123.             if ($niveau && $niveau->getChildren()->count() > && $niveau->getLvl() != 1) {
  124.                 $sublvl null;
  125.                 foreach ($niveau->getChildren() as $lvl) {
  126.                     if (!$sublvl) {
  127.                         foreach ($lvl->getUnivers() as $p) {
  128.                             if ($univers->getId() == $p->getId()) {
  129.                                 $sublvl $lvl;
  130.                             }
  131.                         }
  132.                     }
  133.                 }
  134.                 if ($sublvl) {
  135.                     return $this->redirectToRoute('niveau', ['slug' => $sublvl->getSlug()]);
  136.                 }
  137.             }
  138.             if ($niveau->getType() == Niveau::NIVEAU_PRODUIT) {
  139.                 $produits $entityManager->getRepository(Produit::class)->getByUnivers($user$niveau->getId());
  140.                 if (count($produits) > 0) {
  141.                     return $this->redirectToRoute('contenu_show', ['slug' => $produits[0]->getSlug()]);
  142.                 }
  143.             }
  144.             if ($niveau->getType() == Niveau::NIVEAU_FORMATION) {
  145.                 $formations $entityManager->getRepository(Formation::class)->getByProfilNiveau($profil$niveau->getId());
  146.                 if (count($formations) > 0) {
  147.                     return $this->redirectToRoute('contenu_show', ['slug' => $formations[0]->getSlug()]);
  148.                 }
  149.             }
  150.         }
  151.         $hasMore false;
  152.         if ($niveau->getType() == Niveau::NIVEAU_MENU) {
  153.             $hierarchy $em->getRepository(Niveau::class)->findAllByParentProfil($profil$niveau->getRoot());
  154.         } else {
  155.             if ($niveau->getType() == Niveau::NIVEAU_ARTICLE) {
  156.                 $hierarchy $em->getRepository(Contenu::class)->getByProfilNiveau(
  157.                     $user,
  158.                     $niveau->getId(),
  159.                     false,
  160.                     null,
  161.                     false
  162.                 );
  163.                 //dd($user, $niveau, $hierarchy);
  164.             } else {
  165.                 $hierarchy $em->getRepository(Contenu::class)->getByProfilNiveau($user$niveau->getId(), falsenulltrue);
  166.             }
  167.             if ($niveau->getType() == Niveau::NIVEAU_VIDEO) {
  168.                 //$liens = $em->getRepository(Lien::class)->findBy(['lea' => '1']);
  169.                 $qty $request->query->getInt('qty'600);
  170.                 $hierarchy $paginator->paginate($hierarchy$request->query->getInt('page'1), $qty);
  171.                 $hierarchy->setCustomParameters(['align' => 'center']);
  172.                 $nextContenus $em->getRepository(Contenu::class)->getByProfilNiveau($user$niveau->getId(), 2020);
  173.                 $hasMore count($nextContenus) > 0;
  174.             }
  175.             if ($niveau->getType() == Niveau::NIVEAU_ARTICLE) {
  176.                 $qty $request->query->getInt('qty'24);
  177.                 $hierarchy $paginator->paginate($hierarchy$request->query->getInt('page'1), $qty);
  178.                 $hierarchy->setCustomParameters(['align' => 'center']);
  179.                 $nextContenus $em->getRepository(Contenu::class)->getByProfilNiveau($user$niveau->getId(), 2020$isxhr);
  180.                 $hasMore count($nextContenus) > 0;
  181.             }
  182.         }
  183.         return $this->render('niveau/show.html.twig', [
  184.             'liens' => null,
  185.             'niveau' => $niveau,
  186.             'isxhr' => $isxhr,
  187.             'current' => $request->get('current'null),
  188.             'hierarchy' => $hierarchy,
  189.             'has_more' => $hasMore,
  190.         ]);
  191.     }
  192.     /**
  193.      * @param SessionInterface $session
  194.      * @param Niveau $niveau
  195.      * @param NiveauService $niveauService
  196.      * @param string $pathinfo
  197.      * @return Response
  198.      */
  199.     public function subcats(SessionInterface $sessionNiveau $niveauNiveauService $niveauServicestring $pathinfo)
  200.     {
  201.         $em $this->getDoctrine()->getManager();
  202.         /** @param Profil $profil */
  203.         $profil $session->get('profil'$this->getUser()->getProfil());
  204.         $niveaux $em->getRepository(Niveau::class)->createQueryBuilder('n')
  205.             ->select('n')
  206.             ->leftJoin('n.univers''u')
  207.             ->andWhere('u.id = :uid')
  208.             ->setParameter('uid'$this->getUser()->getUnivers())
  209.             ->addOrderBy('n.root, n.lft''ASC')
  210.             ->andWhere('n.root = 1')
  211.             ->andWhere('n.parent IS NOT NULL')
  212.             ->getQuery()
  213.             ->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNStrue)
  214.             ->getResult('tree');
  215.         $hierarchy $em->getRepository(Niveau::class)->findAllByParentProfil($profil$niveau->getSubRoot());
  216.         return $this->render('niveau/subcats.html.twig', [
  217.             'niveau' => $niveau,
  218.             'pathInfo' => $pathinfo,
  219.             'hierarchy' => $hierarchy,
  220.             'empty' => $niveauService->getNiveaux($this->getUser(), $niveaux)
  221.         ]);
  222.     }
  223. }