Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 00f65ffd45 | |||
| 142cab2203 | |||
| acf3d02d13 | |||
| add43674cf |
@@ -10,12 +10,14 @@ use Contao\Template;
|
||||
use Doctrine\DBAL\ArrayParameterType;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
#[AsHook('parseTemplate', method: 'onParseTemplate')]
|
||||
class OrganizationListingTemplateDataListener
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Connection $connection,
|
||||
private readonly ?LoggerInterface $logger = null,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -34,6 +36,10 @@ class OrganizationListingTemplateDataListener
|
||||
$rowToOrganizationIdMap = [];
|
||||
$rowToTitleMap = [];
|
||||
|
||||
$resolvedByRowIdCount = 0;
|
||||
$resolvedByTitleToIdCount = 0;
|
||||
$enrichedByTitleTagFallbackCount = 0;
|
||||
|
||||
foreach ($tbody as $rowIndex => $row) {
|
||||
if (!\is_array($row)) {
|
||||
continue;
|
||||
@@ -43,6 +49,7 @@ class OrganizationListingTemplateDataListener
|
||||
|
||||
if ($organizationId > 0) {
|
||||
$rowToOrganizationIdMap[(int) $rowIndex] = $organizationId;
|
||||
++$resolvedByRowIdCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -65,6 +72,7 @@ class OrganizationListingTemplateDataListener
|
||||
|
||||
if ($organizationId > 0) {
|
||||
$rowToOrganizationIdMap[$rowIndex] = $organizationId;
|
||||
++$resolvedByTitleToIdCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,6 +83,7 @@ class OrganizationListingTemplateDataListener
|
||||
|
||||
$organizationTagMap = $this->fetchOrganizationTagMap(array_values(array_unique(array_values($rowToOrganizationIdMap))));
|
||||
$organizationLogoUuidMap = $this->fetchOrganizationLogoUuidMap(array_values(array_unique(array_values($rowToOrganizationIdMap))));
|
||||
$organizationTagMapByTitle = $this->fetchOrganizationTagMapByTitle(array_values(array_unique(array_filter($rowToTitleMap))));
|
||||
|
||||
foreach ($rowToOrganizationIdMap as $rowIndex => $organizationId) {
|
||||
$tagData = $organizationTagMap[$organizationId] ?? ['labels' => [], 'slugs' => []];
|
||||
@@ -93,6 +102,62 @@ class OrganizationListingTemplateDataListener
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($tbody as $rowIndex => $row) {
|
||||
if (!\is_array($row) || isset($rowToOrganizationIdMap[$rowIndex])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$title = $this->normalizeTitle($this->extractRowFieldContent($row, 'title'));
|
||||
|
||||
if ('' === $title || !isset($organizationTagMapByTitle[$title])) {
|
||||
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']);
|
||||
++$enrichedByTitleTagFallbackCount;
|
||||
}
|
||||
|
||||
$rowsWithoutTagSlugs = 0;
|
||||
$sampleUnresolvedTitles = [];
|
||||
|
||||
foreach ($tbody as $row) {
|
||||
if (!\is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tagSlugs = $this->extractRowFieldContent($row, 'tag_slugs');
|
||||
|
||||
if ('' !== $tagSlugs) {
|
||||
continue;
|
||||
}
|
||||
|
||||
++$rowsWithoutTagSlugs;
|
||||
|
||||
if (\count($sampleUnresolvedTitles) < 10) {
|
||||
$title = $this->normalizeTitle($this->extractRowFieldContent($row, 'title'));
|
||||
|
||||
if ('' !== $title) {
|
||||
$sampleUnresolvedTitles[] = $title;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $this->logger && $rowsWithoutTagSlugs > 0) {
|
||||
$this->logger->warning('Organization listing tag enrichment left rows without tag slugs.', [
|
||||
'template' => (string) $template->getName(),
|
||||
'totalRows' => \count($tbody),
|
||||
'resolvedByRowId' => $resolvedByRowIdCount,
|
||||
'resolvedByTitleToId' => $resolvedByTitleToIdCount,
|
||||
'enrichedByTitleTagFallback' => $enrichedByTitleTagFallbackCount,
|
||||
'rowsWithoutTagSlugs' => $rowsWithoutTagSlugs,
|
||||
'sampleUnresolvedTitles' => array_values(array_unique($sampleUnresolvedTitles)),
|
||||
]);
|
||||
}
|
||||
|
||||
$template->tbody = $tbody;
|
||||
}
|
||||
|
||||
@@ -247,6 +312,22 @@ class OrganizationListingTemplateDataListener
|
||||
)->fetchAllAssociative();
|
||||
}
|
||||
|
||||
if ([] === $rows) {
|
||||
$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.field = ? AND r.pid IN (?) ORDER BY r.pid ASC, r.tag_id ASC',
|
||||
['tags', $organizationIds],
|
||||
[ParameterType::STRING, ArrayParameterType::INTEGER],
|
||||
)->fetchAllAssociative();
|
||||
}
|
||||
|
||||
if ([] === $rows) {
|
||||
$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.pid IN (?) ORDER BY r.pid ASC, r.tag_id ASC',
|
||||
[$organizationIds],
|
||||
[ArrayParameterType::INTEGER],
|
||||
)->fetchAllAssociative();
|
||||
}
|
||||
|
||||
$map = [];
|
||||
$seen = [];
|
||||
|
||||
@@ -281,6 +362,71 @@ class OrganizationListingTemplateDataListener
|
||||
return $map;
|
||||
}
|
||||
|
||||
/** @param list<string> $titles
|
||||
* @return array<string, array{labels: list<string>, slugs: list<string>}>
|
||||
*/
|
||||
private function fetchOrganizationTagMapByTitle(array $titles): array
|
||||
{
|
||||
if ([] === $titles) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$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',
|
||||
['tl_organization', $titles],
|
||||
[ParameterType::STRING, ArrayParameterType::STRING],
|
||||
)->fetchAllAssociative();
|
||||
|
||||
if ([] === $rows) {
|
||||
$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.field = ? INNER JOIN tl_tags t ON t.id = r.tag_id WHERE o.title IN (?) ORDER BY o.title ASC, r.tag_id ASC',
|
||||
['tags', $titles],
|
||||
[ParameterType::STRING, ArrayParameterType::STRING],
|
||||
)->fetchAllAssociative();
|
||||
}
|
||||
|
||||
if ([] === $rows) {
|
||||
$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 INNER JOIN tl_tags t ON t.id = r.tag_id WHERE o.title IN (?) ORDER BY o.title ASC, r.tag_id ASC',
|
||||
[$titles],
|
||||
[ArrayParameterType::STRING],
|
||||
)->fetchAllAssociative();
|
||||
}
|
||||
|
||||
$map = [];
|
||||
$seen = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$title = $this->normalizeTitle((string) ($row['title'] ?? ''));
|
||||
$tagId = (int) ($row['tag_id'] ?? 0);
|
||||
$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) {
|
||||
$map[$title]['labels'] = array_values(array_unique($tagData['labels'] ?? []));
|
||||
$map[$title]['slugs'] = array_values(array_unique($tagData['slugs'] ?? []));
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/** @param list<int> $organizationIds
|
||||
* @return array<int, string>
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user