Commit ebee2ee6 authored by Fabio Stegmeyer's avatar Fabio Stegmeyer

[TASK] change relation to default record uid + language id (instead of overlay uid)

parent cdc20706
......@@ -299,9 +299,8 @@ abstract class Element {
$availablesLanguages = $languageRep->getLanguages();
foreach ($availablesLanguages as $language) {
$row = $this->getOverLayRecordForCertainLanguage($language->getUid());
$flagRow = $visibilityFlagRepository->getVisibilityFlag($table, $row['uid'], $language->getUid());
$flagRow = $visibilityFlagRepository->getVisibilityFlag($table, $this->getUid(), $language->getUid());
if ($flagRow === FALSE || $flagRow === NULL) {
$flag = '-';
......
......@@ -37,7 +37,6 @@ use TYPO3\Languagevisibility\Repository\LanguageRepository;
use TYPO3\Languagevisibility\Repository\VisibilityFlagRepository;
use TYPO3\Languagevisibility\Service\BackendServices;
use TYPO3\Languagevisibility\Service\VisibilityService;
use TYPO3\CMS\Frontend\Page\PageRepository;
/**
* Class TceMainHook
......@@ -102,11 +101,10 @@ class TceMainHook {
return;
}
$parentUid = $parentPage['uid'];
$parentPageUid = $parentPage['uid'];
$languageRep = GeneralUtility::makeInstance(LanguageRepository::class);
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);
$availableLanguages = $languageRep->getLanguages();
// clear the existing/automatically copied flags on the copied/inserted/moved page
......@@ -119,17 +117,8 @@ class TceMainHook {
$lid = $language->getUid();
$flagRow = [];
if ($lid > 0) {
$parentOverlay = $pageRepository->getPageOverlay($parentUid, $lid);
$parentOverlayUid = $parentOverlay['_PAGES_OVERLAY_UID'];
if ($parentOverlayUid > 0) {
$flagRow = $visibilityFlagRepository->getVisibilityFlag('pages', $parentOverlayUid, $lid);
}
} else {
$flagRow = $visibilityFlagRepository->getVisibilityFlag('pages', $parentUid, $lid);
}
// get visibility from parent page
$flagRow = $visibilityFlagRepository->getVisibilityFlag('pages', $parentPageUid, $lid);
if ($flagRow !== NULL && isset($flagRow['flag'])) {
$flag = $flagRow['flag'];
......@@ -137,52 +126,25 @@ class TceMainHook {
$flag = '-';
}
$recordUid = 0;
if ($lid > 0) {
$pageOverlay = $pageRepository->getPageOverlay($pageId, $lid);
// if there is no overlay, no need to go further
if (empty($pageOverlay)) {
break;
}
$parentOverlayUid = $pageOverlay['_PAGES_OVERLAY_UID'];
if ($parentOverlayUid > 0) {
$recordUid = $parentOverlayUid;
}
$defaultLanguageRecordUid = $pageId;
} else {
$recordUid = $pageId;
$defaultLanguageRecordUid = 0;
}
if ($recordUid > 0) {
// since the data mapper automatically copies the flag records,
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'tx_languagevisibility_visibility_flag'
);
$queryBuilder
->insert('tx_languagevisibility_visibility_flag')
->values(
[
'pid' => $pageId,
'flag' => $flag,
'record_uid' => 'pages_' . $recordUid,
'default_language_record_uid' => $defaultLanguageRecordUid,
'record_language_uid' => $lid,
'tstamp' => time(),
'crdate' => time(),
'cruser_id' => $GLOBALS['BE_USER']->user['uid'],
'record_table' => 'pages',
]
)
->execute();
}
// since the data mapper automatically copies the flag records,
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'tx_languagevisibility_visibility_flag'
);
$queryBuilder
->insert('tx_languagevisibility_visibility_flag')
->values(
[
'pid' => $pageId,
'flag' => $flag,
'record_uid' => 'pages_' . $pageId,
'record_language_uid' => $lid,
'tstamp' => time(),
'crdate' => time(),
'cruser_id' => $GLOBALS['BE_USER']->user['uid'],
'record_table' => 'pages',
]
)
->execute();
}
}
}
......@@ -266,15 +228,9 @@ class TceMainHook {
$languageId = (int) $incomingFieldArray['record_language_uid'];
}
$recordUid = -1;
if (isset($incomingFieldArray['record_uid'])) {
if ($languageId === 0) {
$recordUid = (int) str_replace($recordTable . '_', '', $incomingFieldArray['record_uid']);
} else {
$recordUid = (int) str_replace(
$recordTable . '_', '', $incomingFieldArray['default_language_record_uid']
);
}
$pid = -1;
if (isset($incomingFieldArray['pid'])) {
$pid = (int) $incomingFieldArray['pid'];
}
$visibilityFlag = '';
......@@ -284,15 +240,15 @@ class TceMainHook {
if ($isPage && $applyRecursive) {
$this->applyVisibilityFlagRecursive(
$recordUid, $languageId, $visibilityFlag, $enableLogging, $dataHandler
$pid, $languageId, $visibilityFlag, $enableLogging, $dataHandler
);
}
// flush all caches
CacheManager::getInstance()->flushAllCaches();
GeneralUtility::makeInstance(VisibilityFlagRepository::class)
->deleteDefaultFlags();
//todo: uncomment
//GeneralUtility::makeInstance(VisibilityFlagRepository::class) ->deleteDefaultFlags();
// Flush TYPO3 Caching Framework caches
GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)
......@@ -315,7 +271,7 @@ class TceMainHook {
*/
protected function applyVisibilityFlagRecursive(
int $pageId, int $languageId, string $visibilityFlag, bool $enableLogging, DataHandler $dataHandler
) {
): void {
if ($pageId <= 0 || $languageId < 0 || strlen($visibilityFlag) <= 0) {
return;
......@@ -333,7 +289,7 @@ class TceMainHook {
)
->andWhere(
$queryBuilder->expr()->eq(
'sys_language_uid', $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT)
'sys_language_uid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
)
)
->execute()->fetchAll();
......@@ -345,82 +301,58 @@ class TceMainHook {
foreach ($childPages as $childPage) {
$childPageUid = (int) $childPage['uid'];
$childPageLanguageId = (int) $childPage['sys_language_uid'];
//make sure it's the correct language
if ($childPageLanguageId === $languageId) {
$existingVisibilityFlag = $visibilityFlagRepository->getVisibilityFlag(
'pages', $childPageUid, $childPageLanguageId
);
if (!is_null($existingVisibilityFlag) && $existingVisibilityFlag['uid'] > 0) {
$existingVisibilityFlag = $visibilityFlagRepository->getVisibilityFlag(
'pages', $childPageUid, $languageId
);
//flag already exists and only needs to changed
$flagUid = $existingVisibilityFlag['uid'];
if (!is_null($existingVisibilityFlag) && $existingVisibilityFlag['uid'] > 0) {
//todo: userfunc for custom record title
if ($enableLogging) {
// Prepare the history for logging.
$dataHandler->compareFieldArrayWithCurrentAndUnset(
'tx_languagevisibility_visibility_flag', $flagUid, $existingVisibilityFlag
);
//flag already exists and only needs to changed
$flagUid = $existingVisibilityFlag['uid'];
$dataHandler->updateDB(
'tx_languagevisibility_visibility_flag', $flagUid, ['flag' => $visibilityFlag]
);
} else {
$visibilityFlagRepository->setVisibilityFlag(
$visibilityFlag, 'pages', $childPageUid, $languageId
);
}
if ($enableLogging) {
// Prepare the history for logging.
$dataHandler->compareFieldArrayWithCurrentAndUnset(
'tx_languagevisibility_visibility_flag', $flagUid, $existingVisibilityFlag
);
$dataHandler->updateDB(
'tx_languagevisibility_visibility_flag', $flagUid, ['flag' => $visibilityFlag]
);
} else {
//visibility flag record doesnt exist and needs to be created
$newUid = uniqid('NEW', TRUE);
if ($enableLogging) {
if ($languageId === 0) {
$defaultLanguageRecordUid = 0;
} else {
$defaultLanguageRecordUid = (int) $visibilityFlagRepository->getDefaultLanguageParentUid(
'pages', $childPageUid
);
}
$dataHandler->insertDB(
'tx_languagevisibility_visibility_flag', $newUid,
[
'tstamp' => time(),
'crdate' => time(),
'flag' => $visibilityFlag,
'pid' => $childPageUid,
'record_table' => 'pages',
'record_uid' => 'pages_' . $childPageUid,
'record_language_uid' => $languageId,
'default_language_record_uid' => $defaultLanguageRecordUid,
'cruser_id' => $GLOBALS['BE_USER']->user['uid'],
]
);
} else {
$visibilityFlagRepository->setVisibilityFlag(
$visibilityFlag, 'pages', $childPageUid, $languageId
);
}
$visibilityFlagRepository->setVisibilityFlag(
$visibilityFlag, 'pages', $childPageUid, $languageId
);
}
}
//if the language is not default, we new to fetch the subpages by the default language parent
if ($languageId === 0) {
$recordUid = $childPageUid;
} else {
$recordUid = $visibilityFlagRepository->getDefaultLanguageParentUid('pages', $childPageUid);
//visibility flag record doesnt exist and needs to be created
$newUid = uniqid('NEW', TRUE);
if ($enableLogging) {
$dataHandler->insertDB(
'tx_languagevisibility_visibility_flag', $newUid,
[
'tstamp' => time(),
'crdate' => time(),
'flag' => $visibilityFlag,
'pid' => $childPageUid,
'record_table' => 'pages',
'record_uid' => 'pages_' . $childPageUid,
'record_language_uid' => $languageId,
'cruser_id' => $GLOBALS['BE_USER']->user['uid'],
]
);
} else {
$visibilityFlagRepository->setVisibilityFlag(
$visibilityFlag, 'pages', $childPageUid, $languageId
);
}
}
$this->applyVisibilityFlagRecursive(
$recordUid, $languageId, $visibilityFlag, $enableLogging, $dataHandler
$childPageUid, $languageId, $visibilityFlag, $enableLogging, $dataHandler
);
}
}
......@@ -467,14 +399,6 @@ class TceMainHook {
}
}
/**
* @param DataHandler $dataHandler
*/
public function processCmdmap_afterFinish(DataHandler $dataHandler) {
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$visibilityFlagRepository->fixAllLanguageParentUids();
}
/**
* Applies the given language data recursive.
*
......
......@@ -41,54 +41,6 @@ class VisibilityFlagRepository {
*/
protected static $instance;
/**
* Returns a complete flag row or NULL if none is found, fetched by default language element
*
* @param $table
* @param $recordUid
* @return array|null
*/
public function getVisibilityFlagByLanguageParent($table, $recordUid, $lUid): ?array {
$cacheManager = CacheManager::getInstance();
$cacheData = $cacheManager->get('visibilityFlagRowCache');
$isCacheEnabled = CacheManager::isCacheEnabled();
$cacheKey = $table . '_' . $recordUid . '_' . $lUid;
if (!$isCacheEnabled || !isset($cacheData[$cacheKey])) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
self::DB_TABLE
);
$row = $queryBuilder->select('*')
->from(self::DB_TABLE)
->where(
$queryBuilder->expr()->eq(
'default_language_record_uid', $queryBuilder->createNamedParameter($recordUid, \PDO::PARAM_INT)
)
)
->andWhere(
$queryBuilder->expr()->eq(
'record_table', $queryBuilder->createNamedParameter($table, \PDO::PARAM_STR)
)
)
->andWhere(
$queryBuilder->expr()->eq(
'record_language_uid', $queryBuilder->createNamedParameter($lUid, \PDO::PARAM_INT)
)
)
->execute()->fetch();
if ($row) {
$cacheData[$cacheKey] = $row;
$cacheManager->set('visibilityFlagRowCache', $cacheData);
}
}
return $cacheData[$cacheKey];
}
/**
* Inserts or updates a flag for a given element
*
......@@ -118,8 +70,6 @@ class VisibilityFlagRepository {
} else {
//flag doesnt exist and needs to be created
$languageParentUid = (int) $this->getDefaultLanguageParentUid($table, $recordUid);
$queryBuilder
->insert(self::DB_TABLE)
->values(
......@@ -127,7 +77,6 @@ class VisibilityFlagRepository {
'flag' => $flag,
'record_table' => $table,
'record_uid' => $table . '_' . $recordUid,
'default_language_record_uid' => $languageParentUid,
'record_language_uid' => $lUid,
]
)
......@@ -187,41 +136,6 @@ class VisibilityFlagRepository {
return $cacheData[$cacheKey];
}
/**
* Returns the uid of the default language parent row for a given element
*
* @param $table
* @param $uid
* @return mixed
*/
public function getDefaultLanguageParentUid($table, $uid) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
$table
);
$queryBuilder->getRestrictions()
->removeAll();
$row = $queryBuilder->select($GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'])
->from($table)
->where(
$queryBuilder->expr()->eq(
'uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
)
)
->execute()->fetch();
if ($row['sys_language_uid'] > 0) {
$uid = $row['uid'];
$defaultLanguageParentUid = $this->getDefaultLanguageParentUid($table, $uid);
} else {
$defaultLanguageParentUid = $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']];
}
return $defaultLanguageParentUid;
}
/**
* Return the visibility flag
*
......@@ -238,16 +152,6 @@ class VisibilityFlagRepository {
->removeAll()
->add(GeneralUtility::makeInstance(DeletedRestriction::class));
if ($lid > 0) {
$uidWhere = $queryBuilder->expr()->eq(
'default_language_record_uid', $queryBuilder->createNamedParameter($recordUid, \PDO::PARAM_INT)
);
} else {
$uidWhere = $queryBuilder->expr()->eq(
'record_uid', $queryBuilder->createNamedParameter($table . '_' . $recordUid, \PDO::PARAM_STR)
);
}
$result = $queryBuilder
->select('flag')
->from(self::DB_TABLE)
......@@ -261,7 +165,11 @@ class VisibilityFlagRepository {
'record_language_uid', $queryBuilder->createNamedParameter($lid, \PDO::PARAM_INT)
)
)
->andWhere($uidWhere)
->andWhere(
$queryBuilder->expr()->eq(
'record_uid', $queryBuilder->createNamedParameter($table . '_' . $recordUid, \PDO::PARAM_STR)
)
)
->setMaxResults(1)
->execute()->fetch();
......@@ -271,119 +179,6 @@ class VisibilityFlagRepository {
return '';
}
/**
* Returns the flag string
*
* @param string $table
* @param int $recordUid
* @return string|NULL
*/
public function getFlagByTableAndRecordUid($table, $recordUid): ?string {
$cacheManager = CacheManager::getInstance();
$cacheData = $cacheManager->get('visibilityFlagCache');
$isCacheEnabled = CacheManager::isCacheEnabled();
$cacheKey = $table . '_' . $recordUid;
if (!$isCacheEnabled || !isset($cacheData[$cacheKey])) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
self::DB_TABLE
);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$row = $queryBuilder->select('flag')
->from(self::DB_TABLE)
->where(
$queryBuilder->expr()->eq(
'record_uid', $queryBuilder->createNamedParameter($table . '_' . $recordUid, \PDO::PARAM_STR)
),
$queryBuilder->expr()->eq(
'record_table', $queryBuilder->createNamedParameter($table, \PDO::PARAM_STR)
)
)
->execute()->fetch();
if ($row) {
$cacheData[$cacheKey] = $row['flag'];
$cacheManager->set('visibilityFlagCache', $cacheData);
}
}
return $cacheData[$cacheKey];
}
/**
* Returns the whole flag record
*
* @param string $table
* @param int $recordUid
* @return array|NULL
*/
public function getVisibilityFlagRecordByTableAndRecordUid($table, $recordUid): ?array {
$cacheManager = CacheManager::getInstance();
$cacheData = $cacheManager->get('visibilityFlagCache');
$isCacheEnabled = CacheManager::isCacheEnabled();
$cacheKey = $table . '_' . $recordUid;
if (!$isCacheEnabled || !isset($cacheData[$cacheKey])) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
self::DB_TABLE
);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$row = $queryBuilder->select('*')
->from(self::DB_TABLE)
->where(
$queryBuilder->expr()->eq(
'record_uid', $queryBuilder->createNamedParameter($table . '_' . $recordUid, \PDO::PARAM_STR)
),
$queryBuilder->expr()->eq(
'record_table', $queryBuilder->createNamedParameter($table, \PDO::PARAM_STR)
)
)
->execute()->fetch();
if ($row) {
$cacheData[$cacheKey] = $row;
$cacheManager->set('visibilityFlagCache', $cacheData);
}
}
return $cacheData[$cacheKey];
}
/**
* Returns the uid the translation for a default language page
*
* @param $pageId
* @param $lid
* @return mixed
*/
public function getUidOfPageOverlay($pageId, $lid) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'pages'
);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
return $queryBuilder->select('uid')
->from('pages')
->where(
$queryBuilder->expr()->eq(
'pid', $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
)
)
->AndWhere(
$queryBuilder->expr()->eq(
'sys_language_uid', $queryBuilder->createNamedParameter($lid, \PDO::PARAM_INT)
)
)
->execute()->fetch();
}
/**
* Removes the flags for given table and pid.
*
......@@ -412,47 +207,6 @@ class VisibilityFlagRepository {
->execute();
}
/**
* Iterates over all flag and fixes the language parent uid (is wrong when copied)
*
* @return void
*/
public function fixAllLanguageParentUids(): void {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
self::DB_TABLE
);
$rows = $queryBuilder
->select('*')
->from(self::DB_TABLE)
->execute()->fetchAll();
if (count($rows) > 0) {
foreach ($rows as $row) {
$uid = $row['uid'];
$table = $row['record_table'];
$recordUid = str_replace($table . '_', '', $row['record_uid']);
$currentLanguageParentUid = $row['default_language_record_uid'];
$correctLanguageParentUid = $this->getDefaultLanguageParentUid($table, $recordUid);
if ($correctLanguageParentUid > 0 && $correctLanguageParentUid !== $currentLanguageParentUid) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
self::DB_TABLE
);
$queryBuilder
->update(self::DB_TABLE)
->where($queryBuilder->expr()->eq('uid', $uid))
->set('default_language_record_uid', $correctLan