From 5ae5db985be09bb13676522c1024ad2f3fa2a60f Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 13 May 2025 13:47:33 +0200 Subject: [PATCH] Implement snip content names --- migrations/Version20250513103236.php | 35 +++++++++++++++++++ src/Controller/SnipController.php | 6 +++- src/Entity/SnipContent.php | 15 ++++++++ src/Form/SnipType.php | 6 ++++ .../SnipContent/SnipContentService.php | 7 ++-- templates/generic/datetime.badge.html.twig | 1 + templates/snip/single.html.twig | 5 +-- templates/version/index.html.twig | 9 +++-- 8 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 migrations/Version20250513103236.php create mode 100644 templates/generic/datetime.badge.html.twig diff --git a/migrations/Version20250513103236.php b/migrations/Version20250513103236.php new file mode 100644 index 0000000..26496f4 --- /dev/null +++ b/migrations/Version20250513103236.php @@ -0,0 +1,35 @@ +addSql(<<<'SQL' + ALTER TABLE snip_content ADD name VARCHAR(255) DEFAULT NULL + SQL); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql(<<<'SQL' + ALTER TABLE snip_content DROP name + SQL); + } +} diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 96c633f..746a12e 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -104,7 +104,11 @@ class SnipController extends AbstractController ]); } $this->repository->save($snip); - $contentService->update($snip, $form->get('content')->getData()); + $contentService->update( + $snip, + $form->get('content')->getData(), + $form->get('contentName')->getData() + ); $this->addFlash('success', sprintf('Snip "%s" saved', $snip)); diff --git a/src/Entity/SnipContent.php b/src/Entity/SnipContent.php index babb55e..1cc8305 100644 --- a/src/Entity/SnipContent.php +++ b/src/Entity/SnipContent.php @@ -35,6 +35,9 @@ class SnipContent #[ORM\Column(nullable: true)] private ?array $diff = null; + #[ORM\Column(length: 255, nullable: true)] + private ?string $name = null; + public function __construct() { $this->children = new ArrayCollection(); @@ -122,4 +125,16 @@ class SnipContent return $this; } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(?string $name): static + { + $this->name = $name; + + return $this; + } } diff --git a/src/Form/SnipType.php b/src/Form/SnipType.php index bbfc000..685153a 100644 --- a/src/Form/SnipType.php +++ b/src/Form/SnipType.php @@ -6,7 +6,9 @@ use App\Entity\Snip; use App\Service\SnipParser\ParserFactory; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -31,6 +33,10 @@ class SnipType extends AbstractType ->add('tags', TagsType::class) ->add('public', SwitchType::class) ->add('visible', SwitchType::class) + ->add('contentName', TextType::class, [ + 'label' => 'Change description (optional)', + 'mapped' => false, + ]) ; } diff --git a/src/Service/SnipContent/SnipContentService.php b/src/Service/SnipContent/SnipContentService.php index 5c9270f..e254892 100644 --- a/src/Service/SnipContent/SnipContentService.php +++ b/src/Service/SnipContent/SnipContentService.php @@ -13,18 +13,19 @@ readonly class SnipContentService private EntityManagerInterface $em, ) {} - public function update(Snip $snip, string $snipContents): void + public function update(Snip $snip, string $contents, ?string $contentName): void { $parentContent = $snip->getActiveVersion(); - if (self::rebuildText($parentContent) === $snipContents) { + if (self::rebuildText($parentContent) === $contents) { return; } // Create new snipContent entity with previous one as parent $content = new SnipContent(); $content - ->setText($snipContents) + ->setText($contents) ->setSnip($snip) + ->setName($contentName) ; if ($parentContent !== null) { $content->setParent($parentContent); diff --git a/templates/generic/datetime.badge.html.twig b/templates/generic/datetime.badge.html.twig new file mode 100644 index 0000000..1f9f6de --- /dev/null +++ b/templates/generic/datetime.badge.html.twig @@ -0,0 +1 @@ +{{ date|date('Y-m-d H:i:s') }} \ No newline at end of file diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index 4fecd81..be60e9a 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -54,10 +54,7 @@

Current version: {{ snip.activeVersion.id }} {% if snip.activeVersion == snip.latestVersion %}(latest){% endif %} - - Created at {{ snip.activeVersion.id.dateTime|date('Y-m-d H:i:s') }} - - + Created at {{ include('generic/datetime.badge.html.twig', {date: snip.activeVersion.id.dateTime}) }}

diff --git a/templates/version/index.html.twig b/templates/version/index.html.twig index 4cc09c7..d9eac2c 100644 --- a/templates/version/index.html.twig +++ b/templates/version/index.html.twig @@ -15,8 +15,13 @@

{% for version in snip.snipContents|reverse %} - - {{ version.id.dateTime|date('Y-m-d H:i:s') }} - {{ version.id }} + + + {{ include('generic/datetime.badge.html.twig', {date: version.id.dateTime}) }} + {% if version.name %}{{ version.name }}{% endif %} + + {{ version.id }} {% endfor %}