Commit f17c7b9b authored by Fabio Stegmeyer's avatar Fabio Stegmeyer

[TASK] Copy and move visibility flags when copying or moving records

parent ebee2ee6
......@@ -57,11 +57,8 @@ class TceMainHook {
public function processCmdmap_postProcess(
$command, $table, $identity, $value, $dataHandler, $pasteUpdate, $pasteDatamap
) {
if ($table !== 'pages') {
return;
}
if ($command === 'copy') {
if ($command === 'copy' && $table === 'pages') {
$originalPageId = (int) $identity;
if ($originalPageId <= 0) {
return;
......@@ -80,6 +77,71 @@ class TceMainHook {
} elseif ($command === 'move') {
$this->copyLanguageVisibilityFlagsFromParentPage($identity);
}
if ($command === 'copy' && $table !== 'pages' && in_array(
$table, VisibilityService::getSupportedTables(), TRUE
)) {
$oldId = $identity;
$newId = $dataHandler->copyMappingArray[$table][$identity];
$languageRep = GeneralUtility::makeInstance(LanguageRepository::class);
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$availableLanguages = $languageRep->getLanguages();
$visibilityFlags = [];
foreach ($availableLanguages as $language) {
$lid = $language->getUid();
$visibilityFlag = $visibilityFlagRepository->getVisibilityFlag($table, $oldId, $lid);
if ($visibilityFlag !== FALSE && $visibilityFlag !== NULL) {
$visibilityFlags[$lid] = $visibilityFlag['flag'];
}
}
foreach ($visibilityFlags as $lid => $flag) {
$visibilityFlagRepository->setVisibilityFlag($flag, $table, $newId, $lid);
}
}
}
/**
* Changes the pid of the visibility flags of a moved record
*
* @param $table
* @param $uid
* @param $destPid
* @param $propArr
* @param $moveRec
* @param $resolvedPid
* @param $recordWasMoved
* @param $dataHandler
*/
public function moveRecord($table, $uid, $destPid, $propArr, $moveRec, $resolvedPid, $recordWasMoved, $dataHandler
) {
if ($table !== 'pages' && in_array(
$table, VisibilityService::getSupportedTables(), TRUE
)) {
$languageRep = GeneralUtility::makeInstance(LanguageRepository::class);
$visibilityFlagRepository = GeneralUtility::makeInstance(VisibilityFlagRepository::class);
$availableLanguages = $languageRep->getLanguages();
foreach ($availableLanguages as $language) {
$lid = $language->getUid();
$visibilityFlag = $visibilityFlagRepository->getVisibilityFlag($table, $uid, $lid);
if ($visibilityFlag !== FALSE && $visibilityFlag !== NULL) {
$flagUid = $visibilityFlag['uid'];
$visibilityFlagRepository->moveVisibilityFlag($flagUid, $resolvedPid);
}
}
}
}
/**
......@@ -130,6 +192,7 @@ class TceMainHook {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'tx_languagevisibility_visibility_flag'
);
$queryBuilder
->insert('tx_languagevisibility_visibility_flag')
->values(
......@@ -247,8 +310,8 @@ class TceMainHook {
// flush all caches
CacheManager::getInstance()->flushAllCaches();
//todo: uncomment
//GeneralUtility::makeInstance(VisibilityFlagRepository::class) ->deleteDefaultFlags();
GeneralUtility::makeInstance(VisibilityFlagRepository::class)
->deleteDefaultFlags();
// Flush TYPO3 Caching Framework caches
GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)
......
......@@ -29,6 +29,7 @@ use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\Languagevisibility\Manager\CacheManager;
use TYPO3\CMS\Backend\Utility\BackendUtility;
/**
* @author Fabio Stegmeyer <fabio.stegmeyer@sgalinski.de>
......@@ -58,6 +59,16 @@ class VisibilityFlagRepository {
self::DB_TABLE
);
if ($table === 'pages') {
$pid = $recordUid;
} else {
$record = BackendUtility::getRecord($table, $recordUid, $fields = 'pid');
if (!is_null($record)) {
$pid = $record['pid'];
}
}
if (!is_null($existingVisibilityFlag)) {
//flag already exists and only needs to changed
$uid = (int) $existingVisibilityFlag['uid'];
......@@ -74,16 +85,37 @@ class VisibilityFlagRepository {
->insert(self::DB_TABLE)
->values(
[
'pid' => $pid,
'flag' => $flag,
'record_table' => $table,
'record_uid' => $table . '_' . $recordUid,
'record_language_uid' => $lUid,
'tstamp' => time(),
'crdate' => time(),
'cruser_id' => $GLOBALS['BE_USER']->user['uid'],
]
)
->execute();
}
}
/**
* @param $uid
* @param $destPid
* @return void
*/
public function moveVisibilityFlag($uid, $destPid): void {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
self::DB_TABLE
);
$queryBuilder
->update(self::DB_TABLE)
->where($queryBuilder->expr()->eq('uid', $uid))
->set('pid', $destPid)
->execute();
}
/**
* Returns a complete flag row or NULL if none is found
*
......
......@@ -24,11 +24,11 @@ return [
'config' => [
'type' => 'group',
'internal_type' => 'db',
'allowed' => implode(',', VisibilityService::getSupportedTables()),
'allowed' => implode(',', VisibilityService::getSupportedTables()),
'foreign_table' => 'tt_content',
//'maxitems' => 1,
//'minitems' => 0,
//'size' => 1,
'maxitems' => 1,
'minitems' => 0,
'size' => 1,
'default' => 0,
]
],
......
......@@ -33,6 +33,9 @@ call_user_func(
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['extTablesInclusion-PostProcessing'][$extKey] =
\TYPO3\Languagevisibility\Hook\TcaHook::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['moveRecordClass'][$extKey] =
\TYPO3\Languagevisibility\Hook\TceMainHook::class;
// overriding option because this is done by languagevisibility and will not work if set
$GLOBALS['TYPO3_CONF_VARS']['FE']['hidePagesIfNotTranslatedByDefault'] = 0;
......
......@@ -38,6 +38,5 @@ CREATE TABLE tx_languagevisibility_visibility_flag (
record_uid text DEFAULT '' NOT NULL,
record_language_uid int(11) DEFAULT '0' NOT NULL,
flag text NOT NULL,
default_language_record_uid int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (uid)
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment