Skip to content
Snippets Groups Projects
AbstractController.php 5.88 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 RuntimeException;
use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Domain\Model\News;
use SGalinski\SgNews\Service\ImageService;
Stefan Galinski's avatar
Stefan Galinski committed
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

/**
 * Abstract Controller
 */
abstract class AbstractController extends ActionController {
	/**
	 * @var ImageService
Stefan Galinski's avatar
Stefan Galinski committed
	 */
	protected $imageService;

	/**
	 * @var array
	 */
	protected $extensionConfiguration = [];

	/**
	 * @var array
	 */
	protected $cachedSingleNews = [];

Stefan Galinski's avatar
Stefan Galinski committed
	/**
	 * Initializes any action
	 *
	 * @return void
	 */
	public function initializeAction() {
		$extensionKey = $this->request->getControllerExtensionKey();
		$this->extensionConfiguration = $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extensionKey] ?? [];
Stefan Galinski's avatar
Stefan Galinski committed

		parent::initializeAction();
	}

	/**
	 * @param ImageService $imageService
	 */
	public function injectImageService(ImageService $imageService) {
		$this->imageService = $imageService;
	}

Stefan Galinski's avatar
Stefan Galinski committed
	/**
	 * Error Handler
	 *
	 * @throws \RuntimeException
	 * @return void
	 */
	public function errorAction() {
		throw new RuntimeException(parent::errorAction());
	}

	/**
	 * Returns the meta data of the given news.
	 *
	 * @param News $news
	 * @param Category $category
	 * @return array
	 * @throws \InvalidArgumentException
Stefan Galinski's avatar
Stefan Galinski committed
	 */
	protected function getMetaDataForNews(News $news, Category $category): array {
		$newsId = $news->getUid();
		if (isset($this->cachedSingleNews[$newsId])) {
			return $this->cachedSingleNews[$newsId];
		}

		$singleNewsImageData = $this->getDataForSingleViewImage($news, $category);
		$teaserImageData = $this->getDataForTeaserImage($news, $category);

		// Use single news image data for teaser image data, if the teaser imaga data are empty.
		$teaserIsEmpty = $teaserImageData['teaserImage'] === NULL && $teaserImageData['teaserImageObject'] === NULL;
		$singleNewsIsEmpty = $singleNewsImageData['image'] === NULL && $singleNewsImageData['imageObject'] === NULL;
		if ($teaserIsEmpty && !$singleNewsIsEmpty) {
			$teaserImageData = [
				'teaserImage' => $singleNewsImageData['image'],
				'teaserImageObject' => $singleNewsImageData['imageObject'],
			];
		}

		$newsRecord = array_merge(
			[
				'category' => $category,
				'news' => $news,
			],
			$singleNewsImageData,
			$teaserImageData
		);

		$this->cachedSingleNews[$newsId] = $newsRecord;

		return $newsRecord;
	}

	/**
	 * Returns the single view image data as an array for the given news and category as fallback.
	 *
	 * @param News $news
	 * @param Category $category
	 * @return array
	 * @throws \InvalidArgumentException
	protected function getDataForSingleViewImage(News $news, Category $category): array {
Stefan Galinski's avatar
Stefan Galinski committed
		/** @var FileReference $singleNewsImage */
		$singleNewsImage = $singleNewsImageObject = NULL;
Stefan Galinski's avatar
Stefan Galinski committed
		$singleNewsImages = $news->getTeaser2Image();
		if (count($singleNewsImages)) {
			$singleNewsImage = $singleNewsImages->current();
		} else {
			$categoryImages = $category->getTeaser2Image();
			if (count($categoryImages)) {
				$singleNewsImage = $categoryImages->current();
Stefan Galinski's avatar
Stefan Galinski committed
			}
		}

		if ($singleNewsImage) {
			$singleNewsImageObject = $singleNewsImage;
			$originalResource = $singleNewsImage->getOriginalResource();
			$singleNewsImage = $originalResource->getPublicUrl();
Stefan Galinski's avatar
Stefan Galinski committed

			if ($singleNewsImage) {
				$singleNewsImage = $GLOBALS['TSFE']->absRefPrefix . $singleNewsImage;
			}
		}

		return [
			'image' => $singleNewsImage,
			'imageObject' => $singleNewsImageObject,
		];
	}

	/**
	 * Returns the teaser image data as an array for the given news and category as fallback.
	 *
	 * @param News $news
	 * @param Category $category
	 * @return array
	 * @throws \InvalidArgumentException
	protected function getDataForTeaserImage(News $news, Category $category): array {
		/** @var FileReference $teaserImage */
		$teaserImage = $teaserImageObject = NULL;
Stefan Galinski's avatar
Stefan Galinski committed
		$teaserImages = $news->getTeaser1Image();
		if (count($teaserImages)) {
			$teaserImage = $teaserImages->current();
		} else {
			$categoryImages = $category->getTeaser1Image();
			if (count($categoryImages)) {
				$teaserImage = $categoryImages->current();
Stefan Galinski's avatar
Stefan Galinski committed
			}
		}

		if ($teaserImage) {
			$teaserImageObject = $teaserImage;
			$originalResource = $teaserImage->getOriginalResource();
			$teaserImage = $originalResource->getPublicUrl();
Stefan Galinski's avatar
Stefan Galinski committed

			if ($teaserImage) {
				$teaserImage = $GLOBALS['TSFE']->absRefPrefix . $teaserImage;
			}
		}

		return [
			'teaserImage' => $teaserImage,
			'teaserImageObject' => $teaserImageObject,
Stefan Galinski's avatar
Stefan Galinski committed
		];
	}

	/**
	 * Calculate the pagination offset
	 *
	 * @param int $currentPageBrowserPage
	 * @param int $newsLimitPerPage
	 * @return int
	 */
	protected function calculatePaginationOffset(int $currentPageBrowserPage, int $newsLimitPerPage = NULL){
		$offset = 0;
		$newsPerPage = $newsLimitPerPage ?? (int) $this->settings['newsLimitPerPage'];
		if ($currentPageBrowserPage > 0 && $newsPerPage > 0) {
			$offset = $currentPageBrowserPage * $newsPerPage;
		}
		return $offset;
	}
Stefan Galinski's avatar
Stefan Galinski committed
}