Implement snip hiding
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Dto\SnipFilterRequest;
|
||||
use App\Entity\Snip;
|
||||
use App\Form\ConfirmationType;
|
||||
use App\Form\SnipType;
|
||||
@ -13,6 +14,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/snip', name: 'snip')]
|
||||
@ -24,20 +26,19 @@ class SnipController extends AbstractController
|
||||
) {}
|
||||
|
||||
#[Route('/', name: '_index')]
|
||||
public function index(): Response
|
||||
public function index(#[MapQueryString] SnipFilterRequest $request): Response
|
||||
{
|
||||
return $this->render('snip/index.html.twig', [
|
||||
'snips' => $this->repository->findByUser($this->getUser()),
|
||||
'title' => 'My Snips',
|
||||
'snips' => $this->repository->findByRequest($this->getUser(), $request),
|
||||
'request' => $request,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/public', name: '_public')]
|
||||
public function public(): Response
|
||||
{
|
||||
return $this->render('snip/index.html.twig', [
|
||||
return $this->render('snip/public.html.twig', [
|
||||
'snips' => $this->repository->findPublic(),
|
||||
'title' => 'Public Snips',
|
||||
]);
|
||||
}
|
||||
|
||||
@ -86,7 +87,7 @@ class SnipController extends AbstractController
|
||||
* It technically fully works, but rendering the version history needs an update first
|
||||
*/
|
||||
$isLatest = $snip->getActiveVersion() === $snip->getLatestVersion();
|
||||
if(!$isLatest) {
|
||||
if (!$isLatest) {
|
||||
$this->addFlash('error', 'Snip is not the latest version, changes will not be saved.');
|
||||
}
|
||||
|
||||
|
17
src/Dto/SnipFilterRequest.php
Normal file
17
src/Dto/SnipFilterRequest.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Dto;
|
||||
|
||||
readonly class SnipFilterRequest
|
||||
{
|
||||
public function __construct(
|
||||
public bool $onlyVisible = true,
|
||||
) {}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'onlyVisible' => $this->onlyVisible,
|
||||
];
|
||||
}
|
||||
}
|
@ -33,6 +33,9 @@ class Snip
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $parser = null;
|
||||
|
||||
#[ORM\Column]
|
||||
private bool $visible = true;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->snipContents = new ArrayCollection();
|
||||
@ -130,4 +133,16 @@ class Snip
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isVisible(): ?bool
|
||||
{
|
||||
return $this->visible;
|
||||
}
|
||||
|
||||
public function setVisible(bool $visible): static
|
||||
{
|
||||
$this->visible = $visible;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,8 @@ class SnipType extends AbstractType
|
||||
'attr' => ['rows' => 20],
|
||||
'mapped' => false,
|
||||
])
|
||||
->add('public')
|
||||
->add('public', SwitchType::class)
|
||||
->add('visible', SwitchType::class)
|
||||
;
|
||||
}
|
||||
|
||||
|
22
src/Form/SwitchType.php
Normal file
22
src/Form/SwitchType.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
|
||||
class SwitchType extends AbstractType
|
||||
{
|
||||
public function getParent(): string
|
||||
{
|
||||
return CheckboxType::class;
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options): void
|
||||
{
|
||||
$view->vars['label_attr']['class'] = trim(($view->vars['label_attr']['class'] ?? '') . ' checkbox-switch');
|
||||
$view->vars['required'] = false;
|
||||
}
|
||||
}
|
@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Dto\SnipFilterRequest;
|
||||
use App\Entity\Snip;
|
||||
use App\Entity\User;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Snip>
|
||||
@ -40,12 +42,17 @@ class SnipRepository extends ServiceEntityRepository
|
||||
}
|
||||
}
|
||||
|
||||
public function findByUser(User $user): array
|
||||
public function findByRequest(UserInterface $user, SnipFilterRequest $request): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s');
|
||||
$qb->where('s.createdBy = :user')
|
||||
->setParameter('user', $user)
|
||||
->orderBy('s.createdAt', 'DESC')
|
||||
$qb = $this
|
||||
->createQueryBuilder('s')
|
||||
->where('s.createdBy = :user')
|
||||
->setParameter('user', $user)
|
||||
->orderBy('s.createdAt', 'DESC')
|
||||
;
|
||||
|
||||
$qb->andWhere('s.visible = :visible')
|
||||
->setParameter('visible', $request->onlyVisible)
|
||||
;
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
@ -53,10 +60,11 @@ class SnipRepository extends ServiceEntityRepository
|
||||
|
||||
public function findPublic(?User $user = null): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s')
|
||||
->where('s.public = :public')
|
||||
->setParameter('public', true)
|
||||
->orderBy('s.createdAt', 'DESC')
|
||||
$qb = $this
|
||||
->createQueryBuilder('s')
|
||||
->where('s.public = true')
|
||||
->andWhere('s.visible = true')
|
||||
->orderBy('s.createdAt', 'DESC')
|
||||
;
|
||||
|
||||
if ($user) {
|
||||
|
Reference in New Issue
Block a user