src/AppBundle/Controller/PageController.php line 249

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use AppBundle\Model\Podcast as PodcastModel;
  4. use AppBundle\Service\ArticleProcessService;
  5. use AppBundle\Service\BroadcastProcessService;
  6. use AppBundle\Service\PodcastProcessService;
  7. use AppBundle\Service\PollProcessService;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use AppBundle\Model\Article;
  11. use AppBundle\Model\Broadcast;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. class PageController extends BaseController
  14. {
  15.     /**
  16.      * @var Article
  17.      */
  18.     private $articleModel;
  19.     /** @var PollProcessService */
  20.     private $pollProcessService;
  21.     /** @var ArticleProcessService */
  22.     private $articleProcessService;
  23.     /** @var BroadcastProcessService */
  24.     private $broadcastProcessService;
  25.     /** @var PodcastModel */
  26.     private $podcastModel;
  27.     /** @var PodcastProcessService */
  28.     private $podcastProcessService;
  29.     /**
  30.      * @var Broadcast
  31.      */
  32.     private $broadcastModel;
  33.     private $categoryModel;
  34.     private $galleryModel;
  35.     private $menuModel;
  36.     private $newsModel;
  37.     public function showPageAction($mainCategory$subCategory$id$slug)
  38.     {
  39.         $this->articleModel $this->get('eemce.appbundle.article');
  40.         $viewData $this->getMainPageData();
  41.         $article $this->articleModel->getArticle($id);
  42.         if ($article == null) {
  43.             throw new NotFoundHttpException();
  44.         }
  45.         $redirectUri $article->getRedirectUri();
  46.         if (!empty($redirectUri) && false !== filter_var($redirectUriFILTER_VALIDATE_URL)) {
  47.             return $this->redirect($redirectUriResponse::HTTP_MOVED_PERMANENTLY);
  48.         }
  49.         $categoryId $this->articleModel->getArticleCategoryId($id);
  50.         $viewData['category'] = $this->articleModel->getCategoryDetails($categoryId);
  51.         $viewData['gemius_code'] = $this->getArticleGemiusCode($id);
  52.         $viewData['custom_js'] = $this->articleModel->getArticleCustomJs($id);
  53.         $now = new \DateTime();
  54.         $viewData['strossle_excluded'] = true;
  55.         if ($article->getStatus() == true
  56.             && $article->getDateStart() <= $now
  57.             && ($article->getDateEnd() >= $now || $article->getDateEnd() == null)
  58.             && !$this->articleModel->hasCategoryBySlug($id'sutaze')) {
  59.             $viewData['strossle_excluded'] = false;
  60.         }
  61.         // VIP user
  62. //        $user = $this->getUser();
  63. //        if ($user) {
  64. //            $em = $this->getDoctrine()->getManager();
  65. //            $userBoards = $em->getRepository('AppBundle:Board')->findBy(['user' => $user], ['id' => 'asc']);
  66. //            $profileData = [];
  67. //            foreach ($userBoards as $b) {
  68. //                $profileData['userBoards'][] = [
  69. //                    'id' =>  (string)$b->getId(),
  70. //                    'title' => $b->getTitle()
  71. //                ];
  72. //                if ($b->getArticles()->contains($viewData['article'])) {
  73. //                    $profileData['addedToBoards'][] = (string)$b->getId();
  74. //                }
  75. //            }
  76. //            $viewData['profileData'] = json_encode($profileData);
  77. //        }
  78.         $viewData['article'] = $article;
  79.         $this->articleProcessService $this->get('eemce.appbundle.article_process_service');
  80.         $relatedArticles $this->articleModel->getLatestArticles(6, (int)$id);
  81.         $this->articleProcessService->processArticles($relatedArticles);
  82.         $viewData['relatedArticles'] = $relatedArticles;
  83.         // Prepare data for vue
  84.         $mainArticleData[] = [
  85.             'id' => $article->getId(),
  86.             'title' => $article->getTitle(),
  87.             'slug' => $article->getSlug(),
  88.             'excerpt' => $article->getExcerpt(),
  89.             'dateStart' => $article->getDateStart(),
  90.             'thumbnail' => $article->getThumbnail(),
  91.             'thumbnailTitle' => $article->getThumbnailTitle(),
  92.             'thumbnailDescription' => $article->getThumbnailDescription(),
  93.             'uploadPath' => $article->getUploadPath(),
  94.             'category' => $viewData['category'] ? $viewData['category']->getTitle() : null,
  95.             'categorySlug' => $viewData['category'] ? $viewData['category']->getSlug() : null,
  96.         ];
  97.         $this->articleProcessService->processArticles($mainArticleData);
  98.         $viewData['mainArticleData'] = $mainArticleData[0];
  99.         $response $this->render('article.html.twig'$viewData);
  100.         return $this->setResponseCacheHeadersFromConfig($responsetrue'cache_ttl_seconds_article');
  101.     }
  102.     /**
  103.      * Get Gemius code (HTML snippet) for provided article ID
  104.      *
  105.      * Gemius code preference (waterwall):
  106.      *
  107.      * 1. category 'contests'
  108.      * 2. any other category with non-empty gemius code
  109.      * 3. any broadcast with non-empty gemius code
  110.      * 4. site-wide fallback
  111.      *
  112.      * @param int $articleId
  113.      *
  114.      * @return string
  115.      */
  116.     private function getArticleGemiusCode($articleId)
  117.     {
  118.         $categories $this->get('eemce.appbundle.article')->getArticleCategories($articleId);
  119.         $broadcasts $this->get('eemce.appbundle.article')->getArticleBroadcasts($articleId);
  120.         foreach ($categories as $category) {
  121.             if ($category['slug'] == 'zabava') {
  122.                 // 1. category 'contests'
  123.                 return $category['gemiusCode'];
  124.             }
  125.             if (empty($category['gemiusCode'])) {
  126.                 continue;
  127.             }
  128.             // 2. any other category with non-empty gemius code
  129.             return $category['gemiusCode'];
  130.         }
  131.         foreach($broadcasts as $broadcast) {
  132.             if (empty($broadcast->getGemiusCode())) {
  133.                 continue;
  134.             }
  135.             // 3. any broadcast with non-empty gemius code
  136.             return $broadcast->getGemiusCode();
  137.         }
  138.         // 4. site-wide fallback
  139.         return $this->getOptionValue('gemius_code_other');
  140.     }
  141.     public function showLatestNewsAction($page 0$category null)
  142.     {
  143.         $this->articleModel $this->get('eemce.appbundle.article');
  144.         $this->newsModel $this->get('eemce.appbundle.news');
  145.         $viewData $this->getMainPageData();
  146.         $offset 0;
  147.         if ($page != 0) {
  148.             $offset = ($page 1) * 24;
  149.         }
  150.         if ($category != null) {
  151.             $this->categoryModel $this->get('eemce.appbundle.category');
  152.             $category $this->categoryModel->getCategoryBySlug($category);
  153.             $category $category->getId();
  154.         }
  155.         $viewData['latest_news'] = $this->newsModel->getLatestNews(
  156.             24,
  157.             $offset,
  158.             $category
  159.         );
  160.         $viewData['archive']['title'] = 'Zo sveta golfu';
  161.         $viewData['archive']['url'] = 'kratke-spravy';
  162.         $viewData['pagination'] = $this->newsModel->getNewsPagination(24$category);
  163.         $response $this->render('latest_news.html.twig'$viewData);
  164.         $response $this->setResponseCacheHeaders($response);
  165.         return $response;
  166.     }
  167.     public function showStaticPageAction($id$slug)
  168.     {
  169.         return $this->showPageAction(nullnull$id$slug);
  170.     }
  171.     /**
  172.      * @return Response
  173.      */
  174.     public function showBroadcastsAction(): Response
  175.     {
  176.         $this->broadcastModel $this->get('eemce.appbundle.broadcast');
  177.         $this->articleModel $this->get('eemce.appbundle.article');
  178.         $viewData $this->getMainPageData();
  179.         // TODO: In adminV2 in 'air now' module add a new option to create a program schedule table like this
  180.         // For now, rows are written manually, but they will later be loaded from a separate table
  181.         $projectDir $this->get('kernel')->getProjectDir();
  182.         $programData = require $projectDir '/app/Resources/views/broadcast/air_now_data.php';
  183.         $viewData['airNow'] = $programData;
  184.         $broadcasts $this->broadcastModel->getActiveBroadcasts(true, [
  185.             'slug''title''description''authorBio''uploadPath'
  186.         ]);
  187.         $this->broadcastProcessService $this->get('eemce.appbundle.broadcast_process_service');
  188.         $this->broadcastProcessService->processBroadcasts($broadcasts);
  189.         $viewData['broadcasts'] = $broadcasts;
  190.         $response $this->render('broadcast/index.html.twig'$viewData);
  191.         return $this->setResponseCacheHeadersFromConfig($responsefalse'cache_ttl_seconds_article_list');
  192.     }
  193.     /**
  194.      * @param int $id
  195.      * @return Response
  196.      */
  197.     public function showBroadcastAction(int $id): Response
  198.     {
  199.         $this->broadcastModel $this->get('eemce.appbundle.broadcast');
  200.         $this->articleModel $this->get('eemce.appbundle.article');
  201.         $viewData $this->getMainPageData();
  202.         // Main broadcast data
  203.         $broadcast $this->broadcastModel->getBroadcast($idtrue, [
  204.             'slug''title''description''authorBio''uploadPath''gemiusCode'
  205.         ]);
  206.         $broadcasts = [$broadcast];
  207.         $this->broadcastProcessService $this->get('eemce.appbundle.broadcast_process_service');
  208.         $this->broadcastProcessService->processBroadcasts($broadcasts);
  209.         $viewData['broadcast'] = $broadcasts[0] ?? null;
  210.         if ($viewData['broadcast'] == null) {
  211.             throw new NotFoundHttpException();
  212.         }
  213.         // Category articles
  214.         $viewData['categories'] = $this->broadcastModel->getBroadcastCategories($id);
  215.         $this->articleProcessService $this->get('eemce.appbundle.article_process_service');
  216.         foreach ($viewData['categories'] as $i => $category) {
  217.             $categoryArticles $this->articleModel->getCategoryFull($category['slug']);
  218.             $viewData['categories'][$i]['articles'] = array_slice($categoryArticles06);
  219.             $viewData['categories'][$i]['otherArticles'] = array_slice($categoryArticles6);
  220.             $this->articleProcessService->processArticles($viewData['categories'][$i]['otherArticles']);
  221.         }
  222.         // home icon category articles
  223.         $defaultArticles $this->articleModel->getBroadcastFull($id);
  224.         $viewData['defaultArticles'] = array_slice($defaultArticles06);
  225.         $viewData['defaultOtherArticles'] = array_slice($defaultArticles6);
  226.         $this->articleProcessService->processArticles($viewData['defaultOtherArticles']);
  227.         $viewData['filterMenu'] = [];
  228.         $categoriesTitles array_column($viewData['categories'], 'title');
  229.         if (!empty($categoriesTitles)) {
  230.             $viewData['filterMenu'] = array_merge(['home'], $categoriesTitles);
  231.         }
  232.         $viewData['gemius_code'] = $viewData['broadcast']['gemiusCode'];
  233.         $response $this->render('broadcast/show.html.twig'$viewData);
  234.         return $this->setResponseCacheHeadersFromConfig($responsefalse'cache_ttl_seconds_article_list');
  235.     }
  236.     public function showPhotoPageAction($id$slug$galleryId$gallerySlug$photoId)
  237.     {
  238.         $this->galleryModel $this->get('eemce.appbundle.gallery');
  239.         $viewData $this->getMainPageData();
  240.         $viewData['photo'] = $this->galleryModel->getPhoto($photoId);
  241.         if ($viewData['photo'] == null) {
  242.             throw new NotFoundHttpException();
  243.         }
  244.         $photoSort $viewData['photo']->getSort();
  245.         $viewData['previous'] = $this->galleryModel->getPreviousPhoto($galleryId$photoId$photoSort);
  246.         $viewData['next'] = $this->galleryModel->getNextPhoto($galleryId$photoId$photoSort);
  247.         $this->articleModel $this->get('eemce.appbundle.article');
  248.         $viewData['articleId'] = $id;
  249.         $viewData['articleSlug'] = $slug;
  250.         $viewData['gallery'] = $this->galleryModel->getGallery($galleryId);
  251.         $viewData['photos'] = $this->galleryModel->getGalleryPhotos($galleryId);
  252.         $viewData['indexPhoto'] = array_search($viewData['photo'], $viewData['photos']);
  253.         $response $this->render('photo.html.twig'$viewData);
  254.         $response $this->setResponseCacheHeadersFromConfig($responsefalse'cache_ttl_seconds_article_list');
  255.         return $response;
  256.     }
  257.     public function showCategoryAction($category$page 0)
  258.     {
  259.         $this->categoryModel $this->get('eemce.appbundle.category');
  260.         $viewData $this->getMainPageData();
  261.         $viewData['category'] = $this->categoryModel->getCategoryBySlug($category);
  262.         if ($viewData['category'] == null) {
  263.             throw new NotFoundHttpException();
  264.         }
  265.         $this->articleModel $this->get('eemce.appbundle.article');
  266.         $maxResults $category === 'sutaze' 20 12;
  267.         $articles $this->articleModel->getCategoryFull(
  268.             $viewData['category']->getSlug(),
  269.             $maxResults
  270.         );
  271.         if (empty($articles)) {
  272.             throw new NotFoundHttpException();
  273.         }
  274.         if ($category === 'sutaze') {
  275.             $this->pollProcessService $this->get('eemce.appbundle.poll_process_service');
  276.             $view 'sutaze.html.twig';
  277.             $viewData['polls'] = $this->articleModel->getActivePollsWithCategories();
  278.             $viewData['endedPolls'] = $this->articleModel->getInactivePollsWithCategories();
  279.             if (!empty($viewData['endedPolls'])) {
  280.                 $this->pollProcessService->processPolls($viewData['endedPolls']);
  281.             }
  282.         } else {
  283.             $this->articleProcessService $this->get('eemce.appbundle.article_process_service');
  284.             $viewData['articles'] = array_slice($articles06);
  285.             $viewData['otherArticles'] = array_slice($articles6);
  286.             $this->articleProcessService->processArticles($viewData['otherArticles']);
  287.             $view 'category.html.twig';
  288.         }
  289.         if (empty($viewData['articles'])) {
  290.             $viewData['articles'] = $articles;
  291.         }
  292.         // indicate last modification date and handle conditional GET request when made
  293.         $request Request::createFromGlobals();
  294.         $response = new Response();
  295.         $response $this->setResponseCacheHeadersFromConfig($responsefalse'cache_ttl_seconds_article_list');
  296.         $lastModified $this->getLatestDateFromArticleArray($viewData['articles']);
  297.         $response->setLastModified($lastModified);
  298.         if ($this->isNotModified($request$lastModified)) {
  299.             $response->setStatusCode(Response::HTTP_NOT_MODIFIED);
  300.             return $response;
  301.         }
  302.         // DB calls delegated after is304 check
  303.         $viewData['gemius_code'] = $viewData['category']->getGemiusCode();
  304.         $response $this->render($view$viewData$response);
  305.         return $response;
  306.     }
  307.     public function showFolderAction($folder)
  308.     {
  309.         $this->menuModel $this->get('eemce.appbundle.menu');
  310.         $viewData $this->getMainPageData();
  311.         $viewData['folder'] = $this->menuModel->getFolder($folder);
  312.         if ($viewData['folder'] == null) {
  313.             throw new NotFoundHttpException();
  314.         }
  315.         $children $this->menuModel->getChildrenForFolder($viewData['folder']->getId());
  316.         if (empty($children)) {
  317.             throw new NotFoundHttpException();
  318.         }
  319.         $this->articleModel $this->get('eemce.appbundle.article');
  320.         $articles $this->articleModel->getArticlesForChildren($children);
  321.         if (empty($articles)) {
  322.             throw new NotFoundHttpException();
  323.         }
  324.         // indicate last modification date and handle conditional GET request when made
  325.         $request Request::createFromGlobals();
  326.         $response = new Response();
  327.         $response $this->setResponseCacheHeadersFromConfig($responsefalse'cache_ttl_seconds_article_list');
  328.         $lastModified $this->getLatestDateFromArticleArray($articles);
  329.         $response->setLastModified($lastModified);
  330.         if ($this->isNotModified($request$lastModified)) {
  331.             $response->setStatusCode(Response::HTTP_NOT_MODIFIED);
  332.             return $response;
  333.         }
  334.         $viewData['articles'] = array_slice($articles06);
  335.         $viewData['otherArticles'] = array_slice($articles6);
  336.         $this->articleProcessService $this->get('eemce.appbundle.article_process_service');
  337.         $this->articleProcessService->processArticles($viewData['otherArticles']);
  338.         return $this->render('folder.html.twig'$viewData$response);
  339.     }
  340.     public function playerPopupAction(Request $request)
  341.     {
  342.         $viewData = [];
  343.         $activeStreams $this->get('eemce.appbundle.sepia_stream')->getActiveStreams();
  344.         $selectedStream $request->query->get('stream');
  345.         $viewData['title'] = '';
  346.         $viewData['seoKeywords'] = '';
  347.         $viewData['seoDescription'] = '';
  348.         foreach ($activeStreams as $stream) {
  349.             if ($selectedStream != null && $stream->getSlug() == $selectedStream) {
  350.                 $viewData['title'] = $stream->getTitle();
  351.                 $viewData['seoKeywords'] = $stream->getSeoKeywords();
  352.                 $viewData['seoDescription'] = $stream->getSeoDescription();
  353.             }
  354.         }
  355.         if (empty($viewData['title'])) {
  356.             $viewData['title'] = $activeStreams[0]->getTitle();
  357.             $viewData['seoKeywords'] = $activeStreams[0]->getSeoKeywords();
  358.             $viewData['seoDescription'] = $activeStreams[0]->getSeoDescription();
  359.         }
  360.         $this->podcastModel $this->get('eemce.appbundle.podcast');
  361.         $this->podcastProcessService $this->get('eemce.appbundle.podcast_process_service');
  362.         $viewData['episodes'] = $this->podcastModel->getLatestEpisodes(10);
  363.         if (!empty($viewData['episodes'])) {
  364.             $this->podcastProcessService->processLatestEpisodes($viewData['episodes']);
  365.         }
  366.         $response $this->render('player.html.twig'$viewData);
  367.         $response $this->setResponseCacheHeadersFromConfig($responsefalse'cache_ttl_seconds_other');
  368.         return $response;
  369.     }
  370. }