src/Controller/IndexController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use App\Entity\Option;
  10. use App\Repository\BrandRepository;
  11. use App\Repository\OrderRepository;
  12. use App\Repository\SlideRepository;
  13. use App\Repository\BannerRepository;
  14. use App\Repository\OptionRepository;
  15. use App\Repository\ProductRepository;
  16. use App\Repository\CategoryRepository;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. use App\Form\SearchFilter\ProductSearchType;
  19. use App\Entity\SearchFilter\ProductSearchFilter;
  20. class IndexController extends AbstractController
  21. {
  22.     private $logger;
  23.     public function __construct(
  24.         LoggerInterface $logger
  25.     ) {
  26.         $this->logger $logger;
  27.     }
  28.     #[Route('/'name'app_index'methods: ['GET'])]
  29.     public function index(
  30.         Request $request,
  31.         ProductRepository $productRepository,
  32.         CategoryRepository $categoryRepository,
  33.         OrderRepository $orderRepository,
  34.         SlideRepository $slideRepository,
  35.         BannerRepository $bannerRepository,
  36.         BrandRepository $brandRepository,
  37.         OptionRepository $optionRepository,
  38.         PaginatorInterface $paginator
  39.     ): Response {
  40.         $this->logger->debug(__METHOD__);
  41.         if ($returnTo $request->cookies->get('return_to')) {
  42.             $redirect = new RedirectResponse($returnTo);
  43.             $redirect->headers->removeCookie('return_to');
  44.             return $redirect;
  45.         }
  46.         $filterForm = new ProductSearchFilter();
  47.         $filterForm->setSort($request->query->get('sort'));
  48.         $filterForm->setDirection($request->query->get('direction'));
  49.         $filterForm->setFilter(ProductSearchFilter::FILTER_ACTIVE);
  50.         $searchForm $this->createForm(ProductSearchType::class, $filterForm, [
  51.             'method' => 'GET',
  52.         ]);
  53.         $searchForm->handleRequest($request);
  54.         // list of product groups pending order
  55.         $groupsProducts $orderRepository->getPendingProductGroupsOrders();
  56.         $query $productRepository->getProductSearchFilterQB($filterForm);
  57.          
  58.         $optPagination $optionRepository->getValueByKey(Option::DEFAULT_ITEMS_PER_PAGE15);
  59.         $pagination $paginator->paginate(
  60.             $query,
  61.             $request->query->get('page'1),
  62.             ($request->query->get('limit') ? $request->query->get('limit') : $optPagination),
  63.             ['wrap-queries' => true]
  64.         );
  65.         $featuredCategories $categoryRepository->getHomepageFeatured();
  66.         foreach ($productRepository->getByCategory(array_keys($featuredCategories)) as $prod) {
  67.             foreach ($prod->getCategories() as $cat) {
  68.                 if (isset($featuredCategories[$cat->getId()])) {
  69.                     $featuredCategories[$cat->getId()]['products'][] = $prod;
  70.                 }
  71.             }
  72.         }
  73.         // Shuffle products.
  74.         $featuredCategories array_map(function ($cat) {
  75.             shuffle($cat['products']);
  76.             return $cat;
  77.         }, $featuredCategories);
  78.         $featuredBrands $brandRepository->getRandomBrands();
  79.         shuffle($featuredBrands);
  80.         
  81.         $userId 0;
  82.         if($this->getUser()){
  83.             $userId=$this->getUser()->getId();
  84.         }
  85.         return $this->render('product/index.html.twig', [
  86.             'slider'             => $slideRepository->getAll(),
  87.             'banners'            => $bannerRepository->getAllWithKeys(),
  88.             'featuredCategories' => $featuredCategories,
  89.             'featuredBrands'     => $featuredBrands,
  90.             'searchForm'         => $searchForm->createView(),
  91.             'pagination'         => $pagination,
  92.             'filter'             => $filterForm->getFilter(),
  93.             'groupsProducts'     => $groupsProducts,
  94.             'userId'             => $userId
  95.         ]);
  96.     }
  97. }