src/Controller/PagesController.php line 32

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\NewsletterSubscriber;
  4. use App\Form\NewsletterSubscriberType;
  5. use App\Repository\CategoryRepository;
  6. use App\Repository\ProductCollectionRepository;
  7. use App\Repository\ProductRepository;
  8. use App\Repository\ProductVariantRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Translation\LocaleSwitcher;
  15. class PagesController extends AbstractController
  16. {
  17.     public function __construct(
  18.         private EntityManagerInterface $entityManager,
  19.         private CategoryRepository $categoryRepository,
  20.         private ProductCollectionRepository $collectionRepository,
  21.         private ProductVariantRepository $variantRepository,
  22.         private ProductRepository $productRepository,
  23.         private LocaleSwitcher $localeSwitcher
  24.     )
  25.     {}
  26.     #[Route('/{_locale}'name'app_home')]
  27.     public function index(Request $request): Response
  28.     {
  29.         //menu categories
  30.         $categories $this->categoryRepository->findAll();
  31.         //banner main categories
  32.         $mainCategories $this->categoryRepository->findBy(["level" => 1]);
  33.         // newsletter form handling
  34.         $newsLetterSubscriber = new NewsletterSubscriber();
  35.         $form $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
  36.         $form->handleRequest($request);
  37.         // dd($form);
  38.         if($form->isSubmitted() && $form->isValid()){
  39.             $newsLetterSubscriber $form->getData();
  40.             $this->entityManager->persist($newsLetterSubscriber);
  41.             $this->entityManager->flush();
  42.         }
  43.         return $this->render('front_end/home/home.twig', [
  44.             'controller_name' => 'FrontEndController',
  45.             'categories' => $categories,
  46.             'mainCategories' => $mainCategories,
  47.             'pageName' => 'Home',
  48.             'form' => $form,
  49.             'activePage' => 0
  50.         ]);
  51.     }
  52.     #[Route('{_locale}/mens',name'app_home_men')]
  53.     public function indexMen(Request $request): Response
  54.     {
  55.         
  56.         $session $request->getSession();
  57.         $session->set('gender','mens');
  58.         //all categories
  59.         $categories $this->categoryRepository->findAll();
  60.         //main categories (level 1)
  61.         $mainCategories $this->categoryRepository->findBy(["level" => 1]);
  62.        //Men sub categories (level 2)
  63.        $subCategories $this->categoryRepository->findBy(["parentCategory" => 1,"level" => 2]);
  64.        //Featured Collections (the admin should decide what categories must be considered as featured)
  65.        $featuredCollections $this->collectionRepository->findBy(["gender" => 1,"featured" => true],[],12);
  66.        //New Arrivals
  67.        $newArrivals $this->collectionRepository->findBy(["gender" => 1,'name' => "New Arrivals"],[],8);
  68.        $newArrivals $newArrivals $newArrivals[0]->getProducts() : null;
  69.        
  70.        //Featured Products
  71.        $featuredProducts $this->collectionRepository->findBy(["gender" => 1,'name' => "Featured Products"],[],8);
  72.        $featuredProducts $featuredProducts $featuredProducts[0]->getProducts() : null;
  73.        //Best Sellers
  74.        $bestSellers $this->collectionRepository->findBy(["gender" => 1,'name' => "Best Sellers"],[],8);
  75.        $bestSellers $bestSellers $bestSellers[0]->getProducts() : null;
  76.         // newsletter form handling
  77.         $newsLetterSubscriber = new NewsletterSubscriber();
  78.         $form $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
  79.         $form->handleRequest($request);
  80.         // dd($form);
  81.         if($form->isSubmitted() && $form->isValid()){
  82.             $newsLetterSubscriber $form->getData();
  83.             $this->entityManager->persist($newsLetterSubscriber);
  84.             $this->entityManager->flush();
  85.         }
  86.         return $this->render('mens/home/home_men.twig',[
  87.             'controller_name' => 'FrontEndController',
  88.             'mainCategories' => $mainCategories,
  89.             'categories' => $categories,
  90.             'form' => $form,
  91.             'subCategories' => $subCategories,
  92.             'activePage' => 1,
  93.             'featuredCollections' => $featuredCollections,
  94.             'featuredProducts' => $featuredProducts,
  95.             'newArrivals' => $newArrivals,
  96.             'bestSellers' => $bestSellers
  97.         ]);
  98.     }
  99.     #[Route('{_locale}/women',name'app_home_women')]
  100.     public function indexWomen(Request $request): Response
  101.     {
  102.         $session $request->getSession();
  103.         $session->set('gender','women');
  104.         //menu categories
  105.         // $categories = $this->categoryRepository->findAll();
  106.         //main categories (level 1)
  107.         $mainCategories $this->categoryRepository->findBy(["level" => 1]);
  108.         //banner main categories
  109.         $subCategories $this->categoryRepository->findBy(["parentCategory" => 2,"level" => 2]);
  110.         //Featured Collections (the admin should decide what categories must be considered as featured)
  111.        $featuredCollections $this->collectionRepository->findBy(["gender" => 2,"featured" => true],[],12);
  112.         // dd($featuredCollections);
  113.        //New Arrivals
  114.        $newArrivals $this->collectionRepository->findBy(["gender" => 2,'name' => "New Arrival"],[],8);
  115.        $newArrivals $newArrivals $newArrivals[0]->getProducts() : null;
  116.        //Featured Products
  117.        $featuredProducts $this->collectionRepository->findBy(["gender" => 2,'name' => "Top Products"],[],8);
  118.        $featuredProducts $featuredProducts $featuredProducts[0]->getProducts() : null;
  119.        //Best Sellers
  120.        $bestSellers $this->collectionRepository->findBy(["gender" => 2,'name' => "Top Sellers"],[],8);
  121.        $bestSellers $bestSellers $bestSellers[0]->getProducts() : null;
  122.         // newsletter form handling
  123.         $newsLetterSubscriber = new NewsletterSubscriber();
  124.         $form $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
  125.         $form->handleRequest($request);
  126.         // dd($form);
  127.         if($form->isSubmitted() && $form->isValid()){
  128.             $newsLetterSubscriber $form->getData();
  129.             $this->entityManager->persist($newsLetterSubscriber);
  130.             $this->entityManager->flush();
  131.         }
  132.         return $this->render('women/home/home_women.twig',[
  133.             'controller_name' => 'FrontEndController',
  134.             // 'categories' => $categories,
  135.             'mainCategories' => $mainCategories,
  136.             'subCategories' => $subCategories,
  137.             'featuredCollections' => $featuredCollections,
  138.             'form' => $form,
  139.             'activePage' => 2,
  140.             'newArrivals' => $newArrivals,
  141.             'featuredProducts' => $featuredProducts,
  142.             'bestSellers' => $bestSellers
  143.         ]);
  144.     }
  145.     #[Route('{_locale}/{gender}/policies/{policyPage}',name'app_policies')]
  146.     #[Route('{_locale}/policies/{policyPage}',name'app_index_policies')]
  147.     public function policies($policyPage,Request $request)
  148.     {
  149.         $genderId $request->get("gender") == "mens" 2;
  150.         //menu categories
  151.         $categories $this->categoryRepository->findAll();
  152.         //banner main categories
  153.         $mainCategories $this->categoryRepository->findBy(["level" => 1]);
  154.         if($request->attributes->get('_route') != 'policies' ){
  155.             // //Men sub categories (level 2)
  156.             $subCategories $this->categoryRepository->findBy(["parentCategory" => $genderId,"level" => 2]);
  157.         }else{
  158.             $subCategories '';
  159.         }
  160.         // newsletter form handling
  161.         $newsLetterSubscriber = new NewsletterSubscriber();
  162.         $form $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
  163.         $form->handleRequest($request);
  164.         // dd($form);
  165.         if($form->isSubmitted() && $form->isValid()){
  166.             $newsLetterSubscriber $form->getData();
  167.             $this->entityManager->persist($newsLetterSubscriber);
  168.             $this->entityManager->flush();
  169.         }
  170.         $subsite $genderId == 'mens' 'women';
  171.         $pagePaths = [
  172.                 'privacy' => $subsite.'/policies/privacy.twig',
  173.                 'terms' => $subsite.'/policies/terms.twig',
  174.                 'refund' => $subsite.'/policies/refund.twig'
  175.         ];
  176.         
  177.         return $this->render($pagePaths[$policyPage], [
  178.             'controller_name' => 'FrontEndController',
  179.             'categories' => $categories,
  180.             'subCategories' => $subCategories,
  181.             'mainCategories' => $mainCategories,
  182.             'pageName' => 'Home',
  183.             'form' => $form,
  184.             'activePage' => 'Privacy'
  185.         ]);
  186.     }
  187.     #[Route('{_locale}/search',name'app_search')]
  188.     public function search(Request $request): Response
  189.     {
  190.         // Search logic
  191.         $searchInput $request->get('searchInput');
  192.         $applocale $this->localeSwitcher->getLocale();
  193.         $gender $request->getSession()->get('gender');
  194.         $gender $gender $gender "mens";
  195.         if($products $this->productRepository->findByNameLike($searchInput)){
  196.             $items = [];
  197.             foreach ($products as $product) {
  198.                 $pid $product->getId();
  199.                 $vid $product->firstVariantId();
  200.                 $path =  "/".$applocale."/".$gender."/product/".$pid."/".$vid."/";
  201.                 $name $product->getName();
  202.                 $item = [
  203.                             "name" => $name,
  204.                             "path" => $path,
  205.                             "image" => $product->getMainImage(),
  206.                             "price" => $product->firstVariantPrice(),
  207.                         ];
  208.                 $items[] = $item;
  209.             }
  210.         }
  211.         
  212.         // if($collections = $this->collectionRepository->findByNameLike($searchInput)){
  213.         //     $collections = [];
  214.         //     foreach($collections as $collection){
  215.         //         $name = $collection->getName();
  216.         //         $path = "/";
  217.         //         $collection = ["name" => $name, "path" => $path];
  218.         //         $collections[] = $collection;
  219.         //     }
  220.         // };
  221.         //dump($productResults);
  222.         return $this->json(['results' => $items]);
  223.     }
  224. }