From d0cd79a707142a268792ae70edeb912253f9995a Mon Sep 17 00:00:00 2001
From: Fabian Galinski <fabian@sgalinski.de>
Date: Thu, 6 Oct 2016 15:22:26 +0200
Subject: [PATCH] [FEATURE] Implements a language feature, which should be
 respected in extbase

---
 Classes/Xclass/Typo3DbBackend.php | 157 ++++++++++++++++++++++++++++++
 ext_localconf.php                 |   2 +
 2 files changed, 159 insertions(+)
 create mode 100755 Classes/Xclass/Typo3DbBackend.php

diff --git a/Classes/Xclass/Typo3DbBackend.php b/Classes/Xclass/Typo3DbBackend.php
new file mode 100755
index 0000000..9d99589
--- /dev/null
+++ b/Classes/Xclass/Typo3DbBackend.php
@@ -0,0 +1,157 @@
+<?php
+
+namespace SGalinski\SgNews\Xclass;
+
+/***************************************************************
+ *  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 2 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 TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Extbase\Persistence\Generic\Qom;
+
+/**
+ * Xclass for the TYPO3 db backend, which handles, that the l18n_cfg parameters are used for pages in Extbase.
+ */
+class Typo3DbBackend extends \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend {
+	/**
+	 * Performs workspace and language overlay on the given row array. The language and workspace id is automatically
+	 * detected (depending on FE or BE context). You can also explicitly set the language/workspace id.
+	 *
+	 * @param Qom\SourceInterface $source The source (selector od join)
+	 * @param array $rows
+	 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
+	 * @param null|int $workspaceUid
+	 * @return array
+	 */
+	protected function doLanguageAndWorkspaceOverlay(Qom\SourceInterface $source, array $rows, \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings, $workspaceUid = null)
+	{
+		if ($source instanceof Qom\SelectorInterface) {
+			$tableName = $source->getSelectorName();
+		} elseif ($source instanceof Qom\JoinInterface) {
+			$tableName = $source->getRight()->getSelectorName();
+		} else {
+			// No proper source, so we do not have a table name here
+			// we cannot do an overlay and return the original rows instead.
+			return $rows;
+		}
+
+		$pageRepository = $this->getPageRepository();
+		if (is_object($GLOBALS['TSFE'])) {
+			if ($workspaceUid !== null) {
+				$pageRepository->versioningWorkspaceId = $workspaceUid;
+			}
+		} else {
+			if ($workspaceUid === null) {
+				$workspaceUid = $GLOBALS['BE_USER']->workspace;
+			}
+			$pageRepository->versioningWorkspaceId = $workspaceUid;
+		}
+
+		// Fetches the move-placeholder in case it is supported
+		// by the table and if there's only one row in the result set
+		// (applying this to all rows does not work, since the sorting
+		// order would be destroyed and possible limits not met anymore)
+		if (!empty($pageRepository->versioningWorkspaceId)
+			&& BackendUtility::isTableWorkspaceEnabled($tableName)
+			&& count($rows) === 1
+		) {
+			$movePlaceholder = $this->databaseHandle->exec_SELECTgetSingleRow(
+				$tableName . '.*',
+				$tableName,
+				't3ver_state=3 AND t3ver_wsid=' . $pageRepository->versioningWorkspaceId
+				. ' AND t3ver_move_id=' . $rows[0]['uid']
+			);
+			if (!empty($movePlaceholder)) {
+				$rows = array($movePlaceholder);
+			}
+		}
+
+		$overlaidRows = array();
+		foreach ($rows as $row) {
+			// If current row is a translation select its parent
+			if (isset($tableName) && isset($GLOBALS['TCA'][$tableName])
+				&& isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
+				&& isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
+				&& !isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerTable'])
+			) {
+				if (isset($row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']])
+					&& $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] > 0
+				) {
+					$row = $this->databaseHandle->exec_SELECTgetSingleRow(
+						$tableName . '.*',
+						$tableName,
+						$tableName . '.uid=' . (int)$row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] .
+						' AND ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '=0'
+					);
+				}
+			}
+			$pageRepository->versionOL($tableName, $row, true);
+			if ($tableName == 'pages') {
+				$row = $pageRepository->getPageOverlay($row, $querySettings->getLanguageUid());
+				if ($row === null || !is_array($row)) {
+					continue;
+				}
+
+				$l18nConfiguration = $row['l18n_cfg'];
+				if ($l18nConfiguration > 0) {
+					if (is_object($GLOBALS['TSFE'])) {
+						$sysLanguageUid = $GLOBALS['TSFE']->sys_language_uid;
+						if ($sysLanguageUid > 0) {
+							// Request the overlay record for the sys_language_uid:
+							$pageOverlay = $pageRepository->getPageOverlay($row['uid'], $sysLanguageUid);
+							if (empty($pageOverlay) &&
+								\TYPO3\CMS\Core\Utility\GeneralUtility::hideIfNotTranslated($l18nConfiguration)
+							) {
+								// Page is not available in default language.
+								continue;
+							}
+						}
+
+						if (!$sysLanguageUid &&
+							\TYPO3\CMS\Core\Utility\GeneralUtility::hideIfDefaultLanguage($l18nConfiguration)
+						) {
+							// Page is not available in default language.
+							continue;
+						}
+					}
+				}
+			} elseif (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
+				&& $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] !== ''
+				&& !isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerTable'])
+			) {
+				if (in_array($row[$GLOBALS['TCA'][$tableName]['ctrl']['languageField']], array(-1, 0))) {
+					$overlayMode = $querySettings->getLanguageOverlayMode();
+					if ($overlayMode !== 'hideNonTranslated' || preg_match('/sys_file_.*/is', $tableName)) {
+						$overlayMode = $querySettings->getLanguageMode() === 'strict' ? 'hideNonTranslated' : '';
+					}
+					$row = $pageRepository->getRecordOverlay($tableName, $row, $querySettings->getLanguageUid(), $overlayMode);
+				}
+			}
+			if ($row !== null && is_array($row)) {
+				$overlaidRows[] = $row;
+			}
+		}
+		return $overlaidRows;
+	}
+}
+
+?>
diff --git a/ext_localconf.php b/ext_localconf.php
index 4b4bdb1..1af573d 100644
--- a/ext_localconf.php
+++ b/ext_localconf.php
@@ -70,6 +70,8 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['proc
 // Xclasses
 $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['TYPO3\CMS\Core\Page\PageRenderer'] =
 	['className' => 'SGalinski\SgNews\Xclass\PageRenderer'];
+$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend'] =
+	['className' => 'SGalinski\SgNews\Xclass\Typo3DbBackend'];
 
 // add realurl configuration
 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('realurl')) {
-- 
GitLab