Newer
Older
<?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 RuntimeException;
use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Domain\Model\News;
use SGalinski\SgNews\Service\ImageService;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
/**
* Abstract Controller
*/
abstract class AbstractController extends ActionController {
/**
*/
protected $imageService;
/**
* @var array
*/
protected $extensionConfiguration = [];
/**
* @var array
*/
protected $cachedSingleNews = [];
/**
* Initializes any action
*
* @return void
*/
public function initializeAction() {
$extensionKey = $this->request->getControllerExtensionKey();
$this->extensionConfiguration = $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extensionKey] ?? [];
/**
* @param ImageService $imageService
*/
public function injectImageService(ImageService $imageService) {
$this->imageService = $imageService;
}
/**
* 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
protected function getMetaDataForNews(News $news, Category $category): array {
$newsId = $news->getUid();
if (isset($this->cachedSingleNews[$newsId])) {
return $this->cachedSingleNews[$newsId];
}
Fabian Galinski
committed
$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(
Fabian Galinski
committed
[
'category' => $category,
'news' => $news,
],
$singleNewsImageData,
$teaserImageData
);
$this->cachedSingleNews[$newsId] = $newsRecord;
return $newsRecord;
Fabian Galinski
committed
}
/**
* 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
Fabian Galinski
committed
*/
protected function getDataForSingleViewImage(News $news, Category $category): array {
Fabian Galinski
committed
$singleNewsImage = $singleNewsImageObject = NULL;
$singleNewsImages = $news->getTeaser2Image();
if (count($singleNewsImages)) {
$singleNewsImage = $singleNewsImages->current();
} else {
$categoryImages = $category->getTeaser2Image();
if (count($categoryImages)) {
$singleNewsImage = $categoryImages->current();
$singleNewsImageObject = $singleNewsImage;
$originalResource = $singleNewsImage->getOriginalResource();
$singleNewsImage = $originalResource->getPublicUrl();
if ($singleNewsImage) {
$singleNewsImage = $GLOBALS['TSFE']->absRefPrefix . $singleNewsImage;
}
}
Fabian Galinski
committed
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
Fabian Galinski
committed
*/
protected function getDataForTeaserImage(News $news, Category $category): array {
Fabian Galinski
committed
/** @var FileReference $teaserImage */
$teaserImage = $teaserImageObject = NULL;
$teaserImages = $news->getTeaser1Image();
if (count($teaserImages)) {
$teaserImage = $teaserImages->current();
} else {
$categoryImages = $category->getTeaser1Image();
if (count($categoryImages)) {
$teaserImage = $categoryImages->current();
$teaserImageObject = $teaserImage;
$originalResource = $teaserImage->getOriginalResource();
$teaserImage = $originalResource->getPublicUrl();
if ($teaserImage) {
$teaserImage = $GLOBALS['TSFE']->absRefPrefix . $teaserImage;
}
}
return [
'teaserImage' => $teaserImage,
'teaserImageObject' => $teaserImageObject,
/**
* 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;
}