src/Controller/InsightController.php line 240

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Insight;
  4. use App\Services\InsightService;
  5. use App\Services\NewslettersService;
  6. use App\Services\SonataClassificationService;
  7. use App\Utils\JsonResponseHelper;
  8. use Exception;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\Security;
  16. class InsightController extends BaseController
  17. {
  18.     /** @var InsightService $insightService */
  19.     protected $insightService;
  20.     /** @var  NewslettersService $newslettersService */
  21.     private $newslettersService;
  22.     /** @var  SonataClassificationService $sonataClassificationService */
  23.     private $sonataClassificationService;
  24.     /**
  25.      * InsightController constructor.
  26.      *
  27.      * @param InsightService $insightService
  28.      * @param NewslettersService $newslettersService
  29.      * @param Security $security
  30.      * @param SonataClassificationService $sonataClassificationService
  31.      */
  32.     public function __construct(
  33.         InsightService $insightService,
  34.         NewslettersService $newslettersService,
  35.         Security $security,
  36.         SonataClassificationService $sonataClassificationService
  37.     )
  38.     {
  39.         parent::__construct($security);
  40.         $this->insightService $insightService;
  41.         $this->newslettersService $newslettersService;
  42.         $this->sonataClassificationService $sonataClassificationService;
  43.     }
  44.     /**
  45.      * HomePage Insights
  46.      * @Route("/insights", name="insight_home")
  47.      * @Route("/insights/", name="insight_home_slash", options={"expose" = true})
  48.      *
  49.      * @param SessionInterface $session
  50.      * @param Request $request
  51.      * @return RedirectResponse|Response
  52.      */
  53.     public function homeAction(SessionInterface $sessionRequest $request)
  54.     {
  55.         $categoryParam = ($request->query->has('category')) ? $request->query->get('category') : null;
  56.         $tagParam = ($request->query->has('tag')) ? $request->query->get('tag') : null;
  57.         $tags $this->insightService->getTagsInInsight();
  58.         $categories $this->insightService->getCategoriesInInsight();
  59.         $category $this->sonataClassificationService->getCategoryBySlug($categoryParam);
  60.         $tag $this->sonataClassificationService->getTagBySlug($tagParam);
  61.         if ($tags != null && $tag != null && !in_array($tag$tags)) {
  62.             $request->query->remove('tag');
  63.             $RequestUri $request->getRequestUri();
  64.             $url explode("?"$RequestUri)[0];
  65.             return $this->redirect($url301);
  66.         }
  67.         $featuredInsights $this->insightService->getLastFeaturedInsights();
  68.         if (empty($featuredInsights) || count($featuredInsights ?? []) < 3) {
  69.             $notFeaturedInsights $this->insightService->getLastInsightsNotFeatured((count($featuredInsights ?? [])));
  70.             $featuredInsights array_merge($featuredInsights$notFeaturedInsights);
  71.         }
  72.         $adminUrl null;
  73.         if ($this->security->isGranted("ROLE_ADMIN_INSIGHT_ADMINISTRATOR")) {
  74.             $adminUrl $this->generateUrl('admin_app_insight_list');
  75.         }
  76.         return $this->render('insight/list.html.twig', [
  77.             "featuredInsights" => $featuredInsights,
  78.             "categoryParam" => $category,
  79.             "tagParam" => $tag,
  80.             BaseController::PARAM_ADMIN_URL => $adminUrl,
  81.             "tags" => $tags,
  82.             "categories" => $categories,
  83.             "tagSelect" => $tag
  84.         ]);
  85.     }
  86.     /**
  87.      * Ajax Insights List
  88.      * @Route("/insights/list", name="insights_list", options={"expose" = true})
  89.      * @param SessionInterface $session
  90.      * @param Request $request
  91.      * @return RedirectResponse|Response
  92.      */
  93.     public function insightsListAction(SessionInterface $sessionRequest $request)
  94.     {
  95.         $categories = (!empty($request->get('categories'))) ? $request->get('categories') : null;
  96.         $tags = (!empty($request->get('tags'))) ? $request->get('tags') : null;
  97.         $offset = (!empty($request->get('offset'))) ? $request->get('offset') : 0;
  98.         $limit = (!empty($request->get('limit'))) ? $request->get('limit') : 50;
  99.         $insights $this->insightService->getInsightByFilter($categories$tags$offset$limit);
  100.         $countAll $this->insightService->getFilteredInsightsCount($categories$tags);
  101.         $insightsHtml $this->renderView(
  102.             'insight/templates/insight-list.html.twig', [
  103.                 'insights' => $insights
  104.             ]
  105.         );
  106.         return $this->json(
  107.             array(
  108.                 'html' => $insightsHtml,
  109.                 'count' => $countAll
  110.             )
  111.         );
  112.     }
  113.     /**
  114.      * Insight Details from id
  115.      *
  116.      * @param $insightId
  117.      * @return RedirectResponse|Response
  118.      * @Route("/insights-from-id/{insightId}", name="insight_detail_from_id", options={"expose"=true})
  119.      */
  120.     public function showDetailPageFromIdAction($insightId)
  121.     {
  122.         /**
  123.          * @var Insight $insight
  124.          */
  125.         $insight $this->insightService->getInsightsFromId($insightId);
  126.         if ($insight != null) {
  127.             return $this->redirectToRoute('insight_detail',
  128.                 ['categoryName' => $insight->getCategory()->getSlug(), 'insightName' => $insight->getUrl()]);
  129.         }
  130.         return $this->redirectToRoute("insight_home");
  131.     }
  132.     /**
  133.      * Insight Details
  134.      *
  135.      * @param SessionInterface $session
  136.      * @param string $categoryName
  137.      * @param string $insightName
  138.      * @return RedirectResponse|Response
  139.      * @Route("/insights/{categoryName}/{insightName}", name="insight_detail", options={"expose"=true})
  140.      */
  141.     public function showDetailPageAction(SessionInterface $sessionstring $categoryNamestring $insightName)
  142.     {
  143.         $includeInactive $this->includeInactive($session);
  144.         $insight $this->insightService->getInsightsFromSlug($insightName);
  145.         if ($insight == null) {
  146.             $this->addFlash(
  147.                 'notice''Insights not found'
  148.             );
  149.             return $this->redirectToRoute("insight_home");
  150.         }
  151.         $relatedProducts $this->insightService->getRelatedProducts($insight$includeInactive);
  152.         if ($insight->getCategory()->getSlug() != $categoryName) {
  153.             return $this->redirectToRoute('insight_detail',
  154.                 ['categoryName' => $insight->getCategory()->getSlug(), 'insightName' => $insightName]);
  155.         }
  156.         $relatedInsights $this->insightService->getRelatedInsights($insight$includeInactive4);
  157.         $adminUrl null;
  158.         if ($this->security->isGranted("ROLE_ADMIN_INSIGHT_ADMINISTRATOR")) {
  159.             $adminUrl $this->generateUrl('admin_app_insight_edit', ['id' => $insight->getId()]);
  160.         }
  161.         return $this->render('insight/details.html.twig', [
  162.             BaseController::PARAM_ADMIN_URL => $adminUrl,
  163.             'insight' => $insight,
  164.             'relatedInsights' => $relatedInsights,
  165.             'relatedProducts' => $relatedProducts
  166.         ]);
  167.     }
  168.     /**
  169.      * @Route("/intern/{category}/logged-user/addOrRemove/{addOrRemove}", name="register_login_user_category", options={"expose"=true}, methods={"GET"})
  170.      * @param string $category
  171.      * @param string $addOrRemove
  172.      * @return JsonResponse
  173.      */
  174.     public function addLoggedUserCategory(string $categorystring $addOrRemove)
  175.     {
  176.         try {
  177.             $addUser $addOrRemove === "true" true false;
  178.             $this->newslettersService->modifyLoggedInUserRegistrationCategory($category$addUser);
  179.             return JsonResponseHelper::buildSuccessJson($addUser "Subscription Added" "Subscription Removed");
  180.         } catch (Exception $e) {
  181.             return JsonResponseHelper::buildErrorJson("Error to add subscription : " $e->getMessage());
  182.         }
  183.     }
  184.     /**
  185.      * @Route("/intern/{category}/user-email/add/{email}", name="register_email_user_category", options={"expose"=true}, methods={"GET"})
  186.      * @param string $email
  187.      * @return JsonResponse
  188.      */
  189.     public function addEmailUserCategory(string $categorystring $email)
  190.     {
  191.         try {
  192.             $this->newslettersService->modifyEmailUserRegistrationCategory($category$emailtrue);
  193.             return JsonResponseHelper::buildSuccessJson("Subscription Added");
  194.         } catch (Exception $e) {
  195.             return JsonResponseHelper::buildErrorJson("Error to add subscription : " $e->getMessage());
  196.         }
  197.     }
  198.     /**
  199.      * @Route("/intern/insights/logged-user/issubbed/", name="logged_user_issubbed_insights", options={"expose"=true}, methods={"GET"})
  200.      * @return JsonResponse
  201.      */
  202.     public function isUserSubbedToInsights()
  203.     {
  204.         try {
  205.             $isSubbed $this->newslettersService->isLoggedInUserRegisteredToInsights($this->getUser()->getUid());
  206.             return JsonResponseHelper::buildSuccessJson("Subscription to insights checked"'isSubbed'$isSubbed);
  207.         } catch (Exception $e) {
  208.             return JsonResponseHelper::buildErrorJson("Error to check subscription to insights : " $e->getMessage());
  209.         }
  210.     }
  211.     /**
  212.      * @Route("/insights/latest", name="insight_latest", options={"expose" = true })
  213.      */
  214.     public function getLatestInsightsAction()
  215.     {
  216.         $insights $this->insightService->getLatestInsights();
  217.         return $this->render(
  218.             'dashboard/insights_view.html.twig', [
  219.             'insights' => $insights
  220.         ]);
  221.     }
  222. }