<?php
namespace App\Controller;
use App\Entity\Product;
use App\Services\InsightService;
use App\Services\ProductService;
use App\Entity\SonataClassificationCategory;
use App\Entity\SonataClassificationTag;
use Doctrine\ORM\ORMException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
class ProductController extends BaseController
{
/** @var ProductService $productService */
protected $productService;
/** @var InsightService $insightService */
protected $insightService;
/**
* InsightController constructor.
*
* @param ProductService $productService
* @param InsightService $insightService
* @param Security $security
*/
public function __construct(ProductService $productService, InsightService $insightService, Security $security)
{
parent::__construct($security);
$this->productService = $productService;
$this->insightService = $insightService;
}
/**
* HomePage Products
* @Route("/products/services", name="product_homepage")
* @Route("/products", name="products")
* @Route("/products/", name="products_slash")
* @Route("/products/services/", name="product_homepage_slash")
*/
public function indexAction(SessionInterface $session, Request $request)
{
if ($request != null && $request->get('removeProfile') != null) {
$session->remove('productsAndServices');
}
$includeActive = $this->includeInactive($session);
$categories = $this->getDoctrine()->getRepository(SonataClassificationCategory::class)->findBy(['context' => 'products'], ['position' => 'ASC']);
$tags = $this->getDoctrine()->getRepository(SonataClassificationTag::class)->findBy(['context' => 'user_profiles']);
/** @noinspection PhpUndefinedMethodInspection */
$current_profile = $session->get('productsAndServices') ? $this->getDoctrine()->getRepository(SonataClassificationTag::class)
->findOneBySlug($session->get('productsAndServices')) : null;
/** @noinspection PhpUndefinedMethodInspection */
$current_tag = $request->get('filter') ? $this->getDoctrine()->getRepository(SonataClassificationTag::class)
->findOneBySlug($request->get('filter')) : $current_profile;
$products = $this->productService->getProducts($includeActive);
/**
* We retrieve products declined
*
* @var SonataClassificationTag $current_tag
*/
if ($current_tag !== null) {
$products = $this->productService->getProductsDeclined($products, $current_tag);
$session->set('productsAndServices', $current_tag->getSlug());
}
$adminUrl = null;
if ($this->security->isGranted("ROLE_ADMIN_PRODUCT_ADMINISTRATOR")) {
/** @noinspection PhpRouteMissingInspection */
$adminUrl = $this->generateUrl('admin_app_product_list');
}
return $this->render('product/homepage.html.twig', [
'categories' => $categories,
'current_tag' => $current_tag,
'products' => $products,
'tags' => $tags,
BaseController::PARAM_ADMIN_URL => $adminUrl
]);
}
/**
* Display Product By Category
* @Route("/products/services/content-production", name="product_by_category_content_production")
* @Route("/products/services/content-production/", name="product_by_category_content_production_slash")
*/
public function displayByCategoryContentProductionAction(Request $request)
{
return $this->displayByCategoryAction($request->getSession(), $request, 'content-production');
}
/**
* Display Product By Category
* @Route("/products/services/{category_slug}", name="product_by_category")
* @Route("/products/services/{category_slug}/", name="product_by_category_slash")
*
* @param SessionInterface $session
* @param Request $request
* @param string $category_slug
* @return RedirectResponse|Response
* @throws ORMException
*/
public function displayByCategoryAction(SessionInterface $session, Request $request, string $category_slug)
{
if ($category_slug == null) {
return $this->redirectToRoute('product_homepage');
}
$includeActive = $this->includeInactive($session);
$categories = $this->getDoctrine()->getRepository(SonataClassificationCategory::class)->findBy(['context' => 'products'], ['position' => 'ASC']);
$current_category = $this->getDoctrine()->getRepository(SonataClassificationCategory::class)
->findOneBy(['slug' => $category_slug]);
$tags = $this->getDoctrine()->getRepository(SonataClassificationTag::class)->findBy(['context' => 'user_profiles']);
/** @noinspection PhpUndefinedMethodInspection */
$current_profile = $session->get('productsAndServices') ? $this->getDoctrine()->getRepository(SonataClassificationTag::class)
->findOneBySlug($session->get('productsAndServices')) : null;
/** @noinspection PhpUndefinedMethodInspection */
$current_tag = $request->get('filter') ? $this->getDoctrine()->getRepository(SonataClassificationTag::class)
->findOneBySlug($request->get('filter')) : $current_profile;
$products = $this->productService->getProductsByCategory($current_category, $includeActive);
/**
* @var SonataClassificationTag $current_tag
* We retrieve products declined
*/
if ($current_tag !== null) {
$products = $this->productService->getProductsDeclined($products, $current_tag);
$session->set('productsAndServices', $current_tag->getSlug());
}
$adminUrl = null;
if ($this->security->isGranted("ROLE_ADMIN_PRODUCT_ADMINISTRATOR")) {
/** @noinspection PhpRouteMissingInspection */
$adminUrl = $this->generateUrl('admin_app_product_list');
}
return $this->render('product/homepage.html.twig', [
'categories' => $categories,
'products' => $products,
'current_category' => $current_category,
'tags' => $tags,
'current_tag' => $current_tag
]);
}
/**
* Display Product By Category
* @Route("/products/services/broadcast-services", name="product_by_category_broadcast_services")
* @Route("/products/services/broadcast-services/", name="product_by_category_broadcast_services_slash")
*/
public function displayByCategoryBroadcastServicesAction(Request $request)
{
return $this->displayByCategoryAction($request->getSession(), $request, 'broadcast-services');
}
/**
* Display Product By Category
* @Route("/products/services/content-distribution", name="product_by_category_content_distribution")
* @Route("/products/services/content-distribution/", name="product_by_category_content_distribution_slash")
*/
public function displayByCategoryContenDistributionAction(Request $request)
{
return $this->displayByCategoryAction($request->getSession(), $request, 'content-distribution');
}
/**
* Display Product Details
* @Route("/product-from-id/{productId}", name="product_detail_from_id")
*
* @param string $productId
* @return RedirectResponse
*/
public function showDetailFromIdAction(string $productId)
{
$product = $this->productService->findById($productId);
if ($product != null) {
return $this->redirectToRoute('product_detail',
['category_slug' => $product->getCategory()->getSlug(), 'product_slug' => $product->getSlug()]);
}
return $this->redirectToRoute("product_homepage");
}
/**
* Display Product Details
* @Route("/products/services/{category_slug}/{product_slug}", name="product_detail")
*
* @param SessionInterface $session
* @param Request $request
* @param string $category_slug
* @param string $product_slug
* @return RedirectResponse|Response
* @throws ORMException
*/
public function showDetailAction(SessionInterface $session, Request $request, string $category_slug, string $product_slug)
{
if ($request != null && $request->get('removeProfile') != null) {
$session->remove('productsAndServices');
}
/**
* @var Product $product
*/
$product = $this->productService->findBySlug($product_slug);
if ($product == null) {
return $this->redirectToRoute('product_homepage');
}
if ($product->getCategory() != null && $product->getCategory()->getSlug() != $category_slug) {
return $this->redirectToRoute("product_detail",
["category_slug" => $product->getCategory()->getSlug(), "product_slug" => $product_slug]);
}
$tagRepository = $this->getDoctrine()->getRepository(SonataClassificationTag::class);
$tags = $tagRepository->findBy(['context' => 'user_profiles']);
$current_tag = null;
/**
* @var SonataClassificationTag $current_tag
*/
if ($request != null && $request->get('filter') != null) {
/** @noinspection PhpUndefinedMethodInspection */
$current_tag = $tagRepository
->findOneBySlug($request->get('filter'));
/** We retrieve products declined */
$product = $this->productService->getProductDeclinedWithKeyBenefit($product, $current_tag);
if ($current_tag != null) {
$session->set('productsAndServices', $current_tag->getSlug());
}
} else {
/** @noinspection PhpUndefinedMethodInspection */
$current_tag = $session->get('productsAndServices') ? $tagRepository
->findOneBySlug($session->get('productsAndServices'))
: $tagRepository->findOneBySlug([
'context' => 'user_profiles',
'name' => 'all'
]);
$product = $this->productService->getProductDeclinedWithKeyBenefit($product, $current_tag);
if ($current_tag != null) {
$session->set('productsAndServices', $current_tag->getSlug());
}
}
$relatedInsights = $this->insightService->getProductRelatedInsights($product, $this->includeInactive($session), 3);
$adminUrl = null;
if ($this->security->isGranted("ROLE_ADMIN_PRODUCT_ADMINISTRATOR")) {
/** @noinspection PhpRouteMissingInspection */
$adminUrl = $this->generateUrl('admin_app_product_edit', ['id' => $product->getId()]);
}
return $this->render('product/detail.html.twig',
[
'product' => $product,
'current_tag' => $current_tag,
'relatedInsights' => $relatedInsights,
'tags' => $tags,
BaseController::PARAM_ADMIN_URL => $adminUrl
]);
}
/**
* @Route("/intern/services", name="services_list", options={"expose" = true })
*/
public function getServicesAction()
{
// Exclude product with id 26 = Content Hub
$services = $this->productService->getProductsWithExclusion([26]);
return $this->render(
'product/services.html.twig', [
'services' => $services
]);
}
/**
* Content hub service
* @Route("/products/production-hub", name="product_production_hub")
* @Route("/products/production-hub/", name="product_production_hub_slash")
*/
public function productionHubAction()
{
return $this->render('product/production-hub.html.twig');
}
/**
* Content hub service
* @Route("/products/content-hub", name="product_content_hub")
* @Route("/products/content-hub/", name="product_content_hub_slash")
*/
public function contentHubAction()
{
return $this->render('product/content-hub.html.twig');
}
/**
* Contribution service
* @Route("/products/contribution", name="product_contribution")
* @Route("/products/contribution/", name="product_contribution_slash")
*/
public function contributionAction()
{
return $this->render('product/contribution.html.twig');
}
/**
* Distribution service
* @Route("/products/distribution", name="product_distribution")
* @Route("/products/distribution/", name="product_distribution_slash")
*/
public function distributionAction()
{
return $this->render('product/distribution.html.twig');
}
/**
* Major events
* @Route("/products/major-events", name="product_major_events")
* @Route("/products/major-events/", name="product_major_events_slash")
*/
public function majorEventsAction()
{
return $this->render('product/major-events.html.twig');
}
/**
* Dedicated services
* @Route("/products/dedicated-services", name="product_dedicated_services")
* @Route("/products/dedicated-services/", name="product_dedicated_services_slash")
*/
public function dedicatedServicesAction()
{
return $this->render('product/dedicated-services.html.twig');
}
/**
* Aggregation services
* @Route("/products/aggregation-services", name="product_aggregation_services")
* @Route("/products/aggregation-services/", name="product_aggregation_services_slash")
*/
public function aggregationServicesAction()
{
return $this->render('product/aggregation-services.html.twig');
}
/**
* Permanent services
* @Route("/products/permanent-services", name="product_permanent_services")
* @Route("/products/permanent-services/", name="product_permanent_services_slash")
*/
public function permanentServicesAction()
{
return $this->render('product/permanent-services.html.twig');
}
}