<?php
namespace AppBundle\Controller;
use AppBundle\Model\Podcast as PodcastModel;
use AppBundle\Service\ArticleProcessService;
use AppBundle\Service\BroadcastProcessService;
use AppBundle\Service\PodcastProcessService;
use AppBundle\Service\PollProcessService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Model\Article;
use AppBundle\Model\Broadcast;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class PageController extends BaseController
{
/**
* @var Article
*/
private $articleModel;
/** @var PollProcessService */
private $pollProcessService;
/** @var ArticleProcessService */
private $articleProcessService;
/** @var BroadcastProcessService */
private $broadcastProcessService;
/** @var PodcastModel */
private $podcastModel;
/** @var PodcastProcessService */
private $podcastProcessService;
/**
* @var Broadcast
*/
private $broadcastModel;
private $categoryModel;
private $galleryModel;
private $menuModel;
private $newsModel;
public function showPageAction($mainCategory, $subCategory, $id, $slug)
{
$this->articleModel = $this->get('eemce.appbundle.article');
$viewData = $this->getMainPageData();
$article = $this->articleModel->getArticle($id);
if ($article == null) {
throw new NotFoundHttpException();
}
$redirectUri = $article->getRedirectUri();
if (!empty($redirectUri) && false !== filter_var($redirectUri, FILTER_VALIDATE_URL)) {
return $this->redirect($redirectUri, Response::HTTP_MOVED_PERMANENTLY);
}
$categoryId = $this->articleModel->getArticleCategoryId($id);
$viewData['category'] = $this->articleModel->getCategoryDetails($categoryId);
$viewData['gemius_code'] = $this->getArticleGemiusCode($id);
$viewData['custom_js'] = $this->articleModel->getArticleCustomJs($id);
$now = new \DateTime();
$viewData['strossle_excluded'] = true;
if ($article->getStatus() == true
&& $article->getDateStart() <= $now
&& ($article->getDateEnd() >= $now || $article->getDateEnd() == null)
&& !$this->articleModel->hasCategoryBySlug($id, 'sutaze')) {
$viewData['strossle_excluded'] = false;
}
// VIP user
// $user = $this->getUser();
// if ($user) {
// $em = $this->getDoctrine()->getManager();
// $userBoards = $em->getRepository('AppBundle:Board')->findBy(['user' => $user], ['id' => 'asc']);
// $profileData = [];
// foreach ($userBoards as $b) {
// $profileData['userBoards'][] = [
// 'id' => (string)$b->getId(),
// 'title' => $b->getTitle()
// ];
// if ($b->getArticles()->contains($viewData['article'])) {
// $profileData['addedToBoards'][] = (string)$b->getId();
// }
// }
// $viewData['profileData'] = json_encode($profileData);
// }
$viewData['article'] = $article;
$this->articleProcessService = $this->get('eemce.appbundle.article_process_service');
$relatedArticles = $this->articleModel->getLatestArticles(6, (int)$id);
$this->articleProcessService->processArticles($relatedArticles);
$viewData['relatedArticles'] = $relatedArticles;
// Prepare data for vue
$mainArticleData[] = [
'id' => $article->getId(),
'title' => $article->getTitle(),
'slug' => $article->getSlug(),
'excerpt' => $article->getExcerpt(),
'dateStart' => $article->getDateStart(),
'thumbnail' => $article->getThumbnail(),
'thumbnailTitle' => $article->getThumbnailTitle(),
'thumbnailDescription' => $article->getThumbnailDescription(),
'uploadPath' => $article->getUploadPath(),
'category' => $viewData['category'] ? $viewData['category']->getTitle() : null,
'categorySlug' => $viewData['category'] ? $viewData['category']->getSlug() : null,
];
$this->articleProcessService->processArticles($mainArticleData);
$viewData['mainArticleData'] = $mainArticleData[0];
$response = $this->render('article.html.twig', $viewData);
return $this->setResponseCacheHeadersFromConfig($response, true, 'cache_ttl_seconds_article');
}
/**
* Get Gemius code (HTML snippet) for provided article ID
*
* Gemius code preference (waterwall):
*
* 1. category 'contests'
* 2. any other category with non-empty gemius code
* 3. any broadcast with non-empty gemius code
* 4. site-wide fallback
*
* @param int $articleId
*
* @return string
*/
private function getArticleGemiusCode($articleId)
{
$categories = $this->get('eemce.appbundle.article')->getArticleCategories($articleId);
$broadcasts = $this->get('eemce.appbundle.article')->getArticleBroadcasts($articleId);
foreach ($categories as $category) {
if ($category['slug'] == 'zabava') {
// 1. category 'contests'
return $category['gemiusCode'];
}
if (empty($category['gemiusCode'])) {
continue;
}
// 2. any other category with non-empty gemius code
return $category['gemiusCode'];
}
foreach($broadcasts as $broadcast) {
if (empty($broadcast->getGemiusCode())) {
continue;
}
// 3. any broadcast with non-empty gemius code
return $broadcast->getGemiusCode();
}
// 4. site-wide fallback
return $this->getOptionValue('gemius_code_other');
}
public function showLatestNewsAction($page = 0, $category = null)
{
$this->articleModel = $this->get('eemce.appbundle.article');
$this->newsModel = $this->get('eemce.appbundle.news');
$viewData = $this->getMainPageData();
$offset = 0;
if ($page != 0) {
$offset = ($page - 1) * 24;
}
if ($category != null) {
$this->categoryModel = $this->get('eemce.appbundle.category');
$category = $this->categoryModel->getCategoryBySlug($category);
$category = $category->getId();
}
$viewData['latest_news'] = $this->newsModel->getLatestNews(
24,
$offset,
$category
);
$viewData['archive']['title'] = 'Zo sveta golfu';
$viewData['archive']['url'] = 'kratke-spravy';
$viewData['pagination'] = $this->newsModel->getNewsPagination(24, $category);
$response = $this->render('latest_news.html.twig', $viewData);
$response = $this->setResponseCacheHeaders($response);
return $response;
}
public function showStaticPageAction($id, $slug)
{
return $this->showPageAction(null, null, $id, $slug);
}
/**
* @return Response
*/
public function showBroadcastsAction(): Response
{
$this->broadcastModel = $this->get('eemce.appbundle.broadcast');
$this->articleModel = $this->get('eemce.appbundle.article');
$viewData = $this->getMainPageData();
// TODO: In adminV2 in 'air now' module add a new option to create a program schedule table like this
// For now, rows are written manually, but they will later be loaded from a separate table
$projectDir = $this->get('kernel')->getProjectDir();
$programData = require $projectDir . '/app/Resources/views/broadcast/air_now_data.php';
$viewData['airNow'] = $programData;
$broadcasts = $this->broadcastModel->getActiveBroadcasts(true, [
'slug', 'title', 'description', 'authorBio', 'uploadPath'
]);
$this->broadcastProcessService = $this->get('eemce.appbundle.broadcast_process_service');
$this->broadcastProcessService->processBroadcasts($broadcasts);
$viewData['broadcasts'] = $broadcasts;
$response = $this->render('broadcast/index.html.twig', $viewData);
return $this->setResponseCacheHeadersFromConfig($response, false, 'cache_ttl_seconds_article_list');
}
/**
* @param int $id
* @return Response
*/
public function showBroadcastAction(int $id): Response
{
$this->broadcastModel = $this->get('eemce.appbundle.broadcast');
$this->articleModel = $this->get('eemce.appbundle.article');
$viewData = $this->getMainPageData();
// Main broadcast data
$broadcast = $this->broadcastModel->getBroadcast($id, true, [
'slug', 'title', 'description', 'authorBio', 'uploadPath', 'gemiusCode'
]);
$broadcasts = [$broadcast];
$this->broadcastProcessService = $this->get('eemce.appbundle.broadcast_process_service');
$this->broadcastProcessService->processBroadcasts($broadcasts);
$viewData['broadcast'] = $broadcasts[0] ?? null;
if ($viewData['broadcast'] == null) {
throw new NotFoundHttpException();
}
// Category articles
$viewData['categories'] = $this->broadcastModel->getBroadcastCategories($id);
$this->articleProcessService = $this->get('eemce.appbundle.article_process_service');
foreach ($viewData['categories'] as $i => $category) {
$categoryArticles = $this->articleModel->getCategoryFull($category['slug']);
$viewData['categories'][$i]['articles'] = array_slice($categoryArticles, 0, 6);
$viewData['categories'][$i]['otherArticles'] = array_slice($categoryArticles, 6);
$this->articleProcessService->processArticles($viewData['categories'][$i]['otherArticles']);
}
// home icon category articles
$defaultArticles = $this->articleModel->getBroadcastFull($id);
$viewData['defaultArticles'] = array_slice($defaultArticles, 0, 6);
$viewData['defaultOtherArticles'] = array_slice($defaultArticles, 6);
$this->articleProcessService->processArticles($viewData['defaultOtherArticles']);
$viewData['filterMenu'] = [];
$categoriesTitles = array_column($viewData['categories'], 'title');
if (!empty($categoriesTitles)) {
$viewData['filterMenu'] = array_merge(['home'], $categoriesTitles);
}
$viewData['gemius_code'] = $viewData['broadcast']['gemiusCode'];
$response = $this->render('broadcast/show.html.twig', $viewData);
return $this->setResponseCacheHeadersFromConfig($response, false, 'cache_ttl_seconds_article_list');
}
public function showPhotoPageAction($id, $slug, $galleryId, $gallerySlug, $photoId)
{
$this->galleryModel = $this->get('eemce.appbundle.gallery');
$viewData = $this->getMainPageData();
$viewData['photo'] = $this->galleryModel->getPhoto($photoId);
if ($viewData['photo'] == null) {
throw new NotFoundHttpException();
}
$photoSort = $viewData['photo']->getSort();
$viewData['previous'] = $this->galleryModel->getPreviousPhoto($galleryId, $photoId, $photoSort);
$viewData['next'] = $this->galleryModel->getNextPhoto($galleryId, $photoId, $photoSort);
$this->articleModel = $this->get('eemce.appbundle.article');
$viewData['articleId'] = $id;
$viewData['articleSlug'] = $slug;
$viewData['gallery'] = $this->galleryModel->getGallery($galleryId);
$viewData['photos'] = $this->galleryModel->getGalleryPhotos($galleryId);
$viewData['indexPhoto'] = array_search($viewData['photo'], $viewData['photos']);
$response = $this->render('photo.html.twig', $viewData);
$response = $this->setResponseCacheHeadersFromConfig($response, false, 'cache_ttl_seconds_article_list');
return $response;
}
public function showCategoryAction($category, $page = 0)
{
$this->categoryModel = $this->get('eemce.appbundle.category');
$viewData = $this->getMainPageData();
$viewData['category'] = $this->categoryModel->getCategoryBySlug($category);
if ($viewData['category'] == null) {
throw new NotFoundHttpException();
}
$this->articleModel = $this->get('eemce.appbundle.article');
$maxResults = $category === 'sutaze' ? 20 : 12;
$articles = $this->articleModel->getCategoryFull(
$viewData['category']->getSlug(),
$maxResults
);
if (empty($articles)) {
throw new NotFoundHttpException();
}
if ($category === 'sutaze') {
$this->pollProcessService = $this->get('eemce.appbundle.poll_process_service');
$view = 'sutaze.html.twig';
$viewData['polls'] = $this->articleModel->getActivePollsWithCategories();
$viewData['endedPolls'] = $this->articleModel->getInactivePollsWithCategories();
if (!empty($viewData['endedPolls'])) {
$this->pollProcessService->processPolls($viewData['endedPolls']);
}
} else {
$this->articleProcessService = $this->get('eemce.appbundle.article_process_service');
$viewData['articles'] = array_slice($articles, 0, 6);
$viewData['otherArticles'] = array_slice($articles, 6);
$this->articleProcessService->processArticles($viewData['otherArticles']);
$view = 'category.html.twig';
}
if (empty($viewData['articles'])) {
$viewData['articles'] = $articles;
}
// indicate last modification date and handle conditional GET request when made
$request = Request::createFromGlobals();
$response = new Response();
$response = $this->setResponseCacheHeadersFromConfig($response, false, 'cache_ttl_seconds_article_list');
$lastModified = $this->getLatestDateFromArticleArray($viewData['articles']);
$response->setLastModified($lastModified);
if ($this->isNotModified($request, $lastModified)) {
$response->setStatusCode(Response::HTTP_NOT_MODIFIED);
return $response;
}
// DB calls delegated after is304 check
$viewData['gemius_code'] = $viewData['category']->getGemiusCode();
$response = $this->render($view, $viewData, $response);
return $response;
}
public function showFolderAction($folder)
{
$this->menuModel = $this->get('eemce.appbundle.menu');
$viewData = $this->getMainPageData();
$viewData['folder'] = $this->menuModel->getFolder($folder);
if ($viewData['folder'] == null) {
throw new NotFoundHttpException();
}
$children = $this->menuModel->getChildrenForFolder($viewData['folder']->getId());
if (empty($children)) {
throw new NotFoundHttpException();
}
$this->articleModel = $this->get('eemce.appbundle.article');
$articles = $this->articleModel->getArticlesForChildren($children);
if (empty($articles)) {
throw new NotFoundHttpException();
}
// indicate last modification date and handle conditional GET request when made
$request = Request::createFromGlobals();
$response = new Response();
$response = $this->setResponseCacheHeadersFromConfig($response, false, 'cache_ttl_seconds_article_list');
$lastModified = $this->getLatestDateFromArticleArray($articles);
$response->setLastModified($lastModified);
if ($this->isNotModified($request, $lastModified)) {
$response->setStatusCode(Response::HTTP_NOT_MODIFIED);
return $response;
}
$viewData['articles'] = array_slice($articles, 0, 6);
$viewData['otherArticles'] = array_slice($articles, 6);
$this->articleProcessService = $this->get('eemce.appbundle.article_process_service');
$this->articleProcessService->processArticles($viewData['otherArticles']);
return $this->render('folder.html.twig', $viewData, $response);
}
public function playerPopupAction(Request $request)
{
$viewData = [];
$activeStreams = $this->get('eemce.appbundle.sepia_stream')->getActiveStreams();
$selectedStream = $request->query->get('stream');
$viewData['title'] = '';
$viewData['seoKeywords'] = '';
$viewData['seoDescription'] = '';
foreach ($activeStreams as $stream) {
if ($selectedStream != null && $stream->getSlug() == $selectedStream) {
$viewData['title'] = $stream->getTitle();
$viewData['seoKeywords'] = $stream->getSeoKeywords();
$viewData['seoDescription'] = $stream->getSeoDescription();
}
}
if (empty($viewData['title'])) {
$viewData['title'] = $activeStreams[0]->getTitle();
$viewData['seoKeywords'] = $activeStreams[0]->getSeoKeywords();
$viewData['seoDescription'] = $activeStreams[0]->getSeoDescription();
}
$this->podcastModel = $this->get('eemce.appbundle.podcast');
$this->podcastProcessService = $this->get('eemce.appbundle.podcast_process_service');
$viewData['episodes'] = $this->podcastModel->getLatestEpisodes(10);
if (!empty($viewData['episodes'])) {
$this->podcastProcessService->processLatestEpisodes($viewData['episodes']);
}
$response = $this->render('player.html.twig', $viewData);
$response = $this->setResponseCacheHeadersFromConfig($response, false, 'cache_ttl_seconds_other');
return $response;
}
}