From fee27f4fd534fe901d13e8981191d02dd466bf38 Mon Sep 17 00:00:00 2001
From: Georgi Mateev <georgi.mateev@sgalinski.de>
Date: Wed, 20 Nov 2024 10:58:26 +0200
Subject: [PATCH] [TASK] Show info about Vimeo restricting channels to EU and
 UK

---
 Classes/Controller/VimeoController.php        | 11 ++++-
 .../Exception/RegionRestrictedException.php   | 37 +++++++++++++++
 Classes/Form/Element/ChannelWarning.php       | 46 +++++++++++++++++++
 Classes/Form/Element/LicenceStatus.php        | 10 ++++
 Classes/Service/VimeoService.php              |  4 ++
 .../FlexForms/flexform_sgvimeo_vimeo.xml      |  7 +++
 Resources/Private/Language/de.locallang.xlf   |  8 ++++
 Resources/Private/Language/locallang.xlf      |  6 +++
 Resources/Private/Partials/Error.html         |  3 ++
 Resources/Private/Partials/NotFound.html      |  3 --
 .../Templates/Bootstrap5/Vimeo/Index.html     |  9 +++-
 Resources/Private/Templates/Vimeo/Index.html  |  9 +++-
 ext_localconf.php                             |  8 ++++
 13 files changed, 155 insertions(+), 6 deletions(-)
 create mode 100644 Classes/Exception/RegionRestrictedException.php
 create mode 100644 Classes/Form/Element/ChannelWarning.php
 create mode 100644 Resources/Private/Partials/Error.html
 delete mode 100644 Resources/Private/Partials/NotFound.html

diff --git a/Classes/Controller/VimeoController.php b/Classes/Controller/VimeoController.php
index 08505db..8f73628 100644
--- a/Classes/Controller/VimeoController.php
+++ b/Classes/Controller/VimeoController.php
@@ -31,6 +31,7 @@ use SGalinski\SgVimeo\Event\AfterFilterVideosEvent;
 use SGalinski\SgVimeo\Event\AfterMapCustomThumbnailsEvent;
 use SGalinski\SgVimeo\Event\AfterVimeoCallEvent;
 use SGalinski\SgVimeo\Event\BeforeVimeoCallEvent;
+use SGalinski\SgVimeo\Exception\RegionRestrictedException;
 use SGalinski\SgVimeo\Filter\FilterParameterBag;
 use SGalinski\SgVimeo\Service\CachedImageService;
 use SGalinski\SgVimeo\Service\VimeoService;
@@ -122,7 +123,15 @@ class VimeoController extends ActionController {
 			$this->eventDispatcher->dispatch($beforeVimeoCallEvent);
 
 			// Use the possibly modified parameters
-			$response = $vimeoService->getVimeoData($filterParameterBag);
+			try {
+				$response = $vimeoService->getVimeoData($filterParameterBag);
+			} catch (RegionRestrictedException $exception) {
+				$response = [];
+				$response['items'] = [];
+				$response['kind'] = 'channel';
+				$this->view->assign('regionRestricted', true);
+			}
+
 			if (count($response['items']) < 1) {
 				$this->view->assignMultiple([
 					'notFound' => TRUE,
diff --git a/Classes/Exception/RegionRestrictedException.php b/Classes/Exception/RegionRestrictedException.php
new file mode 100644
index 0000000..700da94
--- /dev/null
+++ b/Classes/Exception/RegionRestrictedException.php
@@ -0,0 +1,37 @@
+<?php
+
+/***************************************************************
+ *  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!
+ ***************************************************************/
+
+namespace SGalinski\SgVimeo\Exception;
+
+use RuntimeException;
+
+/**
+ * Class JsonImportException
+ *
+ * @package SGalinski\SgCookieOptin\Exception
+ */
+class RegionRestrictedException extends RuntimeException {
+}
diff --git a/Classes/Form/Element/ChannelWarning.php b/Classes/Form/Element/ChannelWarning.php
new file mode 100644
index 0000000..8995a1d
--- /dev/null
+++ b/Classes/Form/Element/ChannelWarning.php
@@ -0,0 +1,46 @@
+<?php
+
+/***************************************************************
+ *  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!
+ ***************************************************************/
+
+namespace SGalinski\SgVimeo\Form\Element;
+
+use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
+use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
+
+/**
+ * Licence Status Field
+ */
+class ChannelWarning extends AbstractFormElement {
+	public function render(): array {
+		$errorMessage = LocalizationUtility::translate(
+			'flexform.channelsRestricted',
+			'sg_vimeo'
+		);
+
+		$message = '<div class="alert alert-warning" role="warning">' . $errorMessage . '</div>';
+		$resultArray['html'] = $message;
+		return $resultArray;
+	}
+}
diff --git a/Classes/Form/Element/LicenceStatus.php b/Classes/Form/Element/LicenceStatus.php
index 155e4e0..20f30c1 100644
--- a/Classes/Form/Element/LicenceStatus.php
+++ b/Classes/Form/Element/LicenceStatus.php
@@ -58,6 +58,16 @@ class LicenceStatus extends AbstractFormElement {
 		return $resultArray;
 	}
 
+	/**
+	 * Checks the license key and retrieves license check response data.
+	 *
+	 * This function verifies if the current TYPO3 version is supported,
+	 * updates the last AJAX notification check timestamp, and retrieves
+	 * the license check response data.
+	 *
+	 * @return array An array containing license check response data,
+	 *               or an empty array if the TYPO3 version is not supported.
+	 */
 	private function checkLicenceKey() {
 		if (!LicenceCheckService::isTYPO3VersionSupported()
 		) {
diff --git a/Classes/Service/VimeoService.php b/Classes/Service/VimeoService.php
index 25de782..4377bc4 100644
--- a/Classes/Service/VimeoService.php
+++ b/Classes/Service/VimeoService.php
@@ -28,6 +28,7 @@ namespace SGalinski\SgVimeo\Service;
 
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerAwareTrait;
+use SGalinski\SgVimeo\Exception\RegionRestrictedException;
 use SGalinski\SgVimeo\Filter\FilterParameterBag;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
@@ -321,6 +322,9 @@ class VimeoService implements LoggerAwareInterface {
 			$response = $this->vimeoApiClient->request(
 				self::API_CHANNEL . $channelIdentifier . self::API_VIDEO . '?' . $query
 			);
+			if ($response['status'] === 404 && ($response['body']['error'] ?? '') === 'This resource is restricted in your region.') {
+				throw new RegionRestrictedException($response['body']['error']);
+			}
 		} catch (VimeoRequestException $e) {
 			throw $e;
 		}
diff --git a/Configuration/FlexForms/flexform_sgvimeo_vimeo.xml b/Configuration/FlexForms/flexform_sgvimeo_vimeo.xml
index 73511f0..cb55bd4 100644
--- a/Configuration/FlexForms/flexform_sgvimeo_vimeo.xml
+++ b/Configuration/FlexForms/flexform_sgvimeo_vimeo.xml
@@ -28,6 +28,13 @@
 							<eval>trim,required</eval>
 						</config>
 					</settings.id>
+					<settings.channelWarning>
+						<label></label>
+						<config>
+							<type>user</type>
+							<renderType>SgVimeoChannelWarning</renderType>
+						</config>
+					</settings.channelWarning>
 					<settings.filterId>
 						<exclude>0</exclude>
 						<label>LLL:EXT:sg_vimeo/Resources/Private/Language/locallang.xlf:flexform.filterIds</label>
diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf
index b9c9826..0d2d0cf 100644
--- a/Resources/Private/Language/de.locallang.xlf
+++ b/Resources/Private/Language/de.locallang.xlf
@@ -289,6 +289,14 @@
 				<source>No videos found</source>
 				<target>Keine Videos gefunden</target>
 			</trans-unit>
+			<trans-unit id="response.regionRestricted">
+				<source>This resource has been restricted in your region by Vimeo.</source>
+				<target>Diese Ressource wurde in Ihrer Region von Vimeo gesperrt.</target>
+			</trans-unit>
+			<trans-unit id="flexform.channelsRestricted">
+				<source>Vimeo has restricted the use of channels for visitors from the EU and UK area!</source>
+				<target>Vimeo hat die Nutzung der Kanäle für Besucher aus der EU und dem Vereinigten Königreich gesperrt!</target>
+			</trans-unit>
 			<trans-unit id="flexform.queryString" resname="flexform.queryString" approved="yes">
 				<source><![CDATA[Query String]]></source>
 				<target><![CDATA[Abfragetext]]></target>
diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf
index 523d949..6728921 100644
--- a/Resources/Private/Language/locallang.xlf
+++ b/Resources/Private/Language/locallang.xlf
@@ -218,6 +218,12 @@
 			<trans-unit id="response.noItemsFound">
 				<source>No videos found</source>
 			</trans-unit>
+			<trans-unit id="response.regionRestricted">
+				<source>This resource has been restricted in your region by Vimeo.</source>
+			</trans-unit>
+			<trans-unit id="flexform.channelsRestricted">
+				<source>Vimeo has restricted the use of channels for visitors from the EU and UK area!</source>
+			</trans-unit>
 			<trans-unit id="flexform.queryString" resname="flexform.queryString" approved="yes">
 				<source><![CDATA[Query String]]></source>
 			</trans-unit>
diff --git a/Resources/Private/Partials/Error.html b/Resources/Private/Partials/Error.html
new file mode 100644
index 0000000..8b8c262
--- /dev/null
+++ b/Resources/Private/Partials/Error.html
@@ -0,0 +1,3 @@
+<div style="color: red;">
+	{f:translate(key: message)}
+</div>
diff --git a/Resources/Private/Partials/NotFound.html b/Resources/Private/Partials/NotFound.html
deleted file mode 100644
index a639cf8..0000000
--- a/Resources/Private/Partials/NotFound.html
+++ /dev/null
@@ -1,3 +0,0 @@
-<div style="color: red;">
-	{f:translate(key: 'response.noItemsFound')}
-</div>
diff --git a/Resources/Private/Templates/Bootstrap5/Vimeo/Index.html b/Resources/Private/Templates/Bootstrap5/Vimeo/Index.html
index fdc6b3f..d6417af 100644
--- a/Resources/Private/Templates/Bootstrap5/Vimeo/Index.html
+++ b/Resources/Private/Templates/Bootstrap5/Vimeo/Index.html
@@ -36,7 +36,14 @@
 
 					<f:if condition="{notFound}">
 						<f:then>
-							<f:render partial="NotFound" arguments="{name: filter.name, label: filter.label}" />
+							<f:if condition="{regionRestricted}">
+								<f:then>
+									<f:render partial="Error" arguments="{name: filter.name, label: filter.label, message: 'response.regionRestricted'}" />
+								</f:then>
+								<f:else>
+									<f:render partial="Error" arguments="{name: filter.name, label: filter.label, message: 'response.noItemsFound'}" />
+								</f:else>
+							</f:if>
 						</f:then>
 						<f:else>
 							<f:if condition="{feedCount} < 2">
diff --git a/Resources/Private/Templates/Vimeo/Index.html b/Resources/Private/Templates/Vimeo/Index.html
index 2730de0..2ed98aa 100644
--- a/Resources/Private/Templates/Vimeo/Index.html
+++ b/Resources/Private/Templates/Vimeo/Index.html
@@ -57,7 +57,14 @@
 
 					<f:if condition="{notFound}">
 						<f:then>
-							<f:render partial="NotFound" arguments="{name: filter.name, label: filter.label}" />
+							<f:if condition="{regionRestricted}">
+								<f:then>
+									<f:render partial="Error" arguments="{name: filter.name, label: filter.label, message: 'response.regionRestricted'}" />
+								</f:then>
+								<f:else>
+									<f:render partial="Error" arguments="{name: filter.name, label: filter.label, message: 'response.noItemsFound'}" />
+								</f:else>
+							</f:if>
 						</f:then>
 						<f:else>
 
diff --git a/ext_localconf.php b/ext_localconf.php
index f7f19ab..04869ea 100644
--- a/ext_localconf.php
+++ b/ext_localconf.php
@@ -1,6 +1,7 @@
 <?php
 
 use SGalinski\SgVimeo\Controller\VimeoController;
+use SGalinski\SgVimeo\Form\Element\ChannelWarning;
 use SGalinski\SgVimeo\Form\Element\LicenceStatus;
 use SGalinski\SgVimeo\Hooks\LicenceCheckHook;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
@@ -43,3 +44,10 @@ $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][] = [
 	'priority' => 40,
 	'class' => LicenceStatus::class,
 ];
+
+// Add Channel Warning render type
+$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][] = [
+	'nodeName' => 'SgVimeoChannelWarning',
+	'priority' => 40,
+	'class' => ChannelWarning::class,
+];
-- 
GitLab