<?php
namespace App\Controller\Pages;
use App\Controller\API\ApiController;
use App\Controller\Utils\ContentController;
use App\Entity\Collection;
use App\Entity\Color;
use App\Entity\Product;
use App\Entity\Project;
use App\Entity\SeoPage;
use App\Entity\Shape;
use App\Service\DocumentFileUploader;
use App\Service\FilePathAndUrlService;
use App\Service\LocaleService;
use App\Traits\AttachmentUrls;
use Doctrine\ORM\EntityManagerInterface;
use Liip\ImagineBundle\Service\FilterService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProductController extends ContentController
{
use AttachmentUrls;
private $em;
private $localeService;
private $filePathAndUrlService;
protected $documentFileUploader;
public const PRODUCT_BASE_BREADCRUMB = [
HomeController::HOME_BREADCRUMB,
['route' => 'pages_products_overview', 'name' => 'Products overview']
];
public $apiController;
/**
* @var FilterService
*/
private $liip;
public function __construct(EventDispatcherInterface $dispatcher, LocaleService $localeService, ApiController $apiController,
FilePathAndUrlService $filePathAndUrlService,DocumentFileUploader $documentFileUploader,
FilterService $liip, EntityManagerInterface $entityManager)
{
$this->localeService = $localeService;
$this->filePathAndUrlService = $filePathAndUrlService;
$this->documentFileUploader = $documentFileUploader;
$this->apiController = $apiController;
parent::__construct($dispatcher, self::class);
$this->liip = $liip;
$this->em = $entityManager;
}
/**
* @Route({
* "ja": "/woven-vinyl-floors",
* "en": "/woven-vinyl-floors"
* }, name="pages_products_overview")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function index (Request $request)
{
if ($request->get('product')) {
$product = $this->getDoctrine()->getRepository(Product::class)->findOneBy(['slug' => $request->get('product')]);
if ($product) {
return $this->redirectToRoute('pages_product_details', [
'collectionSlug' => $product->getCollection()->getSlug(),
'slug' => $product->getSlug(),
'id' => $product->getId()
], 301);
}
}
$projects = $this->getDoctrine()->getRepository(Project::class)->findAll();
return $this->render('pages/product/base.html.twig', [
'breadCrumbs' => self::PRODUCT_BASE_BREADCRUMB,
'pictures' => $this->getPicturesToShow($projects, $this->filePathAndUrlService, $this->liip)
], null, $request);
}
/**
* @Route({
* "ja": "/woven-vinyl-floors/shape/{shapeSlug}",
* "en": "/woven-vinyl-floors/shape/{shapeSlug}"
* }, name="pages_products_filter_by_shapes")
* @param Request $request
* @param $shapeSlug
* @return Response
*/
public function shapes(Request $request, $shapeSlug)
{
$products = $this->apiController->getProductsFilterByShape($request->getLocale(), $shapeSlug, true);
$projects = $this->getProjectsFromEntities($products, $this->getDoctrine()->getManager());
$repository = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
$shapeObject = $repository->findObjectByTranslatedField('slug', $shapeSlug, Shape::class);
$currentShapeTranslated = $this->getDoctrine()->getRepository(Shape::class)->find($shapeObject->getId());
$this->localeService->getAllTranslationsFromEntity($currentShapeTranslated);
$currentShapeTranslated = json_decode(json_encode($currentShapeTranslated));
$shape = $this->localeService->getEntityTranslated(Shape::class, $shapeObject->getId(), $request->getLocale());
return $this->render('pages/product/base.html.twig', [
'params_extra' => ['shapeSlug' => $currentShapeTranslated->slug],
'title' => ['shape' => ucfirst($shape->getName())],
'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB, [['name' => $shape->getName()]]),
'filter' => 'shapes',
'filterValue' => $shapeSlug,
'pictures' => $this->getPicturesToShow($projects, $this->filePathAndUrlService, $this->liip),
'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(10)
], null, $request);
}
/**
* @Route({
* "ja": "/woven-vinyl-floors/custome-made-shapes",
* "en": "/woven-vinyl-floors/custome-made-shapes"
* }, name="pages_products_custom_made_shapes")
* @param Request $request
* @return Response
*/
public function customeMadeShapes(Request $request): Response
{
$projects = $this->getDoctrine()->getRepository(Project::class)->findAll();
return $this->render('pages/product/base.html.twig', [
'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB, [['name' => 'Custom made shapes']]),
'filter' => 'shapes',
'filterValue' => 'diamond',
'pictures' => $this->getPicturesToShow($projects, $this->filePathAndUrlService, $this->liip),
'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(11)
], null, $request);
}
/**
* @Route({
* "ja": "/woven-vinyl-floors/product/{productSlug}",
* "en": "/woven-vinyl-floors/product/{productSlug}"
* }, name="pages_products_filter_by_products")
* @param Request $request
* @param $productSlug
* @return Response
*/
public function product(Request $request,$productSlug): Response
{
$product = $this->getDoctrine()->getRepository(Product::class)->findOneBy(['slug' => $productSlug]);
return $this->render('pages/product/base.html.twig', [
'title' => ['productName' => ucfirst($product->getName())],
'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB, [['name' => $product->getName()]]),
'filter' => 'product',
'filterValue' => $productSlug,
'pictures' => $this->getPicturesToShow([$product], $this->filePathAndUrlService, $this->liip),
], null, $request);
}
/**
* @Route({
* "ja": "/woven-vinyl-floors/color/{colorSlug}",
* "en": "/woven-vinyl-floors/color/{colorSlug}"
* }, name="pages_products_filter_by_colors")
* @param Request $request
* @param $colorSlug
* @return Response
*/
public function color(Request $request, $colorSlug): Response
{
$products = $this->apiController->getProductsFilterByColor($request->getLocale(), $colorSlug, true);
$projects = $this->getProjectsFromEntities($products, $this->getDoctrine()->getManager());
$repository = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
$colorObject = $repository->findObjectByTranslatedField('slug', $colorSlug, Color::class);
$currentColorTranslated = $this->getDoctrine()->getRepository(Color::class)->find($colorObject->getId());
$this->localeService->getAllTranslationsFromEntity($currentColorTranslated);
$currentColorTranslated = json_decode(json_encode($currentColorTranslated));
$color = $this->localeService->getEntityTranslated(Color::class, $colorObject->getId(), $request->getLocale());
return $this->render('pages/product/base.html.twig', [
'params_extra' => ['colorSlug' => $currentColorTranslated->slug],
'title' => ['color' => ucfirst($color->getName())],
'color' => $color,
'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB, [['name' => ucfirst($colorSlug)]]),
'filter' => 'color',
'filterValue' => $colorSlug,
'pictures' => $this->getPicturesToShow($projects, $this->filePathAndUrlService, $this->liip),
'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(12)
], null, $request);
}
/** @Route({
* "ja": "/woven-vinyl-floors/{collectionSlug}/{slug}-{id}",
* "en": "/woven-vinyl-floors/{collectionSlug}/{slug}-{id}"
* }, name="pages_product_details", requirements={"slug": "[a-zA-Z0-9\-_\/]+", "collectionSlug": "[a-zA-Z0-9\-_\/]+", "id": "[0-9]+"}, priority=2)
* @param string $collectionSlug
* @param string $slug
* @param Product $product
* @param Request $request
* @return Response
*/
public function show(string $collectionSlug, string $slug, Product $product, Request $request): Response
{
if ($product->getSlug() !== $slug) {
return $this->redirectToRoute('pages_product_details', [
'collectionSlug' => $product->getCollection()->getSlug(),
'slug' => $product->getSlug(),
'id' => $product->getId()
], 301);
}
if ($product->getCollection()->getSlug() !== $collectionSlug) {
return $this->redirectToRoute('pages_product_details', [
'collectionSlug' => $product->getCollection()->getSlug(),
'slug' => $product->getSlug(),
'id' => $product->getId()
], 301);
}
$projects = $this->getProjectsFromEntities([$product], $this->getDoctrine()->getManager());
foreach ($projects as $project) {
$project->urlProject = $this->generateUrl('pages_project_details', [
'kindProjectSlug' => $project->getKindProject()->getSlug(),
'slug' => $project->getSlug(),
'id' => $project->getId()
]);
}
$collection = $this->getDoctrine()->getRepository(Collection::class)->findOneBy(['slug' => $collectionSlug]);
$firstLineProjects = array_slice($projects, 0, ceil(count($projects) / 3));
$secondLineProjects = array_slice($projects, ceil(count($projects) / 3), ceil(count($projects) / 3));
$thirdLineProjects = array_slice($projects,2 * ceil(count($projects) / 3),count($projects));
$this->setAttachmentUrls($product, $this->filePathAndUrlService);
$this->setAttachmentUrls($projects, $this->filePathAndUrlService);
$urlBreadCrumb = $this->generateUrl('pages_products_filter_by_collection', [
'collectionSlug' => $collectionSlug,
]);
return $this->render('pages/product/base.html.twig', [
'title' => ['productName' => ucfirst($product->getName())],
'product' => $product,
'downloads' => $this->documentFileUploader->getGenericPDF([$product], $request),
'firstLineProjects' => $firstLineProjects,
'secondLineProjects' => $secondLineProjects,
'thirdLineProjects' => $thirdLineProjects,
'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB,
[['url' => $urlBreadCrumb, 'name' => $collection->getName()]],
[['name' => $product->getName()]]
),
'filter' => 'product',
'filterValue' => $product->getSlug(),
'pictures' => $projects ? $this->getPicturesToShow($projects, $this->filePathAndUrlService, $this->liip) : $this->getPicturesToShow([$product], $this->filePathAndUrlService, $this->liip),
// 'threeDSimulationUrl' => $request->getSchemeAndHttpHost() . $this->generateUrl('pages_3d_simulation_index') . '?lang=' . $request->getLocale() . '&material=' . $product->getSlug(),
'threeDSimulationUrl' => "https://www.2tec2.com/3d-simulation" . '?lang=' . $request->getLocale() . '&material=' . $product->getSlug(),
'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(8)
], null, $request);
}
/**
* @Route({
* "ja": "/woven-vinyl-floors/collection/{collectionSlug}",
* "en": "/woven-vinyl-floors/collection/{collectionSlug}"
* }, name="pages_products_filter_by_collection", requirements={"slug": "[a-zA-Z1-9\-_\/]+", "collectionSlug": "[a-zA-Z1-9\-_\/]+"})
* @param Request $request
* @param $collectionSlug
* @return Response
*/
public function filterProductByCollection(Request $request, $collectionSlug): Response
{
$collection = $this->getDoctrine()->getRepository(Collection::class)->findOneBy(['slug' => $collectionSlug]);
if(!$collection) {
throw $this->createNotFoundException('The collection does not exist');
}
$products = $collection->getProducts()->getValues();
foreach ($products as &$product) {
$product->urlProduct = $this->generateUrl('pages_product_details', [
'collectionSlug' => $product->getCollection()->getSlug(),
'slug' => $product->getSlug(),
'id' => $product->getId()
]);
}
$firstLineProducts = array_slice($products, 0, ceil(count($products) / 3));
$secondLineProducts = array_slice($products, ceil(count($products) / 3), ceil(count($products) / 3));
$thirdLineProducts = array_slice($products,2 * ceil(count($products) / 3),count($products));
$projects = $this->getProjectsFromEntities($products, $this->getDoctrine()->getManager());
return $this->render('pages/product/base.html.twig', [
'title' => ['collectionName' => ucfirst($collection->getName())],
'collection' => $this->getDoctrine()->getRepository(Collection::class)->findOneBy(['slug' => $collectionSlug]),
'firstLineProducts' => $firstLineProducts,
'secondLineProducts' => $secondLineProducts,
'thirdLineProducts' => $thirdLineProducts,
'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB,
[['name' => $collection->getName()]]
),
'filter' => 'collection',
'filterValue' => $collectionSlug,
'pictures' => $this->getPicturesToShow($projects, $this->filePathAndUrlService, $this->liip),
'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(13)
], null, $request);
}
}