From 4c31459bd5ce9194d7e36449b64dddc5f8b4e2d3 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 22 Jul 2025 16:42:13 +0200 Subject: [PATCH] Add an enddate for user absence and allow a time to be set --- src/Bundle/ChillMainBundle/Entity/User.php | 20 ++++++- .../ChillMainBundle/Form/AbsenceType.php | 10 +++- .../Resources/views/Menu/absence.html.twig | 53 +++++++++---------- .../migrations/Version20250722140048.php | 27 ++++++++++ .../translations/messages.fr.yml | 3 +- 5 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/migrations/Version20250722140048.php diff --git a/src/Bundle/ChillMainBundle/Entity/User.php b/src/Bundle/ChillMainBundle/Entity/User.php index 61263ef85..4ff7aca14 100644 --- a/src/Bundle/ChillMainBundle/Entity/User.php +++ b/src/Bundle/ChillMainBundle/Entity/User.php @@ -45,6 +45,8 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: true)] private ?\DateTimeImmutable $absenceStart = null; + #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: true)] + private ?\DateTimeImmutable $absenceEnd = null; /** * Array where SAML attributes's data are stored. */ @@ -157,6 +159,11 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter return $this->absenceStart; } + public function getAbsenceEnd(): ?\DateTimeImmutable + { + return $this->absenceEnd; + } + /** * Get attributes. * @@ -336,7 +343,13 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter public function isAbsent(): bool { - return null !== $this->getAbsenceStart() && $this->getAbsenceStart() <= new \DateTimeImmutable('now'); + $now = new \DateTimeImmutable('now'); + $absenceStart = $this->getAbsenceStart(); + $absenceEnd = $this->getAbsenceEnd(); + + return null !== $absenceStart + && $absenceStart <= $now + && (null === $absenceEnd || $now <= $absenceEnd); } /** @@ -410,6 +423,11 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter $this->absenceStart = $absenceStart; } + public function setAbsenceEnd(?\DateTimeImmutable $absenceEnd): void + { + $this->absenceEnd = $absenceEnd; + } + public function setAttributeByDomain(string $domain, string $key, $value): self { $this->attributes[$domain][$key] = $value; diff --git a/src/Bundle/ChillMainBundle/Form/AbsenceType.php b/src/Bundle/ChillMainBundle/Form/AbsenceType.php index d0aed640c..a10e3c0a2 100644 --- a/src/Bundle/ChillMainBundle/Form/AbsenceType.php +++ b/src/Bundle/ChillMainBundle/Form/AbsenceType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Form; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Form\Type\ChillDateTimeType; use Chill\MainBundle\Form\Type\ChillDateType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; @@ -22,10 +23,15 @@ class AbsenceType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('absenceStart', ChillDateType::class, [ - 'required' => true, + ->add('absenceStart', ChillDateTimeType::class, [ + 'required' => false, 'input' => 'datetime_immutable', 'label' => 'absence.Absence start', + ]) + ->add('absenceEnd', ChillDateTimeType::class, [ + 'required' => false, + 'input' => 'datetime_immutable', + 'label' => 'absence.Absence end', ]); } diff --git a/src/Bundle/ChillMainBundle/Resources/views/Menu/absence.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Menu/absence.html.twig index 3258b5ac1..687d6f307 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Menu/absence.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Menu/absence.html.twig @@ -8,36 +8,31 @@

{{ 'absence.My absence'|trans }}

+
+ {% if user.absenceStart is not null %} +
{{ 'absence.You are listed as absent, as of %date%'|trans({'%date%': user.absenceStart|format_datetime('short') }) }} + {% if user.absenceEnd is not null %} + {{ 'until %date%'|trans({'%date%': user.absenceEnd|format_datetime('short') }) }} + {% endif %} +
+ {% else %} +
{{ 'absence.No absence listed'|trans }}
+ {% endif %} +
+
+ {{ form_start(form) }} + {{ form_row(form.absenceStart) }} + {{ form_row(form.absenceEnd) }} - {% if user.absenceStart is not null %} -
-

{{ 'absence.You are listed as absent, as of'|trans }} {{ user.absenceStart|format_date('long') }}

- -
- {% else %} -
-

{{ 'absence.No absence listed'|trans }}

-
-
- {{ form_start(form) }} - {{ form_row(form.absenceStart) }} - -
    -
  • - -
  • -
- - {{ form_end(form) }} -
- {% endif %} +
    +
  • + +
  • +
+ {{ form_end(form) }} +
{% endblock %} diff --git a/src/Bundle/ChillMainBundle/migrations/Version20250722140048.php b/src/Bundle/ChillMainBundle/migrations/Version20250722140048.php new file mode 100644 index 000000000..64443047b --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20250722140048.php @@ -0,0 +1,27 @@ +addSql('ALTER TABLE users ADD absenceEnd TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL'); + $this->addSql('COMMENT ON COLUMN users.absenceEnd IS \'(DC2Type:datetime_immutable)\''); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE users DROP absenceEnd'); + } +} diff --git a/src/Bundle/ChillMainBundle/translations/messages.fr.yml b/src/Bundle/ChillMainBundle/translations/messages.fr.yml index 055575516..f692bce84 100644 --- a/src/Bundle/ChillMainBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/messages.fr.yml @@ -844,9 +844,10 @@ absence: Unset absence: Supprimer la date d'absence Set absence date: Indiquer une date d'absence Absence start: Absent à partir du + Absence end: Jusqu'au Absent: Absent You are marked as being absent: Vous êtes indiqué absent. - You are listed as absent, as of: Votre absence est indiquée à partir du + You are listed as absent, as of %date%: Votre absence est indiquée à partir du %date% No absence listed: Aucune absence indiquée. Is absent: Absent?