src/Controller/shop/ShopController.php line 388

  1. <?php
  2. namespace App\Controller\shop;
  3. use App\Classes\AppConfig;
  4. use App\Entity\Category;
  5. use App\Entity\Product;
  6. use App\Entity\Sale;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. #[Route('shop')]
  17. class ShopController extends AbstractController {
  18.   public function __construct(private readonly ManagerRegistry $em, private readonly UrlGeneratorInterface $router) {
  19.   }
  20.   #[Route('/all/'name'app_shop_all')]
  21.   public function shopAll(PaginatorInterface $paginatorRequest $request): Response {
  22.     if ($this->isGranted('ROLE_USER')) {
  23.       if (!$this->isGranted('ROLE_EMPLOYEE')) {
  24.         return $this->redirect($this->generateUrl('app_home'));
  25.       }
  26.     }
  27.     $args = [];
  28.     $categories $this->em->getRepository(Category::class)->getAllParent();
  29.     $pagination $paginator->paginate(
  30.       $categories/* query NOT result */
  31.       $request->query->getInt('page'1), /*page number*/
  32.       20
  33.     );
  34.     $session = new Session();
  35.     $session->set('url'$request->getRequestUri());
  36.     $args['pagination'] = $pagination;
  37.     return $this->render('front/shop/all.html.twig'$args);
  38.   }
  39.   #[Route('/list/{id}'name'app_shop')]
  40.   public function shop(Category $categoryPaginatorInterface $paginatorRequest $request): Response {
  41.     if ($this->isGranted('ROLE_USER')) {
  42.       if (!$this->isGranted('ROLE_EMPLOYEE')) {
  43.         return $this->redirect($this->generateUrl('app_home'));
  44.       }
  45.     }
  46. //    $parent = $this->em->getRepository(Category::class)->findOneBy(['parent' => $category]);
  47.     $parent $this->em->getRepository(Category::class)->getParentCategory($category);
  48.     if ($category->isImages()) {
  49.         $categories $this->em->getRepository(Category::class)->findBy(['parent' => $category'isSuspended' => 0], ['title' => 'ASC']);
  50.         $pagination $paginator->paginate(
  51.             $categories/* query NOT result */
  52.             $request->query->getInt('page'1), /*page number*/
  53.             20
  54.         );
  55.         $session = new Session();
  56.         $session->set('url'$request->getRequestUri());
  57.         $args['pagination'] = $pagination;
  58.         $args['category'] = $category;
  59.         return $this->render('front/shop/all.html.twig'$args);
  60.     }
  61.     $telefon $request->query->get('telefon');
  62.     //ako je kategorija mobilni telefoni
  63.     if ($category->getId() == 1235 && !is_null($telefon)) {
  64.       $session = new Session();
  65.       $session->set('url'$request->getRequestUri());
  66.       $args['category'] = $category;
  67.       return $this->render('front/shop/all_telefoni.html.twig'$args);
  68.     }
  69.     $args = [];
  70.     $search = [];
  71.     $title $request->query->get('title');
  72.     $proizvodjac $request->query->get('pro');
  73.     $proizvodjacX $request->query->get('prox');
  74.     $sale $request->query->get('sale');
  75.     $saleX $request->query->get('salex');
  76.     if (!is_null($proizvodjacX)) {
  77.       $session = new Session();
  78.       $session->remove('proizvodjac');
  79.     }
  80.     if (!is_null($saleX)) {
  81.       $session = new Session();
  82.       $session->remove('sale');
  83.     }
  84.     if (!empty($title)) {
  85.       $search['title'] = $title;
  86.     }
  87.     $search['catId'] = $category->getId();
  88.     $args['root'] = $this->em->getRepository(Category::class)->isRootCategoryInList($category, [AppConfig::ALATI]);
  89.     if (!is_null($parent)) {
  90.       if ($args['root']) {
  91.         if (!is_null($proizvodjac)) {
  92.           $session = new Session();
  93.           $session->set('proizvodjac'$proizvodjac);
  94.           $search['pro'] = $session->get('proizvodjac');
  95.         }
  96.       }
  97.       $session = new Session();
  98.       if ($session->has('proizvodjac') && $args['root']) {
  99.         $search['pro'] = $session->get('proizvodjac');
  100.       }
  101.       if (!is_null($sale)) {
  102.         $session = new Session();
  103.         $session->set('sale'$sale);
  104.         $search['sale'] = $session->get('sale');
  105.       }
  106.       $session = new Session();
  107.       if ($session->has('sale')) {
  108.         $search['sale'] = $session->get('sale');
  109.       }
  110.       $products $this->em->getRepository(Product::class)->getAllByCategoryPaginator($search);
  111. //      $products = $this->em->getRepository(Product::class)->getAllByCategoryPaginator($search);
  112.       $pagination $paginator->paginate(
  113.         $products/* query NOT result */
  114.         $request->query->getInt('page'1), /*page number*/
  115.         20
  116.       );
  117.       $session = new Session();
  118.       $session->set('url'$request->getRequestUri());
  119.       $args['search'] = $search;
  120.       $args['pagination'] = $pagination;
  121. //      $args['categories'] = $this->em->getRepository(Category::class)->findBy(['isSuspended' => false, 'parent' => $category], ['title' => 'ASC']);
  122.       $args['categories'] = $this->em->getRepository(Category::class)->getCategoriesByParent($search['catId']);
  123.       $args['category'] = $category;
  124. //      $args['categoryTree'] = $this->em->getRepository(Category::class)->findAllSubcategories($search['catId']);
  125.       $args['categoryTree'] = $this->em->getRepository(Category::class)->findAllSubcategoryDetails($search['catId']);
  126. //dd($args['root']);
  127.       $args['vendors'] = [
  128.         973 => 'INGCO',
  129.         986 => 'MAKITA',
  130.         987 => 'BLACK AND DECKER',
  131.         990 => 'STANLEY'
  132.       ];
  133.       return $this->render('front/shop/list.html.twig'$args);
  134.     }
  135.     if (!is_null($sale)) {
  136.       $session = new Session();
  137.       $session->set('sale'$sale);
  138.       $search['sale'] = $session->get('sale');
  139.     }
  140.     $session = new Session();
  141.     if ($session->has('sale')) {
  142.       $search['sale'] = $session->get('sale');
  143.     }
  144.     $session = new Session();
  145.     if ($session->has('proizvodjac') && $args['root']) {
  146.       $search['vendor'] = $session->get('proizvodjac');
  147. //      $search['pro'] = $search['vendor'];
  148.       $search['priceRange'] = $request->query->get('priceRange');
  149.       foreach ($request->query->all() as $key => $value) {
  150.         if (is_array($value)) {
  151.           $key str_replace('_'' '$key);
  152.           $search[$key] = $value;
  153.         }
  154.       }
  155.       $products $this->em->getRepository(Product::class)->getAllByCategoryFilter($search);
  156.       $pagination $paginator->paginate(
  157.         $products/* query NOT result */
  158.         $request->query->getInt('page'1), /*page number*/
  159.         20
  160.       );
  161.       $session = new Session();
  162.       $session->set('url'$request->getRequestUri());
  163.       $args['search'] = $search;
  164.       $args['pagination'] = $pagination;
  165.       $args['categories'] = $this->em->getRepository(Category::class)->getCategoriesByParent($search['catId']);
  166.       $args['attributes'] = $this->em->getRepository(Product::class)->findUniqueAttributesByCategoryVendor($search['catId'], $search['vendor']);
  167. //      $args['vendors'] = $this->em->getRepository(Product::class)->findUniqueFactoriesByCategory($search['catId']);
  168.       $args['vendorsi'] = [
  169.         973 => 'INGCO',
  170.         986 => 'MAKITA',
  171.         987 => 'BLACK AND DECKER',
  172.         990 => 'STANLEY',
  173.       ];
  174.       $args['maxMin'] = $this->em->getRepository(Product::class)->getMaxMinPrice($search['catId']);
  175.       $args['category'] = $category;
  176. //    $args['categoryTree'] = $this->em->getRepository(Category::class)->findAllSubcategoryDetails($search['catId']);
  177.       return $this->render('front/shop/list_filter_new_pro.html.twig'$args);
  178.     }
  179.     $search['priceRange'] = $request->query->get('priceRange');
  180.     foreach ($request->query->all() as $key => $value) {
  181.         if (is_array($value)) {
  182.           $key str_replace('_'' '$key);
  183.           $search[$key] = $value;
  184.         }
  185.       }
  186.     $products $this->em->getRepository(Product::class)->getAllByCategoryFilter($search);
  187.     $pagination $paginator->paginate(
  188.         $products/* query NOT result */
  189.         $request->query->getInt('page'1), /*page number*/
  190.         20
  191.     );
  192.     $session = new Session();
  193.     $session->set('url'$request->getRequestUri());
  194.     $args['search'] = $search;
  195.     $args['pagination'] = $pagination;
  196.     $args['categories'] = $this->em->getRepository(Category::class)->getCategoriesByParent($search['catId']);
  197.     $args['attributes'] = $this->em->getRepository(Product::class)->findUniqueAttributesByCategory($search['catId']);
  198.     $args['vendors'] = $this->em->getRepository(Product::class)->findUniqueFactoriesByCategory($search['catId']);
  199.     $args['maxMin'] = $this->em->getRepository(Product::class)->getMaxMinPrice($search['catId']);
  200.     $args['category'] = $category;
  201. //    $args['categoryTree'] = $this->em->getRepository(Category::class)->findAllSubcategoryDetails($search['catId']);
  202.       return $this->render('front/shop/list_filter_new.html.twig'$args);
  203.   }
  204.   #[Route('/sale/'name'app_shop_sale')]
  205.   public function shopSale(PaginatorInterface $paginatorRequest $request): Response {
  206.     if ($this->isGranted('ROLE_USER')) {
  207.       if (!$this->isGranted('ROLE_EMPLOYEE')) {
  208.         return $this->redirect($this->generateUrl('app_home'));
  209.       }
  210.     }
  211.     $args = [];
  212.     $args['category'] = $request->query->get('id') !== null $request->query->get('id') : 0;
  213.     $args['sale'] = $request->query->get('sale') !== null $request->query->get('sale') : 0;
  214.     $products $this->em->getRepository(Product::class)->getProductsSale($args['category'], $args['sale']);
  215.     $pagination $paginator->paginate(
  216.       $products/* query NOT result */
  217.       $request->query->getInt('page'1), /*page number*/
  218.       20
  219.     );
  220.     $session = new Session();
  221.     $session->set('url'$request->getRequestUri());
  222.     $args['pagination'] = $pagination;
  223.     $args['sales'] = $this->em->getRepository(Sale::class)->findBy(['isActive' => true], ['title' => 'ASC']);
  224.     $args['saleTitle'] = $this->em->getRepository(Sale::class)->findOneBy(['id' => $args['sale']]);
  225.     $args['categories'] = [];
  226.     $args['catTitle'] = $this->em->getRepository(Category::class)->findOneBy(['id' => $args['category']]);
  227.     foreach ($products->getQuery()->getResult() as $product) {
  228.         // Proveri da li kategorija već postoji u $categories nizu
  229.       if (!in_array($product['category'], $args['categories'])) {
  230.           // Dodaj kategoriju sa ID-em kao ključem
  231.         $args['categories'][$product['cid']] = $product['category'];
  232.       }
  233.     }
  234.     return $this->render('front/shop/sale.html.twig'$args);
  235.   }
  236.   #[Route('/new/'name'app_shop_new')]
  237.   public function shopNew(PaginatorInterface $paginatorRequest $request): Response {
  238.     if ($this->isGranted('ROLE_USER')) {
  239.       if (!$this->isGranted('ROLE_EMPLOYEE')) {
  240.         return $this->redirect($this->generateUrl('app_home'));
  241.       }
  242.     }
  243.     $args = [];
  244.     $products $this->em->getRepository(Product::class)->getProductsNew();
  245.     $pagination $paginator->paginate(
  246.       $products/* query NOT result */
  247.       $request->query->getInt('page'1), /*page number*/
  248.       20
  249.     );
  250.     $session = new Session();
  251.     $session->set('url'$request->getRequestUri());
  252.     $args['pagination'] = $pagination;
  253.     return $this->render('front/shop/new.html.twig'$args);
  254.   }
  255.   #[Route('/outlet/'name'app_shop_outlet')]
  256.   public function shopOutlet(PaginatorInterface $paginatorRequest $request): Response {
  257.     if ($this->isGranted('ROLE_USER')) {
  258.       if (!$this->isGranted('ROLE_EMPLOYEE')) {
  259.         return $this->redirect($this->generateUrl('app_home'));
  260.       }
  261.     }
  262.     $args = [];
  263.     $products $this->em->getRepository(Product::class)->getProductsOutlet();
  264.     $pagination $paginator->paginate(
  265.       $products/* query NOT result */
  266.       $request->query->getInt('page'1), /*page number*/
  267.       20
  268.     );
  269.     $session = new Session();
  270.     $session->set('url'$request->getRequestUri());
  271.     $args['pagination'] = $pagination;
  272.     return $this->render('front/shop/outlet.html.twig'$args);
  273.   }
  274.   #[Route('/search/'name'app_search_products')]
  275.   public function pretraga(Request $requestPaginatorInterface $paginator): Response {
  276.     if ($this->isGranted('ROLE_USER')) {
  277.       if (!$this->isGranted('ROLE_EMPLOYEE')) {
  278.         return $this->redirect($this->generateUrl('app_home'));
  279.       }
  280.     }
  281.     $keywords $request->query->get('keywords');
  282.     $products $this->em->getRepository(Product::class)->getProductsSearch($keywords);
  283.     $pagination $paginator->paginate(
  284.       $products/* query NOT result */
  285.       $request->query->getInt('page'1), /*page number*/
  286.       20
  287.     );
  288.     $session = new Session();
  289.     $session->set('url'$request->getRequestUri());
  290.     $args['pagination'] = $pagination;
  291.     $args['keywords'] = $keywords;
  292.     return $this->render('front/shop/search.html.twig'$args);
  293.   }
  294. }