Commit 261f9205 authored by Fabio Stegmeyer's avatar Fabio Stegmeyer

[TASK] Remove and replace language repository and Element/Language

parent 4b527a10
......@@ -27,13 +27,15 @@ namespace TYPO3\Languagevisibility\Element;
***************************************************************/
use ReflectionClass;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\Languagevisibility\Exception\InvalidRowException;
use TYPO3\Languagevisibility\Manager\CacheManager;
use TYPO3\Languagevisibility\Repository\LanguageRepository;
use TYPO3\Languagevisibility\Repository\VisibilityFlagRepository;
/**
......@@ -292,14 +294,29 @@ abstract class Element {
$table = $this->getTable();
$languageRep = GeneralUtility::makeInstance(LanguageRepository::class);
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$availablesLanguages = $languageRep->getLanguages();
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
if ($table === 'pages') {
$pid = $this->getUid();
} else {
$pid = $this->getPid();
}
try {
$rootLine = BackendUtility::BEgetRootLine($pid);
$site = $siteFinder->getSiteByPageId($pid, $rootLine);
} catch (\Exception $e) {
return [];
}
$availablesLanguages = $site->getAllLanguages();
foreach ($availablesLanguages as $language) {
$flagRow = $visibilityFlagRepository->getVisibilityFlag($table, $this->getUid(), $language->getUid());
$flagRow = $visibilityFlagRepository->getVisibilityFlag(
$table, $this->getUid(), $language->getLanguageId()
);
if ($flagRow === FALSE || $flagRow === NULL) {
$flag = '-';
......@@ -307,7 +324,7 @@ abstract class Element {
$flag = $flagRow['flag'];
}
$globalVisibilitySettings[$language->getUid()] = $flag;
$globalVisibilitySettings[$language->getLanguageId()] = $flag;
}
return $globalVisibilitySettings;
......@@ -316,11 +333,11 @@ abstract class Element {
/**
* Receive relevant fallbackOrder
*
* @param Language $language
* @param SiteLanguage $language
* @return array
*/
public function getFallbackOrder(Language $language): array {
return $language->getFallbackOrder($this);
public function getFallbackOrder(SiteLanguage $language): array {
return $language->getFallbackLanguageIds();
}
/**
......@@ -362,11 +379,11 @@ abstract class Element {
/**
* Compare element-language and foreign language.
*
* @param Language $language
* @param SiteLanguage $language
* @return bool
*/
public function languageEquals(Language $language): bool {
return (int) $this->row['sys_language_uid'] === $language->getUid();
public function languageEquals(SiteLanguage $language): bool {
return (int) $this->row['sys_language_uid'] === $language->getLanguageId();
}
/**
......
......@@ -29,6 +29,7 @@ namespace TYPO3\Languagevisibility\Element;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\Languagevisibility\Manager\CacheManager;
use TYPO3\Languagevisibility\Service\FrontendServices;
......@@ -111,11 +112,11 @@ class ElementFactory {
* configured or is the first element of the rootline
*
* @param Element $element
* @param Language $language
* @param SiteLanguage $language
* @return array $elements (collection of tx_languagevisibility_element)
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
public function getParentElementsFromElement(Element $element, $language) {
public function getParentElementsFromElement(Element $element, SiteLanguage $language) {
$parentPage = $this->getElementForTable('pages', $element->getRow());
return $this->getParentElementsFromElement($parentPage, $language);
}
......
<?php
namespace TYPO3\Languagevisibility\Element;
/***************************************************************
* Copyright notice
*
* (c) 2007 AOE media (dev@aoemedia.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\Backend\Utility\IconUtility;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
*
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @coauthor Tolleiv Nietsch <nietsch@aoemedia.de>
* @coauthor Timo Schmidt <schmidt@aoemedia.de>
*/
class Language {
/**
* @var array
*/
private $row;
/**
* @var array
*/
protected static $flagCache;
/**
* Holds the exploded fallBackOrderArray
*
* @var array
*/
protected $defaultFallBackOrderArray;
/**
* Holds the exploded elementFallBackOrderArray
*
* @var array
*/
protected $elementFallBackOrderArray;
/**
* @var string holds the lg_iso_2 isocode
*/
protected $lg_iso_2;
/**
* @param array $row
*/
public function setData(array $row) {
$this->row = $row;
}
/**
* Returns the fallback order of this language as array.
*
* @param Element $contextElement
* @return array
*/
public function getFallbackOrder(Element $contextElement): array {
// determine and explode only once
if (!isset($this->defaultFallBackOrderArray)) {
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
if ($contextElement->getTable() === 'pages') {
$pid = $contextElement->getUid();
} else {
$pid = $contextElement->getPid();
}
try {
$site = $siteFinder->getSiteByPageId($pid);
} catch (\Exception $e) {
return [];
}
$siteLanguage = $site->getLanguageById($this->getUid());
$fallbackLanguageIds = $siteLanguage->getFallbackLanguageIds();
$this->defaultFallBackOrderArray = $fallbackLanguageIds;
}
return $this->triggerFallbackHooks('getFallbackOrder', $this->defaultFallBackOrderArray, $contextElement);
}
/**
* Returns the fallback order for this language for elements
*
* @param Element $contextElement
* @return array
*/
public function getFallbackOrderElement(Element $contextElement): array {
// determine and explode only once
if (!isset($this->elementFallBackOrderArray)) {
$this->elementFallBackOrderArray = $this->getFallbackOrder($contextElement);
}
return $this->triggerFallbackHooks(
'getFallbackOrderElement', $this->elementFallBackOrderArray, $contextElement
);
}
/**
*
* @param string $key
* @param array $fallbackorder
* @param Element $contextElement
* @return array
*/
protected function triggerFallbackHooks($key, $fallbackorder, Element $contextElement): array {
$result = [
'priority' => 10,
'fallbackorder' => $fallbackorder,
];
$fallback = $result;
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['languagevisibility']['$key'])) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['languagevisibility'][$key] as $classRef) {
$hookObj = GeneralUtility::makeInstance($classRef);
if (method_exists($hookObj, $key)) {
$result = $hookObj->$key($this, $fallback, $contextElement);
if ($result['priority'] > $fallback['priority']) {
$fallback = $result;
}
}
}
}
return $fallback['fallbackorder'];
}
/**
* Method to read the defaultVisibility setting of pages.
*
* @param Element $contextElement
* @return string
*/
public function getDefaultVisibilityForPage(Element $contextElement): string {
return $this->triggerDefaultVisibilityHooks(
'getDefaultVisibilityForPage', $this->row['tx_languagevisibility_defaultvisibility'] ?? '', $contextElement
);
}
/**
* Method to read the defaultVisibility for elements
*
* @param Element $contextElement
* @return string
*/
public function getDefaultVisibilityForElement(Element $contextElement): string {
return $this->triggerDefaultVisibilityHooks(
'getDefaultVisibilityForElement', $this->row['tx_languagevisibility_defaultvisibilityel'] ?? '',
$contextElement
);
}
/**
* @param string $key
* @param string $visibilityDefault
* @param Element $contextElement
* @return string
*/
protected function triggerDefaultVisibilityHooks(string $key, string $visibilityDefault, Element $contextElement
): string {
$result = [
'priority' => 10,
'visibility' => $visibilityDefault,
];
$visibility = $result;
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['languagevisibility'][$key])) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['languagevisibility'][$key] as $classRef) {
$hookObj = GeneralUtility::makeInstance($classRef);
if (method_exists($hookObj, $key)) {
$result = $hookObj->$key($this, $visibility, $contextElement);
if ($result['priority'] > $visibility['priority']) {
$visibility = $result;
}
}
}
}
return $visibility['visibility'];
}
/**
* Method to get the primary key of the language record.
*
* @return int
*/
public function getUid(): int {
return (int) $this->row['uid'];
}
/**
* Method to determine the lg_iso_2 code from the static languages record.
*
* @return string
*/
public function getIsoCode(): string {
if (!isset($this->lg_iso_2)) {
// Finding the ISO code:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('static_languages');
$row = $queryBuilder->select('lg_iso_2')
->from('static_languages')
->where(
$queryBuilder->expr()->eq(
'uid', $queryBuilder->createNamedParameter($this->row['static_lang_isocode'], \PDO::PARAM_INT)
)
)
->execute()->fetch();
$this->lg_iso_2 = $row['lg_iso_2'];
}
return $this->lg_iso_2;
}
/**
* Returns the title of the language.
*
* @param int $pidForDefault
* @return string
*/
public function getTitle(int $pidForDefault = 0): string {
if ($this->getUid() === 0) {
if ($pidForDefault === 0) {
$pidForDefault = $this->_guessCurrentPid();
}
$sharedTSconfig['properties'] = BackendUtility::getPagesTSconfig($pidForDefault)['mod.']['SHARED.'] ?? [];
if ($sharedTSconfig['properties']['defaultLanguageLabel'] !== '' &&
$sharedTSconfig['properties']['defaultLanguageLabel'] !== NULL
) {
if (GeneralUtility::isFirstPartOfStr($sharedTSconfig['properties']['defaultLanguageLabel'], 'LLL:')) {
return (string) LocalizationUtility::translate(
$sharedTSconfig['properties']['defaultLanguageLabel']
);
}
return (string) $sharedTSconfig['properties']['defaultLanguageLabel'];
}
return 'Default';
}
return (string) $this->row['title'];
}
/**
* @return int
*/
protected function _guessCurrentPid(): int {
return (int) GeneralUtility::_GP('id');
}
/**
* @param int $pidForDefault Optional the pid of the page. This can be used to get the correct flag for default language (which is set in tsconfig)
*
* @return string
*/
public function getFlagImg(int $pidForDefault = 0): string {
$cache_key = 'pid:' . $pidForDefault . 'uid:' . $this->getUid();
if (!isset(self::$flagCache[$cache_key])) {
/** @var IconFactory $iconFactory */
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
self::$flagCache[$cache_key] = $iconFactory->getIcon(
$this->getFlagName($pidForDefault), Icon::SIZE_SMALL
)->render();
}
return self::$flagCache[$cache_key];
}
/**
* @param int $pidForDefault
* @return string
*/
protected function getFlagName(int $pidForDefault = 0): string {
if ($this->getUid() === 0) {
$sharedTSconfig['properties'] = BackendUtility::getPagesTSconfig($pidForDefault)['mod.']['SHARED.'] ?? [];
$flag = $sharedTSconfig['properties']['defaultLanguage.']['flag'];
} else {
$flag = $this->row['flag'];
}
return 'flags-' . $flag;
}
/**
* @param int $pidForDefault Optional the pid of the page. This can be used to get the correct flagpath for default language (which is set in tsconfig)
* @param string $BACK_PATH
* @return string|null
*/
public function getFlagImgPath(int $pidForDefault = 0, string $BACK_PATH = ''): ?string {
$flagAbsPath = GeneralUtility::getFileAbsFileName(
$GLOBALS['TCA']['sys_language']['columns']['flag']['config']['fileFolder']
);
$flagIconPath = $BACK_PATH . '../' . substr($flagAbsPath, strlen(Environment::getPublicPath() . '/'));
if ($this->getUid() === 0) {
if ($pidForDefault === 0) {
$pidForDefault = $this->_guessCurrentPid();
}
$sharedTSconfig['properties'] = BackendUtility::getPagesTSconfig($pidForDefault)['mod.']['SHARED.'] ?? [];
$path = $sharedTSconfig['properties']['defaultLanguageFlag'] !== '' && @is_file(
$flagAbsPath . $sharedTSconfig['properties']['defaultLanguageFlag']
) ? $flagIconPath . $sharedTSconfig['properties']['defaultLanguageFlag'] : NULL;
} else {
$path = $flagIconPath . $this->row['flag'];
}
return $path;
}
/**
* checks if the given languageid is part of the fallback of this language
* (used for permission options in the backend)
*
* @param int uid
* @param Element $el
* @return bool
*/
public function isLanguageUidInFallbackOrder(int $uid, Element $el): bool {
$fallbacks = $this->getFallbackOrder($el);
return in_array($uid, $fallbacks, TRUE);
}
}
......@@ -27,6 +27,7 @@ namespace TYPO3\Languagevisibility\Element;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -101,10 +102,10 @@ class RecordElement extends Element {
*
* (non-PHPdoc)
* @see classes/tx_languagevisibility_element#getFallbackOrder($language)
* @param Language $language
* @param SiteLanguage $language
* @return array
*/
public function getFallbackOrder(Language $language): array {
return $language->getFallbackOrderElement($this);
public function getFallbackOrder(SiteLanguage $language): array {
return $language->getFallbackLanguageIds();
}
}
......@@ -93,7 +93,7 @@ class PageHook implements PageRepositoryGetPageOverlayHookInterface, PageReposit
}
// call service to know if element is visible and which overlay language to use
$overlayLanguage = FrontendServices::getOverlayLanguageIdForElementRecord($row, 'pages', $lUid);
$overlayLanguage = FrontendServices::getOverlayLanguageIdForElementRecord($row, 'pages', $languageUid);
if ($overlayLanguage === FALSE) {
if ($pageInputIsArray) {
$pageInput['_NOTVISIBLE'] = TRUE;
......
......@@ -30,10 +30,11 @@ use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\Languagevisibility\Element\ElementFactory;
use TYPO3\Languagevisibility\Manager\CacheManager;
use TYPO3\Languagevisibility\Repository\LanguageRepository;
use TYPO3\Languagevisibility\Repository\VisibilityFlagRepository;
use TYPO3\Languagevisibility\Service\BackendServices;
use TYPO3\Languagevisibility\Service\VisibilityService;
......@@ -85,14 +86,29 @@ class TceMainHook {
$oldId = $identity;
$newId = $dataHandler->copyMappingArray[$table][$identity];
$languageRep = GeneralUtility::makeInstance(LanguageRepository::class);
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$availableLanguages = $languageRep->getLanguages();
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
$originalPageId = (int) $identity;
if ($originalPageId <= 0) {
return;
}
$newPageId = (int) $dataHandler->copyMappingArray['pages'][$originalPageId];
try {
$site = $siteFinder->getSiteByPageId($newPageId);
} catch (\Exception $e) {
return FALSE;
}
$availableLanguages = $site->getAllLanguages();
$visibilityFlags = [];
foreach ($availableLanguages as $language) {
$lid = $language->getUid();
$lid = $language->getLanguageId();
$visibilityFlag = $visibilityFlagRepository->getVisibilityFlag($table, $oldId, $lid);
......@@ -125,13 +141,27 @@ class TceMainHook {
$table, VisibilityService::getSupportedTables(), TRUE
)) {
$languageRep = GeneralUtility::makeInstance(LanguageRepository::class);
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$availableLanguages = $languageRep->getLanguages();
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
if ($table === 'pages') {
$pid = $uid;
}else{
$pid = $destPid;
}
try {
$site = $siteFinder->getSiteByPageId($pid);
} catch (\Exception $e) {
return;
}
$availableLanguages = $site->getAllLanguages();
foreach ($availableLanguages as $language) {
$lid = $language->getUid();
$lid = $language->getLanguageId();
$visibilityFlag = $visibilityFlagRepository->getVisibilityFlag($table, $uid, $lid);
......@@ -165,11 +195,18 @@ class TceMainHook {
$parentPageUid = $parentPage['uid'];
$languageRep = GeneralUtility::makeInstance(LanguageRepository::class);
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$availableLanguages = $languageRep->getLanguages();
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
try {
$site = $siteFinder->getSiteByPageId($pageId, []);
} catch (\Exception $e) {
return;
}
$availableLanguages = $site->getAllLanguages();
// clear the existing/automatically copied flags on the copied/inserted/moved page
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$visibilityFlagRepository->removeFlagsByTableAndPid('pages', $pageId);
if ($availableLanguages && count($availableLanguages) > 0) {
......@@ -177,7 +214,7 @@ class TceMainHook {
// collect the flags of the parent page and its overlay
foreach ($availableLanguages as $language) {
$lid = $language->getUid();
$lid = $language->getLanguageId();
// get visibility from parent page
$flagRow = $visibilityFlagRepository->getVisibilityFlag('pages', $parentPageUid, $lid);
......
<?php
namespace TYPO3\Languagevisibility\Repository;
/***************************************************************
* Copyright notice
*
* (c) 2007 AOE media (dev@aoemedia.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