src/Controller/ProductController.php line 221
<?php
namespace App\Controller;
use App\Controller\Traits\HreflangsControllerTrait;
use App\Controller\Traits\MetatagsControllerTrait;
use App\Controller\Traits\StaticPageHelperTrait;
use App\Entity\OpenPosition;
use App\Entity\Product;
use App\Entity\ProductCategory;
use App\Entity\ProductLine;
use App\Entity\ProductType;
use App\Entity\RecipeType;
use App\Form\CustomerServiceType;
use App\Form\WorkWithUsType;
use App\Repository\ProductCategoryRepository;
use App\Repository\ProductRepository;
use App\Repository\ProductLineRepository;
use App\Repository\OpenPositionRepository;
use App\Repository\ProductTypeRepository;
use App\Repository\RecipeRepository;
use App\Repository\RecipeTypeRepository;
use App\Service\ProductCategoryUrlChainGenerator;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\Recipe;
class ProductController extends AbstractController
{
use MetatagsControllerTrait, HreflangsControllerTrait, StaticPageHelperTrait;
private $em;
private $transalor;
private $request;
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, RequestStack $requestStack){
$this->em = $em;
$this->transalor = $translator;
$this->request = $requestStack->getCurrentRequest();
}
/**
* PRODUCT_CATEGORY
*/
#[Route( path: [
'it' => '/categorie/{urlChain}',
'en' => 'en/categories/{urlChain}',
'es' => 'es/categorias/{urlChain}'
], name: 'app_product_category_show', requirements: ['urlChain' => '.+'])]
public function productCategory(
Request $request,
ProductCategoryUrlChainGenerator $productCategoryUrlChainGenerator,
string|null $urlChain
)
{
$productCategories = $productCategoryUrlChainGenerator->getProductCategoryResources($urlChain, $request->getLocale());
if(!is_array($productCategories)) {
throw $this->createNotFoundException("Unable to find {$urlChain} path");
}
/** @var ProductCategory $productCategory */
$productCategory = null;
if (count($productCategories)) { // ricava l'ultima categoria
$productCategory = end($productCategories);
}
if (!$productCategory) { // controlla che la categoria esista
return $this->redirectToRoute('homepage', [], 301);
}
// eventuali sottocategorie
/** @var ProductCategoryRepository $repoProductCategory */
$repoProductCategory = $this->em->getRepository(ProductCategory::class);
$productSubCategories = $repoProductCategory->findBy([
'parent' => $productCategory,
'locale' => $request->getLocale(),
'enabled' => true,
],
[
'sortablePosition' => 'ASC'
]);
// eventuali prodotti associati
/** @var ProductRepository $repoProduct */
$repoProduct = $this->em->getRepository(Product::class);
$products = $repoProduct->findBy([
'productCategory' => $productCategory,
'locale' => $this->request->getLocale(),
'isEnabled' => true,
],
[
'sortablePosition' => 'ASC'
]);
$metatags = $this->getMetatags($productCategory, 'name', 'name');
$listBreadcrumbs = [];
/** @var ProductCategory $productCategoryItem */
foreach ($productCategories as $productCategoryItem) {
$listBreadcrumbs[] = [
$this->generateUrl('app_product_category_show', [
'urlChain' => $productCategoryUrlChainGenerator->buildPath(($productCategoryItem->getParent() ? $productCategories : [$productCategoryItem]))
]),
$productCategoryItem->getName(),
];
}
$template = 'prodotti/productCategory.html.twig';
if ($productCategory->getIdentifier() == ProductCategory::IDENTIFIER_CAMPIAMATI) {
$template = 'prodotti/campiamati.html.twig';
}
if ($productCategory->getIdentifier() == ProductCategory::IDENTIFIER_KROCCANTI) {
$template = 'prodotti/kroccanti.html.twig';
}
return $this->render($template, [
'listBreadcrumbs' => $listBreadcrumbs,
'products' => $products,
'productCategory' => $productCategory,
'productSubCategories' => $productSubCategories,
'metatags' => $metatags,
]);
}
/**
* RENDER ALL THE PRODUCTS FOR THE PRODUCT_TYPE
*/
// public function showCategory(string|null $category_slug)
// {
// $arrayRedirect = [
// 'ortofrutta',
// 'quienes-somos'
// ];
// if (in_array($category_slug, $arrayRedirect)){
// return $this->redirectToRoute('app_show_category_ortofrutta');
// }
//
// if($category_slug == 'spalmabile'){
// return $this->redirectToRoute('spalmabile_vegetale');
// }
// if ($category_slug == 'kroccanti') {
// return $this->redirectToRoute('kroccanti');
// }
// /** @var ProductTypeRepository $productTypeRepo */
// $productTypeRepo = $this->em->getRepository(ProductType::class);
// $routeName = $this->request->attributes->get('_route');
// if($routeName == 'app_show_category_ortofrutta'){
// switch ($this->request->getLocale()) {
// case 'es':
// $category_slug = 'quienes-somos';
// break;
// default:
// $category_slug = 'ortofrutta';
// break;
// }
// }
//
// $productType = $productTypeRepo->findOneBy([
// 'slug' => $category_slug,
// 'locale' => $this->request->getLocale(),
// ]);
// if (!$productType) { // controlla che la linea prodotti esista
//// throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
// return $this->redirectToRoute('homepage');
// }
// /** @var ProductLineRepository $productLineRepo */
// $productLineRepo = $this->em->getRepository(ProductLine::class);
// $productLine = $productLineRepo->findBy([
// 'productType' => $productType,
// 'locale' => $this->request->getLocale(),
// 'isEnabled' => true,
// ]);
// if (!$productLine) { // controlla che la linea prodotti esista
//// throw $this->createNotFoundException($this->transalor->trans('exception.product_line_not_found'));
// return $this->redirectToRoute('homepage');
// }
//
// /** @var ProductRepository $productRepo */
// $productRepo = $this->em->getRepository(Product::class);
// $products = $productRepo->findBy([
// 'type' => $productLine,
// 'locale' => $this->request->getLocale(),
// 'isEnabled' => true,
// ],
// [
// 'sortablePosition' => 'ASC'
// ]);
//
//
// $metatags = $this->getMetatags($productType, 'name', 'name');
//
// return $this->render('prodotti/productIndex.html.twig', [
// 'products' => $products,
// 'productLine' => $productLine,
// 'productType' => $productType,
// 'metatags' => $metatags,
// ]);
// }
/**
* RENDER THE PRODUCT
*/
#[Route(path: [
'it' => '/prodotti/{product_slug}',
'en' => 'en/products/{product_slug}',
'es' => 'es/productos/{product_slug}'
] , name: 'app_product_show')]
public function showProduct(
ProductCategoryUrlChainGenerator $productCategoryUrlChainGenerator,
Request $request, string|null
$category_slug,
string $product_slug
)
{
// if($category_slug == 'spalmabile'){
// return $this->redirectToRoute('spalmabile_vegetale');
// }
// $arrayRedirect = [
// 'ortofrutta',
// 'quienes-somos'
// ];
// if (in_array($category_slug, $arrayRedirect)){
// return $this->redirectToRoute('app_show_product_ortofrutta', [
// 'category_slug' => null,
// 'product_slug' => $product_slug
// ]);
// }
/** @var ProductRepository $productRepo */
$productRepo = $this->em->getRepository(Product::class);
// $routeName = $this->request->attributes->get('_route');
// if($routeName == 'app_show_product_ortofrutta'){
// switch ($this->request->getLocale()) {
// case 'es':
// $category_slug = 'quienes-somos';
// break;
// default:
// $category_slug = 'ortofrutta';
// break;
// }
// }
/** @var Product $product */
$product = $productRepo->findOneBy([
'slug' => $product_slug,
'isEnabled' => true,
'locale' => $this->request->getLocale(),
]);
if (!$product) { // controlla che la linea prodotti esista
return $this->redirectToRoute('homepage', [], 301);
// throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
}
$listBreadcrumbs = [];
/** @var ProductCategory $productCategoryItem */
$productCategoryItem = $product->getProductCategory();
if ($productCategoryItem instanceof ProductCategory) {
do {
$listBreadcrumbs[] = [
$this->generateUrl('app_product_category_show', [
'urlChain' => $productCategoryItem->getSlugComposite(),
]),
$productCategoryItem->getName(),
];
} while ($productCategoryItem = $productCategoryItem->getParent());
}
krsort($listBreadcrumbs);
$listBreadcrumbs[] = [
$this->generateUrl('app_product_show', [
'product_slug' => $product->getSlug()
]),
$product->getName(),
];
if ($product->getIdentifier() == Product::IDENTIFIER_SPALMABILE) {
$metatags = $this->getMetatags($product, 'name', 'description');
return $this->render('prodotti/spalmabile.html.twig', [
'metatags' => $metatags,
'listBreadcrumbs' => $listBreadcrumbs
]);
}
if (
$request->get('_route') == 'app_show_product_ortofrutta'
or $request->get('_route') == 'app_show_product'
) {
return $this->redirectToRoute('app_product_show', [
'slug' => $product->getSlug()
], 301);
}
// /** @var ProductTypeRepository $productTypeRepo */
// $productTypeRepo = $this->em->getRepository(ProductType::class);
// $productType = $productTypeRepo->findOneBy([
// 'slug' => $category_slug,
// 'locale' => $this->request->getLocale(),
// ]);
// // dd($productType, $category_slug, $this->request->getLocale());
// if (!$productType) { // controlla che la linea prodotti esista
// return $this->redirectToRoute('homepage');
//// throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
// }
// /** @var ProductLineRepository $productLineRepo */
// $productLineRepo = $this->em->getRepository(ProductLine::class);
// $productLine = $productLineRepo->findOneBy([
// 'productType' => $productType,
// 'id' => $product->getType()->getId(),
// 'locale' => $this->request->getLocale(),
// 'isEnabled' => true,
// ]); // if the category exists
// if ($product->getType() != $productLine) { // if product doesn't belong to the category
// return $this->redirectToRoute('homepage');
//// throw $this->createNotFoundException($this->transalor->trans('exception.product_not_found'));
// }
$correlatedProducts = $productRepo->findCorrelatedProducts($product->getId(), $product->getType()->getId(), $this->request->getLocale());
$metatags = $this->getMetatags($product, 'name', 'description');
return $this->render('prodotti/productDetail.html.twig', [
'product' => $product,
'metatags' => $metatags,
'correlatedProducts' => $correlatedProducts,
'listBreadcrumbs' => $listBreadcrumbs
]);
}
// #[Route(['/prodotti/spalmabile-vegetale',
// // 'en' => '/en',
// // 'es' => '/es',
// ], name: 'spalmabile_vegetale', priority:2)]
// public function landingSpalmabileVegetale(Request $request, EntityManagerInterface $em)
// {
// list ($metatags, $hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request, $em, $this->getParameter('locales'));
//
// return $this->render('landing/spalmabileVegetale.html.twig', [
// 'metatags' => $metatags,
// 'hreflangs' => $hreflangs
// ]);
// }
}