<?php
namespace App\Controller;
use App\Entity\Insight;
use App\Services\InsightService;
use App\Services\NewslettersService;
use App\Services\SonataClassificationService;
use App\Utils\JsonResponseHelper;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
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 InsightController extends BaseController
{
/** @var InsightService $insightService */
protected $insightService;
/** @var NewslettersService $newslettersService */
private $newslettersService;
/** @var SonataClassificationService $sonataClassificationService */
private $sonataClassificationService;
/**
* InsightController constructor.
*
* @param InsightService $insightService
* @param NewslettersService $newslettersService
* @param Security $security
* @param SonataClassificationService $sonataClassificationService
*/
public function __construct(
InsightService $insightService,
NewslettersService $newslettersService,
Security $security,
SonataClassificationService $sonataClassificationService
)
{
parent::__construct($security);
$this->insightService = $insightService;
$this->newslettersService = $newslettersService;
$this->sonataClassificationService = $sonataClassificationService;
}
/**
* HomePage Insights
* @Route("/insights", name="insight_home")
* @Route("/insights/", name="insight_home_slash", options={"expose" = true})
*
* @param SessionInterface $session
* @param Request $request
* @return RedirectResponse|Response
*/
public function homeAction(SessionInterface $session, Request $request)
{
$categoryParam = ($request->query->has('category')) ? $request->query->get('category') : null;
$tagParam = ($request->query->has('tag')) ? $request->query->get('tag') : null;
$tags = $this->insightService->getTagsInInsight();
$categories = $this->insightService->getCategoriesInInsight();
$category = $this->sonataClassificationService->getCategoryBySlug($categoryParam);
$tag = $this->sonataClassificationService->getTagBySlug($tagParam);
if ($tags != null && $tag != null && !in_array($tag, $tags)) {
$request->query->remove('tag');
$RequestUri = $request->getRequestUri();
$url = explode("?", $RequestUri)[0];
return $this->redirect($url, 301);
}
$featuredInsights = $this->insightService->getLastFeaturedInsights();
if (empty($featuredInsights) || count($featuredInsights ?? []) < 3) {
$notFeaturedInsights = $this->insightService->getLastInsightsNotFeatured((3 - count($featuredInsights ?? [])));
$featuredInsights = array_merge($featuredInsights, $notFeaturedInsights);
}
$adminUrl = null;
if ($this->security->isGranted("ROLE_ADMIN_INSIGHT_ADMINISTRATOR")) {
$adminUrl = $this->generateUrl('admin_app_insight_list');
}
return $this->render('insight/list.html.twig', [
"featuredInsights" => $featuredInsights,
"categoryParam" => $category,
"tagParam" => $tag,
BaseController::PARAM_ADMIN_URL => $adminUrl,
"tags" => $tags,
"categories" => $categories,
"tagSelect" => $tag
]);
}
/**
* Ajax Insights List
* @Route("/insights/list", name="insights_list", options={"expose" = true})
* @param SessionInterface $session
* @param Request $request
* @return RedirectResponse|Response
*/
public function insightsListAction(SessionInterface $session, Request $request)
{
$categories = (!empty($request->get('categories'))) ? $request->get('categories') : null;
$tags = (!empty($request->get('tags'))) ? $request->get('tags') : null;
$offset = (!empty($request->get('offset'))) ? $request->get('offset') : 0;
$limit = (!empty($request->get('limit'))) ? $request->get('limit') : 50;
$insights = $this->insightService->getInsightByFilter($categories, $tags, $offset, $limit);
$countAll = $this->insightService->getFilteredInsightsCount($categories, $tags);
$insightsHtml = $this->renderView(
'insight/templates/insight-list.html.twig', [
'insights' => $insights
]
);
return $this->json(
array(
'html' => $insightsHtml,
'count' => $countAll
)
);
}
/**
* Insight Details from id
*
* @param $insightId
* @return RedirectResponse|Response
* @Route("/insights-from-id/{insightId}", name="insight_detail_from_id", options={"expose"=true})
*/
public function showDetailPageFromIdAction($insightId)
{
/**
* @var Insight $insight
*/
$insight = $this->insightService->getInsightsFromId($insightId);
if ($insight != null) {
return $this->redirectToRoute('insight_detail',
['categoryName' => $insight->getCategory()->getSlug(), 'insightName' => $insight->getUrl()]);
}
return $this->redirectToRoute("insight_home");
}
/**
* Insight Details
*
* @param SessionInterface $session
* @param string $categoryName
* @param string $insightName
* @return RedirectResponse|Response
* @Route("/insights/{categoryName}/{insightName}", name="insight_detail", options={"expose"=true})
*/
public function showDetailPageAction(SessionInterface $session, string $categoryName, string $insightName)
{
$includeInactive = $this->includeInactive($session);
$insight = $this->insightService->getInsightsFromSlug($insightName);
if ($insight == null) {
$this->addFlash(
'notice', 'Insights not found'
);
return $this->redirectToRoute("insight_home");
}
$relatedProducts = $this->insightService->getRelatedProducts($insight, $includeInactive);
if ($insight->getCategory()->getSlug() != $categoryName) {
return $this->redirectToRoute('insight_detail',
['categoryName' => $insight->getCategory()->getSlug(), 'insightName' => $insightName]);
}
$relatedInsights = $this->insightService->getRelatedInsights($insight, $includeInactive, 4);
$adminUrl = null;
if ($this->security->isGranted("ROLE_ADMIN_INSIGHT_ADMINISTRATOR")) {
$adminUrl = $this->generateUrl('admin_app_insight_edit', ['id' => $insight->getId()]);
}
return $this->render('insight/details.html.twig', [
BaseController::PARAM_ADMIN_URL => $adminUrl,
'insight' => $insight,
'relatedInsights' => $relatedInsights,
'relatedProducts' => $relatedProducts
]);
}
/**
* @Route("/intern/{category}/logged-user/addOrRemove/{addOrRemove}", name="register_login_user_category", options={"expose"=true}, methods={"GET"})
* @param string $category
* @param string $addOrRemove
* @return JsonResponse
*/
public function addLoggedUserCategory(string $category, string $addOrRemove)
{
try {
$addUser = $addOrRemove === "true" ? true : false;
$this->newslettersService->modifyLoggedInUserRegistrationCategory($category, $addUser);
return JsonResponseHelper::buildSuccessJson($addUser ? "Subscription Added" : "Subscription Removed");
} catch (Exception $e) {
return JsonResponseHelper::buildErrorJson("Error to add subscription : " . $e->getMessage());
}
}
/**
* @Route("/intern/{category}/user-email/add/{email}", name="register_email_user_category", options={"expose"=true}, methods={"GET"})
* @param string $email
* @return JsonResponse
*/
public function addEmailUserCategory(string $category, string $email)
{
try {
$this->newslettersService->modifyEmailUserRegistrationCategory($category, $email, true);
return JsonResponseHelper::buildSuccessJson("Subscription Added");
} catch (Exception $e) {
return JsonResponseHelper::buildErrorJson("Error to add subscription : " . $e->getMessage());
}
}
/**
* @Route("/intern/insights/logged-user/issubbed/", name="logged_user_issubbed_insights", options={"expose"=true}, methods={"GET"})
* @return JsonResponse
*/
public function isUserSubbedToInsights()
{
try {
$isSubbed = $this->newslettersService->isLoggedInUserRegisteredToInsights($this->getUser()->getUid());
return JsonResponseHelper::buildSuccessJson("Subscription to insights checked", 'isSubbed', $isSubbed);
} catch (Exception $e) {
return JsonResponseHelper::buildErrorJson("Error to check subscription to insights : " . $e->getMessage());
}
}
/**
* @Route("/insights/latest", name="insight_latest", options={"expose" = true })
*/
public function getLatestInsightsAction()
{
$insights = $this->insightService->getLatestInsights();
return $this->render(
'dashboard/insights_view.html.twig', [
'insights' => $insights
]);
}
}