src/Controller/CategoryController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Category;
  4. use App\Form\Type\ProductSearchFiltersType;
  5. use App\Helper\VeltisControllerTrait;
  6. use App\Repository\ArticleRepository;
  7. use App\Repository\AvailabilityRepository;
  8. use App\Repository\BannerRepository;
  9. use App\Repository\CategoryRepository;
  10. use App\Repository\FurnitureTypeRepository;
  11. use App\Repository\NewsRepository;
  12. use App\Repository\ProductParameterRepository;
  13. use App\Repository\ProductRepository;
  14. use App\Repository\ProductViewRepository;
  15. use App\Repository\WorkingHoursRepository;
  16. use App\Services\CompareManager;
  17. use App\Services\PasswordProtectedService;
  18. use App\Services\SearchManager;
  19. use Doctrine\Common\Collections\ArrayCollection;
  20. use Knp\Component\Pager\PaginatorInterface;
  21. use Liip\ImagineBundle\Service\FilterService;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\HttpFoundation\JsonResponse;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\Routing\RouterInterface;
  31. class CategoryController extends AbstractController
  32. {
  33.     use VeltisControllerTrait;
  34.     /**
  35.      * @Route("/{slug}/{id}/c/{av24h}", name="category", defaults={"av24h": null})
  36.      */
  37.     public function indexAction(Request $requestCategoryRepository $categoryRepositoryProductRepository $productRepositoryPaginatorInterface $paginatorFurnitureTypeRepository $furnitureTypeRepositorySessionInterface $sessionProductViewRepository $productViewRepositoryCompareManager $compareManagerPasswordProtectedService $passwordProtectedService)
  38.     {
  39.         if ($passwordProtectedService->isPasswordProtected()) {
  40.             return $this->redirectToRoute('password_protected');
  41.         }
  42.         $categories $categoryRepository->getMainCategories($request->getLocale(), [Category::TYPE_24hCategory::TYPE_SZOK_CENOWY])->getQuery()->getResult();
  43.         $furnitureTypes $furnitureTypeRepository->getForCategory($request->getLocale())->getQuery()->getResult();
  44.         $categoryQuery $categoryRepository->getCategories($request->getLocale(), [$request->attributes->get('id')])->getQuery()->getResult();
  45.         if (isset($categoryQuery[0]) and count($categoryQuery) == 1) {
  46.             /** @var $category Category */
  47.             $category $categoryQuery[0];
  48.         } else {
  49.             throw new NotFoundHttpException();
  50.         }
  51.  
  52.         if ($request->attributes->get('slug') !== $category->translate($request->getLocale())->getSlug()) {
  53.             return $this->redirect($this->generateUrl('category', ['id'=>$category->getId(), 'slug'=>$category->translate($request->getLocale())->getSlug()]), 301);
  54.         }
  55.         if ($category->getDeletedBy()) {
  56.             throw new NotFoundHttpException();
  57.         }
  58.         foreach ($category->getLangParams() as $langParam) {
  59.             if ($langParam->getLanguage()->getLocale() == $request->getLocale()) {
  60.                 if ($langParam->getVisible() == false && $category->getId() != Category::TYPE_SZOK_CENOWY && $category->getId() != Category::TYPE_NEW_PRODUCTS && $category->getId() != Category::TYPE_BESTSELLERS && $category->getId() != Category::TYPE_24h && $category->getId() != 272) {
  61.                     throw new NotFoundHttpException();
  62.                 }
  63.             }
  64.         }
  65.         $gclid '';
  66.         if ($request->query->get('gclid')) {
  67.             $gclid '&gclid='.$request->query->get('gclid');
  68.         }
  69.         // search engine
  70.         if ($request->attributes->get('id') == and $request->query->get('search_text')) {
  71.             return new RedirectResponse($this->generateUrl('search').'?query='.$request->query->get('search_text').$gclid);
  72.         }
  73.         $form $this->createForm(
  74.             ProductSearchFiltersType::class
  75.         );
  76.         $form->handleRequest($request);
  77.         $searchData = [];
  78.         if ($form->isSubmitted()) {
  79.             $searchData $form->getData();
  80.         }
  81.         $searchData['locale'] = $request->getLocale();
  82.         if ($request->attributes->get(('av24h'))) {
  83.             if ($request->attributes->get('av24h') != '24h') {
  84.                 throw new NotFoundHttpException();
  85.             }
  86.             $arr = new ArrayCollection();
  87.             $arr->add($this->em()->getRepository('App:Availability')->find(1));
  88.             $arr->add($this->em()->getRepository('App:Availability')->find(5));
  89.             $arr->add($this->em()->getRepository('App:Availability')->find(15));
  90.             $searchData['availability'] = $arr;
  91.         }
  92.         if ($category->getId() == Category::TYPE_SZOK_CENOWY) {
  93.             $products $productRepository->getForPromotions($category$searchData)->getQuery();
  94.         } elseif ($category->getId() == Category::TYPE_NEW_PRODUCTS) {
  95.             $products $productRepository->getForNewProducts($category$searchData)->getQuery();
  96.         } elseif ($category->getId() == Category::TYPE_BESTSELLERS) {
  97.             $products $productRepository->getForBestsellersProduct($category$searchData)->getQuery();
  98.         } else {
  99.             $products $productRepository->getForCategory($category$searchData)->getQuery();
  100.         }
  101.         $lastViewed $productViewRepository->getLastViewed($session->getId(), $request->getLocale())->getQuery()->getResult();
  102.         $pagination $paginator->paginate($products$request->query->getInt('page'1), $session->get('productsPerPage',30), ['wrap-queries'=>true]);
  103.         return $this->render('frontend/category/category.html.twig', [
  104.             'categories' => $categories,
  105.             'furnitureTypes' =>$furnitureTypes,
  106.             'lastViewed' => $lastViewed,
  107.             'language' => $this->resolveLanguage($request->getLocale()),
  108.             'locale' => $request->getLocale(),
  109.             'products' => $pagination,
  110.             'compareIds' => $compareManager->getIdsCompareProducts(),
  111.             'searchData' => $searchData,
  112.             'form' => $form->createView(),
  113.             'category' => $category,
  114.             'productsPerPage' => $session->get('productsPerPage',30),
  115.         ]);
  116.     }
  117.     /**
  118.      * @Route("/search/query", name="search")
  119.      */
  120.     public function searchAction(Request $requestCategoryRepository $categoryRepositoryProductRepository $productRepositoryPaginatorInterface $paginatorFurnitureTypeRepository $furnitureTypeRepositorySessionInterface $sessionProductViewRepository $productViewRepositoryCompareManager $compareManagerSearchManager $searchManager)
  121.     {
  122.         $categories $categoryRepository->getMainCategories($request->getLocale(), [Category::TYPE_24hCategory::TYPE_SZOK_CENOWY])->getQuery()->getResult();
  123.         $furnitureTypes $furnitureTypeRepository->getForCategory($request->getLocale())->getQuery()->getResult();
  124.         $form $this->createForm(
  125.             ProductSearchFiltersType::class
  126.         );
  127.         $form->handleRequest($request);
  128.         $searchData = [];
  129.         $searchData['query'] = '';
  130.         if ($form->isSubmitted()) {
  131.             $searchData $form->getData();
  132.         }
  133.         $searchData['locale'] = $request->getLocale();
  134.         if ($request->query->get('query')) {
  135.             $searchData['query'] = trim($request->query->get('query'));
  136.         }
  137.         if (!isset($searchData['query']) and !$request->query->has('productProducer')) {
  138.             //return new RedirectResponse($this->generateUrl('homepage'));
  139.         }
  140.         //handle phrase query counter
  141.         if ($request->query->getInt('page'1) == or !$request->query->has('page')) {
  142.             $searchManager->countSearch(strtolower($searchData['query']), $this->resolveLanguage($request->getLocale()));
  143.         }
  144.         //handle replace query for different query based on settings in marketing section
  145.         $searchData['query'] = $searchManager->format(strtolower($searchData['query']), $this->resolveLanguage($request->getLocale()));
  146.         if ($searchData['query'] instanceof RedirectResponse) {
  147.             return $searchData['query'];
  148.         }
  149.         //logo click
  150.         if ($request->query->get('productProducer')) {
  151.             $searchData['productProducer'] = (int)$request->query->get('productProducer');
  152.         }
  153.         $products $productRepository->searchStore($searchData);
  154.         $pagination $paginator->paginate($products$request->query->getInt('page'1)/*page number*/$session->get('productsPerPage',30), ['wrap-queries'=>true]);
  155.         $ids = [];
  156.         $prods = [];
  157.         foreach ($pagination as $product) {
  158.             $ex explode(" "$searchData['query']);
  159.             foreach ($ex as $e) {
  160.                 $lowerName strtolower($product[0]->getName());
  161.                 $lowerQuery strtolower($e);
  162.                 $exName explode(" "$lowerName);
  163.                 foreach ($exName as $exPartName) {
  164.                     if ($exPartName === $lowerQuery and !in_array($product[0]->getId(), $ids)) {
  165.                         $prods[] = $product;
  166.                         $ids[] = $product[0]->getId();
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.         foreach ($pagination as $product) {
  172.             if (!in_array($product[0]->getId(), $ids)) {
  173.                 $prods[] = $product;
  174.                 $ids[] = $product[0]->getId();
  175.             }
  176.         }
  177.         return $this->render('frontend/category/search.html.twig', [
  178.             'categories' => $categories,
  179.             'furnitureTypes' =>$furnitureTypes,
  180.             'language' => $this->resolveLanguage($request->getLocale()),
  181.             'products' => $pagination,
  182.             'prodsMod' => $prods,
  183.             'productsPerPage' => $session->get('productsPerPage',30),
  184.             'compareIds' => $compareManager->getIdsCompareProducts(),
  185.             'searchData' => $searchData,
  186.             'form' => $form->createView(),
  187.         ]);
  188.     }
  189.     /**
  190.      * @Route("/s/search/1/c", name="search_old_site_redirect")
  191.      */
  192.     public function searchRedirectOldSiteAction(Request $request)
  193.     {
  194.         if ($request->query->get('search_text')) {
  195.             $gclid '';
  196.             if ($request->query->get('gclid')) {
  197.                 $gclid '&gclid='.$request->query->get('gclid');
  198.             }
  199.             return new RedirectResponse($this->generateUrl('search').'?query='.$request->query->get('search_text').$gclid301);
  200.         }
  201.         if ($request->query->get('category_id')) {
  202.             $category $this->em()->getRepository('App:Category')->find($request->query->get('category_id'));
  203.             if ($category) {
  204.                 return new RedirectResponse(
  205.                     $this->generateUrl('category', ['id' => $category->getId(), 'slug' => $category->getSlug()]), 301
  206.                 );
  207.             }
  208.         }
  209.         return new RedirectResponse($this->generateUrl('homepage'));
  210.     }
  211.     /**
  212.      * @Route("/change-products-per-page", name="change_products_per_page")
  213.      */
  214.     public function changeProductsPerPageAction(Request $requestSessionInterface $session)
  215.     {
  216.         if ($request->request->get('productsPerPage') and is_numeric($request->request->get('productsPerPage'))) {
  217.             $session->set('productsPerPage', (int)$request->request->get('productsPerPage'));
  218.         }
  219.         return new JsonResponse([]);
  220.     }
  221.     /**
  222.      * @Route("/search/auto-complete/query", name="search_autocomplete")
  223.      */
  224.     public function searchAutoCompleteAction(Request $request, \Symfony\Component\Asset\Packages $assetsManagerFilterService $filterServiceCategoryRepository $categoryRepositoryProductRepository $productRepositoryPaginatorInterface $paginatorFurnitureTypeRepository $furnitureTypeRepositoryRouterInterface $routerProductViewRepository $productViewRepositoryCompareManager $compareManagerSearchManager $searchManager)
  225.     {
  226.         $query $request->query->get('query');
  227.         $searchData = [];
  228.         $searchData['query'] = $query;
  229.         $searchData['locale'] = $request->getLocale();
  230.         //handle replace query for different query based on settings in marketing section
  231.         $searchData['query'] = $searchManager->format(strtolower($searchData['query']), $this->resolveLanguage($request->getLocale()));
  232.         if ($searchData['query'] instanceof RedirectResponse) {
  233.             return $searchData['query'];
  234.         }
  235.         $products $productRepository->searchStore($searchData);
  236.         $pagination $paginator->paginate($products$request->query->getInt('page'1)/*page number*/30, ['wrap-queries'=>true]);
  237.         $json = [];
  238.         foreach ($pagination as $product) {
  239.             /**
  240.              * array:9 [▼
  241.             0 => Product {#33587 ▶}
  242.             "isNew" => false
  243.             "mainPhoto" => "krzeslo-tulipan-529.jpeg"
  244.             "isBestseller" => null
  245.             "vatId" => 1
  246.             "currencySign" => "zł"
  247.             "currencyId" => 1
  248.             "priceDescription" => "100 - 113"
  249.             "priceCrossed" => null
  250.             ]
  251.              */
  252.             $path $assetsManager->getUrl('/images/product/'.$product['mainPhoto']);
  253.             if (file_exists(getcwd()."/images/product/".$product['mainPhoto'])) {
  254.                 $resourcePath $filterService->getUrlOfFilteredImage($path'smallProdImageQuality');
  255.             } else {
  256.                 $resourcePath false;
  257.             }
  258.             if ($request->getLocale() == 'ro') {
  259.                 $product['priceDescription'] = str_replace("."","$product['priceDescription']);
  260.             }
  261.             if ($resourcePath) {
  262.                 $url $router->generate('product', ['slug'=>$product[0]->getSlug(), 'id'=>$product[0]->getId()]);
  263.                 $json[] = ['url'=>$url'imageUrl'=>$resourcePath'price'=>$product['priceDescription'].' '.$product['currencySign'], 'value'=>$product[0]->getName(), 'data'=>$product[0]->getId()];
  264.             }
  265.         }
  266.         return new JsonResponse(['suggestions'=>$json]);
  267.     }
  268. }