Skip to content

Commit

Permalink
[BUGFIX] Remove usage of sys_language table
Browse files Browse the repository at this point in the history
because this table was removed in TYPO3 12. Now we are using the first site for a mapping from language identifier to a language label

Related: #40
  • Loading branch information
einpraegsam committed Aug 7, 2023
1 parent 8020710 commit 7626b29
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Classes/Domain/DataProvider/LanguagesDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception as ExceptionDbalDriver;
use In2code\Lux\Domain\Repository\LanguageRepository;
use In2code\Lux\Domain\Repository\PagevisitRepository;
use In2code\Lux\Utility\LocalizationUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -47,10 +48,11 @@ public function prepareData(): void
protected function getLanguagesFromSystem(): array
{
$pagevisitRepository = GeneralUtility::makeInstance(PagevisitRepository::class);
$languageRepository = GeneralUtility::makeInstance(LanguageRepository::class);
$rows = $pagevisitRepository->getAllLanguages($this->filter);

foreach ($rows as &$row) {
$row['label'] = $row['title'] ?: 'Standard';
$row['label'] = $languageRepository->getLabelToLanguageIdentifier($row['language']);
$row['label'] = LocalizationUtility::translateByKey('dataprovider.languages.label', [$row['label']]);
}
return $rows;
Expand Down
4 changes: 3 additions & 1 deletion Classes/Domain/DataProvider/LanguagesNewsDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception as ExceptionDbalDriver;
use In2code\Lux\Domain\Repository\LanguageRepository;
use In2code\Lux\Domain\Repository\NewsvisitRepository;
use In2code\Lux\Utility\LocalizationUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -47,10 +48,11 @@ public function prepareData(): void
protected function getLanguagesFromSystem(): array
{
$newsvisitRepository = GeneralUtility::makeInstance(NewsvisitRepository::class);
$languageRepository = GeneralUtility::makeInstance(LanguageRepository::class);
$rows = $newsvisitRepository->getAllLanguages($this->filter);

foreach ($rows as &$row) {
$row['label'] = $row['title'] ?: 'Standard';
$row['label'] = $languageRepository->getLabelToLanguageIdentifier($row['language']);
$row['label'] = LocalizationUtility::translateByKey('dataprovider.languages.label', [$row['label']]);
}
return $rows;
Expand Down
41 changes: 41 additions & 0 deletions Classes/Domain/Repository/LanguageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);
namespace In2code\Lux\Domain\Repository;

use In2code\Lux\Domain\Service\SiteService;
use TYPO3\CMS\Core\SingletonInterface;

class LanguageRepository implements SingletonInterface
{
/**
* e.g.
* [
* 0 => 'English',
* 1 => 'Deutsch',
* ]
*
* @var array
*/
protected array $languages = [];

protected SiteService $siteService;

public function __construct(SiteService $siteService)
{
$this->siteService = $siteService;
}

public function getLabelToLanguageIdentifier(int $identifier): string
{
if (array_key_exists($identifier, $this->languages) === false) {
$languages = $this->siteService->getDefaultSite()->getLanguages();
foreach ($languages as $language) {
if ($language->getLanguageId() === $identifier) {
$this->languages[$identifier] = $language->getTitle();
}
}
}
return $this->languages[$identifier] ?? 'undefined';
}
}
7 changes: 3 additions & 4 deletions Classes/Domain/Repository/NewsvisitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ public function getAllDomains(FilterDto $filter): array
*/
public function getAllLanguages(FilterDto $filter): array
{
$connection = DatabaseUtility::getConnectionForTable('sys_language');
$sql = 'SELECT count(distinct nv.uid) as count, nv.language, l.title FROM ' . Newsvisit::TABLE_NAME . ' nv'
. ' left join sys_language l on l.uid = nv.language'
$connection = DatabaseUtility::getConnectionForTable(Newsvisit::TABLE_NAME);
$sql = 'SELECT count(distinct nv.uid) as count, nv.language FROM ' . Newsvisit::TABLE_NAME . ' nv'
. ' left join ' . Pagevisit::TABLE_NAME . ' pv on pv.uid = nv.pagevisit'
. ' left join ' . Visitor::TABLE_NAME . ' v on v.uid = nv.visitor'
. ' left join ' . Categoryscoring::TABLE_NAME . ' cs on v.uid = cs.visitor'
Expand All @@ -189,7 +188,7 @@ public function getAllLanguages(FilterDto $filter): array
. $this->extendWhereClauseWithFilterDomain($filter, 'pv')
. $this->extendWhereClauseWithFilterScoring($filter, 'v')
. $this->extendWhereClauseWithFilterCategoryScoring($filter, 'cs')
. ' group by nv.language, l.title order by count desc ';
. ' group by nv.language order by count desc';
return $connection->executeQuery($sql)->fetchAllAssociative();
}

Expand Down
7 changes: 3 additions & 4 deletions Classes/Domain/Repository/PagevisitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,15 @@ public function getAllDomains(FilterDto $filter): array
*/
public function getAllLanguages(FilterDto $filter): array
{
$connection = DatabaseUtility::getConnectionForTable('sys_language');
$sql = 'SELECT count(*) as count, pv.language, l.title FROM ' . Pagevisit::TABLE_NAME . ' pv'
. ' left join sys_language l on l.uid = pv.language'
$connection = DatabaseUtility::getConnectionForTable(Pagevisit::TABLE_NAME);
$sql = 'SELECT count(*) as count, pv.language FROM ' . Pagevisit::TABLE_NAME . ' pv'
. ' left join ' . Visitor::TABLE_NAME . ' v on v.uid = pv.visitor'
. ' left join ' . Categoryscoring::TABLE_NAME . ' cs on v.uid = cs.visitor'
. ' where ' . $this->extendWhereClauseWithFilterTime($filter, false, 'pv')
. $this->extendWhereClauseWithFilterDomain($filter, 'pv')
. $this->extendWhereClauseWithFilterScoring($filter, 'v')
. $this->extendWhereClauseWithFilterCategoryScoring($filter, 'cs')
. ' group by pv.language, l.title order by count desc ';
. ' group by pv.language order by count desc ';
return $connection->executeQuery($sql)->fetchAllAssociative();
}

Expand Down
5 changes: 5 additions & 0 deletions Classes/Domain/Service/SiteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function getLanguageCodeFromLanguageAndDomain(int $languageId, string $do
return '';
}

/**
* Return first Site as default site (ordered alphabetical)
*
* @return Site
*/
public function getDefaultSite(): Site
{
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
Expand Down

0 comments on commit 7626b29

Please sign in to comment.