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

namespace SGalinski\SgNews\Controller;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (http://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\Xclass\PageRenderer;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
 * 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) {
		/** @var FileReference $teaserImage */
		/** @var FileReference $singleNewsImage */
		$objectForData = $news;
		$teaserImage = $singleNewsImage = NULL;

		$singleNewsImages = $news->getTeaser2Image();
		$useImageCropForSingleNews = $news->getUseImage2Crop();

		if (count($singleNewsImages)) {
			$singleNewsImage = $singleNewsImages->current();
		} else {
			$singleNewsImages = $category->getTeaser2Image();
			$useImageCropForSingleNews = $category->getUseImage2Crop();

			if (count($singleNewsImages)) {
				$singleNewsImage = $singleNewsImages->current();
				$objectForData = $category;
			}
		}

		$imageHeadlineColor = 0;
		if ($singleNewsImage) {
			if (!$useImageCropForSingleNews) {
				$originalResource = $singleNewsImage->getOriginalResource();
				$singleNewsImage = $originalResource->getPublicUrl();
			} else {
				$coordinates = $objectForData->getTeaser2Coordinates();
				$imageHeadlineColor = $objectForData->getTeaser2HeaderColor();
				$singleNewsImage = $this->imageService->cropFirstMediaImage(
					$singleNewsImage, $coordinates, 'fileadmin/sgNews/', $news->getTitle()
				);
			}

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

		$objectForData = $news;
		$teaserImages = $news->getTeaser1Image();
		$useImageCropForTeaser = $news->getUseImageCrop();

		if (count($teaserImages)) {
			$teaserImage = $teaserImages->current();
		} else {
			$teaserImages = $category->getTeaser1Image();
			$useImageCropForTeaser = $category->getUseImageCrop();

			if (count($teaserImages)) {
				$teaserImage = $teaserImages->current();
				$objectForData = $category;
			}
		}

		$teaserImageHeadlineColor = 0;
		if ($teaserImage) {
			if (!$useImageCropForTeaser) {
				$originalResource = $teaserImage->getOriginalResource();
				$teaserImage = $originalResource->getPublicUrl();
			} else {
				$coordinates = $objectForData->getTeaser1Coordinates();
				$teaserImageHeadlineColor = $objectForData->getTeaser1HeaderColor();
				$teaserImage = $this->imageService->cropFirstMediaImage(
					$teaserImage, $coordinates, 'fileadmin/sgNews/', 'teaser-' . $news->getTitle()
				);
			}

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

		return [
			'category' => $category,
			'news' => $news,
			'image' => $singleNewsImage,
			'teaserImage' => $teaserImage,
			'imageHeadlineColor' => $imageHeadlineColor,
			'teaserImageHeadlineColor' => $teaserImageHeadlineColor,
		];
	}
}

?>