From 800f051379337e7ee72c59e9a5a3d849e569cfda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 3 Jan 2024 16:54:43 +0100 Subject: [PATCH] Add QOTD context --- migrations/Version20240103154351.php | 31 ++++++++++++++++++++++++ src/Controller/QotdController.php | 17 +++---------- src/Entity/Qotd.php | 22 +++++++++++++++++ src/Form/Extension/TextTypeExtension.php | 22 +++++++++++++++++ src/Form/Type/QotdType.php | 19 +++++++++++++++ templates/macros/qotd/message.html.twig | 7 +++++- templates/qotd/show_details.html.twig | 8 ++++++ tests/Controller/QotdControllerTest.php | 2 +- 8 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 migrations/Version20240103154351.php create mode 100644 src/Form/Extension/TextTypeExtension.php create mode 100644 src/Form/Type/QotdType.php diff --git a/migrations/Version20240103154351.php b/migrations/Version20240103154351.php new file mode 100644 index 0000000..180660c --- /dev/null +++ b/migrations/Version20240103154351.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE qotd ADD context TEXT'); + $this->addSql('UPDATE qotd SET context = \'\''); + $this->addSql('ALTER TABLE qotd ALTER context SET NOT NULL'); + $this->addSql('ALTER TABLE qotd ADD updated_at TIMESTAMP(0) WITHOUT TIME ZONE'); + $this->addSql('COMMENT ON COLUMN qotd.updated_at IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('UPDATE qotd SET updated_at = NOW()'); + $this->addSql('ALTER TABLE qotd ALTER updated_at SET NOT NULL'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE qotd DROP context'); + $this->addSql('ALTER TABLE qotd DROP updated_at'); + } +} diff --git a/src/Controller/QotdController.php b/src/Controller/QotdController.php index e1b8694..f966d27 100644 --- a/src/Controller/QotdController.php +++ b/src/Controller/QotdController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Entity\Qotd; +use App\Form\Type\QotdType; use App\Repository\Model\QotdDirection; use App\Repository\Model\QotdVote; use App\Repository\QotdRepository; @@ -15,8 +16,6 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Http\Attribute\CurrentUser; -use Symfony\Component\Validator\Constraints\Length; -use Symfony\Component\Validator\Constraints\NotBlank; class QotdController extends AbstractController { @@ -87,17 +86,9 @@ public function edit( Request $request, #[MapEntity()] Qotd $qotd, ): Response { - $form = $this->createFormBuilder( - $qotd, - [ - 'action' => $this->generateUrl('qotd_show_edit', ['id' => $qotd->id]), - ]) - ->add('message', null, [ - 'constraints' => [new NotBlank(), new Length(min: 10)], - 'required' => false, - ]) - ->getForm() - ; + $form = $this->createForm(QotdType::class, $qotd, [ + 'action' => $this->generateUrl('qotd_show_edit', ['id' => $qotd->id]), + ]); if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) { $this->em->flush(); diff --git a/src/Entity/Qotd.php b/src/Entity/Qotd.php index 6109884..40b1d6b 100644 --- a/src/Entity/Qotd.php +++ b/src/Entity/Qotd.php @@ -6,10 +6,13 @@ use App\Repository\QotdRepository; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Doctrine\ORM\Mapping\HasLifecycleCallbacks; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Validator\Constraints as Assert; #[ORM\Entity(repositoryClass: QotdRepository::class)] +#[HasLifecycleCallbacks] class Qotd { #[Groups(['qotd:read'])] @@ -29,6 +32,13 @@ class Qotd #[ORM\Column(type: Types::JSON, options: ['jsonb' => true])] public array $images = []; + #[Assert\Length(min: 5)] + #[ORM\Column(type: Types::TEXT)] + public string $context = ''; + + #[ORM\Column()] + public \DateTimeImmutable $updatedAt; + public function __construct( #[Groups(['qotd:read'])] #[ORM\Column(type: Types::DATE_IMMUTABLE)] @@ -47,6 +57,7 @@ public function __construct( public readonly string $username, ) { $this->id = uuid_create(); + $this->updatedAt = new \DateTimeImmutable(); } public function applyVote(QotdVote $vote, UserInterface $user): void @@ -86,4 +97,15 @@ public function getImageUrls(): array { return array_map(fn (string $image) => sprintf('uploads/%s---%s', $this->id, $image), $this->images); } + + #[ORM\PreUpdate] + public function onUpdate(): void + { + $this->updatedAt = new \DateTimeImmutable(); + } + + public function getContext(): string + { + return $this->context; + } } diff --git a/src/Form/Extension/TextTypeExtension.php b/src/Form/Extension/TextTypeExtension.php new file mode 100644 index 0000000..586163b --- /dev/null +++ b/src/Form/Extension/TextTypeExtension.php @@ -0,0 +1,22 @@ +setDefaults([ + 'empty_data' => '', + ]); + } + + public static function getExtendedTypes(): iterable + { + yield TextType::class; + } +} diff --git a/src/Form/Type/QotdType.php b/src/Form/Type/QotdType.php new file mode 100644 index 0000000..bb28b6b --- /dev/null +++ b/src/Form/Type/QotdType.php @@ -0,0 +1,19 @@ +add('context', TextareaType::class, [ + 'required' => false, + ]) + ; + } +} diff --git a/templates/macros/qotd/message.html.twig b/templates/macros/qotd/message.html.twig index 717b3de..1e8026b 100644 --- a/templates/macros/qotd/message.html.twig +++ b/templates/macros/qotd/message.html.twig @@ -1,6 +1,11 @@ {% macro message(qotd) %} - {% cache "qotd-message-#{qotd.message}" %} + {% cache "qotd-message-#{qotd.id}-#{qotd.updatedAt.format('c')}" %} {# we use |replace because it better like that! #} + {% if qotd.context %} +
+ {{ qotd.context|replace_emoji|replace_username|replace({'\n': '\n\n'})|markdown_to_html }} +
+ {% endif %} {{ qotd.message|replace_emoji|replace_username|replace({'\n': '\n\n'})|markdown_to_html }} {% endcache %} {% endmacro %} diff --git a/templates/qotd/show_details.html.twig b/templates/qotd/show_details.html.twig index 703c2eb..399baf3 100644 --- a/templates/qotd/show_details.html.twig +++ b/templates/qotd/show_details.html.twig @@ -21,6 +21,14 @@ + + Raw Context + +
+
{{ qotd.context|nl2br }}
+
+ + Message {{ qotd_macro.message(qotd) }} diff --git a/tests/Controller/QotdControllerTest.php b/tests/Controller/QotdControllerTest.php index 33823bc..0b9fe1a 100644 --- a/tests/Controller/QotdControllerTest.php +++ b/tests/Controller/QotdControllerTest.php @@ -62,7 +62,7 @@ public function testNotVotedYet(): void self::assertCount(20, $quotes); $firstQuote = $quotes->first(); - $id = str_replace('qotd-default-', '', $firstQuote->attr('id')); + $id = str_replace('qotd-', '', $firstQuote->attr('id')); try { $form = $firstQuote->filter('[data-test=vote-up]')->form();