src/Controller/PagesController.php line 32
<?php
namespace App\Controller;
use App\Entity\NewsletterSubscriber;
use App\Form\NewsletterSubscriberType;
use App\Repository\CategoryRepository;
use App\Repository\ProductCollectionRepository;
use App\Repository\ProductRepository;
use App\Repository\ProductVariantRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Translation\LocaleSwitcher;
class PagesController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
private CategoryRepository $categoryRepository,
private ProductCollectionRepository $collectionRepository,
private ProductVariantRepository $variantRepository,
private ProductRepository $productRepository,
private LocaleSwitcher $localeSwitcher
)
{}
#[Route('/{_locale}', name: 'app_home')]
public function index(Request $request): Response
{
//menu categories
$categories = $this->categoryRepository->findAll();
//banner main categories
$mainCategories = $this->categoryRepository->findBy(["level" => 1]);
// newsletter form handling
$newsLetterSubscriber = new NewsletterSubscriber();
$form = $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
$form->handleRequest($request);
// dd($form);
if($form->isSubmitted() && $form->isValid()){
$newsLetterSubscriber = $form->getData();
$this->entityManager->persist($newsLetterSubscriber);
$this->entityManager->flush();
}
return $this->render('front_end/home/home.twig', [
'controller_name' => 'FrontEndController',
'categories' => $categories,
'mainCategories' => $mainCategories,
'pageName' => 'Home',
'form' => $form,
'activePage' => 0
]);
}
#[Route('{_locale}/mens',name: 'app_home_men')]
public function indexMen(Request $request): Response
{
$session = $request->getSession();
$session->set('gender','mens');
//all categories
$categories = $this->categoryRepository->findAll();
//main categories (level 1)
$mainCategories = $this->categoryRepository->findBy(["level" => 1]);
//Men sub categories (level 2)
$subCategories = $this->categoryRepository->findBy(["parentCategory" => 1,"level" => 2]);
//Featured Collections (the admin should decide what categories must be considered as featured)
$featuredCollections = $this->collectionRepository->findBy(["gender" => 1,"featured" => true],[],12);
//New Arrivals
$newArrivals = $this->collectionRepository->findBy(["gender" => 1,'name' => "New Arrivals"],[],8);
$newArrivals = $newArrivals ? $newArrivals[0]->getProducts() : null;
//Featured Products
$featuredProducts = $this->collectionRepository->findBy(["gender" => 1,'name' => "Featured Products"],[],8);
$featuredProducts = $featuredProducts ? $featuredProducts[0]->getProducts() : null;
//Best Sellers
$bestSellers = $this->collectionRepository->findBy(["gender" => 1,'name' => "Best Sellers"],[],8);
$bestSellers = $bestSellers ? $bestSellers[0]->getProducts() : null;
// newsletter form handling
$newsLetterSubscriber = new NewsletterSubscriber();
$form = $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
$form->handleRequest($request);
// dd($form);
if($form->isSubmitted() && $form->isValid()){
$newsLetterSubscriber = $form->getData();
$this->entityManager->persist($newsLetterSubscriber);
$this->entityManager->flush();
}
return $this->render('mens/home/home_men.twig',[
'controller_name' => 'FrontEndController',
'mainCategories' => $mainCategories,
'categories' => $categories,
'form' => $form,
'subCategories' => $subCategories,
'activePage' => 1,
'featuredCollections' => $featuredCollections,
'featuredProducts' => $featuredProducts,
'newArrivals' => $newArrivals,
'bestSellers' => $bestSellers
]);
}
#[Route('{_locale}/women',name: 'app_home_women')]
public function indexWomen(Request $request): Response
{
$session = $request->getSession();
$session->set('gender','women');
//menu categories
// $categories = $this->categoryRepository->findAll();
//main categories (level 1)
$mainCategories = $this->categoryRepository->findBy(["level" => 1]);
//banner main categories
$subCategories = $this->categoryRepository->findBy(["parentCategory" => 2,"level" => 2]);
//Featured Collections (the admin should decide what categories must be considered as featured)
$featuredCollections = $this->collectionRepository->findBy(["gender" => 2,"featured" => true],[],12);
// dd($featuredCollections);
//New Arrivals
$newArrivals = $this->collectionRepository->findBy(["gender" => 2,'name' => "New Arrival"],[],8);
$newArrivals = $newArrivals ? $newArrivals[0]->getProducts() : null;
//Featured Products
$featuredProducts = $this->collectionRepository->findBy(["gender" => 2,'name' => "Top Products"],[],8);
$featuredProducts = $featuredProducts ? $featuredProducts[0]->getProducts() : null;
//Best Sellers
$bestSellers = $this->collectionRepository->findBy(["gender" => 2,'name' => "Top Sellers"],[],8);
$bestSellers = $bestSellers ? $bestSellers[0]->getProducts() : null;
// newsletter form handling
$newsLetterSubscriber = new NewsletterSubscriber();
$form = $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
$form->handleRequest($request);
// dd($form);
if($form->isSubmitted() && $form->isValid()){
$newsLetterSubscriber = $form->getData();
$this->entityManager->persist($newsLetterSubscriber);
$this->entityManager->flush();
}
return $this->render('women/home/home_women.twig',[
'controller_name' => 'FrontEndController',
// 'categories' => $categories,
'mainCategories' => $mainCategories,
'subCategories' => $subCategories,
'featuredCollections' => $featuredCollections,
'form' => $form,
'activePage' => 2,
'newArrivals' => $newArrivals,
'featuredProducts' => $featuredProducts,
'bestSellers' => $bestSellers
]);
}
#[Route('{_locale}/{gender}/policies/{policyPage}',name: 'app_policies')]
#[Route('{_locale}/policies/{policyPage}',name: 'app_index_policies')]
public function policies($policyPage,Request $request)
{
$genderId = $request->get("gender") == "mens" ? 1 : 2;
//menu categories
$categories = $this->categoryRepository->findAll();
//banner main categories
$mainCategories = $this->categoryRepository->findBy(["level" => 1]);
if($request->attributes->get('_route') != 'policies' ){
// //Men sub categories (level 2)
$subCategories = $this->categoryRepository->findBy(["parentCategory" => $genderId,"level" => 2]);
}else{
$subCategories = '';
}
// newsletter form handling
$newsLetterSubscriber = new NewsletterSubscriber();
$form = $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
$form->handleRequest($request);
// dd($form);
if($form->isSubmitted() && $form->isValid()){
$newsLetterSubscriber = $form->getData();
$this->entityManager->persist($newsLetterSubscriber);
$this->entityManager->flush();
}
$subsite = $genderId == 1 ? 'mens' : 'women';
$pagePaths = [
'privacy' => $subsite.'/policies/privacy.twig',
'terms' => $subsite.'/policies/terms.twig',
'refund' => $subsite.'/policies/refund.twig'
];
return $this->render($pagePaths[$policyPage], [
'controller_name' => 'FrontEndController',
'categories' => $categories,
'subCategories' => $subCategories,
'mainCategories' => $mainCategories,
'pageName' => 'Home',
'form' => $form,
'activePage' => 'Privacy'
]);
}
#[Route('{_locale}/search',name: 'app_search')]
public function search(Request $request): Response
{
// Search logic
$searchInput = $request->get('searchInput');
$applocale = $this->localeSwitcher->getLocale();
$gender = $request->getSession()->get('gender');
$gender = $gender ? $gender : "mens";
if($products = $this->productRepository->findByNameLike($searchInput)){
$items = [];
foreach ($products as $product) {
$pid = $product->getId();
$vid = $product->firstVariantId();
$path = "/".$applocale."/".$gender."/product/".$pid."/".$vid."/";
$name = $product->getName();
$item = [
"name" => $name,
"path" => $path,
"image" => $product->getMainImage(),
"price" => $product->firstVariantPrice(),
];
$items[] = $item;
}
}
// if($collections = $this->collectionRepository->findByNameLike($searchInput)){
// $collections = [];
// foreach($collections as $collection){
// $name = $collection->getName();
// $path = "/";
// $collection = ["name" => $name, "path" => $path];
// $collections[] = $collection;
// }
// };
//dump($productResults);
return $this->json(['results' => $items]);
}
}