<?php
namespace App\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Entity\Option;
use App\Repository\BrandRepository;
use App\Repository\OrderRepository;
use App\Repository\SlideRepository;
use App\Repository\BannerRepository;
use App\Repository\OptionRepository;
use App\Repository\ProductRepository;
use App\Repository\CategoryRepository;
use Knp\Component\Pager\PaginatorInterface;
use App\Form\SearchFilter\ProductSearchType;
use App\Entity\SearchFilter\ProductSearchFilter;
class IndexController extends AbstractController
{
private $logger;
public function __construct(
LoggerInterface $logger
) {
$this->logger = $logger;
}
#[Route('/', name: 'app_index', methods: ['GET'])]
public function index(
Request $request,
ProductRepository $productRepository,
CategoryRepository $categoryRepository,
OrderRepository $orderRepository,
SlideRepository $slideRepository,
BannerRepository $bannerRepository,
BrandRepository $brandRepository,
OptionRepository $optionRepository,
PaginatorInterface $paginator
): Response {
$this->logger->debug(__METHOD__);
if ($returnTo = $request->cookies->get('return_to')) {
$redirect = new RedirectResponse($returnTo);
$redirect->headers->removeCookie('return_to');
return $redirect;
}
$filterForm = new ProductSearchFilter();
$filterForm->setSort($request->query->get('sort'));
$filterForm->setDirection($request->query->get('direction'));
$filterForm->setFilter(ProductSearchFilter::FILTER_ACTIVE);
$searchForm = $this->createForm(ProductSearchType::class, $filterForm, [
'method' => 'GET',
]);
$searchForm->handleRequest($request);
// list of product groups pending order
$groupsProducts = $orderRepository->getPendingProductGroupsOrders();
$query = $productRepository->getProductSearchFilterQB($filterForm);
$optPagination = $optionRepository->getValueByKey(Option::DEFAULT_ITEMS_PER_PAGE, 15);
$pagination = $paginator->paginate(
$query,
$request->query->get('page', 1),
($request->query->get('limit') ? $request->query->get('limit') : $optPagination),
['wrap-queries' => true]
);
$featuredCategories = $categoryRepository->getHomepageFeatured();
foreach ($productRepository->getByCategory(array_keys($featuredCategories)) as $prod) {
foreach ($prod->getCategories() as $cat) {
if (isset($featuredCategories[$cat->getId()])) {
$featuredCategories[$cat->getId()]['products'][] = $prod;
}
}
}
// Shuffle products.
$featuredCategories = array_map(function ($cat) {
shuffle($cat['products']);
return $cat;
}, $featuredCategories);
$featuredBrands = $brandRepository->getRandomBrands();
shuffle($featuredBrands);
$userId = 0;
if($this->getUser()){
$userId=$this->getUser()->getId();
}
return $this->render('product/index.html.twig', [
'slider' => $slideRepository->getAll(),
'banners' => $bannerRepository->getAllWithKeys(),
'featuredCategories' => $featuredCategories,
'featuredBrands' => $featuredBrands,
'searchForm' => $searchForm->createView(),
'pagination' => $pagination,
'filter' => $filterForm->getFilter(),
'groupsProducts' => $groupsProducts,
'userId' => $userId
]);
}
}