Refactor type handling to contao-tags and add module tag filters

This commit is contained in:
Jürgen Mummert
2026-02-21 22:23:53 +01:00
parent a6440c74a2
commit f93ed0d0c6
18 changed files with 413 additions and 63 deletions
+140 -1
View File
@@ -131,6 +131,12 @@ class EventRepository
);
}
$tagIds = $this->getTagIdsForEvent($eventId);
if ([] !== $tagIds) {
$this->assignTagsToEvent($newEventId, $tagIds);
}
return $newEventId;
}
@@ -257,6 +263,12 @@ class EventRepository
public function delete(int $eventId): void
{
$this->connection->delete(
'tl_tags_rel',
['ptable' => 'tl_calendar_events', 'field' => 'tags', 'pid' => $eventId],
['pid' => ParameterType::INTEGER],
);
$this->connection->delete(
'tl_calendar_events_organization',
['event_id' => $eventId],
@@ -324,6 +336,73 @@ class EventRepository
return array_values(array_unique(array_map('intval', $rows)));
}
/**
* @return array<string, int>
*/
public function getTagChoicesForEventType(array $allowedTagIds = []): array
{
$rows = $this->connection->createQueryBuilder()
->select('DISTINCT t.id', 't.tag')
->from('tl_tags', 't')
->innerJoin('t', 'tl_tags_rel', 'r', 'r.tag_id = t.id')
->where('r.ptable = :ptable')
->andWhere('r.field = :field')
->setParameter('ptable', 'tl_calendar_events')
->setParameter('field', 'tags')
->orderBy('t.tag', 'ASC')
->executeQuery()
->fetchAllAssociative();
if ([] === $rows) {
$rows = $this->connection->createQueryBuilder()
->select('t.id', 't.tag')
->from('tl_tags', 't')
->orderBy('t.tag', 'ASC')
->executeQuery()
->fetchAllAssociative();
}
$choices = [];
foreach ($rows as $row) {
$choices[(string) ($row['tag'] ?? '')] = (int) $row['id'];
}
$allowedTagIds = array_values(array_unique(array_map('intval', $allowedTagIds)));
if ([] !== $allowedTagIds) {
$choices = array_filter(
$choices,
static fn (int $id): bool => in_array($id, $allowedTagIds, true),
);
}
return $choices;
}
/** @return array<int> */
public function getTagIdsForEvent(int $eventId): array
{
if ($eventId <= 0) {
return [];
}
$ids = $this->connection->createQueryBuilder()
->select('tag_id')
->from('tl_tags_rel')
->where('ptable = :ptable')
->andWhere('field = :field')
->andWhere('pid = :pid')
->setParameter('ptable', 'tl_calendar_events')
->setParameter('field', 'tags')
->setParameter('pid', $eventId, ParameterType::INTEGER)
->orderBy('tag_id', 'ASC')
->executeQuery()
->fetchFirstColumn();
return array_values(array_unique(array_map('intval', $ids)));
}
/**
* @return array<string, int>
*/
@@ -496,6 +575,41 @@ class EventRepository
return $eventId;
}
/** @param array<int|string> $tagIds */
public function assignTagsToEvent(int $eventId, array $tagIds): void
{
$this->connection->delete(
'tl_tags_rel',
['ptable' => 'tl_calendar_events', 'field' => 'tags', 'pid' => $eventId],
['pid' => ParameterType::INTEGER],
);
$tagIds = array_values(array_unique(array_map('intval', $tagIds)));
if ([] === $tagIds) {
return;
}
$allowedTagIds = $this->getAllowedTagIdsForEvents();
$tagIds = array_values(array_intersect($tagIds, $allowedTagIds));
foreach ($tagIds as $tagId) {
$this->connection->insert(
'tl_tags_rel',
[
'tag_id' => $tagId,
'pid' => $eventId,
'ptable' => 'tl_calendar_events',
'field' => 'tags',
],
[
'tag_id' => ParameterType::INTEGER,
'pid' => ParameterType::INTEGER,
],
);
}
}
/**
* @param array<string, mixed> $data
*
@@ -533,7 +647,6 @@ class EventRepository
'addTime' => $addTime ? 1 : 0,
'startTime' => $startTimeTimestamp,
'endTime' => $endTimeTimestamp,
'type' => serialize($data['type'] ?? []),
'teaser' => $data['teaser'] ?? null,
'description' => $data['description'] ?? null,
'url' => $url,
@@ -616,4 +729,30 @@ class EventRepository
$types,
);
}
/** @return array<int> */
private function getAllowedTagIdsForEvents(): array
{
$ids = $this->connection->createQueryBuilder()
->select('DISTINCT r.tag_id')
->from('tl_tags_rel', 'r')
->where('r.ptable = :ptable')
->andWhere('r.field = :field')
->setParameter('ptable', 'tl_calendar_events')
->setParameter('field', 'tags')
->executeQuery()
->fetchFirstColumn();
$ids = array_values(array_unique(array_map('intval', $ids)));
if ([] !== $ids) {
return $ids;
}
return array_values(array_unique(array_map('intval', $this->connection->createQueryBuilder()
->select('id')
->from('tl_tags')
->executeQuery()
->fetchFirstColumn())));
}
}
+128 -1
View File
@@ -79,7 +79,6 @@ class OrganizationRepository
'email' => $data['email'] ?? '',
'website' => $data['website'] ?? '',
'description' => $data['description'] ?? null,
'type' => serialize($data['type'] ?? []),
'tstamp' => time(),
],
['id' => $organizationId],
@@ -109,4 +108,132 @@ class OrganizationRepository
$types,
);
}
/**
* @return array<string, int>
*/
public function getTagChoicesForOrganizationType(array $allowedTagIds = []): array
{
$rows = $this->connection->createQueryBuilder()
->select('DISTINCT t.id', 't.tag')
->from('tl_tags', 't')
->innerJoin('t', 'tl_tags_rel', 'r', 'r.tag_id = t.id')
->where('r.ptable = :ptable')
->andWhere('r.field = :field')
->setParameter('ptable', 'tl_organization')
->setParameter('field', 'tags')
->orderBy('t.tag', 'ASC')
->executeQuery()
->fetchAllAssociative();
if ([] === $rows) {
$rows = $this->connection->createQueryBuilder()
->select('t.id', 't.tag')
->from('tl_tags', 't')
->orderBy('t.tag', 'ASC')
->executeQuery()
->fetchAllAssociative();
}
$choices = [];
foreach ($rows as $row) {
$choices[(string) ($row['tag'] ?? '')] = (int) $row['id'];
}
$allowedTagIds = array_values(array_unique(array_map('intval', $allowedTagIds)));
if ([] !== $allowedTagIds) {
$choices = array_filter(
$choices,
static fn (int $id): bool => in_array($id, $allowedTagIds, true),
);
}
return $choices;
}
/** @return array<int> */
public function getTagIdsForOrganization(int $organizationId): array
{
if ($organizationId <= 0) {
return [];
}
$ids = $this->connection->createQueryBuilder()
->select('tag_id')
->from('tl_tags_rel')
->where('ptable = :ptable')
->andWhere('field = :field')
->andWhere('pid = :pid')
->setParameter('ptable', 'tl_organization')
->setParameter('field', 'tags')
->setParameter('pid', $organizationId, ParameterType::INTEGER)
->orderBy('tag_id', 'ASC')
->executeQuery()
->fetchFirstColumn();
return array_values(array_unique(array_map('intval', $ids)));
}
/** @param array<int|string> $tagIds */
public function assignTagsToOrganization(int $organizationId, array $tagIds): void
{
$this->connection->delete(
'tl_tags_rel',
['ptable' => 'tl_organization', 'field' => 'tags', 'pid' => $organizationId],
['pid' => ParameterType::INTEGER],
);
$tagIds = array_values(array_unique(array_map('intval', $tagIds)));
if ([] === $tagIds) {
return;
}
$allowedTagIds = $this->getAllowedTagIdsForOrganization();
$tagIds = array_values(array_intersect($tagIds, $allowedTagIds));
foreach ($tagIds as $tagId) {
$this->connection->insert(
'tl_tags_rel',
[
'tag_id' => $tagId,
'pid' => $organizationId,
'ptable' => 'tl_organization',
'field' => 'tags',
],
[
'tag_id' => ParameterType::INTEGER,
'pid' => ParameterType::INTEGER,
],
);
}
}
/** @return array<int> */
private function getAllowedTagIdsForOrganization(): array
{
$ids = $this->connection->createQueryBuilder()
->select('DISTINCT r.tag_id')
->from('tl_tags_rel', 'r')
->where('r.ptable = :ptable')
->andWhere('r.field = :field')
->setParameter('ptable', 'tl_organization')
->setParameter('field', 'tags')
->executeQuery()
->fetchFirstColumn();
$ids = array_values(array_unique(array_map('intval', $ids)));
if ([] !== $ids) {
return $ids;
}
return array_values(array_unique(array_map('intval', $this->connection->createQueryBuilder()
->select('id')
->from('tl_tags')
->executeQuery()
->fetchFirstColumn())));
}
}