src/Controller/shop/ShopController.php line 388
<?phpnamespace App\Controller\shop;use App\Classes\AppConfig;use App\Entity\Category;use App\Entity\Product;use App\Entity\Sale;use Doctrine\Persistence\ManagerRegistry;use Knp\Component\Pager\PaginatorInterface;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpFoundation\Session\Session;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;#[Route('shop')]class ShopController extends AbstractController {public function __construct(private readonly ManagerRegistry $em, private readonly UrlGeneratorInterface $router) {}#[Route('/all/', name: 'app_shop_all')]public function shopAll(PaginatorInterface $paginator, Request $request): Response {if ($this->isGranted('ROLE_USER')) {if (!$this->isGranted('ROLE_EMPLOYEE')) {return $this->redirect($this->generateUrl('app_home'));}}$args = [];$categories = $this->em->getRepository(Category::class)->getAllParent();$pagination = $paginator->paginate($categories, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['pagination'] = $pagination;return $this->render('front/shop/all.html.twig', $args);}#[Route('/list/{id}', name: 'app_shop')]public function shop(Category $category, PaginatorInterface $paginator, Request $request): Response {if ($this->isGranted('ROLE_USER')) {if (!$this->isGranted('ROLE_EMPLOYEE')) {return $this->redirect($this->generateUrl('app_home'));}}// $parent = $this->em->getRepository(Category::class)->findOneBy(['parent' => $category]);$parent = $this->em->getRepository(Category::class)->getParentCategory($category);if ($category->isImages()) {$categories = $this->em->getRepository(Category::class)->findBy(['parent' => $category, 'isSuspended' => 0], ['title' => 'ASC']);$pagination = $paginator->paginate($categories, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['pagination'] = $pagination;$args['category'] = $category;return $this->render('front/shop/all.html.twig', $args);}$telefon = $request->query->get('telefon');//ako je kategorija mobilni telefoniif ($category->getId() == 1235 && !is_null($telefon)) {$session = new Session();$session->set('url', $request->getRequestUri());$args['category'] = $category;return $this->render('front/shop/all_telefoni.html.twig', $args);}$args = [];$search = [];$title = $request->query->get('title');$proizvodjac = $request->query->get('pro');$proizvodjacX = $request->query->get('prox');$sale = $request->query->get('sale');$saleX = $request->query->get('salex');if (!is_null($proizvodjacX)) {$session = new Session();$session->remove('proizvodjac');}if (!is_null($saleX)) {$session = new Session();$session->remove('sale');}if (!empty($title)) {$search['title'] = $title;}$search['catId'] = $category->getId();$args['root'] = $this->em->getRepository(Category::class)->isRootCategoryInList($category, [AppConfig::ALATI]);if (!is_null($parent)) {if ($args['root']) {if (!is_null($proizvodjac)) {$session = new Session();$session->set('proizvodjac', $proizvodjac);$search['pro'] = $session->get('proizvodjac');}}$session = new Session();if ($session->has('proizvodjac') && $args['root']) {$search['pro'] = $session->get('proizvodjac');}if (!is_null($sale)) {$session = new Session();$session->set('sale', $sale);$search['sale'] = $session->get('sale');}$session = new Session();if ($session->has('sale')) {$search['sale'] = $session->get('sale');}$products = $this->em->getRepository(Product::class)->getAllByCategoryPaginator($search);// $products = $this->em->getRepository(Product::class)->getAllByCategoryPaginator($search);$pagination = $paginator->paginate($products, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['search'] = $search;$args['pagination'] = $pagination;// $args['categories'] = $this->em->getRepository(Category::class)->findBy(['isSuspended' => false, 'parent' => $category], ['title' => 'ASC']);$args['categories'] = $this->em->getRepository(Category::class)->getCategoriesByParent($search['catId']);$args['category'] = $category;// $args['categoryTree'] = $this->em->getRepository(Category::class)->findAllSubcategories($search['catId']);$args['categoryTree'] = $this->em->getRepository(Category::class)->findAllSubcategoryDetails($search['catId']);//dd($args['root']);$args['vendors'] = [973 => 'INGCO',986 => 'MAKITA',987 => 'BLACK AND DECKER',990 => 'STANLEY'];return $this->render('front/shop/list.html.twig', $args);}if (!is_null($sale)) {$session = new Session();$session->set('sale', $sale);$search['sale'] = $session->get('sale');}$session = new Session();if ($session->has('sale')) {$search['sale'] = $session->get('sale');}$session = new Session();if ($session->has('proizvodjac') && $args['root']) {$search['vendor'] = $session->get('proizvodjac');// $search['pro'] = $search['vendor'];$search['priceRange'] = $request->query->get('priceRange');foreach ($request->query->all() as $key => $value) {if (is_array($value)) {$key = str_replace('_', ' ', $key);$search[$key] = $value;}}$products = $this->em->getRepository(Product::class)->getAllByCategoryFilter($search);$pagination = $paginator->paginate($products, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['search'] = $search;$args['pagination'] = $pagination;$args['categories'] = $this->em->getRepository(Category::class)->getCategoriesByParent($search['catId']);$args['attributes'] = $this->em->getRepository(Product::class)->findUniqueAttributesByCategoryVendor($search['catId'], $search['vendor']);// $args['vendors'] = $this->em->getRepository(Product::class)->findUniqueFactoriesByCategory($search['catId']);$args['vendorsi'] = [973 => 'INGCO',986 => 'MAKITA',987 => 'BLACK AND DECKER',990 => 'STANLEY',];$args['maxMin'] = $this->em->getRepository(Product::class)->getMaxMinPrice($search['catId']);$args['category'] = $category;// $args['categoryTree'] = $this->em->getRepository(Category::class)->findAllSubcategoryDetails($search['catId']);return $this->render('front/shop/list_filter_new_pro.html.twig', $args);}$search['priceRange'] = $request->query->get('priceRange');foreach ($request->query->all() as $key => $value) {if (is_array($value)) {$key = str_replace('_', ' ', $key);$search[$key] = $value;}}$products = $this->em->getRepository(Product::class)->getAllByCategoryFilter($search);$pagination = $paginator->paginate($products, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['search'] = $search;$args['pagination'] = $pagination;$args['categories'] = $this->em->getRepository(Category::class)->getCategoriesByParent($search['catId']);$args['attributes'] = $this->em->getRepository(Product::class)->findUniqueAttributesByCategory($search['catId']);$args['vendors'] = $this->em->getRepository(Product::class)->findUniqueFactoriesByCategory($search['catId']);$args['maxMin'] = $this->em->getRepository(Product::class)->getMaxMinPrice($search['catId']);$args['category'] = $category;// $args['categoryTree'] = $this->em->getRepository(Category::class)->findAllSubcategoryDetails($search['catId']);return $this->render('front/shop/list_filter_new.html.twig', $args);}#[Route('/sale/', name: 'app_shop_sale')]public function shopSale(PaginatorInterface $paginator, Request $request): Response {if ($this->isGranted('ROLE_USER')) {if (!$this->isGranted('ROLE_EMPLOYEE')) {return $this->redirect($this->generateUrl('app_home'));}}$args = [];$args['category'] = $request->query->get('id') !== null ? $request->query->get('id') : 0;$args['sale'] = $request->query->get('sale') !== null ? $request->query->get('sale') : 0;$products = $this->em->getRepository(Product::class)->getProductsSale($args['category'], $args['sale']);$pagination = $paginator->paginate($products, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['pagination'] = $pagination;$args['sales'] = $this->em->getRepository(Sale::class)->findBy(['isActive' => true], ['title' => 'ASC']);$args['saleTitle'] = $this->em->getRepository(Sale::class)->findOneBy(['id' => $args['sale']]);$args['categories'] = [];$args['catTitle'] = $this->em->getRepository(Category::class)->findOneBy(['id' => $args['category']]);foreach ($products->getQuery()->getResult() as $product) {// Proveri da li kategorija već postoji u $categories nizuif (!in_array($product['category'], $args['categories'])) {// Dodaj kategoriju sa ID-em kao ključem$args['categories'][$product['cid']] = $product['category'];}}return $this->render('front/shop/sale.html.twig', $args);}#[Route('/new/', name: 'app_shop_new')]public function shopNew(PaginatorInterface $paginator, Request $request): Response {if ($this->isGranted('ROLE_USER')) {if (!$this->isGranted('ROLE_EMPLOYEE')) {return $this->redirect($this->generateUrl('app_home'));}}$args = [];$products = $this->em->getRepository(Product::class)->getProductsNew();$pagination = $paginator->paginate($products, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['pagination'] = $pagination;return $this->render('front/shop/new.html.twig', $args);}#[Route('/outlet/', name: 'app_shop_outlet')]public function shopOutlet(PaginatorInterface $paginator, Request $request): Response {if ($this->isGranted('ROLE_USER')) {if (!$this->isGranted('ROLE_EMPLOYEE')) {return $this->redirect($this->generateUrl('app_home'));}}$args = [];$products = $this->em->getRepository(Product::class)->getProductsOutlet();$pagination = $paginator->paginate($products, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['pagination'] = $pagination;return $this->render('front/shop/outlet.html.twig', $args);}#[Route('/search/', name: 'app_search_products')]public function pretraga(Request $request, PaginatorInterface $paginator): Response {if ($this->isGranted('ROLE_USER')) {if (!$this->isGranted('ROLE_EMPLOYEE')) {return $this->redirect($this->generateUrl('app_home'));}}$keywords = $request->query->get('keywords');$products = $this->em->getRepository(Product::class)->getProductsSearch($keywords);$pagination = $paginator->paginate($products, /* query NOT result */$request->query->getInt('page', 1), /*page number*/20);$session = new Session();$session->set('url', $request->getRequestUri());$args['pagination'] = $pagination;$args['keywords'] = $keywords;return $this->render('front/shop/search.html.twig', $args);}}