239 lines
7.3 KiB
PHP
239 lines
7.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Mummert\EventManagerBundle\Service;
|
|
|
|
use Doctrine\DBAL\Connection;
|
|
use Doctrine\DBAL\ParameterType;
|
|
|
|
class OrganizationRepository
|
|
{
|
|
public function __construct(
|
|
private readonly Connection $connection,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
public function findByMemberId(int $memberId): array
|
|
{
|
|
return $this->connection->createQueryBuilder()
|
|
->select('o.*')
|
|
->from('tl_organization', 'o')
|
|
->innerJoin('o', 'tl_member_organization', 'mo', 'mo.organization_id = o.id')
|
|
->where('mo.member_id = :memberId')
|
|
->setParameter('memberId', $memberId, ParameterType::INTEGER)
|
|
->orderBy('o.title', 'ASC')
|
|
->executeQuery()
|
|
->fetchAllAssociative();
|
|
}
|
|
|
|
public function memberHasOrganization(int $memberId, int $organizationId): bool
|
|
{
|
|
$exists = $this->connection->createQueryBuilder()
|
|
->select('1')
|
|
->from('tl_member_organization', 'mo')
|
|
->where('mo.member_id = :memberId')
|
|
->andWhere('mo.organization_id = :organizationId')
|
|
->setParameter('memberId', $memberId, ParameterType::INTEGER)
|
|
->setParameter('organizationId', $organizationId, ParameterType::INTEGER)
|
|
->setMaxResults(1)
|
|
->executeQuery()
|
|
->fetchOne();
|
|
|
|
return false !== $exists;
|
|
}
|
|
|
|
/** @return array<string, mixed>|null */
|
|
public function findById(int $organizationId): ?array
|
|
{
|
|
$row = $this->connection->createQueryBuilder()
|
|
->select('*')
|
|
->from('tl_organization')
|
|
->where('id = :id')
|
|
->setParameter('id', $organizationId, ParameterType::INTEGER)
|
|
->setMaxResults(1)
|
|
->executeQuery()
|
|
->fetchAssociative();
|
|
|
|
return false === $row ? null : $row;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public function update(int $organizationId, array $data): void
|
|
{
|
|
$this->connection->update(
|
|
'tl_organization',
|
|
[
|
|
'title' => $data['title'] ?? '',
|
|
'street' => $data['street'] ?? '',
|
|
'street2' => $data['street2'] ?? '',
|
|
'postal' => $data['postal'] ?? '',
|
|
'city' => $data['city'] ?? '',
|
|
'phone' => $data['phone'] ?? '',
|
|
'email' => $data['email'] ?? '',
|
|
'website' => $data['website'] ?? '',
|
|
'description' => $data['description'] ?? null,
|
|
'tstamp' => time(),
|
|
],
|
|
['id' => $organizationId],
|
|
[
|
|
'id' => ParameterType::INTEGER,
|
|
],
|
|
);
|
|
}
|
|
|
|
public function updateLogo(int $organizationId, ?string $logoUuid): void
|
|
{
|
|
$types = [
|
|
'id' => ParameterType::INTEGER,
|
|
];
|
|
|
|
if (null !== $logoUuid) {
|
|
$types['logo'] = ParameterType::BINARY;
|
|
}
|
|
|
|
$this->connection->update(
|
|
'tl_organization',
|
|
[
|
|
'logo' => $logoUuid,
|
|
'tstamp' => time(),
|
|
],
|
|
['id' => $organizationId],
|
|
$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())));
|
|
}
|
|
}
|