Skip to content
Snippets Groups Projects
Typo3DbBackend.php 6.7 KiB
Newer Older
<?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\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom;
use TYPO3\Languagevisibility\Service\FrontendServices;

/**
 * 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);
			}
		}

		$isLanguagevisibilityLoaded = ExtensionManagementUtility::isLoaded('languagevisibility');
		$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') {
				// Start of the patch
				// Applies the language visibility logic.
				if ($isLanguagevisibilityLoaded && is_object($GLOBALS['TSFE']))  {
					$sysLanguageUid = $GLOBALS['TSFE']->sys_language_uid;
					if (!FrontendServices::checkVisiblityForElement((int) $row['uid'], $tableName, $sysLanguageUid)) {
						// Page not visible
						continue;
					}
				}

				// End of the patch

				$row = $pageRepository->getPageOverlay($row, $querySettings->getLanguageUid());
				if ($row === null || !is_array($row)) {
					continue;
				}

				// Start of the patch
				$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;
						}
					}
				}
				// End of the patch

			} 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;
	}
}

?>