<?php namespace SGalinski\SgNews\Controller; /*************************************************************** * Copyright notice * * (c) sgalinski Internet Services (https://www.sgalinski.de) * * All rights reserved * * This script is part of the TYPO3 project. The TYPO3 project is * free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * The GNU General Public License can be found at * http://www.gnu.org/copyleft/gpl.html. * * This script is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ use SGalinski\SgNews\Service\HeaderMetaDataService; use SGalinski\SgNews\Domain\Model\Category; use SGalinski\SgNews\Domain\Model\News; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Persistence\Generic\Query; /** * Controller that handles the overview page of categories and their news */ class OverviewController extends AbstractController { /** * @inject * @var \SGalinski\SgNews\Domain\Repository\CategoryRepository */ protected $categoryRepository; /** * @inject * @var \SGalinski\SgNews\Domain\Repository\NewsRepository */ protected $newsRepository; /** * Renders the news overview * * @return void */ public function overviewAction() { if ($this->settings['groupByCategories']) { $this->overviewWithCategories(); } else { $this->forward('overviewWithoutCategories'); } } /** * Highlights the best fitting news in the meta data of the page * * @param array $categoryIds */ protected function highlightBestFitNews(array $categoryIds) { /** @var News $highlightedNews */ $highlightedNews = $this->newsRepository ->findLastUpdatedOrHighlightedNewsByCategories(1, FALSE, $categoryIds)->getFirst(); /** @var Category $category */ $category = $this->categoryRepository->findByUid($highlightedNews->getPid()); $highlightedNewsMetaData = NULL; if ($category) { $highlightedNewsMetaData = $this->getMetaDataForNews($highlightedNews, $category); } if ($highlightedNewsMetaData['image']) { HeaderMetaDataService::addOgImageToHeader($highlightedNewsMetaData['image']); } elseif ($highlightedNewsMetaData['teaserImage']) { HeaderMetaDataService::addOgImageToHeader($highlightedNewsMetaData['teaserImage']); } } /** * Renders the news overview grouped by categories * * @return void */ protected function overviewWithCategories() { $newsLimitPerCategory = (int) $this->settings['newsLimit']; $this->categoryRepository->setDefaultOrderings(['sorting' => Query::ORDER_ASCENDING]); $offset = 0; $currentPageBrowserPage = (int) GeneralUtility::_GP('tx_sgnews_pagebrowser')['currentPage']; if ($currentPageBrowserPage && $newsLimitPerCategory) { $offset = $currentPageBrowserPage * $newsLimitPerCategory; } if ($this->settings['onlyNewsWithinThisPageSection']) { /** @noinspection PhpUndefinedMethodInspection */ $categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id); } else { $categories = $this->categoryRepository->findAll(); } $categoryIds = []; $categoriesById = []; $newsMetaData = []; foreach ($categories as $category) { /** @var $category Category */ $categoryId = $category->getUid(); $categoryIds[] = $category->getUid(); $categoriesById[$categoryId] = $category; $news = $this->newsRepository->findAllSortedNewsByCategories([$categoryId], $newsLimitPerCategory, $offset); foreach ($news as $newsEntry) { /** @var News $newsEntry */ $categoryId = $newsEntry->getPid(); $category = $categoriesById[$categoryId]; $data = $this->getMetaDataForNews($newsEntry, $category); $newsMetaData[$categoryId][] = $data; } } $newsByCategory = []; foreach ($categoriesById as $categoryId => $category) { /** @var $category Category */ $newsByCategory[$categoryId] = [ 'category' => $category, 'categoryId' => $categoryId, 'newsMetaData' => $newsMetaData[$categoryId] ]; } $allNews = []; $news = $this->newsRepository->findAllSortedNewsByCategories($categoryIds, $newsLimitPerCategory, $offset); foreach ($news as $newsEntry) { /** @var News $newsEntry */ $categoryId = $newsEntry->getPid(); $category = $categoriesById[$categoryId]; $data = $this->getMetaDataForNews($newsEntry, $category); $allNews[] = $data; } $this->highlightBestFitNews($categoryIds); $newsCount = $this->newsRepository->newsCountByCategories($categoryIds); $numberOfPages = ($newsLimitPerCategory <= 0 ? 0 : ceil($newsCount / $newsLimitPerCategory)); $this->view->assign('numberOfPages', $numberOfPages); $this->view->assign('newsByCategory', $newsByCategory); $this->view->assign('allNews', $allNews); } /** * Renders the news in a paginated list * * @return void */ protected function overviewWithoutCategoriesAction() { $offset = 0; $newsPerPage = (int) $this->settings['newsLimit']; $currentPageBrowserPage = (int) GeneralUtility::_GP('tx_sgnews_pagebrowser')['currentPage']; if ($currentPageBrowserPage && $newsPerPage) { $offset = ($currentPageBrowserPage * $newsPerPage) - 1; } if ($this->settings['onlyNewsWithinThisPageSection']) { /** @noinspection PhpUndefinedMethodInspection */ $categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id); $categoryIds = []; $categoriesById = []; foreach ($categories as $category) { /** @var $category Category */ $categoryId = $category->getUid(); $categoryIds[] = $categoryId; $categoriesById[$categoryId] = $category; } $newsCount = $this->newsRepository->newsCountByCategories($categoryIds); } else { $newsCount = $this->newsRepository->countAll(); $categoryIds = NULL; $categoriesById = []; $categories = $this->categoryRepository->findAll(); foreach ($categories as $category) { /** @var $category Category */ $categoriesById[$category->getUid()] = $category; } } $newsMetaData = []; $news = $this->newsRepository->findAllSortedNewsByCategories($categoryIds, $newsPerPage, $offset); foreach ($news as $newsEntry) { /** @var News $newsEntry */ $data = $this->getMetaDataForNews($newsEntry, $categoriesById[$newsEntry->getPid()]); $newsMetaData[] = $data; } $this->highlightBestFitNews($categoryIds); $numberOfPages = ($newsPerPage <= 0 ? 0 : ceil($newsCount / $newsPerPage)); $this->view->assign('numberOfPages', $numberOfPages); $this->view->assign('newsMetaData', $newsMetaData); } }