YoutubeController.php 4.46 KB
Newer Older
Johannes Kreiner's avatar
Johannes Kreiner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace SGalinski\SgYoutube\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!
 ***************************************************************/

23
use Exception;
24
use SGalinski\SgYoutube\Service\YoutubeService;
25
26
27
28
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;
29
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
30
use TYPO3\CMS\Extbase\Service\ImageService;
31
32
33
34
35

/**
 *  The Youtube Controller
 */
class YoutubeController extends ActionController {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	/**
	 * @var YoutubeService
	 */
	protected $youtubeService;

	/**
	 * Injects the youtube service. Is lot faster to use the inject method than the inject annotation!
	 *
	 * @param YoutubeService $youtubeService
	 * @return void
	 */
	public function injectYoutubeService(YoutubeService $youtubeService) {
		$this->youtubeService = $youtubeService;
	}

51
52
53
	/**
	 * Renders the Youtube video view
	 *
54
	 * @return NULL|string
55
	 */
56
	public function indexAction() {
57
		$id = $this->settings['id'];
58
		$maxResults = $this->settings['maxResults'];
59
		$apiKey = $this->settings['apiKey'];
60
61
62
63
64
		$thumbnailType = $this->settings['thumbnailType'];
		$aspectRatio = $this->settings['aspectRatio'];
		$showApiResult = (boolean) $this->settings['showApiResult'];
		$debugOutput = '';

65
66
		try {
			$jsonArray = $this->youtubeService->getJsonAsArray($id, $maxResults, $apiKey);
67
68
69
70
71
72

			if ($showApiResult) {
				$debugOutput = nl2br($id . "\n\n" . print_r($jsonArray, TRUE));
			}

			$jsonArray['items'] = $this->youtubeService->mapArray(
73
				$jsonArray['items'], $id, $aspectRatio, $thumbnailType, $apiKey
74
75
			);

76
			$jsonArray['items'] = $this->mapJsonArrayWithPossibleCustomThumbnails($jsonArray['items']);
77
78
79
		} catch (Exception $exception) {
			return '<div style="color: red;">' . $exception->getMessage() . '</div>';
		}
80

81
82
83
		$this->view->assignMultiple(
			[
				'feed' => $jsonArray['items'],
84
85
				'response' => $jsonArray,
				'debugOutput' => $debugOutput,
86
				'showTitle' => (int) ($this->settings['showTitle'] ?? 1),
87
				'showDescription' => (int) ($this->settings['showDescription'] ?? 1),
88
89
			]
		);
90
91

		return NULL;
Johannes Kreiner's avatar
Johannes Kreiner committed
92
	}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

	/**
	 * Maps the given jsonArray thumbnails with the possible custom ones from the plugin settings.
	 *
	 * @param array $jsonArray
	 * @return array
	 */
	protected function mapJsonArrayWithPossibleCustomThumbnails(array $jsonArray): array {
		$contentElementUid = (int) $this->configurationManager->getContentObject()->data['uid'];
		if ($contentElementUid <= 0) {
			return $jsonArray;
		}

		$fileRepository = GeneralUtility::makeInstance(FileRepository::class);
		$fileObjects = $fileRepository->findByRelation(
			'tt_content', 'tx_sgyoutube_thumbnail_image', $contentElementUid
		);
		if (count($fileObjects) <= 0) {
			return $jsonArray;
		}

114
		/** @var FileReference $fileObject */
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
		foreach ($fileObjects as $index => $fileObject) {
			if (!isset($jsonArray[$index])) {
				break;
			}

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

			$cropVariantCollection = CropVariantCollection::create((string) $cropString);
			$cropArea = $cropVariantCollection->getCropArea('default');
			$processingInstructions = [
				'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($fileObject),
			];
			$imageService = GeneralUtility::makeInstance(ImageService::class);
			$processedImage = $imageService->applyProcessingInstructions($fileObject, $processingInstructions);
			$jsonArray[$index]['thumbnail'] = $imageService->getImageUri($processedImage);
		}

		return $jsonArray;
	}
Johannes Kreiner's avatar
Johannes Kreiner committed
137
}