Allow snips to be archived

This commit is contained in:
Tim
2025-05-10 16:48:03 +02:00
parent 50a7ab7985
commit 47ea226ed7
8 changed files with 107 additions and 10 deletions

View File

@ -151,4 +151,19 @@ class SnipController extends AbstractController
'form' => $form->createView(),
]);
}
#[Route('/archive/{snip}', name: '_archive')]
public function archive(Snip $snip): Response
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$snip->setArchived(!$snip->isArchived());
$this->repository->save($snip);
if ($snip->isArchived()) {
$this->addFlash('success', sprintf('Snip "%s" archived', $snip));
} else {
$this->addFlash('success', sprintf('Snip "%s" unarchived', $snip));
}
return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]);
}
}

View File

@ -7,6 +7,7 @@ readonly class SnipFilterRequest implements CachableDtoInterface
public const string VISIBILITY_ALL = 'all';
public const string VISIBILITY_VISIBLE = 'visible';
public const string VISIBILITY_HIDDEN = 'hidden';
public const string VISIBILITY_ARCHIVED = 'archived';
public const string SORT_NAME = 'name';
public const string SORT_DATE = 'date';

View File

@ -36,6 +36,9 @@ class Snip
#[ORM\Column]
private bool $visible = true;
#[ORM\Column]
private bool $archived = false;
public function __construct()
{
$this->snipContents = new ArrayCollection();
@ -145,4 +148,16 @@ class Snip
return $this;
}
public function isArchived(): ?bool
{
return $this->archived;
}
public function setArchived(bool $archived): static
{
$this->archived = $archived;
return $this;
}
}

View File

@ -50,6 +50,7 @@ class SnipRepository extends ServiceEntityRepository
->setParameter('user', $user)
;
$showArchived = false;
switch ($request->visibility) {
case SnipFilterRequest::VISIBILITY_ALL:
break;
@ -59,9 +60,13 @@ class SnipRepository extends ServiceEntityRepository
case SnipFilterRequest::VISIBILITY_HIDDEN:
$qb->andWhere('s.visible = false');
break;
case SnipFilterRequest::VISIBILITY_ARCHIVED:
$showArchived = true;
break;
default:
throw new \InvalidArgumentException('Invalid visibility option: ', $request->visibility);
}
$qb->andWhere('s.archived = ' . ($showArchived ? 'true' : 'false'));
switch ($request->sort) {
case SnipFilterRequest::SORT_NAME:
@ -73,6 +78,7 @@ class SnipRepository extends ServiceEntityRepository
default:
throw new \InvalidArgumentException('Invalid sort option: ', $request->sort);
}
return $qb->getQuery()->getResult();
}
@ -82,6 +88,7 @@ class SnipRepository extends ServiceEntityRepository
->createQueryBuilder('s')
->where('s.public = true')
->andWhere('s.visible = true')
->andWhere('s.archived = false')
->orderBy('s.createdAt', 'DESC')
;