Support visibility for tags (#4086)
This commit is contained in:
@@ -11,12 +11,15 @@ namespace App\Repository\Query;
|
||||
|
||||
class TagQuery extends BaseQuery
|
||||
{
|
||||
use VisibilityTrait;
|
||||
|
||||
public const TAG_ORDER_ALLOWED = ['name', 'amount'];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setDefaults([
|
||||
'orderBy' => 'name',
|
||||
'visibility' => VisibilityInterface::SHOW_VISIBLE,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@ use App\Repository\Paginator\QueryBuilderPaginator;
|
||||
use App\Repository\Query\TagFormTypeQuery;
|
||||
use App\Repository\Query\TagQuery;
|
||||
use App\Utils\Pagination;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\Exception\ORMException;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
|
||||
/**
|
||||
@@ -28,24 +28,14 @@ class TagRepository extends EntityRepository
|
||||
*/
|
||||
public const MAX_AMOUNT_SELECT = 500;
|
||||
|
||||
/**
|
||||
* @param Tag $tag
|
||||
* @throws ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function saveTag(Tag $tag)
|
||||
public function saveTag(Tag $tag): void
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->persist($tag);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tag $tag
|
||||
* @throws ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function deleteTag(Tag $tag)
|
||||
public function deleteTag(Tag $tag): void
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->remove($tag);
|
||||
@@ -53,26 +43,33 @@ class TagRepository extends EntityRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $tagNames
|
||||
* @param array<string> $tagNames
|
||||
* @return array<Tag>
|
||||
*/
|
||||
public function findTagsByName(array $tagNames): array
|
||||
public function findTagsByName(array $tagNames, ?bool $visible = null): array
|
||||
{
|
||||
return $this->findBy(['name' => $tagNames]);
|
||||
if ($visible === null) {
|
||||
return $this->findBy(['name' => $tagNames]);
|
||||
}
|
||||
|
||||
return $this->findBy(['name' => $tagNames, 'visible' => $visible]);
|
||||
}
|
||||
|
||||
public function findTagByName(string $tagName): ?Tag
|
||||
public function findTagByName(string $tagName, ?bool $visible = null): ?Tag
|
||||
{
|
||||
return $this->findOneBy(['name' => $tagName]);
|
||||
if ($visible === null) {
|
||||
return $this->findOneBy(['name' => $tagName]);
|
||||
}
|
||||
|
||||
return $this->findOneBy(['name' => $tagName, 'visible' => $visible]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all tag names in an alphabetical order
|
||||
* Find all visible tag names in alphabetical order.
|
||||
*
|
||||
* @param string $filter
|
||||
* @return array
|
||||
* @return array<string>
|
||||
*/
|
||||
public function findAllTagNames($filter = null): array
|
||||
public function findAllTagNames(?string $filter = null): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('t');
|
||||
|
||||
@@ -80,10 +77,12 @@ class TagRepository extends EntityRepository
|
||||
->select('t.name')
|
||||
->addOrderBy('t.name', 'ASC');
|
||||
|
||||
$qb->andWhere($qb->expr()->eq('t.visible', ':visible'));
|
||||
$qb->setParameter('visible', true, ParameterType::BOOLEAN);
|
||||
|
||||
if (null !== $filter) {
|
||||
$qb
|
||||
->andWhere('t.name LIKE :filter')
|
||||
->setParameter('filter', '%' . $filter . '%');
|
||||
$qb->andWhere('t.name LIKE :filter');
|
||||
$qb->setParameter('filter', '%' . $filter . '%');
|
||||
}
|
||||
|
||||
return array_column($qb->getQuery()->getScalarResult(), 'name');
|
||||
@@ -123,7 +122,7 @@ class TagRepository extends EntityRepository
|
||||
{
|
||||
$qb = $this->createQueryBuilder('tag');
|
||||
|
||||
$qb->select('tag.id, tag.name, tag.color, SIZE(tag.timesheets) as amount');
|
||||
$qb->select('tag.id, tag.name, tag.color, tag.visible, SIZE(tag.timesheets) as amount');
|
||||
|
||||
$orderBy = $query->getOrderBy();
|
||||
$orderBy = match ($orderBy) {
|
||||
@@ -131,6 +130,14 @@ class TagRepository extends EntityRepository
|
||||
default => 'tag.' . $orderBy,
|
||||
};
|
||||
|
||||
if ($query->isShowVisible()) {
|
||||
$qb->andWhere($qb->expr()->eq('tag.visible', ':visible'));
|
||||
$qb->setParameter('visible', true, ParameterType::BOOLEAN);
|
||||
} elseif ($query->isShowHidden()) {
|
||||
$qb->andWhere($qb->expr()->eq('tag.visible', ':visible'));
|
||||
$qb->setParameter('visible', false, ParameterType::BOOLEAN);
|
||||
}
|
||||
|
||||
$qb->addOrderBy($orderBy, $query->getOrder());
|
||||
|
||||
if ($query->hasSearchTerm()) {
|
||||
@@ -159,6 +166,8 @@ class TagRepository extends EntityRepository
|
||||
$qb = $this->createQueryBuilder('tag');
|
||||
|
||||
$qb->orderBy('tag.name', 'ASC');
|
||||
$qb->andWhere($qb->expr()->eq('tag.visible', ':visible'));
|
||||
$qb->setParameter('visible', true, ParameterType::BOOLEAN);
|
||||
|
||||
return $qb;
|
||||
}
|
||||
@@ -183,4 +192,25 @@ class TagRepository extends EntityRepository
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tag[] $tags
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function multiUpdate(iterable $tags): void
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
$em->beginTransaction();
|
||||
|
||||
try {
|
||||
foreach ($tags as $tag) {
|
||||
$em->persist($tag);
|
||||
}
|
||||
$em->flush();
|
||||
$em->commit();
|
||||
} catch (\Exception $ex) {
|
||||
$em->rollback();
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user