Implement snip tags with very elegant tags form

This commit is contained in:
Tim 2025-05-10 20:06:16 +02:00
parent 47ea226ed7
commit e2bd1a7c3b
7 changed files with 325 additions and 8 deletions

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250510180413 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql(<<<'SQL'
CREATE TABLE tag (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, name VARCHAR(255) NOT NULL, INDEX IDX_389B783A76ED395 (user_id), UNIQUE INDEX user_tag_unique (name, user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
SQL);
$this->addSql(<<<'SQL'
CREATE TABLE tag_snip (tag_id INT NOT NULL, snip_id INT NOT NULL, INDEX IDX_10B22820BAD26311 (tag_id), INDEX IDX_10B22820140FD260 (snip_id), PRIMARY KEY(tag_id, snip_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
SQL);
$this->addSql(<<<'SQL'
ALTER TABLE tag ADD CONSTRAINT FK_389B783A76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id)
SQL);
$this->addSql(<<<'SQL'
ALTER TABLE tag_snip ADD CONSTRAINT FK_10B22820BAD26311 FOREIGN KEY (tag_id) REFERENCES tag (id) ON DELETE CASCADE
SQL);
$this->addSql(<<<'SQL'
ALTER TABLE tag_snip ADD CONSTRAINT FK_10B22820140FD260 FOREIGN KEY (snip_id) REFERENCES snip (id) ON DELETE CASCADE
SQL);
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql(<<<'SQL'
ALTER TABLE tag DROP FOREIGN KEY FK_389B783A76ED395
SQL);
$this->addSql(<<<'SQL'
ALTER TABLE tag_snip DROP FOREIGN KEY FK_10B22820BAD26311
SQL);
$this->addSql(<<<'SQL'
ALTER TABLE tag_snip DROP FOREIGN KEY FK_10B22820140FD260
SQL);
$this->addSql(<<<'SQL'
DROP TABLE tag
SQL);
$this->addSql(<<<'SQL'
DROP TABLE tag_snip
SQL);
}
}

View File

@ -39,9 +39,16 @@ class Snip
#[ORM\Column]
private bool $archived = false;
/**
* @var Collection<int, Tag>
*/
#[ORM\ManyToMany(targetEntity: Tag::class, mappedBy: 'snips')]
private Collection $tags;
public function __construct()
{
$this->snipContents = new ArrayCollection();
$this->tags = new ArrayCollection();
}
public function __toString(): string
@ -160,4 +167,31 @@ class Snip
return $this;
}
/**
* @return Collection<int, Tag>
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(Tag $tag): static
{
if (!$this->tags->contains($tag)) {
$this->tags->add($tag);
$tag->addSnip($this);
}
return $this;
}
public function removeTag(Tag $tag): static
{
if ($this->tags->removeElement($tag)) {
$tag->removeSnip($this);
}
return $this;
}
}

94
src/Entity/Tag.php Normal file
View File

@ -0,0 +1,94 @@
<?php
namespace App\Entity;
use App\Repository\TagRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TagRepository::class)]
#[ORM\UniqueConstraint(name: 'user_tag_unique', columns: ['name', 'user_id'])]
class Tag
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
/**
* @var Collection<int, Snip>
*/
#[ORM\ManyToMany(targetEntity: Snip::class, inversedBy: 'tags')]
private Collection $snips;
public function __construct()
{
$this->snips = new ArrayCollection();
}
public function __toString(): string
{
return $this->name ?? '';
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
/**
* @return Collection<int, Snip>
*/
public function getSnips(): Collection
{
return $this->snips;
}
public function addSnip(Snip $snip): static
{
if (!$this->snips->contains($snip)) {
$this->snips->add($snip);
}
return $this;
}
public function removeSnip(Snip $snip): static
{
$this->snips->removeElement($snip);
return $this;
}
}

View File

@ -28,6 +28,7 @@ class SnipType extends AbstractType
'attr' => ['rows' => 20],
'mapped' => false,
])
->add('tags', TagsType::class)
->add('public', SwitchType::class)
->add('visible', SwitchType::class)
;

79
src/Form/TagsType.php Normal file
View File

@ -0,0 +1,79 @@
<?php
namespace App\Form;
use App\Entity\Tag;
use App\Repository\TagRepository;
use Doctrine\Common\Collections\Collection;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TagsType extends AbstractType implements DataTransformerInterface
{
public function __construct(
private readonly TagRepository $repository,
private readonly Security $security,
) {}
public function transform($value): string
{
if ($value === null) {
return '';
}
if ($value instanceof Collection) {
$value = $value->toArray();
}
if (is_array($value)) {
$tags = array_map(fn(Tag $tag) => $tag->getName(), $value);
} else {
return '';
}
return implode(', ', $tags);
}
public function reverseTransform($value): array
{
$tags = array_filter(array_map('trim', explode(',', $value)));
$user = $this->security->getUser();
$tagEntities = [];
foreach ($tags as $tag) {
$tagEntity = $this->repository->findOneBy(['name' => $tag, 'user' => $user]);
if ($tagEntity === null) {
$tagEntity = new Tag()
->setName($tag)
->setUser($user)
;
$this->repository->save($tagEntity);
}
$tagEntities[] = $tagEntity;
}
return $tagEntities;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer($this);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => null, // No specific entity class
'label' => 'Tags (comma-separated)',
'attr' => ['class' => 'tags-input'], // Optional: Add custom attributes
]);
}
public function getParent(): string
{
return TextType::class;
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Repository;
use App\Entity\Tag;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Tag>
*/
class TagRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Tag::class);
}
public function save(Tag $tag): void
{
$this->getEntityManager()->persist($tag);
$this->getEntityManager()->flush();
}
// /**
// * @return Tag[] Returns an array of Tag objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('t')
// ->andWhere('t.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('t.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Tag
// {
// return $this->createQueryBuilder('t')
// ->andWhere('t.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@ -19,15 +19,13 @@
<a class="btn btn-warning" href="{{ path('snip_edit', {snip: snip.id}) }}">
<i class="fa fa-pencil" aria-hidden="true"></i> Edit
</a>
<a href="{{ path('snip_archive', {snip: snip.id}) }}" class="btn btn-secondary">
{% if snip.archived %}
<a href="{{ path('snip_archive', {snip: snip.id}) }}" class="btn btn-secondary">
<i class="fa fa-undo"></i> Unarchive
</a>
{% else %}
<a href="{{ path('snip_archive', {snip: snip.id}) }}" class="btn btn-secondary">
<i class="fa fa-archive"></i> Archive
</a>
{% endif %}
</a>
<a href="{{ path('snip_delete', {snip: snip.id}) }}" class="btn btn-danger">
<i class="fa fa-trash"></i> Delete
</a>
@ -39,7 +37,10 @@
<div class="card" style="width: 100%;">
<h4 class="card-header">
{{ include('snip/badge.html.twig', {snip: snip}) }}
{{ snip }} <small class="text-muted">#{{ snip.id }}</small>
{{ snip }} <small class="text-muted">#{{ snip.id }}</small><br>
{% for tag in snip.tags %}
<span class="badge bg-secondary">{{ tag }}</span>
{% endfor %}
</h4>
<div class="card-body">
{{ content|raw }}