src/Controller/Espacepros/HotelsController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Espacepros;
  3. use App\Data\SearchhotelsData;
  4. use App\Entity\Hotels\Annonceshotels;
  5. use App\Entity\Hotels\Hotels;
  6. use App\Form\Hotels\SearchFormHotelsType;
  7. use App\Repository\Hotels\HotelsRepository;
  8. use App\Repository\Posts\PostsRepository;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use MercurySeries\FlashyBundle\FlashyNotifier;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @Route("/sejourner/hotels")
  18.  */
  19. class HotelsController extends AbstractController
  20. {
  21.     /**
  22.      * @var FlashyNotifier
  23.      */
  24.     private $flashy;
  25.     /**
  26.      * @var HotelsRepository
  27.      */
  28.     private $hotelsRepository;
  29.     /**
  30.      * @var PostsRepository
  31.      */
  32.     private $postsRepository;
  33.     public function __construct(HotelsRepository $hotelsRepositoryPostsRepository $postsRepositoryFlashyNotifier $flashy)
  34.     {
  35.         $this->hotelsRepository $hotelsRepository;
  36.         $this->flashy $flashy;
  37.         $this->postsRepository $postsRepository;
  38.     }
  39.     /**
  40.      * @Route("/", name="hotels.index")
  41.      * @param Request $request
  42.      * @param PaginatorInterface $paginator
  43.      * @return Response
  44.      */
  45.     public function index(Request $requestPaginatorInterface $paginator)
  46.     {
  47.         $data = new SearchhotelsData();
  48.         $data->page $request->get('page'1);
  49.         $formsearch $this->createForm(SearchFormHotelsType::class, $data);
  50.         $formsearch->handleRequest($request);
  51.         $hotels $this->hotelsRepository->findSearch($data);
  52.         if ($request->get('ajax')) {
  53.             return new JsonResponse([
  54.                 'content' => $this->renderView('pages/espacepro/hotels/partials/__card.html.twig', ['hotels' => $hotels]),
  55.                 'sorting' => $this->renderView('pages/espacepro/hotels/partials/__sorting.html.twig', ['hotels' => $hotels]),
  56.                 'pagination' => $this->renderView('pages/espacepro/hotels/partials/__pagination.html.twig', ['hotels' => $hotels]),
  57.             ]);
  58.         }
  59.         $posts $paginator->paginate(
  60.             $this->postsRepository->findBy(['categories' => 1'online' => true], ['id' => 'DESC']), /* query NOT result */
  61.             $request->query->getInt('page'1)/*page number*/,
  62.             $request->query->getInt('limit'12)/*limit per page*/
  63.         );
  64.         $min = [];
  65.         $minPrice = [];
  66.         foreach ($hotels as $hotel){
  67.             foreach ($hotel->getAnnonceshotels() as $annonceshotel){
  68.                 $min[$annonceshotel->getId()] = $annonceshotel->getPrice();
  69.                 $minPrice[$hotel->getId()] = min($min );
  70.             }
  71.         }
  72.         //$hotels = $this->hotelsRepository->findHotelsByActivated($request->query->getInt('page', 1));
  73.         return $this->render('pages/espacepro/hotels/index.html.twig', [
  74.             'title' => 'Hôtels à reserver',
  75.             'hotels' => $hotels,
  76.             'minPrice' => $minPrice,
  77.             'posts' => $posts,
  78.             'formsearch' => $formsearch->createView(),
  79.             'menu' => "Séjourner",
  80.             'current_page' => 'sejourner'
  81.         ]);
  82.     }
  83.     /**
  84.      * @Route("/details/{slug}/{id}", name="hotels.show")
  85.      * @param Request $request
  86.      * @param $slug
  87.      * @param Hotels $hotels
  88.      * @param PaginatorInterface $paginator
  89.      * @return Response
  90.      */
  91.     public function show(Request $request$slugHotels $hotelsPaginatorInterface $paginator)
  92.     {
  93.         if ($hotels->getSlug() !== $slug) {
  94.             return $this->redirectToRoute('hotels.show', [
  95.                 'id' => $hotels->getId(),
  96.                 'slug' => $hotels->getSlug(),
  97.             ], 301);
  98.         }
  99.         $othersHotels $this->hotelsRepository->getHotelRandom($hotels3);
  100.         $chambres $paginator->paginate(
  101.             $this->getDoctrine()->getRepository(Annonceshotels::class)->findBy(['hotels' => $hotels->getId(), 'online' => true]), /* query NOT result */
  102.             $request->query->getInt('page'1)/*page number*/,
  103.             $request->query->getInt('limit'12)/*limit per page*/
  104.         );
  105.         return $this->render('pages/espacepro/hotels/show.html.twig', [
  106.             'title' => $hotels->getName(),
  107.             'hotel' => $hotels,
  108.             'chambres' => $chambres,
  109.             'othersHotels' => $othersHotels,
  110.             'menu' => "Séjourner",
  111.             'current_page' => 'sejourner'
  112.         ]);
  113.     }
  114. }