From 2710f984b5ac5487d63650ba3e214b7359167d8d Mon Sep 17 00:00:00 2001
From: Michael Kessler <michael.kessler@sgalinski.de>
Date: Fri, 5 Aug 2022 16:22:10 +0200
Subject: [PATCH] [FEATURE] Add filter setting

---
 Classes/Controller/VimeoController.php        | 28 ++++++++++++++++++-
 .../FlexForms/flexform_sgvimeo_vimeo.xml      | 11 ++++++++
 Resources/Private/Language/de.locallang.xlf   |  8 ++++++
 Resources/Private/Language/locallang.xlf      |  6 ++++
 4 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/Classes/Controller/VimeoController.php b/Classes/Controller/VimeoController.php
index 6217d6e..d233e2d 100644
--- a/Classes/Controller/VimeoController.php
+++ b/Classes/Controller/VimeoController.php
@@ -39,6 +39,7 @@ class VimeoController extends ActionController {
 	 */
 	public function indexAction(): void {
 		$id = $this->settings['id'];
+		$filterIds = $this->settings['filterId'] ?? '';
 		$maxResults = (int) $this->settings['maxResults'];
 		$clientId = $this->settings['clientId'];
 		$clientSecret = $this->settings['clientSecret'];
@@ -48,6 +49,14 @@ class VimeoController extends ActionController {
 			$this->view->assign('error', 'Please configure a client id and a client secret for sg_vimeo.');
 			return;
 		}
+
+		$filterIds = explode(',', $filterIds);
+		foreach ($filterIds as &$filterId) {
+			$filterId = trim($filterId, " ");
+		}
+
+		$maxResultsWithFilters = (string) ($maxResults + count($filterIds));
+
 		/** @var VimeoService $vimeoService */
 		$vimeoService = GeneralUtility::makeInstance(
 			VimeoService::class,
@@ -55,13 +64,30 @@ class VimeoController extends ActionController {
 			$clientSecret,
 			$personalAccessToken
 		);
-		$response = $vimeoService->getVimeoData($id, $maxResults);
+		$response = $vimeoService->getVimeoData($id, $maxResultsWithFilters);
 		$response = $this->mapVimeoApiResponseWithPossibleCustomThumbnails($response);
 		if ($response === NULL) {
 			return;
 		}
 
 		if (is_array($response['items'])) {
+			$response['items'] = array_filter($response['items'], function ($item) use ($filterIds) {
+				$videoId = (string) $item['videoId'];
+				foreach ($filterIds as $filterId) {
+					if ($videoId === $filterId) {
+						return false;
+					}
+				}
+				return true;
+			});
+
+			// Fix the array indexes from previous filtering
+			$response['items'] = array_values($response['items']);
+
+			while (count($response['items']) > $maxResults) {
+				array_pop($response['items']);
+			}
+
 			foreach ($response['items'] as &$item) {
 
 				/*
diff --git a/Configuration/FlexForms/flexform_sgvimeo_vimeo.xml b/Configuration/FlexForms/flexform_sgvimeo_vimeo.xml
index e5013d0..a9a7643 100644
--- a/Configuration/FlexForms/flexform_sgvimeo_vimeo.xml
+++ b/Configuration/FlexForms/flexform_sgvimeo_vimeo.xml
@@ -25,6 +25,17 @@
 							</config>
 						</TCEforms>
 					</settings.id>
+					<settings.filterId>
+						<TCEforms>
+							<exclude>0</exclude>
+							<label>LLL:EXT:sg_vimeo/Resources/Private/Language/locallang.xlf:flexform.filterIds</label>
+							<description>LLL:EXT:sg_vimeo/Resources/Private/Language/locallang.xlf:flexform.filterIds.description</description>
+							<config>
+								<type>input</type>
+								<eval>trim</eval>
+							</config>
+						</TCEforms>
+					</settings.filterId>
 					<settings.maxResults>
 						<TCEforms>
 							<exclude>0</exclude>
diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf
index 8c04d5c..6efd153 100644
--- a/Resources/Private/Language/de.locallang.xlf
+++ b/Resources/Private/Language/de.locallang.xlf
@@ -105,6 +105,14 @@
 				<source><![CDATA[Example inputs - Single video: 123456789 | Channel: channel/channelname | Showcase: showcase/987645]]></source>
 				<target><![CDATA[Beispieleingaben - Einzelnes Video: 123456789 | Kanal: channel/kanalname | Showcase: showcase/987645]]></target>
 			</trans-unit>
+			<trans-unit id="flexform.filterIds" approved="yes">
+				<source><![CDATA[IDs of the videos to exclude]]></source>
+				<target><![CDATA[Auszuschließende Video-IDs]]></target>
+			</trans-unit>
+			<trans-unit id="flexform.filterIds.description" approved="yes">
+				<source><![CDATA[Insert as a comma seperated list. (12345, 67890)]]></source>
+				<target><![CDATA[Als ein List getrennt durch Kommas angeben. (12345, 67890)]]></target>
+			</trans-unit>
 			<trans-unit id="flexform.layout" approved="yes">
 				<source><![CDATA[Layout Style]]></source>
 				<target><![CDATA[Layout-Art]]></target>
diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf
index 41e222d..878851a 100644
--- a/Resources/Private/Language/locallang.xlf
+++ b/Resources/Private/Language/locallang.xlf
@@ -32,6 +32,12 @@
 			<trans-unit id="flexform.id">
 				<source><![CDATA[Video ID (https://vimeo.com/%VIDEO_ID%), channel name (https://vimeo.com/channels/%CHANNEL_NAME%) or showcase (https://vimeo.com/showcase/%SHOWCASE_ID%)]]></source>
 			</trans-unit>
+			<trans-unit id="flexform.filterIds">
+				<source><![CDATA[IDs of the videos to exclude]]></source>
+			</trans-unit>
+			<trans-unit id="flexform.filterIds.description">
+				<source><![CDATA[Insert as a comma seperated list. (12345, 67890)]]></source>
+			</trans-unit>
 			<trans-unit id="flexform.layout">
 				<source><![CDATA[Layout Style]]></source>
 			</trans-unit>
-- 
GitLab