Compare commits

..

4 Commits

Author SHA1 Message Date
Jürgen Mummert ff608f7833 Fix organization listing numeric tag rendering and template mapping 2026-02-22 17:41:36 +01:00
Jürgen Mummert 00f65ffd45 Fix nullable logger guard in organization listing listener 2026-02-22 17:12:29 +01:00
Jürgen Mummert 142cab2203 Broaden organization tag enrichment fallbacks for production data variants 2026-02-22 17:10:39 +01:00
Jürgen Mummert acf3d02d13 Add diagnostics for unresolved org tag enrichment rows 2026-02-22 17:06:08 +01:00
@@ -10,12 +10,14 @@ use Contao\Template;
use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use Psr\Log\LoggerInterface;
#[AsHook('parseTemplate', method: 'onParseTemplate')] #[AsHook('parseTemplate', method: 'onParseTemplate')]
class OrganizationListingTemplateDataListener class OrganizationListingTemplateDataListener
{ {
public function __construct( public function __construct(
private readonly Connection $connection, private readonly Connection $connection,
private readonly ?LoggerInterface $logger = null,
) { ) {
} }
@@ -32,7 +34,6 @@ class OrganizationListingTemplateDataListener
} }
$rowToOrganizationIdMap = []; $rowToOrganizationIdMap = [];
$rowToTitleMap = [];
foreach ($tbody as $rowIndex => $row) { foreach ($tbody as $rowIndex => $row) {
if (!\is_array($row)) { if (!\is_array($row)) {
@@ -43,29 +44,6 @@ class OrganizationListingTemplateDataListener
if ($organizationId > 0) { if ($organizationId > 0) {
$rowToOrganizationIdMap[(int) $rowIndex] = $organizationId; $rowToOrganizationIdMap[(int) $rowIndex] = $organizationId;
continue;
}
$title = $this->extractRowFieldContent($row, 'title');
if ('' !== $title) {
$rowToTitleMap[(int) $rowIndex] = $this->normalizeTitle($title);
}
}
if ([] !== $rowToTitleMap) {
$titleToOrganizationIdMap = $this->resolveOrganizationIdsByTitle(array_values(array_unique(array_filter($rowToTitleMap))));
foreach ($rowToTitleMap as $rowIndex => $title) {
if (isset($rowToOrganizationIdMap[$rowIndex])) {
continue;
}
$organizationId = $titleToOrganizationIdMap[$title] ?? 0;
if ($organizationId > 0) {
$rowToOrganizationIdMap[$rowIndex] = $organizationId;
}
} }
} }
@@ -73,21 +51,53 @@ class OrganizationListingTemplateDataListener
return; return;
} }
$organizationTagMap = $this->fetchOrganizationTagMap(array_values(array_unique(array_values($rowToOrganizationIdMap)))); $organizationIds = array_values(array_unique(array_values($rowToOrganizationIdMap)));
$organizationLogoUuidMap = $this->fetchOrganizationLogoUuidMap(array_values(array_unique(array_values($rowToOrganizationIdMap)))); $organizationTagMap = $this->fetchOrganizationTagMap($organizationIds);
$organizationTagMapByTitle = $this->fetchOrganizationTagMapByTitle(array_values(array_unique(array_filter($rowToTitleMap)))); $organizationLogoUuidMap = $this->fetchOrganizationLogoUuidMap($organizationIds);
$organizationTagIdsMap = [];
$organizationTagLabelMap = [];
$rowTagIdsMap = [];
$rowTagIdsList = [];
foreach ($organizationTagMap as $organizationId => $tagData) {
if ([] !== ($tagData['ids'] ?? [])) {
$organizationTagIdsMap[(string) $organizationId] = implode(',', $tagData['ids']);
}
foreach (($tagData['ids'] ?? []) as $index => $tagId) {
$label = trim((string) (($tagData['labels'][$index] ?? '') ?: ''));
if ('' !== $tagId && '' !== $label && !isset($organizationTagLabelMap[(string) $tagId])) {
$organizationTagLabelMap[(string) $tagId] = $label;
}
}
}
foreach ($rowToOrganizationIdMap as $rowIndex => $organizationId) { foreach ($rowToOrganizationIdMap as $rowIndex => $organizationId) {
$tagData = $organizationTagMap[$organizationId] ?? ['labels' => [], 'slugs' => []];
$logoUuid = $organizationLogoUuidMap[$organizationId] ?? '';
if (!isset($tbody[$rowIndex]) || !\is_array($tbody[$rowIndex])) { if (!isset($tbody[$rowIndex]) || !\is_array($tbody[$rowIndex])) {
continue; continue;
} }
$tbody[$rowIndex]['tag_labels']['content'] = implode(', ', $tagData['labels']); $tagData = $organizationTagMap[$organizationId] ?? ['ids' => [], 'labels' => [], 'slugs' => []];
$tbody[$rowIndex]['tag_slugs']['content'] = implode(',', $tagData['slugs']); $logoUuid = $organizationLogoUuidMap[$organizationId] ?? '';
$tbody[$rowIndex]['tags']['content'] = implode(', ', $tagData['labels']); $tagIdsCsv = implode(',', $tagData['ids']);
$tagLabelsCsv = implode(', ', $tagData['labels']);
$tagSlugsCsv = implode(',', $tagData['slugs']);
$tbody[$rowIndex]['tag_ids']['content'] = $tagIdsCsv;
$tbody[$rowIndex]['tag_labels']['content'] = $tagLabelsCsv;
$tbody[$rowIndex]['tag_slugs']['content'] = $tagSlugsCsv;
$tbody[$rowIndex]['tags']['content'] = $tagLabelsCsv;
$tbody[$rowIndex]['tag_ids'] = $tagIdsCsv;
$tbody[$rowIndex]['tag_labels'] = $tagLabelsCsv;
$tbody[$rowIndex]['tag_slugs'] = $tagSlugsCsv;
$tbody[$rowIndex]['tags'] = $tagLabelsCsv;
if ('' !== $tagIdsCsv) {
$rowTagIdsMap[(string) $rowIndex] = $tagIdsCsv;
}
if ('' !== $logoUuid) { if ('' !== $logoUuid) {
$tbody[$rowIndex]['logo_uuid']['content'] = $logoUuid; $tbody[$rowIndex]['logo_uuid']['content'] = $logoUuid;
@@ -95,23 +105,41 @@ class OrganizationListingTemplateDataListener
} }
foreach ($tbody as $rowIndex => $row) { foreach ($tbody as $rowIndex => $row) {
if (!\is_array($row) || isset($rowToOrganizationIdMap[$rowIndex])) { if (!\is_array($row)) {
$rowTagIdsList[] = '';
continue; continue;
} }
$title = $this->normalizeTitle($this->extractRowFieldContent($row, 'title')); $organizationId = $rowToOrganizationIdMap[(int) $rowIndex] ?? 0;
$rowTagIdsList[] = ($organizationId > 0 && isset($organizationTagMap[$organizationId]))
if ('' === $title || !isset($organizationTagMapByTitle[$title])) { ? implode(',', $organizationTagMap[$organizationId]['ids'] ?? [])
continue; : '';
}
$tagData = $organizationTagMapByTitle[$title];
$tbody[$rowIndex]['tag_labels']['content'] = implode(', ', $tagData['labels']);
$tbody[$rowIndex]['tag_slugs']['content'] = implode(',', $tagData['slugs']);
$tbody[$rowIndex]['tags']['content'] = implode(', ', $tagData['labels']);
} }
if (null !== $this->logger) {
$rowsWithoutTagIds = 0;
foreach ($rowToOrganizationIdMap as $rowIndex => $organizationId) {
if (!isset($organizationTagMap[$organizationId]) || [] === ($organizationTagMap[$organizationId]['ids'] ?? [])) {
++$rowsWithoutTagIds;
}
}
if ($rowsWithoutTagIds > 0) {
$this->logger->warning('Organization listing enrichment found rows without tag IDs.', [
'template' => (string) $template->getName(),
'totalRows' => \count($tbody),
'mappedRows' => \count($rowToOrganizationIdMap),
'rowsWithoutTagIds' => $rowsWithoutTagIds,
]);
}
}
$template->organization_tag_ids_map = $organizationTagIdsMap;
$template->organization_tag_label_map = $organizationTagLabelMap;
$template->organization_row_tag_ids_map = $rowTagIdsMap;
$template->organization_row_tag_ids_list = $rowTagIdsList;
$template->tbody = $tbody; $template->tbody = $tbody;
} }
@@ -119,6 +147,10 @@ class OrganizationListingTemplateDataListener
private function extractOrganizationId(array $row): int private function extractOrganizationId(array $row): int
{ {
foreach (['id', 'organization_id', 'org_id'] as $fieldName) { foreach (['id', 'organization_id', 'org_id'] as $fieldName) {
if (isset($row[$fieldName]) && \is_scalar($row[$fieldName]) && ctype_digit((string) $row[$fieldName])) {
return (int) $row[$fieldName];
}
$value = $this->extractRowFieldContent($row, $fieldName); $value = $this->extractRowFieldContent($row, $fieldName);
if ('' !== $value && ctype_digit($value)) { if ('' !== $value && ctype_digit($value)) {
@@ -126,27 +158,21 @@ class OrganizationListingTemplateDataListener
} }
} }
$urlCandidates = [];
foreach ($row as $column) { foreach ($row as $column) {
if (!\is_array($column)) { if (!\is_array($column)) {
continue; continue;
} }
if (isset($column['url']) && \is_scalar($column['url'])) { foreach (['url', 'href'] as $urlField) {
$urlCandidates[] = (string) $column['url']; if (!isset($column[$urlField]) || !\is_scalar($column[$urlField])) {
} continue;
}
if (isset($column['href']) && \is_scalar($column['href'])) { $organizationId = $this->extractIdFromUrl((string) $column[$urlField]);
$urlCandidates[] = (string) $column['href'];
}
}
foreach ($urlCandidates as $url) { if ($organizationId > 0) {
$organizationId = $this->extractIdFromUrl($url); return $organizationId;
}
if ($organizationId > 0) {
return $organizationId;
} }
} }
@@ -156,7 +182,15 @@ class OrganizationListingTemplateDataListener
/** @param array<string, mixed> $row */ /** @param array<string, mixed> $row */
private function extractRowFieldContent(array $row, string $fieldName): string private function extractRowFieldContent(array $row, string $fieldName): string
{ {
if (!isset($row[$fieldName]) || !\is_array($row[$fieldName])) { if (!isset($row[$fieldName])) {
return '';
}
if (\is_scalar($row[$fieldName])) {
return trim((string) $row[$fieldName]);
}
if (!\is_array($row[$fieldName])) {
return ''; return '';
} }
@@ -194,57 +228,8 @@ class OrganizationListingTemplateDataListener
return 0; return 0;
} }
/** @param list<string> $titles
* @return array<string, int>
*/
private function resolveOrganizationIdsByTitle(array $titles): array
{
if ([] === $titles) {
return [];
}
$rows = $this->connection->executeQuery(
'SELECT o.id, o.title FROM tl_organization o WHERE o.title IN (?)',
[$titles],
[ArrayParameterType::STRING],
)->fetchAllAssociative();
$titleToIdsMap = [];
foreach ($rows as $row) {
$organizationId = (int) ($row['id'] ?? 0);
$title = $this->normalizeTitle((string) ($row['title'] ?? ''));
if ($organizationId <= 0 || '' === $title) {
continue;
}
$titleToIdsMap[$title][] = $organizationId;
}
$titleToOrganizationIdMap = [];
foreach ($titleToIdsMap as $title => $organizationIds) {
$organizationIds = array_values(array_unique(array_map('intval', $organizationIds)));
if (1 === \count($organizationIds)) {
$titleToOrganizationIdMap[$title] = $organizationIds[0];
}
}
return $titleToOrganizationIdMap;
}
private function normalizeTitle(string $title): string
{
$title = html_entity_decode($title, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$title = trim(strip_tags($title));
return preg_replace('/\s+/u', ' ', $title) ?? '';
}
/** @param list<int> $organizationIds /** @param list<int> $organizationIds
* @return array<int, array{labels: list<string>, slugs: list<string>}> * @return array<int, array{ids: list<string>, labels: list<string>, slugs: list<string>}>
*/ */
private function fetchOrganizationTagMap(array $organizationIds): array private function fetchOrganizationTagMap(array $organizationIds): array
{ {
@@ -252,17 +237,46 @@ class OrganizationListingTemplateDataListener
return []; return [];
} }
$scope = $this->resolveTagRelationScope($organizationIds);
$conditions = ['r.pid IN (?)'];
$params = [$organizationIds];
$types = [ArrayParameterType::INTEGER];
if ('' === $scope['ptable']) {
$conditions[] = "(r.ptable = '' OR r.ptable IS NULL)";
} else {
$conditions[] = 'r.ptable = ?';
$params[] = $scope['ptable'];
$types[] = ParameterType::STRING;
}
if ('' === $scope['field']) {
$conditions[] = "(r.field = '' OR r.field IS NULL)";
} else {
$conditions[] = 'r.field = ?';
$params[] = $scope['field'];
$types[] = ParameterType::STRING;
}
$rows = $this->connection->executeQuery( $rows = $this->connection->executeQuery(
'SELECT r.pid AS organization_id, r.tag_id, t.tag AS label FROM tl_tags_rel r INNER JOIN tl_tags t ON t.id = r.tag_id WHERE r.ptable = ? AND r.field = ? AND r.pid IN (?) ORDER BY r.pid ASC, r.tag_id ASC', 'SELECT r.pid AS organization_id, r.tag_id, t.tag AS label FROM tl_tags_rel r INNER JOIN tl_tags t ON t.id = r.tag_id WHERE '.implode(' AND ', $conditions).' ORDER BY r.pid ASC, r.tag_id ASC',
['tl_organization', 'tags', $organizationIds], $params,
[ParameterType::STRING, ParameterType::STRING, ArrayParameterType::INTEGER], $types,
)->fetchAllAssociative(); )->fetchAllAssociative();
if ([] === $rows) { if ([] === $rows && '' !== $scope['field']) {
$fieldFreeConditions = array_values(array_filter($conditions, static fn (string $condition): bool => 'r.field = ?' !== $condition));
$fieldFreeParams = $params;
$fieldFreeTypes = $types;
array_pop($fieldFreeParams);
array_pop($fieldFreeTypes);
$rows = $this->connection->executeQuery( $rows = $this->connection->executeQuery(
'SELECT r.pid AS organization_id, r.tag_id, t.tag AS label FROM tl_tags_rel r INNER JOIN tl_tags t ON t.id = r.tag_id WHERE r.ptable = ? AND r.pid IN (?) ORDER BY r.pid ASC, r.tag_id ASC', 'SELECT r.pid AS organization_id, r.tag_id, t.tag AS label FROM tl_tags_rel r INNER JOIN tl_tags t ON t.id = r.tag_id WHERE '.implode(' AND ', $fieldFreeConditions).' ORDER BY r.pid ASC, r.tag_id ASC',
['tl_organization', $organizationIds], $fieldFreeParams,
[ParameterType::STRING, ArrayParameterType::INTEGER], $fieldFreeTypes,
)->fetchAllAssociative(); )->fetchAllAssociative();
} }
@@ -274,15 +288,12 @@ class OrganizationListingTemplateDataListener
$tagId = (int) ($row['tag_id'] ?? 0); $tagId = (int) ($row['tag_id'] ?? 0);
$label = trim((string) ($row['label'] ?? '')); $label = trim((string) ($row['label'] ?? ''));
if ($organizationId <= 0 || $tagId <= 0 || '' === $label) { if ($organizationId <= 0 || $tagId <= 0 || '' === $label || isset($seen[$organizationId][$tagId])) {
continue;
}
if (isset($seen[$organizationId][$tagId])) {
continue; continue;
} }
$seen[$organizationId][$tagId] = true; $seen[$organizationId][$tagId] = true;
$map[$organizationId]['ids'][] = (string) $tagId;
$map[$organizationId]['labels'][] = $label; $map[$organizationId]['labels'][] = $label;
$slug = $this->slugify($label); $slug = $this->slugify($label);
@@ -293,6 +304,7 @@ class OrganizationListingTemplateDataListener
} }
foreach ($map as $organizationId => $tagData) { foreach ($map as $organizationId => $tagData) {
$map[$organizationId]['ids'] = array_values(array_unique($tagData['ids'] ?? []));
$map[$organizationId]['labels'] = array_values(array_unique($tagData['labels'] ?? [])); $map[$organizationId]['labels'] = array_values(array_unique($tagData['labels'] ?? []));
$map[$organizationId]['slugs'] = array_values(array_unique($tagData['slugs'] ?? [])); $map[$organizationId]['slugs'] = array_values(array_unique($tagData['slugs'] ?? []));
} }
@@ -300,53 +312,43 @@ class OrganizationListingTemplateDataListener
return $map; return $map;
} }
/** @param list<string> $titles /** @param list<int> $organizationIds
* @return array<string, array{labels: list<string>, slugs: list<string>}> * @return array{ptable: string, field: string}
*/ */
private function fetchOrganizationTagMapByTitle(array $titles): array private function resolveTagRelationScope(array $organizationIds): array
{ {
if ([] === $titles) {
return [];
}
$rows = $this->connection->executeQuery( $rows = $this->connection->executeQuery(
'SELECT o.title, r.tag_id, t.tag AS label FROM tl_organization o INNER JOIN tl_tags_rel r ON r.pid = o.id AND r.ptable = ? INNER JOIN tl_tags t ON t.id = r.tag_id WHERE o.title IN (?) ORDER BY o.title ASC, r.tag_id ASC', "SELECT COALESCE(NULLIF(TRIM(r.ptable), ''), '') AS ptable_scope, COALESCE(NULLIF(TRIM(r.field), ''), '') AS field_scope, COUNT(DISTINCT r.pid) AS pid_count, COUNT(*) AS rel_count FROM tl_tags_rel r WHERE r.pid IN (?) GROUP BY COALESCE(NULLIF(TRIM(r.ptable), ''), ''), COALESCE(NULLIF(TRIM(r.field), ''), '') ORDER BY pid_count DESC, rel_count DESC",
['tl_organization', $titles], [$organizationIds],
[ParameterType::STRING, ArrayParameterType::STRING], [ArrayParameterType::INTEGER],
)->fetchAllAssociative(); )->fetchAllAssociative();
$map = []; if ([] === $rows) {
$seen = []; return ['ptable' => 'tl_organization', 'field' => 'tags'];
}
foreach ($rows as $row) { foreach ($rows as $row) {
$title = $this->normalizeTitle((string) ($row['title'] ?? '')); if ('tl_organization' === (string) ($row['ptable_scope'] ?? '') && 'tags' === (string) ($row['field_scope'] ?? '')) {
$tagId = (int) ($row['tag_id'] ?? 0); return ['ptable' => 'tl_organization', 'field' => 'tags'];
$label = trim((string) ($row['label'] ?? ''));
if ('' === $title || $tagId <= 0 || '' === $label) {
continue;
}
if (isset($seen[$title][$tagId])) {
continue;
}
$seen[$title][$tagId] = true;
$map[$title]['labels'][] = $label;
$slug = $this->slugify($label);
if ('' !== $slug) {
$map[$title]['slugs'][] = $slug;
} }
} }
foreach ($map as $title => $tagData) { foreach ($rows as $row) {
$map[$title]['labels'] = array_values(array_unique($tagData['labels'] ?? [])); if ('tl_organization' === (string) ($row['ptable_scope'] ?? '')) {
$map[$title]['slugs'] = array_values(array_unique($tagData['slugs'] ?? [])); return ['ptable' => 'tl_organization', 'field' => (string) ($row['field_scope'] ?? '')];
}
} }
return $map; foreach ($rows as $row) {
if ('tags' === (string) ($row['field_scope'] ?? '')) {
return ['ptable' => (string) ($row['ptable_scope'] ?? ''), 'field' => 'tags'];
}
}
return [
'ptable' => (string) ($rows[0]['ptable_scope'] ?? 'tl_organization'),
'field' => (string) ($rows[0]['field_scope'] ?? 'tags'),
];
} }
/** @param list<int> $organizationIds /** @param list<int> $organizationIds