src/Controller/App/PagesController.php line 134

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author <akartis-dev>
  4.  */
  5. namespace App\Controller\App;
  6. use App\Controller\AppAbstractController;
  7. use App\Entity\Application\ShippingMethod;
  8. use App\Entity\Blog\Blog;
  9. use App\Entity\Orders\OrdersProducts;
  10. use App\Entity\Products\Categories\ProductsCategories;
  11. use App\Entity\Products\ProductBrand;
  12. use App\Entity\Products\Products;
  13. use App\ObjectManager\EntityObjectManager;
  14. use App\Repository\Products\Categories\ProductsCategoriesRepository;
  15. use App\Repository\Products\ProductsRepository;
  16. use App\Service\Cart\CartService;
  17. use App\Service\Filter\FilterServices;
  18. use App\Service\Hci\HciService;
  19. use App\Service\Newletter\NewletterService;
  20. use App\Service\Reporting\ReportingService;
  21. use App\Service\Utils\StrService;
  22. use Symfony\Component\HttpFoundation\RedirectResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Translation\TranslatableMessage;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. #[Route('/{_locale}')]
  29. class PagesController extends AppAbstractController
  30. {
  31.     public function __construct(
  32.         private ReportingService    $reportingService,
  33.         private EntityObjectManager $em,
  34.         private FilterServices      $filterServices,
  35.         private CartService         $cartService,
  36.         private HciService          $hciService,
  37.         private TranslatorInterface $translator
  38.     )
  39.     {
  40.     }
  41.     #[Route('/'name"app_index")]
  42.     public function index(): Response
  43.     {
  44.         $productRepository $this->em->getEm()->getRepository(Products::class);
  45.         $bestSellerProductEntity $this->reportingService->getBestSellingProductsByTag(limit12);
  46.         $recommandedProductEntity $this->reportingService->getRecommandedProducts(limit12);
  47.         $categories $this->em->getEm()
  48.             ->getRepository(OrdersProducts::class)->getBestCategoriesSeller(limit7);
  49.         $blogs $this->em->getEm()->getRepository(Blog::class)
  50.             ->getLastInsert(limit3);
  51. //        dd($bestSellerProductEntity);
  52.         return $this->render('pages/index.html.twig', [
  53.             'bestSellerProduct' => $bestSellerProductEntity,
  54.             'recommanded' => $recommandedProductEntity,
  55.             "categories" => $categories,
  56.             'blogs' => $blogs
  57.         ]);
  58.     }
  59.     #[Route('/shop'name'app_shop')]
  60.     public function onlineShop(Request $request): Response
  61.     {
  62.         
  63.         // Sanitize the query parameter
  64.         $q_search $request->query->get('q''');
  65.         // Encode special HTML characters
  66.         $q_search htmlspecialchars($q_searchENT_QUOTES'UTF-8');
  67.         // Remove potentially harmful attributes like "onfocus", "onclick", "style", etc.
  68.         $q_search preg_replace('/\s*on\w+\s*=\s*"[^"]*"/i'''$q_search); // Double quotes
  69.         $q_search preg_replace("/\s*on\w+\s*=\s*'[^']*'/i"''$q_search); // Single quotes
  70.         $q_search preg_replace('/\s*on\w+\s*=\s*[^\s>]*/i'''$q_search); // No quotes
  71.         // Remove inline styles
  72.         $q_search preg_replace('/\s*style\s*=\s*"[^"]*"/i'''$q_search);
  73.         $q_search preg_replace("/\s*style\s*=\s*'[^']*'/i"''$q_search);
  74.         // Remove "javascript:" or other potentially harmful protocols
  75.         $q_search preg_replace('/javascript:/i'''$q_search);
  76.         $q_search preg_replace('/vbscript:/i'''$q_search);
  77.         $q_search preg_replace('/data:/i'''$q_search);
  78.         // Strip additional HTML tags if needed (optional, for strict filtering)
  79.         $q_search strip_tags($q_search);
  80.         // Decode categories and brands safely
  81.         try {
  82.             $q_categories json_decode($request->get('categories''[]'), true512JSON_THROW_ON_ERROR);
  83.             $q_brands json_decode($request->get('brands''[]'), true512JSON_THROW_ON_ERROR);
  84.         } catch (\JsonException $e) {
  85.             $q_categories = [];
  86.             $q_brands = [];
  87.         }
  88.         // Sanitize and validate price parameters
  89.         $q_min filter_var($request->get('min'1), FILTER_VALIDATE_FLOAT, ['options' => ['default' => 1]]);
  90.         $q_max filter_var($request->get('max'100), FILTER_VALIDATE_FLOAT, ['options' => ['default' => 100]]);
  91.         
  92.         // Validate order parameter
  93.         $q_order filter_var($request->get('order'0), FILTER_VALIDATE_INT, ['options' => ['default' => 0]]);
  94.         // Call the shop filter service
  95.         $query $this->filterServices->shopFilter(
  96.             categories$q_categories,
  97.             brands$q_brands,
  98.             minPrice$q_min,
  99.             maxPrice$q_max,
  100.             order$q_order,
  101.             q$q_search
  102.         );
  103.         // Paginate products
  104.         $products $this->em->paginate($query$request20);
  105.         // Render the template
  106.         return $this->render('pages/online_shop.html.twig', [
  107.             'products' => $products,
  108.             'categories' => $q_categories,
  109.             'brands' => $q_brands,
  110.             'q_order' => $q_order,
  111.             'q_search' => $q_search,
  112.         ]);
  113.     }
  114.     #[Route('/product/{id}-{brand}-{slug}'name"product_detail")]
  115.     public function productDetail(Request $requestint $idstring $slugProductsRepository $repositoryProductsCategoriesRepository $productsCategoriesRepository): RedirectResponse|Response
  116.     {
  117.         $product $repository->find($id);
  118.         if ($product && $slug) {
  119.             $translated $product->translate($request->getLocale());
  120.             if ($translated->getSlug() !== $slug) {
  121.                 return $this->redirectToRoute("product_detail", [
  122.                     'id' => $id,
  123.                     'brand' => StrService::urlSanitizer($product->getBrand()->getName()),
  124.                     'slug' => $translated->getSlug()
  125.                 ]);
  126.             }
  127.         }
  128.         $categories = [];
  129.         if ($product->getProductsCategories()->first()) {
  130.             $categories $productsCategoriesRepository->findOneCategoryWithChild(
  131.                 $product->getProductsCategories()->first()->getId()
  132.             );
  133.         }
  134.         $withThisProducts $repository->findOftenBuyProducts($product);
  135.         $hciInformation $this->hciService->getCompediumInformation($product->getPharmacode());
  136.         $patient $this->hciService->formatCompediumHtml($hciInformation['content']);
  137.         $pro $this->hciService->formatCompediumHtml($hciInformation['pro']);
  138.         $recommandedProduct $repository->findBy([], [], 3);
  139.         $attributs $repository->getProductAttributsTerms(productId$id);
  140.         return $this->render("pages/product_detail.html.twig", [
  141.             'product' => $product,
  142.             'attributs' => $attributs,
  143.             'recommandedProduct' => $recommandedProduct,
  144.             'patient' => $patient,
  145.             'pro' => $pro,
  146.             'withProducts' => $withThisProducts,
  147.             'categories' => $categories
  148.         ]);
  149.     }
  150.     #[Route('/cart'name"app_cart")]
  151.     public function cart(): Response
  152.     {
  153.         $cart $this->cartService->getHydratedCartNoSerializable();
  154.         $coupons $this->cartService->getCouponCode();
  155.         $shippings $this->em->getEm()->getRepository(ShippingMethod::class)->findAll();
  156.         $prestations $this->cartService->getCartPrestation();
  157.         return $this->render('pages/cart.html.twig', [
  158.             'totalBrut' => $this->cartService->getTotalBrut(),
  159.             'cart' => $cart,
  160.             'coupons' => $coupons,
  161.             'shippings' => $shippings,
  162.             'prestations' => $prestations
  163.         ]);
  164.     }
  165.     #[Route('/choose-pharmacy'name"choose_pharmacy")]
  166.     public function choosePharmacy(Request $request)
  167.     {
  168.         $referer $request->headers->get('referer');
  169.         $refererSession $request->getSession()->get('session_referer'null);
  170.         if (!$refererSession) {
  171.             $request->getSession()->set('session_referer'$referer);
  172.         }
  173.         if (str_contains($referer"choose-pharmacy")) {
  174.             $request->getSession()->set('session_referer''/');
  175.         }
  176.         if (!$referer) {
  177.             return $this->redirect('/');
  178.         }
  179.         if (Request::METHOD_POST === $request->getMethod()) {
  180.             $product $request->query->get('product'null);
  181.             $qt $request->query->get('qt'1);
  182.             $result $this->cartService->setPharmacyCart($request->get('selected_pharmacy'));
  183.             $modal "";
  184.             if ($product) {
  185.                 $this->cartService->addInCart($product1null$qt);
  186.                 $this->addFlash('success'$this->translator->trans("Produit ajouté avec succès"));
  187.                 $modal "#panier_pharmacy";
  188.             }
  189.             $request->getSession()->set('session_referer'null);
  190.             if ($result) {
  191.                 if (str_contains($refererSession"choose-pharmacy")) {
  192.                     return $this->redirectToRoute('app_index');
  193.                 }
  194.                 return new RedirectResponse(sprintf("%s%s"$refererSession$modal));
  195.             }
  196.         }
  197.         return $this->render('pages/choosePharmacy.twig');
  198.     }
  199.     #[Route('/contact'name"app_contact")]
  200.     public function contact()
  201.     {
  202.         return $this->render('pages/contact.html.twig');
  203.     }
  204.     #[Route('/unsubscribe/{token}'name"app_unsubscribe_newletter")]
  205.     public function unsubscribeNewletter(string $tokenNewletterService $newletterService)
  206.     {
  207.         $res $newletterService->unsubscribe($token);
  208.         if ($res) {
  209.             $this->addFlash('success'$this->translator->trans('Désabonnement du newletter avec succès'));
  210.         } else {
  211.             $this->addFlash('error'$this->translator->trans('Une erreur s\'est produite'));
  212.         }
  213.         return $this->redirectToRoute('app_index');
  214.     }
  215.     #[Route('/pharmacy'name"app_pharmacy")]
  216.     public function pharmacy()
  217.     {
  218.         return $this->render('pages/pharmacy.html.twig');
  219.     }
  220.     #[Route("/cgu"name"cgu")]
  221.     public function cgu(Request $request)
  222.     {
  223.         $locale $request->getLocale();
  224.         return $this->render("pages/cgu_{$locale}.html.twig");
  225.     }
  226.     #[Route("/privacy"name"privacy")]
  227.     public function privacy(Request $request)
  228.     {
  229.         $locale $request->getLocale();
  230.         return $this->render("pages/privacy_{$locale}.html.twig");
  231.     }
  232. }