From 3b5f1b28ecc9c351e4af5a8b68d0a82a34229cee Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Fri, 30 Aug 2024 11:41:14 +0200 Subject: [PATCH] Make leads 3 compatible with Haste 4 --- composer.json | 3 +- phpstan.neon | 1 + .../DataContainer/LeadLabelListener.php | 14 ++++++-- src/Export/AbstractExporter.php | 34 ++++++++++++++----- src/Export/PhpSpreadsheetExporter.php | 2 +- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 2f4b005..c62d9e1 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "require": { "php": "^8.1", "contao/core-bundle": "^4.13 || ^5.0", - "codefog/contao-haste": "^5.0", + "codefog/contao-haste": "^4.25 || ^5.0", "menatwork/contao-multicolumnwizard-bundle": "^3.6", "phpoffice/phpspreadsheet": "^1.26 || ^2.0", "doctrine/dbal": "^3.0", @@ -72,6 +72,7 @@ "config": { "allow-plugins": { "contao-components/installer": false, + "contao-community-alliance/composer-plugin": false, "php-http/discovery": false, "contao/manager-plugin": false, "terminal42/contao-build-tools": true diff --git a/phpstan.neon b/phpstan.neon index bb824f6..9a3fe31 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,3 +1,4 @@ parameters: ignoreErrors: - '#no value type specified in iterable type array#' + - '#unknown class Haste\\Util\\StringUtil#' diff --git a/src/EventListener/DataContainer/LeadLabelListener.php b/src/EventListener/DataContainer/LeadLabelListener.php index 44ded7b..6c98c9a 100644 --- a/src/EventListener/DataContainer/LeadLabelListener.php +++ b/src/EventListener/DataContainer/LeadLabelListener.php @@ -16,7 +16,7 @@ class LeadLabelListener { public function __construct( private readonly Connection $connection, - private readonly StringParser $stringParser, + private readonly StringParser|null $stringParser = null, ) { } @@ -48,10 +48,18 @@ public function __invoke(array $row, string $label): string $values = $this->connection->fetchAllKeyValue('SELECT name, value FROM tl_lead_data WHERE pid=?', [$row['id']]); foreach ($values as $name => $value) { - $this->stringParser->flatten(StringUtil::deserialize($value), $name, $tokens); + if ($this->stringParser) { + $this->stringParser->flatten(StringUtil::deserialize($value), $name, $tokens); + } else { + \Haste\Util\StringUtil::flatten(StringUtil::deserialize($value), $name, $tokens); + } } - return $this->stringParser->recursiveReplaceTokensAndTags($lead['leadLabel'], $tokens); + if ($this->stringParser) { + return $this->stringParser->recursiveReplaceTokensAndTags($lead['leadLabel'], $tokens); + } + + return \Haste\Util\StringUtil::recursiveReplaceTokensAndTags($lead['leadLabel'], $tokens); } private function formatToken(string $title, int|string $value): string diff --git a/src/Export/AbstractExporter.php b/src/Export/AbstractExporter.php index 12804e0..19263ea 100644 --- a/src/Export/AbstractExporter.php +++ b/src/Export/AbstractExporter.php @@ -34,7 +34,7 @@ public function __construct( private readonly ServiceLocator $formatters, private readonly Connection $connection, private readonly TranslatorInterface $translator, - private readonly StringParser $parser, + private readonly StringParser|null $parser, private readonly ExpressionLanguage|null $expressionLanguage = null, ) { } @@ -308,9 +308,15 @@ protected function getColumns(): array $this->columns = []; foreach (StringUtil::deserialize($this->config['tokenFields'], true) as $config) { + if ($this->parser) { + $value = fn ($lead) => $this->parser->recursiveReplaceTokensAndTags($config['tokensValue'], $this->getTokens($lead)); + } else { + $value = fn ($lead) => \Haste\Util\StringUtil::recursiveReplaceTokensAndTags($config['tokensValue'], $this->getTokens($lead)); + } + $this->columns[] = [ 'name' => $config['headerField'], - 'value' => fn ($lead) => $this->parser->recursiveReplaceTokensAndTags($config['tokensValue'], $this->getTokens($lead)), + 'value' => $value, 'label' => static fn () => '', 'output' => 'value', 'targetColumn' => $config['targetColumn'], @@ -335,11 +341,19 @@ protected function getFilename(): string 'datim' => Date::parse(Config::get('datimFormat')), ]; - $filename = $this->parser->recursiveReplaceTokensAndTags( - $filename, - $tokens, - StringParser::NO_TAGS & StringParser::NO_BREAKS & StringParser::NO_ENTITIES, - ); + if ($this->parser) { + $filename = $this->parser->recursiveReplaceTokensAndTags( + $filename, + $tokens, + StringParser::NO_TAGS & StringParser::NO_BREAKS & StringParser::NO_ENTITIES, + ); + } else { + $filename = \Haste\Util\StringUtil::recursiveReplaceTokensAndTags( + $filename, + $tokens, + \Haste\Util\StringUtil::NO_TAGS & \Haste\Util\StringUtil::NO_BREAKS & \Haste\Util\StringUtil::NO_ENTITIES, + ); + } if (!str_contains($filename, '.')) { return $filename.$this->getFileExtension(); @@ -391,7 +405,11 @@ protected function getTokens(array $lead): array ]; foreach ($lead['data'] as $data) { - $this->parser->flatten(StringUtil::deserialize($data['value']), $data['name'], $tokens); + if ($this->parser) { + $this->parser->flatten(StringUtil::deserialize($data['value']), $data['name'], $tokens); + } else { + \Haste\Util\StringUtil::flatten(StringUtil::deserialize($data['value']), $data['name'], $tokens); + } } return $tokens; diff --git a/src/Export/PhpSpreadsheetExporter.php b/src/Export/PhpSpreadsheetExporter.php index 25c8e33..5f6a413 100644 --- a/src/Export/PhpSpreadsheetExporter.php +++ b/src/Export/PhpSpreadsheetExporter.php @@ -31,7 +31,7 @@ public function __construct( ServiceLocator $formatters, Connection $connection, TranslatorInterface $translator, - StringParser $parser, + StringParser|null $parser = null, ExpressionLanguage|null $expressionLanguage = null, ) { parent::__construct($formatters, $connection, $translator, $parser, $expressionLanguage);