src/HomeBundle/Controller/HomePageController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\HomeBundle\Controller;
  3. use App\BaseBundle\Controller\AbstractController;
  4. use App\CMSBundle\Enum\BannerPlacementEnum;
  5. use App\CMSBundle\Repository\BlogRepository;
  6. use App\CMSBundle\Service\BannerService;
  7. use App\MediaBundle\Entity\Image;
  8. use App\ProductBundle\Entity\Category;
  9. use App\ProductBundle\Entity\Product;
  10. use App\ProductBundle\Repository\CategoryRepository;
  11. use App\ProductBundle\Repository\CollectionRepository;
  12. use App\ProductBundle\Repository\ProductSearchRepository;
  13. use App\ProductBundle\Service\ProductSearchService;
  14. use App\UserBundle\Entity\User;
  15. use PN\SeoBundle\Repository\SeoPageRepository;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. /**
  20. * HomePage controller.
  21. *
  22. * @Route("")
  23. */
  24. class HomePageController extends AbstractController
  25. {
  26. /**
  27. * @Route("", name="fe_home", methods={"GET"})
  28. */
  29. public function index(
  30. SeoPageRepository $seoPageRepository,
  31. BannerService $bannerService,
  32. ): Response
  33. {
  34. $seoPage = $seoPageRepository->findOneByType("home-page");
  35. $banners = $bannerService->getBanners(BannerPlacementEnum::HOME_PAGE_SLIDER, 6);
  36. return $this->render('home/homePage/index.html.twig', [
  37. "seoPage" => $seoPage,
  38. "banners" => $banners,
  39. ]);
  40. }
  41. /**
  42. * @Route("/remove-image", name="fe_remove", methods={"GET"})
  43. */
  44. public function removeImage()
  45. {
  46. $this->createAccessDeniedException();
  47. $products = $this->em()->getRepository(Product::class)->findAll();
  48. foreach ($products as $product) {
  49. $images = $product->getPost()->getImages();
  50. foreach ($images as $image) {
  51. $this->em()->remove($image);
  52. }
  53. $this->em()->flush();
  54. }
  55. $categories = $this->em()->getRepository(Category::class)->findAll();
  56. foreach ($categories as $category) {
  57. $image = $category->getImage();
  58. if ($image != null) {
  59. $category->setImage(null);
  60. $this->em()->remove($image);
  61. }
  62. $images = $category->getPost()->getImages();
  63. foreach ($images as $image) {
  64. $this->em()->remove($image);
  65. }
  66. $this->em()->flush();
  67. }
  68. $images = $this->em()->getRepository(Image::class)->findAll();
  69. foreach ($images as $image) {
  70. if (str_contains($image->getBasePath(), "/product/")) {
  71. $this->em()->remove($image);
  72. }
  73. $this->em()->flush();
  74. }
  75. }
  76. /**
  77. * @Route("/first-banners-section", name="fe_home_first_banners_section_ajax", methods={"GET"})
  78. */
  79. public function firstBannersSection(BannerService $bannerService): Response
  80. {
  81. $banners = $bannerService->getBanners(BannerPlacementEnum::HOME_PAGE_TOP_2_ITEM, 2);
  82. return $this->render('home/homePage/firstBannersSection.html.twig', [
  83. "banners" => $banners,
  84. ]);
  85. }
  86. /**
  87. * @Route("/second-banners-section", name="fe_home_second_banners_section_ajax", methods={"GET"})
  88. */
  89. public function secondBannersSection(BannerService $bannerService): Response
  90. {
  91. $threeBanners = $bannerService->getBanners(BannerPlacementEnum::HOME_PAGE_MIDDLE_3_ITEMS, 3);
  92. $twoBanners = $bannerService->getBanners(BannerPlacementEnum::HOME_PAGE_MIDDLE_2_ITEMS, 2);
  93. return $this->render('home/homePage/secondBannersSection.html.twig', [
  94. "threeBanners" => $threeBanners,
  95. "twoBanners" => $twoBanners,
  96. ]);
  97. }
  98. /**
  99. * @Route("/new-arrivals", name="fe_home_new_arrivals_section_ajax", methods={"GET"})
  100. */
  101. public function newArrivalsSection(
  102. TranslatorInterface $translator,
  103. ProductSearchService $productSearchService,
  104. ProductSearchRepository $productSearchRepository
  105. ): Response
  106. {
  107. $products = $this->getNewArrivalsProducts($productSearchRepository);
  108. $return = [
  109. "title" => [
  110. "title" => $translator->trans("new_arrivals_txt"),
  111. "subTitle" => null,
  112. "icon" => null,
  113. "style" => 3,
  114. "actionBtn" => null,
  115. ],
  116. "products" => [],
  117. ];
  118. foreach ($products as $product) {
  119. $return["products"][] = $productSearchService->convertEntityToObject($product);
  120. }
  121. return $this->json($return);
  122. }
  123. /**
  124. * @Route("/recommended-for-you-section", name="fe_home_recommended_for_you_section_ajax", methods={"GET"})
  125. */
  126. public function recommendedForYouSection(
  127. TranslatorInterface $translator,
  128. ProductSearchService $productSearchService,
  129. ProductSearchRepository $productSearchRepository
  130. ): Response
  131. {
  132. $products = $this->getRecommendedForYouProducts($productSearchRepository);
  133. $return = [
  134. "title" => [
  135. "title" => $translator->trans("recommended_for_you_txt"),
  136. "subTitle" => null,
  137. "icon" => null,
  138. "style" => 3,
  139. "actionBtn" => null,
  140. ],
  141. "products" => [],
  142. ];
  143. foreach ($products as $product) {
  144. $return["products"][] = $productSearchService->convertEntityToObject($product);
  145. }
  146. return $this->json($return);
  147. }
  148. /**
  149. * @Route("/third-banners-section", name="fe_home_third_banners_section_ajax", methods={"GET"})
  150. */
  151. public function thirdBannersSection(BannerService $bannerService): Response
  152. {
  153. $twoBanners = $bannerService->getBanners(BannerPlacementEnum::HOME_PAGE_UNDER_MIDDLE_2_ITEMS, 2);
  154. $oneBanner = $bannerService->getOneBanner(BannerPlacementEnum::HOME_PAGE_UNDER_MIDDLE_FULL_WIDTH);
  155. return $this->render('home/homePage/thirdBannersSection.html.twig', [
  156. "twoBanners" => $twoBanners,
  157. "oneBanner" => $oneBanner,
  158. ]);
  159. }
  160. /**
  161. * @Route("/best-seller-section", name="fe_home_best_seller_section_ajax", methods={"GET"})
  162. */
  163. public function bestSellerSection(
  164. TranslatorInterface $translator,
  165. ProductSearchService $productSearchService,
  166. ProductSearchRepository $productSearchRepository
  167. ): Response
  168. {
  169. $products = $this->getBestSellerProducts($productSearchRepository);
  170. $return = [
  171. "title" => [
  172. "title" => $translator->trans("best_seller_txt"),
  173. "style" => 3,
  174. "icon" => null,
  175. "subTitle" => $translator->trans("products_txt"),
  176. "actionBtn" => null,
  177. ],
  178. "products" => [],
  179. ];
  180. foreach ($products as $product) {
  181. $return["products"][] = $productSearchService->convertEntityToObject($product);
  182. }
  183. return $this->json($return);
  184. }
  185. /**
  186. * @Route("/collections-section", name="fe_home_collection_section_ajax", methods={"GET"})
  187. */
  188. public function collectionsSection(CollectionRepository $collectionRepository): Response
  189. {
  190. $search = new \stdClass();
  191. $search->deleted = 0;
  192. $search->publish = true;
  193. $search->featured = true;
  194. $search->hasProducts = true;
  195. $collections = $collectionRepository->filter($search, false, 0, 6);
  196. return $this->render('home/homePage/collectionsSection.html.twig', [
  197. "collections" => $collections,
  198. ]);
  199. }
  200. /**
  201. * @Route("/blogs-section", name="fe_home_blog_section_ajax", methods={"GET"})
  202. */
  203. public function blogsSection(BlogRepository $blogRepository): Response
  204. {
  205. $search = new \stdClass;
  206. $search->ordr = ["column" => 1, "dir" => "ASC"];
  207. $search->deleted = 0;
  208. $search->publish = 1;
  209. $search->featured = 1;
  210. $blogs = $blogRepository->filter($search, false, 0, 4);
  211. return $this->render('home/homePage/blogsSection.html.twig', [
  212. "blogs" => $blogs,
  213. ]);
  214. }
  215. /**
  216. * @Route("/fourth-banners-section", name="fe_home_fourth_banners_section_ajax", methods={"GET"})
  217. */
  218. public function fourthBannersSection(BannerService $bannerService): Response
  219. {
  220. $oneBanner = $bannerService->getOneBanner(BannerPlacementEnum::HOME_PAGE_BOTTOM_FULL_WIDTH);
  221. return $this->render('home/homePage/fourthBannersSection.html.twig', [
  222. "oneBanner" => $oneBanner,
  223. ]);
  224. }
  225. /**
  226. * @Route("/featured-category-section", name="fe_home_featured_category_section_ajax", methods={"GET"})
  227. */
  228. public function featuredCategorySection(
  229. TranslatorInterface $translator,
  230. ProductSearchRepository $productSearchRepository,
  231. CategoryRepository $categoryRepository,
  232. ProductSearchService $productSearchService
  233. ): Response
  234. {
  235. $categories = $this->getFeaturedCategories($productSearchRepository, $categoryRepository);
  236. $return = [];
  237. foreach ($categories as $category) {
  238. $object = [
  239. "title" => [
  240. "title" => $category->getTitle(),
  241. "subTitle" => $translator->trans("products_txt"),
  242. "icon" => null,
  243. "style" => 3,
  244. "actionBtn" => [
  245. "text" => $translator->trans("view_all_txt"),
  246. "link" => $this->generateUrl("fe_product_filter_category",
  247. ["slug" => $category->getSeo()->getSlug()]),
  248. ],
  249. ],
  250. "products" => [],
  251. ];
  252. foreach ($category->virtualProducts as $product) {
  253. $object["products"][] = $productSearchService->convertEntityToObject($product);
  254. }
  255. $return[] = $object;
  256. }
  257. return $this->json($return);
  258. }
  259. //====================================================================PRIVATE METHODS====================================================================
  260. private function getRecommendedForYouProducts(ProductSearchRepository $productSearchRepository): array
  261. {
  262. $search = new \stdClass();
  263. $search->ordr = ["column" => 4, "dir" => "DESC"];;
  264. // $search->offer = true;
  265. $search->featured = true;
  266. $search->hasStock = true;
  267. if ($this->getUser() instanceof User) {
  268. $search->currentUserId = $this->getUser()->getId();
  269. }
  270. return $productSearchRepository->filter($search, false, 0, 12);
  271. }
  272. private function getNewArrivalsProducts(ProductSearchRepository $productSearchRepository): array
  273. {
  274. $search = new \stdClass();
  275. $search->ordr = ["column" => 4, "dir" => "DESC"];;
  276. // $search->offer = true;
  277. // $search->featured = true;
  278. $search->newArrival = true;
  279. $search->hasStock = true;
  280. if ($this->getUser() instanceof User) {
  281. $search->currentUserId = $this->getUser()->getId();
  282. }
  283. return $productSearchRepository->filter($search, false, 0, 12);
  284. }
  285. // not optimized
  286. private function getFeaturedCategories(
  287. ProductSearchRepository $productSearchRepository,
  288. CategoryRepository $categoryRepository
  289. ): array
  290. {
  291. $featuredCategories = $categoryRepository->findBy([
  292. // 'parent' => null,
  293. 'deleted' => null,
  294. 'featured' => true,
  295. 'publish' => true,
  296. ], null, 3);
  297. $categoryProducts = [];
  298. foreach ($featuredCategories as $featuredCategory) {
  299. $products = $this->getFeaturedProductsByCategory(
  300. $productSearchRepository,
  301. $featuredCategory
  302. );
  303. if (count($products) > 0) {
  304. $featuredCategory->virtualProducts = $products;
  305. $categoryProducts[] = $featuredCategory;
  306. }
  307. }
  308. return $categoryProducts;
  309. }
  310. private function getFeaturedProductsByCategory(ProductSearchRepository $productSearchRepository, Category $category): array
  311. {
  312. $search = new \stdClass();
  313. $search->ordr = ["column" => 4, "dir" => "DESC"];;
  314. // $search->featured = 1;
  315. $search->hasStock = true;
  316. if ($this->getUser() instanceof User) {
  317. $search->currentUserId = $this->getUser()->getId();
  318. }
  319. $search->categories = explode(',', $category->getConcatIds());
  320. return $productSearchRepository->filter($search, false, 0, 12);
  321. }
  322. private function getBestSellerProducts(ProductSearchRepository $productSearchRepository): array
  323. {
  324. $search = new \stdClass();
  325. $search->bestSeller = true;
  326. $search->hasStock = true;
  327. $search->ordr = ["column" => 4, "dir" => "DESC"];;
  328. if ($this->getUser() instanceof User) {
  329. $search->currentUserId = $this->getUser()->getId();
  330. }
  331. return $productSearchRepository->filter($search, false, 0, 12);
  332. }
  333. }