<?php
namespace App\Controller\Product\Public;
use App\Entity\Contact;
use App\Entity\Product\Product;
use App\Entity\Product\ProductCategory;
use App\Form\ContactType;
use App\Repository\ArrayJsonRepository;
use App\Repository\ConfigurationRepository;
use App\Repository\Gallery\CollectionGalleryRepository;
use App\Repository\ModuleInformationRepository;
use App\Repository\Product\ProductCategoryRepository;
use App\Repository\Product\ProductConfigRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\Product\ProductSubCategoryRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
#[Route('/product')]
class ProductController extends AbstractController
{
#[Route('/', name: 'app_product')]
public function index(ConfigurationRepository $configurationRepository, ProductRepository $productRepository, Security $security, ProductCategoryRepository $productCategoryRepository, Request $request, ArrayJsonRepository $arrayJsonRepository): Response
{
$dynamicLinkArray = $arrayJsonRepository->findOneBy(['name' => 'dynamic_link']);
if ($dynamicLinkArray == null) {
$dynamicLink = null;
} else {
$dynamicLink = $dynamicLinkArray->getContent() ?? null;
}
$config = $configurationRepository->findOneBy([]);
$is_online = $config->isIsOnline();
if ($is_online == true) {
if ($security->isGranted('ROLE_ADMIN')) {
} else {
return $this->redirectToRoute('app_maintenance');
}
}
$categories = $productCategoryRepository->findAll();
$products = $productRepository->findBy(['is_activ' => true]);
$cat = $request->get('cat');
if (isset($cat)) {
$products = $productRepository->searchByCategory($cat);
}
$brand = $request->get('brand');
if (isset($brand)) {
$products = $productRepository->searchByBrand($brand);
}
return $this->render('product/public/index.html.twig', [
'products' => $products,
'categories' => $categories,
'dynamic_link' => $dynamicLink,
]);
}
#[Route('/{slug}', name: 'app_product_show')]
public function show(ConfigurationRepository $configurationRepository, Product $product, ProductConfigRepository $productConfigRepository, ArrayJsonRepository $arrayJsonRepository): Response
{
$dynamicLinkArray = $arrayJsonRepository->findOneBy(['name' => 'dynamic_link']);
if ($dynamicLinkArray == null) {
$dynamicLink = null;
} else {
$dynamicLink = $dynamicLinkArray->getContent() ?? null;
}
$productConfig = $productConfigRepository->findOneBy([]);
$config = $configurationRepository->findOneBy([]);
if (!isset($collection)) {
$collection = false;
}
if ($product->getProductImages() == null) {
$metaImg = $config->getMetaImgDefault();
} else {
$metaImg = $config->getWebsiteUrl() . "uploads/" . $product->getProductImages()[0]->getImage()->getName();
}
$similars = $product->getSimilarToProducts();
$products = [];
foreach ($similars as $similar) {
$products[] = $similar->getProduct();
}
$allCategories = $product->getCategories();
$leafCategories = $allCategories->filter(function (ProductCategory $category) {
return $category->getProductCategories()->isEmpty();
});
return $this->render('product/public/show.html.twig', [
'product' => $product,
'productConfig' => $productConfig,
'metaImg' => $metaImg,
'products' => $products,
'categories' => $leafCategories,
'dynamicLink' => $dynamicLink,
]);
}
}