src/Controller/Pages/ProductController.php line 274

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Pages;
  3. use App\Controller\API\ApiController;
  4. use App\Controller\Utils\ContentController;
  5. use App\Entity\Collection;
  6. use App\Entity\Color;
  7. use App\Entity\Product;
  8. use App\Entity\Project;
  9. use App\Entity\SeoPage;
  10. use App\Entity\Shape;
  11. use App\Service\DocumentFileUploader;
  12. use App\Service\FilePathAndUrlService;
  13. use App\Service\LocaleService;
  14. use App\Traits\AttachmentUrls;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Liip\ImagineBundle\Service\FilterService;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. class ProductController extends ContentController
  22. {
  23.     use AttachmentUrls;
  24.     private $em;
  25.     private $localeService;
  26.     private $filePathAndUrlService;
  27.     protected $documentFileUploader;
  28.     public const PRODUCT_BASE_BREADCRUMB = [
  29.         HomeController::HOME_BREADCRUMB,
  30.         ['route' => 'pages_products_overview''name' => 'Products overview']
  31.     ];
  32.     public $apiController;
  33.     /**
  34.      * @var FilterService
  35.      */
  36.     private $liip;
  37.     public function __construct(EventDispatcherInterface $dispatcherLocaleService $localeServiceApiController $apiController,
  38.                                 FilePathAndUrlService $filePathAndUrlService,DocumentFileUploader $documentFileUploader,
  39.                                 FilterService $liipEntityManagerInterface $entityManager)
  40.     {
  41.         $this->localeService $localeService;
  42.         $this->filePathAndUrlService $filePathAndUrlService;
  43.         $this->documentFileUploader $documentFileUploader;
  44.         $this->apiController $apiController;
  45.         parent::__construct($dispatcherself::class);
  46.         $this->liip $liip;
  47.         $this->em $entityManager;
  48.     }
  49.     /**
  50.      * @Route({
  51.      *     "ja": "/woven-vinyl-floors",
  52.      *     "en": "/woven-vinyl-floors"
  53.      * }, name="pages_products_overview")
  54.      * @param Request $request
  55.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  56.      */
  57.     public function index (Request $request)
  58.     {
  59.         if ($request->get('product')) {
  60.             $product $this->getDoctrine()->getRepository(Product::class)->findOneBy(['slug' => $request->get('product')]);
  61.             if ($product) {
  62.                 return $this->redirectToRoute('pages_product_details', [
  63.                     'collectionSlug' => $product->getCollection()->getSlug(),
  64.                     'slug' => $product->getSlug(),
  65.                     'id' => $product->getId()
  66.                 ], 301);
  67.             }
  68.         }
  69.         $projects $this->getDoctrine()->getRepository(Project::class)->findAll();
  70.         return $this->render('pages/product/base.html.twig', [
  71.             'breadCrumbs' => self::PRODUCT_BASE_BREADCRUMB,
  72.             'pictures' => $this->getPicturesToShow($projects$this->filePathAndUrlService$this->liip)
  73.         ], null$request);
  74.     }
  75.     /**
  76.      * @Route({
  77.      *     "ja": "/woven-vinyl-floors/shape/{shapeSlug}",
  78.      *     "en": "/woven-vinyl-floors/shape/{shapeSlug}"
  79.      * }, name="pages_products_filter_by_shapes")
  80.      * @param Request $request
  81.      * @param $shapeSlug
  82.      * @return Response
  83.      */
  84.     public function shapes(Request $request$shapeSlug)
  85.     {
  86.         $products $this->apiController->getProductsFilterByShape($request->getLocale(), $shapeSlugtrue);
  87.         $projects $this->getProjectsFromEntities($products$this->getDoctrine()->getManager());
  88.         $repository $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
  89.         $shapeObject $repository->findObjectByTranslatedField('slug'$shapeSlugShape::class);
  90.         $currentShapeTranslated $this->getDoctrine()->getRepository(Shape::class)->find($shapeObject->getId());
  91.         $this->localeService->getAllTranslationsFromEntity($currentShapeTranslated);
  92.         $currentShapeTranslated  json_decode(json_encode($currentShapeTranslated));
  93.         $shape $this->localeService->getEntityTranslated(Shape::class, $shapeObject->getId(), $request->getLocale());
  94.         return $this->render('pages/product/base.html.twig', [
  95.             'params_extra' => ['shapeSlug' => $currentShapeTranslated->slug],
  96.             'title' => ['shape' => ucfirst($shape->getName())],
  97.             'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB, [['name' => $shape->getName()]]),
  98.             'filter' => 'shapes',
  99.             'filterValue' => $shapeSlug,
  100.             'pictures' => $this->getPicturesToShow($projects$this->filePathAndUrlService$this->liip),
  101.             'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(10)
  102.         ], null$request);
  103.     }
  104.     /**
  105.      * @Route({
  106.      *     "ja": "/woven-vinyl-floors/custome-made-shapes",
  107.      *     "en": "/woven-vinyl-floors/custome-made-shapes"
  108.      * }, name="pages_products_custom_made_shapes")
  109.      * @param Request $request
  110.      * @return Response
  111.      */
  112.     public function customeMadeShapes(Request $request): Response
  113.     {
  114.         $projects $this->getDoctrine()->getRepository(Project::class)->findAll();
  115.         return $this->render('pages/product/base.html.twig', [
  116.             'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB, [['name' => 'Custom made shapes']]),
  117.             'filter' => 'shapes',
  118.             'filterValue' => 'diamond',
  119.             'pictures' => $this->getPicturesToShow($projects$this->filePathAndUrlService$this->liip),
  120.             'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(11)
  121.         ], null$request);
  122.     }
  123.     /**
  124.      * @Route({
  125.      *     "ja": "/woven-vinyl-floors/product/{productSlug}",
  126.      *     "en": "/woven-vinyl-floors/product/{productSlug}"
  127.      * }, name="pages_products_filter_by_products")
  128.      * @param Request $request
  129.      * @param $productSlug
  130.      * @return Response
  131.      */
  132.     public function product(Request $request,$productSlug): Response
  133.     {
  134.         $product $this->getDoctrine()->getRepository(Product::class)->findOneBy(['slug' => $productSlug]);
  135.         return $this->render('pages/product/base.html.twig', [
  136.             'title' => ['productName' => ucfirst($product->getName())],
  137.             'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB, [['name' => $product->getName()]]),
  138.             'filter' => 'product',
  139.             'filterValue' => $productSlug,
  140.             'pictures' => $this->getPicturesToShow([$product], $this->filePathAndUrlService$this->liip),
  141.         ], null$request);
  142.     }
  143.     /**
  144.      * @Route({
  145.      *     "ja": "/woven-vinyl-floors/color/{colorSlug}",
  146.      *     "en": "/woven-vinyl-floors/color/{colorSlug}"
  147.      * }, name="pages_products_filter_by_colors")
  148.      * @param Request $request
  149.      * @param $colorSlug
  150.      * @return Response
  151.      */
  152.     public function color(Request $request$colorSlug): Response
  153.     {
  154.         $products $this->apiController->getProductsFilterByColor($request->getLocale(), $colorSlugtrue);
  155.         $projects $this->getProjectsFromEntities($products$this->getDoctrine()->getManager());
  156.         $repository $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
  157.         $colorObject $repository->findObjectByTranslatedField('slug'$colorSlugColor::class);
  158.         $currentColorTranslated $this->getDoctrine()->getRepository(Color::class)->find($colorObject->getId());
  159.         $this->localeService->getAllTranslationsFromEntity($currentColorTranslated);
  160.         $currentColorTranslated  json_decode(json_encode($currentColorTranslated));
  161.         $color $this->localeService->getEntityTranslated(Color::class, $colorObject->getId(), $request->getLocale());
  162.         return $this->render('pages/product/base.html.twig', [
  163.             'params_extra' => ['colorSlug' => $currentColorTranslated->slug],
  164.             'title' => ['color' => ucfirst($color->getName())],
  165.             'color' => $color,
  166.             'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB, [['name' => ucfirst($colorSlug)]]),
  167.             'filter' => 'color',
  168.             'filterValue' => $colorSlug,
  169.             'pictures' => $this->getPicturesToShow($projects$this->filePathAndUrlService$this->liip),
  170.             'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(12)
  171.         ], null$request);
  172.     }
  173.     /** @Route({
  174.      *     "ja": "/woven-vinyl-floors/{collectionSlug}/{slug}-{id}",
  175.      *     "en": "/woven-vinyl-floors/{collectionSlug}/{slug}-{id}"
  176.      * }, name="pages_product_details", requirements={"slug": "[a-zA-Z0-9\-_\/]+", "collectionSlug": "[a-zA-Z0-9\-_\/]+", "id": "[0-9]+"}, priority=2)
  177.      * @param string $collectionSlug
  178.      * @param string $slug
  179.      * @param Product $product
  180.      * @param Request $request
  181.      * @return Response
  182.      */
  183.     public function show(string $collectionSlugstring $slugProduct $productRequest $request): Response
  184.     {
  185.         if ($product->getSlug() !== $slug) {
  186.             return $this->redirectToRoute('pages_product_details', [
  187.                 'collectionSlug' => $product->getCollection()->getSlug(),
  188.                 'slug' => $product->getSlug(),
  189.                 'id' => $product->getId()
  190.             ], 301);
  191.         }
  192.         if ($product->getCollection()->getSlug() !== $collectionSlug) {
  193.             return $this->redirectToRoute('pages_product_details', [
  194.                 'collectionSlug' => $product->getCollection()->getSlug(),
  195.                 'slug' => $product->getSlug(),
  196.                 'id' => $product->getId()
  197.             ], 301);
  198.         }
  199.         $projects $this->getProjectsFromEntities([$product], $this->getDoctrine()->getManager());
  200.         foreach ($projects as $project) {
  201.             $project->urlProject $this->generateUrl('pages_project_details', [
  202.                 'kindProjectSlug' => $project->getKindProject()->getSlug(),
  203.                 'slug' => $project->getSlug(),
  204.                 'id' => $project->getId()
  205.             ]);
  206.         }
  207.         $collection $this->getDoctrine()->getRepository(Collection::class)->findOneBy(['slug' => $collectionSlug]);
  208.         $firstLineProjects array_slice($projects0ceil(count($projects) / 3));
  209.         $secondLineProjects array_slice($projectsceil(count($projects) / 3), ceil(count($projects) / 3));
  210.         $thirdLineProjects array_slice($projects,ceil(count($projects) / 3),count($projects));
  211.         $this->setAttachmentUrls($product$this->filePathAndUrlService);
  212.         $this->setAttachmentUrls($projects$this->filePathAndUrlService);
  213.         $urlBreadCrumb $this->generateUrl('pages_products_filter_by_collection', [
  214.             'collectionSlug' => $collectionSlug,
  215.         ]);
  216.         return $this->render('pages/product/base.html.twig', [
  217.             'title' => ['productName' => ucfirst($product->getName())],
  218.             'product' => $product,
  219.             'downloads' => $this->documentFileUploader->getGenericPDF([$product], $request),
  220.             'firstLineProjects' => $firstLineProjects,
  221.             'secondLineProjects' => $secondLineProjects,
  222.             'thirdLineProjects' => $thirdLineProjects,
  223.             'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB,
  224.                 [['url' => $urlBreadCrumb'name' => $collection->getName()]],
  225.                 [['name' => $product->getName()]]
  226.             ),
  227.             'filter' => 'product',
  228.             'filterValue' => $product->getSlug(),
  229.             'pictures' => $projects $this->getPicturesToShow($projects$this->filePathAndUrlService$this->liip) : $this->getPicturesToShow([$product], $this->filePathAndUrlService$this->liip),
  230.             // 'threeDSimulationUrl' => $request->getSchemeAndHttpHost() . $this->generateUrl('pages_3d_simulation_index') . '?lang=' . $request->getLocale() . '&material=' . $product->getSlug(),
  231.             'threeDSimulationUrl' => "https://www.2tec2.com/3d-simulation" '?lang=' $request->getLocale() . '&material=' $product->getSlug(),
  232.             'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(8)
  233.         ], null$request);
  234.     }
  235.     /**
  236.      * @Route({
  237.      *     "ja": "/woven-vinyl-floors/collection/{collectionSlug}",
  238.      *     "en": "/woven-vinyl-floors/collection/{collectionSlug}"
  239.      * }, name="pages_products_filter_by_collection", requirements={"slug": "[a-zA-Z1-9\-_\/]+", "collectionSlug": "[a-zA-Z1-9\-_\/]+"})
  240.      * @param Request $request
  241.      * @param $collectionSlug
  242.      * @return Response
  243.      */
  244.     public function filterProductByCollection(Request $request$collectionSlug): Response
  245.     {
  246.         $collection $this->getDoctrine()->getRepository(Collection::class)->findOneBy(['slug' => $collectionSlug]);
  247.         if(!$collection) {
  248.             throw $this->createNotFoundException('The collection does not exist');
  249.         }
  250.         
  251.         $products $collection->getProducts()->getValues();
  252.         foreach ($products as &$product) {
  253.             $product->urlProduct $this->generateUrl('pages_product_details', [
  254.                 'collectionSlug' => $product->getCollection()->getSlug(),
  255.                 'slug' => $product->getSlug(),
  256.                 'id' => $product->getId()
  257.             ]);
  258.         }
  259.         $firstLineProducts array_slice($products0ceil(count($products) / 3));
  260.         $secondLineProducts array_slice($productsceil(count($products) / 3), ceil(count($products) / 3));
  261.         $thirdLineProducts array_slice($products,ceil(count($products) / 3),count($products));
  262.         $projects $this->getProjectsFromEntities($products$this->getDoctrine()->getManager());
  263.         return $this->render('pages/product/base.html.twig', [
  264.             'title' => ['collectionName' => ucfirst($collection->getName())],
  265.             'collection' => $this->getDoctrine()->getRepository(Collection::class)->findOneBy(['slug' => $collectionSlug]),
  266.             'firstLineProducts' => $firstLineProducts,
  267.             'secondLineProducts' => $secondLineProducts,
  268.             'thirdLineProducts' => $thirdLineProducts,
  269.             'breadCrumbs' => array_merge(self::PRODUCT_BASE_BREADCRUMB,
  270.                 [['name' => $collection->getName()]]
  271.             ),
  272.             'filter' => 'collection',
  273.             'filterValue' => $collectionSlug,
  274.             'pictures' => $this->getPicturesToShow($projects$this->filePathAndUrlService$this->liip),
  275.             'seoPage' => $this->getDoctrine()->getRepository(SeoPage::class)->find(13)
  276.         ], null$request);
  277.     }
  278. }