<?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 TYPO3\CMS\Extbase\Domain\Model\FileReference; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; /** * Abstract Controller */ abstract class AbstractController extends ActionController { /** * @inject * @var \SGalinski\SgNews\Service\ImageService */ protected $imageService; /** * @var array */ protected $extensionConfiguration = []; /** * Initializes any action * * @return void */ public function initializeAction() { $extensionKey = $this->request->getControllerExtensionKey(); $serializedConfiguration = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey]; $this->extensionConfiguration = unserialize($serializedConfiguration); parent::initializeAction(); } /** * 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) { $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'], ]; } return array_merge( [ 'category' => $category, 'news' => $news, ], $singleNewsImageData, $teaserImageData ); } /** * 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 */ protected function getDataForSingleViewImage(News $news, Category $category) { /** @var FileReference $singleNewsImage */ $singleNewsImage = $singleNewsImageObject = NULL; $singleNewsImages = $news->getTeaser2Image(); if (count($singleNewsImages)) { $singleNewsImage = $singleNewsImages->current(); } else { $singleNewsImages = $category->getTeaser2Image(); if (count($singleNewsImages)) { $singleNewsImage = $singleNewsImages->current(); } } if ($singleNewsImage) { $singleNewsImageObject = $singleNewsImage; $originalResource = $singleNewsImage->getOriginalResource(); $singleNewsImage = $originalResource->getPublicUrl(); 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 */ protected function getDataForTeaserImage(News $news, Category $category) { /** @var FileReference $teaserImage */ $teaserImage = $teaserImageObject = NULL; $teaserImages = $news->getTeaser1Image(); if (count($teaserImages)) { $teaserImage = $teaserImages->current(); } else { $teaserImages = $category->getTeaser1Image(); if (count($teaserImages)) { $teaserImage = $teaserImages->current(); } } if ($teaserImage) { $teaserImageObject = $teaserImage; $originalResource = $teaserImage->getOriginalResource(); $teaserImage = $originalResource->getPublicUrl(); if ($teaserImage) { $teaserImage = $GLOBALS['TSFE']->absRefPrefix . $teaserImage; } } return [ 'teaserImage' => $teaserImage, 'teaserImageObject' => $teaserImageObject, ]; } } ?>