src/HomeBundle/Controller/HeaderFooterController.php line 127

Open in your IDE?
  1. <?php
  2. namespace App\HomeBundle\Controller;
  3. use App\BaseBundle\Controller\AbstractController;
  4. use App\CMSBundle\Service\SiteSettingService;
  5. use App\HomeBundle\Utils\MailChimp;
  6. use App\ProductBundle\Repository\CategoryRepository;
  7. use App\ProductBundle\Repository\OccasionRepository;
  8. use App\ProductBundle\Repository\ProductSearchRepository;
  9. use App\ProductBundle\Service\CategoryWebsiteHeaderService;
  10. use App\ProductBundle\Service\ProductSearchService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use PN\ServiceBundle\Service\ContainerParameterService;
  13. use PN\ServiceBundle\Utils\Validate;
  14. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\Cache\ItemInterface;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. /**
  21. * @Route("")
  22. */
  23. class HeaderFooterController extends AbstractController
  24. {
  25. private ContainerParameterService $containerParameterService;
  26. public function __construct(EntityManagerInterface $em,ContainerParameterService $containerParameterService)
  27. {
  28. parent::__construct($em);
  29. $this->containerParameterService = $containerParameterService;
  30. }
  31. /**
  32. * @Route("/auto-complete", name="fe_filter_product_auto_complete_ajax", methods={"GET"})
  33. */
  34. public function autoCompleteAction(
  35. Request $request,
  36. TranslatorInterface $translator,
  37. ProductSearchRepository $productSearchRepository,
  38. ProductSearchService $productSearchService
  39. ): Response
  40. {
  41. $jsonArray = ["error" => false, "message" => null, "products" => []];
  42. $submittedToken = $request->query->get('_token');
  43. if (!$this->isCsrfTokenValid('search-auto-complete', $submittedToken)) {
  44. $jsonArray["error"] = true;
  45. $jsonArray["message"] = $translator->trans("invalid_token_msg");
  46. return $this->json($jsonArray);
  47. }
  48. $search = new \stdClass();
  49. $search->deleted = 0;
  50. $search->ordr = ["column" => 0, "dir" => "DESC"];;
  51. $search->autocomplete = true;
  52. $search->string = $request->get('str');
  53. if (strlen(trim($search->string)) < 4) {
  54. $jsonArray["message"] = 1;
  55. return $this->json($jsonArray);
  56. }
  57. $entities = $productSearchRepository->filter($search, false, 0, 12);
  58. foreach ($entities as $entity) {
  59. $title = $entity->getTitle();
  60. $object = $productSearchService->convertEntityToObject($entity);
  61. $object["title"] = ($title == null) ? $entity->getTitle() : $title;
  62. $jsonArray["products"][] = $object;
  63. }
  64. return $this->json($jsonArray);
  65. }
  66. /**
  67. * @Route("/subscribe", name="fe_subscribe", methods={"POST"})
  68. */
  69. public function subscribe(Request $request, TranslatorInterface $translator, SiteSettingService $siteSettingService): Response
  70. {
  71. $submittedToken = $request->request->get('_token');
  72. if (!$this->isCsrfTokenValid('newsletter-token', $submittedToken)) {
  73. return $this->json(["error" => true, "message" => $translator->trans("invalid_token_msg")]);
  74. }
  75. $apiKey = $siteSettingService->getByConstantName("mailchimp-api-key");
  76. if (!Validate::not_null($apiKey)) {
  77. return $this->json(["error" => true, "message" => "There is no t APIKey"]);
  78. }
  79. $email = $request->request->get('_token');
  80. if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  81. $mailChimp = new MailChimp($apiKey);
  82. $listId = $siteSettingService->getByConstantName("mailchimp-list-id");
  83. $mailChimp->call("lists/$listId/members", [
  84. 'email_address' => $email,
  85. 'status' => "subscribed",
  86. ]);
  87. return $this->json(["error" => false, "message" => $translator->trans("subscribed_successfully_msg")]);
  88. }
  89. return $this->json(["error" => true, "message" => $translator->trans("enter_valid_email_msg")]);
  90. }
  91. public function menu(
  92. Request $request,
  93. CategoryWebsiteHeaderService $categoryWebsiteHeaderService,
  94. OccasionRepository $occasionRepository,
  95. ProductSearchRepository $productSearchRepository,
  96. $cr = null
  97. ): Response
  98. {
  99. $data = [];
  100. $data['mainCategories'] = $this->getHeaderCategories($request, $categoryWebsiteHeaderService);
  101. $data['occasion'] = $this->getActiveOccasion($request, $occasionRepository);
  102. $data['hasOnSale'] = $this->hasOnSaleProducts($productSearchRepository);
  103. $data["cr"] = $cr;
  104. return $this->render('fe/_menu.html.twig', $data);
  105. }
  106. public function footer(Request $request): Response
  107. {
  108. return $this->render('fe/_footer.html.twig');
  109. }
  110. private function getHeaderCategories(Request $request, CategoryWebsiteHeaderService $categoryWebsiteHeaderService): array
  111. {
  112. return $categoryWebsiteHeaderService->getData($request->getLocale());
  113. }
  114. private function getActiveOccasion(Request $request, OccasionRepository $occasionRepository)
  115. {
  116. $cacheKey = 'menu.occasion' . $request->getLocale();
  117. $cache = new FilesystemAdapter(directory: $this->containerParameterService->get("kernel.cache_dir") . "/filesystemAdapter-cache");
  118. return $cache->get($cacheKey, function (ItemInterface $item) use ($occasionRepository) {
  119. $item->expiresAfter(86400);// expire after 24 hrs
  120. return $occasionRepository->getActiveOccasion();
  121. });
  122. }
  123. private function hasOnSaleProducts(ProductSearchRepository $productSearchRepository): bool
  124. {
  125. $cacheKey = 'menu.on-sale';
  126. $cache = new FilesystemAdapter(directory: $this->containerParameterService->get("kernel.cache_dir") . "/filesystemAdapter-cache");
  127. return $cache->get($cacheKey, function (ItemInterface $item) use ($productSearchRepository) {
  128. $item->expiresAfter(3600);// expire after 1 hr
  129. $search = new \stdClass();
  130. $search->offer = true;
  131. $count = $productSearchRepository->filter($search, true);
  132. return $count > 0;
  133. });
  134. }
  135. }