Skip to content

Commit

Permalink
[BUGFIX] Minor fixes in Helper class (#992)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
beatrycze-volk and sebastian-meyer authored Jul 21, 2023
1 parent f1451a4 commit 0869183
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions Classes/Common/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Frontend\Page\PageRepository;


/**
* Helper class for the 'dlf' extension
Expand Down Expand Up @@ -83,7 +88,7 @@ class Helper
*/
public static function addMessage($message, $title, $severity, $session = false, $queue = 'kitodo.default.flashMessages')
{
$flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$flashMessageQueue = $flashMessageService->getMessageQueueByIdentifier($queue);
$flashMessage = GeneralUtility::makeInstance(
\TYPO3\CMS\Core\Messaging\FlashMessage::class,
Expand Down Expand Up @@ -308,12 +313,12 @@ public static function encrypt($string)
self::log('No encryption key set in TYPO3 configuration', LOG_SEVERITY_ERROR);
return false;
}
// Generate random initialisation vector.
// Generate random initialization vector.
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::$cipherAlgorithm));
$key = openssl_digest($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], self::$hashAlgorithm, true);
// Encrypt data.
$encrypted = openssl_encrypt($string, self::$cipherAlgorithm, $key, OPENSSL_RAW_DATA, $iv);
// Merge initialisation vector and encrypted data.
// Merge initialization vector and encrypted data.
if ($encrypted !== false) {
$encrypted = base64_encode($iv . $encrypted);
}
Expand Down Expand Up @@ -499,7 +504,7 @@ public static function getDocumentStructures($pid = -1)
->where($where)
->execute();

$allStructures = $kitodoStructures->fetchAll();
$allStructures = $kitodoStructures->fetchAllAssociative();

// make lookup-table indexName -> uid
$allStructures = array_column($allStructures, 'indexName', 'uid');
Expand Down Expand Up @@ -613,7 +618,7 @@ public static function isValidHttpUrl($url)

/**
* Merges two arrays recursively and actually returns the modified array.
* @see \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule()
* @see ArrayUtility::mergeRecursiveWithOverrule()
*
* @access public
*
Expand All @@ -627,7 +632,7 @@ public static function isValidHttpUrl($url)
*/
public static function mergeRecursiveWithOverrule(array $original, array $overrule, $addKeys = true, $includeEmptyValues = true, $enableUnsetFeature = true)
{
\TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($original, $overrule, $addKeys, $includeEmptyValues, $enableUnsetFeature);
ArrayUtility::mergeRecursiveWithOverrule($original, $overrule, $addKeys, $includeEmptyValues, $enableUnsetFeature);
return $original;
}

Expand All @@ -642,7 +647,7 @@ public static function mergeRecursiveWithOverrule(array $original, array $overru
*/
public static function renderFlashMessages($queue = 'kitodo.default.flashMessages')
{
$flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$flashMessageQueue = $flashMessageService->getMessageQueueByIdentifier($queue);
$flashMessages = $flashMessageQueue->getAllMessagesAndFlush();
$content = GeneralUtility::makeInstance(\Kitodo\Dlf\Common\KitodoFlashMessageRenderer::class)
Expand Down Expand Up @@ -671,13 +676,14 @@ public static function translate($index_name, $table, $pid)
self::log('Invalid PID ' . $pid . ' for translation', LOG_SEVERITY_WARNING);
return $index_name;
}
/** @var \TYPO3\CMS\Frontend\Page\PageRepository $pageRepository */
$pageRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
/** @var PageRepository $pageRepository */
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);

$languageAspect = GeneralUtility::makeInstance(Context::class)->getAspect('language');
$languageContentId = $languageAspect->getContentId();

// Check if "index_name" is an UID.
if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($index_name)) {
if (MathUtility::canBeInterpretedAsInteger($index_name)) {
$index_name = self::getIndexNameFromUid($index_name, $table, $pid);
}
/* $labels already contains the translated content element, but with the index_name of the translated content element itself
Expand All @@ -702,42 +708,40 @@ public static function translate($index_name, $table, $pid)
->setMaxResults(1)
->execute();

$allResults = $result->fetchAll();
$row = $result->fetchAssociative();

if (count($allResults) == 1) {
if ($row) {
// Now we use the uid of the l18_parent to fetch the index_name of the translated content element.
$resArray = $allResults[0];

$result = $queryBuilder
->select($table . '.index_name AS index_name')
->from($table)
->where(
$queryBuilder->expr()->eq($table . '.pid', $pid),
$queryBuilder->expr()->eq($table . '.uid', $resArray['l18n_parent']),
$queryBuilder->expr()->eq($table . '.sys_language_uid', intval($languageAspect->getContentId())),
$queryBuilder->expr()->eq($table . '.uid', $row['l18n_parent']),
$queryBuilder->expr()->eq($table . '.sys_language_uid', intval($languageContentId)),
self::whereExpression($table, true)
)
->setMaxResults(1)
->execute();

$allResults = $result->fetchAll();
$row = $result->fetchAssociative();

if (count($allResults) == 1) {
if ($row) {
// If there is an translated content element, overwrite the received $index_name.
$index_name = $allResults[0]['index_name'];
$index_name = $row['index_name'];
}
}

// Check if we already got a translation.
if (empty($labels[$table][$pid][$languageAspect->getContentId()][$index_name])) {
if (empty($labels[$table][$pid][$languageContentId][$index_name])) {
// Check if this table is allowed for translation.
if (in_array($table, ['tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_structures'])) {
$additionalWhere = $queryBuilder->expr()->in($table . '.sys_language_uid', [-1, 0]);
if ($languageAspect->getContentId() > 0) {
if ($languageContentId > 0) {
$additionalWhere = $queryBuilder->expr()->andX(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->in($table . '.sys_language_uid', [-1, 0]),
$queryBuilder->expr()->eq($table . '.sys_language_uid', intval($languageAspect->getContentId()))
$queryBuilder->expr()->eq($table . '.sys_language_uid', intval($languageContentId))
),
$queryBuilder->expr()->eq($table . '.l18n_parent', 0)
);
Expand All @@ -758,11 +762,11 @@ public static function translate($index_name, $table, $pid)
if ($result->rowCount() > 0) {
while ($resArray = $result->fetchAssociative()) {
// Overlay localized labels if available.
if ($languageAspect->getContentId() > 0) {
$resArray = $pageRepository->getRecordOverlay($table, $resArray, $languageAspect->getContentId(), $languageAspect->getLegacyOverlayType());
if ($languageContentId > 0) {
$resArray = $pageRepository->getRecordOverlay($table, $resArray, $languageContentId, $languageAspect->getLegacyOverlayType());
}
if ($resArray) {
$labels[$table][$pid][$languageAspect->getContentId()][$resArray['index_name']] = $resArray['label'];
$labels[$table][$pid][$languageContentId][$resArray['index_name']] = $resArray['label'];
}
}
} else {
Expand All @@ -773,8 +777,8 @@ public static function translate($index_name, $table, $pid)
}
}

if (!empty($labels[$table][$pid][$languageAspect->getContentId()][$index_name])) {
return $labels[$table][$pid][$languageAspect->getContentId()][$index_name];
if (!empty($labels[$table][$pid][$languageContentId][$index_name])) {
return $labels[$table][$pid][$languageContentId][$index_name];
} else {
return $index_name;
}
Expand All @@ -798,8 +802,8 @@ public static function whereExpression($table, $showHidden = false)
if ($showHidden) {
$ignoreHide = 1;
}
/** @var \TYPO3\CMS\Frontend\Page\PageRepository $pageRepository */
$pageRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
/** @var PageRepository $pageRepository */
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);

$expression = $pageRepository->enableFields($table, $ignoreHide);
if (!empty($expression)) {
Expand Down

0 comments on commit 0869183

Please sign in to comment.