Refactor type handling to contao-tags and add module tag filters
This commit is contained in:
@@ -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())));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user