Skip to content
Snippets Groups Projects
OverviewController.php 6.78 KiB
Newer Older
Stefan Galinski's avatar
Stefan Galinski committed
<?php

namespace SGalinski\SgNews\Controller;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
Stefan Galinski's avatar
Stefan Galinski committed
 *
 *  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;
Stefan Galinski's avatar
Stefan Galinski committed
use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Domain\Model\News;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Stefan Galinski's avatar
Stefan Galinski committed
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'];
Stefan Galinski's avatar
Stefan Galinski committed
		$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();
		}
		$categoriesById = [];
		$newsMetaData = [];
Stefan Galinski's avatar
Stefan Galinski committed
		foreach ($categories as $category) {
			/** @var $category Category */
Stefan Galinski's avatar
Stefan Galinski committed
			$categoryId = $category->getUid();
			$categoryIds[] = $category->getUid();
			$categoriesById[$categoryId] = $category;
Stefan Galinski's avatar
Stefan Galinski committed

			$news = $this->newsRepository->findAllSortedNewsByCategories([$categoryId], $newsLimitPerCategory, $offset);
Stefan Galinski's avatar
Stefan Galinski committed
			foreach ($news as $newsEntry) {
				/** @var News $newsEntry */
				$categoryId = $newsEntry->getPid();
				$category = $categoriesById[$categoryId];

Stefan Galinski's avatar
Stefan Galinski committed
				$data = $this->getMetaDataForNews($newsEntry, $category);
				$newsMetaData[$categoryId][] = $data;
Stefan Galinski's avatar
Stefan Galinski committed
			}
Stefan Galinski's avatar
Stefan Galinski committed

		$newsByCategory = [];
		foreach ($categoriesById as $categoryId => $category) {
			/** @var $category Category */
Stefan Galinski's avatar
Stefan Galinski committed
			$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);
Stefan Galinski's avatar
Stefan Galinski committed
		$this->view->assign('newsByCategory', $newsByCategory);
		$this->view->assign('allNews', $allNews);
Stefan Galinski's avatar
Stefan Galinski committed
	}

	/**
	 * 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);
			$categoriesById = [];
			foreach ($categories as $category) {
				/** @var $category Category */
				$categoryId = $category->getUid();
				$categoryIds[] = $categoryId;
				$categoriesById[$categoryId] = $category;

			$newsCount = $this->newsRepository->newsCountByCategories($categoryIds);
			$newsCount = $this->newsRepository->countAll();
			$categoryIds = NULL;
			$categoriesById = [];
			$categories = $this->categoryRepository->findAll();
			foreach ($categories as $category) {
				/** @var $category Category */
				$categoriesById[$category->getUid()] = $category;
			}
		$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);
	}
}