Skip to content

Commit

Permalink
[TASK] Merge Csv/AbstractListView into AbstractRegistrationListView
Browse files Browse the repository at this point in the history
Fixes #3697
  • Loading branch information
oliverklee committed Oct 4, 2024
1 parent 7a63612 commit 6e3af07
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 201 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Changed

- Merge `Csv/AbstractListView` into `Csv/AbstractRegistrationListView` (#3771)
- Convert the manual to the new PHP-based rendering (#3692, #3696)
- Use PHP 7.4 language features and native type declarations
(#3643, #3682, #3683, #3689, #3698, #3699, #3701, #3702, #3703, #3704, #3705,
Expand Down
190 changes: 0 additions & 190 deletions Classes/Csv/AbstractListView.php

This file was deleted.

155 changes: 154 additions & 1 deletion Classes/Csv/AbstractRegistrationListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,48 @@

namespace OliverKlee\Seminars\Csv;

use OliverKlee\Oelib\Configuration\ConfigurationRegistry;
use OliverKlee\Oelib\Interfaces\Configuration;
use OliverKlee\Seminars\BagBuilder\RegistrationBagBuilder;
use OliverKlee\Seminars\Hooks\HookProvider;
use OliverKlee\Seminars\Hooks\Interfaces\RegistrationListCsv;
use OliverKlee\Seminars\OldModel\LegacyRegistration;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* This class creates a CSV export of registrations.
*
* @internal
*/
abstract class AbstractRegistrationListView extends AbstractListView
abstract class AbstractRegistrationListView
{
/**
* @var non-empty-string
*/
protected const COLUMN_SEPARATOR = ';';

/**
* @var string
*/
protected const LINE_SEPARATOR = "\r\n";

/**
* @var int the depth of the recursion for the back-end pages
*/
protected const RECURSION_DEPTH = 250;

protected Configuration $configuration;

/**
* @var int<0, max>
*/
protected int $pageUid = 0;

protected ?LanguageService $translator = null;

/**
* @var non-empty-string
*/
Expand All @@ -27,6 +56,130 @@ abstract class AbstractRegistrationListView extends AbstractListView
*/
protected int $eventUid = 0;

public function __construct()
{
$this->configuration = ConfigurationRegistry::get('plugin.tx_seminars');
}

protected function getLanguageService(): ?LanguageService
{
$languageService = $GLOBALS['LANG'] ?? null;
\assert(($languageService instanceof LanguageService) || $languageService === null);

return $languageService;
}

protected function getBackEndUser(): ?BackendUserAuthentication
{
$backendUser = $GLOBALS['BE_USER'] ?? null;
\assert(($backendUser instanceof BackendUserAuthentication) || $backendUser === null);

return $backendUser;
}

/**
* Loads the language data and returns the corresponding translator instance.
*/
protected function getInitializedTranslator(): LanguageService
{
if ($this->translator instanceof LanguageService) {
return $this->translator;
}

if ($this->getLanguageService() instanceof LanguageService) {
$languageService = $this->getLanguageService();
} else {
$backEndUser = $this->getBackEndUser();
if ($backEndUser instanceof BackendUserAuthentication) {
$languageService = GeneralUtility::makeInstance(LanguageServiceFactory::class)
->createFromUserPreferences($backEndUser);
} else {
$languageService = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
}
}

$this->translator = $languageService;
$languageService->includeLLFile('EXT:core/Resources/Private/Language/locallang_general.xlf');
$languageService->includeLLFile('EXT:seminars/Resources/Private/Language/locallang_db.xlf');
$this->includeAdditionalLanguageFiles();

return $languageService;
}

/**
* Includes additional language files for $this->translator.
*
* This function is intended to be overwritten in subclasses.
*/
protected function includeAdditionalLanguageFiles(): void
{
}

/**
* @return int<0, max>
*/
protected function getPageUid(): int
{
return $this->pageUid;
}

/**
* Checks whether a non-zero page UID has been set.
*/
protected function hasPageUid(): bool
{
return $this->getPageUid() > 0;
}

/**
* Returns the name of the main table for this CSV export.
*
* @return non-empty-string
*/
protected function getTableName(): string
{
return $this->tableName;
}

/**
* Depending on the configuration, either returns the first line containing the specification of the separator character
* or just an empty string.
*/
protected function createCsvSeparatorLine(): string
{
if (!$this->configuration->getAsBoolean('addExcelSpecificSeparatorLineToCsv')) {
return '';
}

return 'sep=' . self::COLUMN_SEPARATOR . self::LINE_SEPARATOR;
}

/**
* Creates the heading line for a CSV event list.
*
* @return string header list, will not be empty if the CSV export has been configured correctly
*/
protected function createCsvHeading(): string
{
return \implode(self::COLUMN_SEPARATOR, $this->getLocalizedCsvHeadings());
}

/**
* Escapes a single field for CSV.
*/
protected function escapeFieldForCsv(string $fieldContent): string
{
if (\str_contains($fieldContent, '"')) {
$escapedFieldValue = '"' . \str_replace('"', '""', $fieldContent) . '"';
} elseif (\str_contains($fieldContent, ';') || \str_contains($fieldContent, "\n")) {
$escapedFieldValue = '"' . $fieldContent . '"';
} else {
$escapedFieldValue = $fieldContent;
}

return $escapedFieldValue;
}

/**
* Sets the page UID of the records to retrieve.
*
Expand Down
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,6 @@ parameters:
count: 1
path: Classes/Controller/EventRegistrationController.php

-
message: "#^Method OliverKlee\\\\Seminars\\\\Csv\\\\AbstractListView\\:\\:getBackEndUser\\(\\) should return TYPO3\\\\CMS\\\\Core\\\\Authentication\\\\BackendUserAuthentication\\|null but returns mixed\\.$#"
count: 1
path: Classes/Csv/AbstractListView.php

-
message: "#^Method OliverKlee\\\\Seminars\\\\Csv\\\\AbstractListView\\:\\:getLanguageService\\(\\) should return TYPO3\\\\CMS\\\\Core\\\\Localization\\\\LanguageService\\|null but returns mixed\\.$#"
count: 1
path: Classes/Csv/AbstractListView.php

-
message: "#^Method OliverKlee\\\\Seminars\\\\Csv\\\\AbstractRegistrationListView\\:\\:render\\(\\) should return string but returns mixed\\.$#"
count: 1
Expand Down

0 comments on commit 6e3af07

Please sign in to comment.