Skip to content
Snippets Groups Projects
VimeoController.php 5.06 KiB
Newer Older
Kevin von Spiczak's avatar
Kevin von Spiczak committed
<?php

namespace SGalinski\SgVimeo\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 SGalinski\SgVimeo\Service\VimeoService;
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Service\ImageService;

/**
 *  The Vimeo Controller
 */
class VimeoController extends ActionController {

	/**
	 * Renders the Vimeo video view
	 *
	 * @return void
	 */
	public function indexAction(): void {
		$id = $this->settings['id'];
		$maxResults = (int) $this->settings['maxResults'];
		$clientId = $this->settings['clientId'];
		$clientSecret = $this->settings['clientSecret'];
		$personalAccessToken = $this->settings['personalAccessToken'];

		if (trim($clientId) === '' || trim($clientSecret) === '') {
			$this->view->assign('error', 'Please configure a client id and a client secret for sg_vimeo.');
			return;
		}
		/** @var VimeoService $vimeoService */
		$vimeoService = GeneralUtility::makeInstance(
			VimeoService::class, $clientId, $clientSecret, $personalAccessToken
		);
		$response = $vimeoService->getVimeoData($id, $maxResults);
Kevin von Spiczak's avatar
Kevin von Spiczak committed
		$response = $this->mapVimeoApiResponseWithPossibleCustomThumbnails($response);
		if ($response === NULL) {
			return;
		}

		if (is_array($response['items'])) {
			foreach ($response['items'] as &$item) {
				/*
				 * Check if link is of the form "https://vimeo.com/123456789", not "https://vimeo.com/username/videoname".
				 * Just checks if the end of `link` is the same as `videoId`, and if not, it replaces `link`.
				 * This is needed as a workaround so that glightbox can detect the video as a vimeo video correctly in the frontend.
				 */
				$endOfLink = substr($item['link'], -strlen($item['videoId']));
				if (!((int) $endOfLink === $item['videoId'])) {
					$item['link'] = strstr($item['link'], '.com/', TRUE) . '.com/' . $item['videoId'];
				}
			unset($item);
		$showApiResult = (int) ($this->settings['showApiResult'] ?? 1);
		$context = GeneralUtility::getApplicationContext();
		// disable API debug output in production context
		if ($context->isProduction()) {
			$showApiResult = 0;
		}

Kevin von Spiczak's avatar
Kevin von Spiczak committed
		$this->view->assignMultiple(
			[
				'response' => $response,
				'showApiResult' => $showApiResult,
Kevin von Spiczak's avatar
Kevin von Spiczak committed
				'showTitle' => (int) ($this->settings['showTitle'] ?? 1),
				'showDescription' => (int) ($this->settings['showDescription'] ?? 1),
			]
		);
	}

	/**
	 * Maps the given vimeo API response thumbnails with the possible custom ones from the plugin settings.
	 *
	 * @param array|null $response
	 * @return array|null
	 */
	protected function mapVimeoApiResponseWithPossibleCustomThumbnails(?array $response): ?array {
		if (!is_array($response) || !array_key_exists('items', $response)) {
Kevin von Spiczak's avatar
Kevin von Spiczak committed
			return NULL;
		}

		$contentElementUid = (int) $this->configurationManager->getContentObject()->data['uid'];
		if ($contentElementUid <= 0) {
			return $response;
		}

		/** @var FileRepository $fileRepository */
Kevin von Spiczak's avatar
Kevin von Spiczak committed
		$fileRepository = GeneralUtility::makeInstance(FileRepository::class);
		$fileObjects = $fileRepository->findByRelation(
			'tt_content', 'tx_sgvimeo_thumbnail_image', $contentElementUid
		);
		if (count($fileObjects) <= 0) {
			return $response;
		}

		/** @var FileReference $fileObject */
		foreach ($fileObjects as $index => $fileObject) {
			if (!isset($response['items'][$index])) {
				break;
			}

			$cropString = '';
			if ($fileObject->hasProperty('crop') && $fileObject->getProperty('crop')) {
				$cropString = $fileObject->getProperty('crop');
			}

			$cropVariantCollection = CropVariantCollection::create((string) $cropString);
			$cropArea = $cropVariantCollection->getCropArea();
			$processingInstructions = [
				'crop' => $cropArea->isEmpty() ? NULL : $cropArea->makeAbsoluteBasedOnFile($fileObject),
			];
			/** @var ImageService $imageService */
Kevin von Spiczak's avatar
Kevin von Spiczak committed
			$imageService = GeneralUtility::makeInstance(ImageService::class);
			$processedImage = $imageService->applyProcessingInstructions($fileObject, $processingInstructions);
			$response['items'][$index]['thumbnail'] = $imageService->getImageUri($processedImage);
		}

		return $response;
	}
}