diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php index ae8114ea7..6705d7bb7 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php @@ -13,6 +13,7 @@ namespace Chill\CalendarBundle\Controller; use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Form\CalendarType; +use Chill\CalendarBundle\Form\CancelType; use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface; use Chill\CalendarBundle\Repository\CalendarACLAwareRepositoryInterface; use Chill\CalendarBundle\Security\Voter\CalendarVoter; @@ -30,6 +31,7 @@ use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; use Chill\PersonBundle\Security\Authorization\PersonVoter; use Chill\ThirdPartyBundle\Entity\ThirdParty; +use Doctrine\ORM\EntityManagerInterface; use http\Exception\UnexpectedValueException; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -60,6 +62,7 @@ class CalendarController extends AbstractController private readonly UserRepositoryInterface $userRepository, private readonly TranslatorInterface $translator, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry, + private readonly EntityManagerInterface $em, ) {} /** @@ -111,6 +114,55 @@ class CalendarController extends AbstractController ]); } + #[Route(path: '/{_locale}/calendar/calendar/{id}/cancel', name: 'chill_calendar_calendar_cancel')] + public function cancelAction(Calendar $calendar, Request $request): Response + { + // Deal with sms being sent or not + // Communicate cancellation with the remote calendar. + + $this->denyAccessUnlessGranted(CalendarVoter::EDIT, $calendar); + + [$person, $accompanyingPeriod] = [$calendar->getPerson(), $calendar->getAccompanyingPeriod()]; + + $form = $this->createForm(CancelType::class, $calendar); + $form->add('submit', SubmitType::class); + + if ($accompanyingPeriod instanceof AccompanyingPeriod) { + $view = '@ChillCalendar/Calendar/cancelCalendarByAccompanyingCourse.html.twig'; + $redirectRoute = $this->generateUrl('chill_calendar_calendar_list_by_period', ['id' => $accompanyingPeriod->getId()]); + } elseif ($person instanceof Person) { + $view = '@ChillCalendar/Calendar/cancelCalendarByPerson.html.twig'; + $redirectRoute = $this->generateUrl('chill_calendar_calendar_list_by_person', ['id' => $person->getId()]); + } else { + throw new \RuntimeException('nor person or accompanying period'); + } + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + + $this->logger->notice('A calendar event has been cancelled', [ + 'by_user' => $this->getUser()->getUsername(), + 'calendar_id' => $calendar->getId(), + ]); + + $calendar->setStatus($calendar::STATUS_CANCELED); + $calendar->setSmsStatus($calendar::SMS_CANCEL_PENDING); + $this->em->flush(); + + $this->addFlash('success', $this->translator->trans('chill_calendar.calendar_canceled')); + + return new RedirectResponse($redirectRoute); + } + + return $this->render($view, [ + 'calendar' => $calendar, + 'form' => $form->createView(), + 'accompanyingCourse' => $accompanyingPeriod, + 'person' => $person, + ]); + } + /** * Edit a calendar item. */ diff --git a/src/Bundle/ChillCalendarBundle/DataFixtures/ORM/LoadCancelReason.php b/src/Bundle/ChillCalendarBundle/DataFixtures/ORM/LoadCancelReason.php index d7e552d5d..2a8e371e0 100644 --- a/src/Bundle/ChillCalendarBundle/DataFixtures/ORM/LoadCancelReason.php +++ b/src/Bundle/ChillCalendarBundle/DataFixtures/ORM/LoadCancelReason.php @@ -35,7 +35,7 @@ class LoadCancelReason extends Fixture implements FixtureGroupInterface $arr = [ ['name' => CancelReason::CANCELEDBY_USER], ['name' => CancelReason::CANCELEDBY_PERSON], - ['name' => CancelReason::CANCELEDBY_DONOTCOUNT], + ['name' => CancelReason::CANCELEDBY_OTHER], ]; foreach ($arr as $a) { diff --git a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php index dad302193..c194c247e 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php +++ b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php @@ -269,6 +269,11 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface, HasCente return $this->cancelReason; } + public function isCanceled(): bool + { + return null !== $this->cancelReason; + } + public function getCenters(): ?iterable { return match ($this->getContext()) { diff --git a/src/Bundle/ChillCalendarBundle/Entity/CancelReason.php b/src/Bundle/ChillCalendarBundle/Entity/CancelReason.php index d4a2ed9a9..04192133c 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/CancelReason.php +++ b/src/Bundle/ChillCalendarBundle/Entity/CancelReason.php @@ -18,14 +18,14 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Table(name: 'chill_calendar.cancel_reason')] class CancelReason { - final public const CANCELEDBY_DONOTCOUNT = 'CANCELEDBY_DONOTCOUNT'; + final public const CANCELEDBY_OTHER = 'CANCELEDBY_OTHER'; final public const CANCELEDBY_PERSON = 'CANCELEDBY_PERSON'; final public const CANCELEDBY_USER = 'CANCELEDBY_USER'; - #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN)] - private ?bool $active = null; + #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN, options: ['default' => true])] + private bool $active = true; #[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, length: 255)] private ?string $canceledBy = null; diff --git a/src/Bundle/ChillCalendarBundle/Form/CancelReasonType.php b/src/Bundle/ChillCalendarBundle/Form/CancelReasonType.php index c0ac4ddb0..311d3ac02 100644 --- a/src/Bundle/ChillCalendarBundle/Form/CancelReasonType.php +++ b/src/Bundle/ChillCalendarBundle/Form/CancelReasonType.php @@ -15,7 +15,7 @@ use Chill\CalendarBundle\Entity\CancelReason; use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; -use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -28,7 +28,14 @@ class CancelReasonType extends AbstractType ->add('active', CheckboxType::class, [ 'required' => false, ]) - ->add('canceledBy', TextType::class); + ->add('canceledBy', ChoiceType::class, [ + 'choices' => [ + 'chill_calendar.canceled_by.user' => CancelReason::CANCELEDBY_USER, + 'chill_calendar.canceled_by.person' => CancelReason::CANCELEDBY_PERSON, + 'chill_calendar.canceled_by.other' => CancelReason::CANCELEDBY_OTHER, + ], + 'required' => true, + ]); } public function configureOptions(OptionsResolver $resolver) diff --git a/src/Bundle/ChillCalendarBundle/Form/CancelType.php b/src/Bundle/ChillCalendarBundle/Form/CancelType.php new file mode 100644 index 000000000..ad41a6105 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Form/CancelType.php @@ -0,0 +1,42 @@ +add('cancelReason', EntityType::class, [ + 'class' => CancelReason::class, + 'required' => true, + 'choice_label' => fn (CancelReason $cancelReason) => $this->translatableStringHelper->localize($cancelReason->getName()), + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Calendar::class, + + ]); + } +} diff --git a/src/Bundle/ChillCalendarBundle/Messenger/Doctrine/CalendarEntityListener.php b/src/Bundle/ChillCalendarBundle/Messenger/Doctrine/CalendarEntityListener.php index 8f62fdcdb..2319fde99 100644 --- a/src/Bundle/ChillCalendarBundle/Messenger/Doctrine/CalendarEntityListener.php +++ b/src/Bundle/ChillCalendarBundle/Messenger/Doctrine/CalendarEntityListener.php @@ -21,6 +21,7 @@ namespace Chill\CalendarBundle\Messenger\Doctrine; use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Messenger\Message\CalendarMessage; use Chill\CalendarBundle\Messenger\Message\CalendarRemovedMessage; +use Chill\MainBundle\Entity\User; use Doctrine\ORM\Event\PostPersistEventArgs; use Doctrine\ORM\Event\PostRemoveEventArgs; use Doctrine\ORM\Event\PostUpdateEventArgs; @@ -31,6 +32,17 @@ class CalendarEntityListener { public function __construct(private readonly MessageBusInterface $messageBus, private readonly Security $security) {} + private function getAuthenticatedUser(): User + { + $user = $this->security->getUser(); + + if (!$user instanceof User) { + throw new \LogicException('Expected an instance of User.'); + } + + return $user; + } + public function postPersist(Calendar $calendar, PostPersistEventArgs $args): void { if (!$calendar->preventEnqueueChanges) { @@ -38,7 +50,7 @@ class CalendarEntityListener new CalendarMessage( $calendar, CalendarMessage::CALENDAR_PERSIST, - $this->security->getUser() + $this->getAuthenticatedUser() ) ); } @@ -50,7 +62,7 @@ class CalendarEntityListener $this->messageBus->dispatch( new CalendarRemovedMessage( $calendar, - $this->security->getUser() + $this->getAuthenticatedUser() ) ); } @@ -58,12 +70,19 @@ class CalendarEntityListener public function postUpdate(Calendar $calendar, PostUpdateEventArgs $args): void { - if (!$calendar->preventEnqueueChanges) { + if ($calendar->getStatus() === $calendar::STATUS_CANCELED) { + $this->messageBus->dispatch( + new CalendarRemovedMessage( + $calendar, + $this->getAuthenticatedUser() + ) + ); + } elseif (!$calendar->preventEnqueueChanges) { $this->messageBus->dispatch( new CalendarMessage( $calendar, CalendarMessage::CALENDAR_UPDATE, - $this->security->getUser() + $this->getAuthenticatedUser() ) ); } diff --git a/src/Bundle/ChillCalendarBundle/Messenger/Message/CalendarRemovedMessage.php b/src/Bundle/ChillCalendarBundle/Messenger/Message/CalendarRemovedMessage.php index 53dcea28c..7d2e88fef 100644 --- a/src/Bundle/ChillCalendarBundle/Messenger/Message/CalendarRemovedMessage.php +++ b/src/Bundle/ChillCalendarBundle/Messenger/Message/CalendarRemovedMessage.php @@ -70,6 +70,8 @@ class CalendarRemovedMessage public function getRemoteId(): string { + dump($this->remoteId); + return $this->remoteId; } } diff --git a/src/Bundle/ChillCalendarBundle/Repository/CalendarRepository.php b/src/Bundle/ChillCalendarBundle/Repository/CalendarRepository.php index 3121854e5..e6f90f279 100644 --- a/src/Bundle/ChillCalendarBundle/Repository/CalendarRepository.php +++ b/src/Bundle/ChillCalendarBundle/Repository/CalendarRepository.php @@ -191,6 +191,7 @@ class CalendarRepository implements ObjectRepository $qb->expr()->eq('c.mainUser', ':user'), $qb->expr()->gte('c.startDate', ':startDate'), $qb->expr()->lte('c.endDate', ':endDate'), + $qb->expr()->isNull('c.cancelReason'), ) ) ->setParameters([ diff --git a/src/Bundle/ChillCalendarBundle/Resources/config/services/controller.yml b/src/Bundle/ChillCalendarBundle/Resources/config/services/controller.yml index a0457c5a8..cce562d7d 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/config/services/controller.yml +++ b/src/Bundle/ChillCalendarBundle/Resources/config/services/controller.yml @@ -1,5 +1,6 @@ services: Chill\CalendarBundle\Controller\: autowire: true + autoconfigure: true resource: '../../../Controller' tags: ['controller.service_arguments'] diff --git a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/_list.html.twig b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/_list.html.twig index 01241643b..c827ca4f4 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/_list.html.twig +++ b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/_list.html.twig @@ -5,29 +5,45 @@
- {% if context == 'person' and calendar.context == 'accompanying_period' %} - - - {{ calendar.accompanyingPeriod.id }} - - - {% endif %} - {% if calendar.endDate.diff(calendar.startDate).days >= 1 %} - {{ calendar.startDate|format_datetime('short', 'short') }} - - {{ calendar.endDate|format_datetime('short', 'short') }} - {% else %} - {{ calendar.startDate|format_datetime('short', 'short') }} - - {{ calendar.endDate|format_datetime('none', 'short') }} - {% endif %} -
+ {% if calendar.status == 'canceled' %} +
+ {% if calendar.status == 'canceled' %}
+
+ {% endif %}
+ {% if context == 'person' and calendar.context == 'accompanying_period' %}
+
+
+ {{ calendar.accompanyingPeriod.id }}
+
+
+ {% endif %}
+ {% if calendar.status == 'canceled' %}
+
+ {% endif %}
+ {% if calendar.endDate.diff(calendar.startDate).days >= 1 %}
+ {{ calendar.startDate|format_datetime('short', 'short') }}
+ - {{ calendar.endDate|format_datetime('short', 'short') }}
+ {% else %}
+ {{ calendar.startDate|format_datetime('short', 'short') }}
+ - {{ calendar.endDate|format_datetime('none', 'short') }}
+ {% endif %}
+ {% if calendar.status == 'canceled' %}
+
+ {% endif %}
{% if calendar.comment.comment is not empty
- or calendar.users|length > 0
- or calendar.thirdParties|length > 0
- or calendar.users|length > 0 %}
-
- {% if is_granted('CHILL_CALENDAR_DOC_EDIT', calendar) %}
+ {% if is_granted('CHILL_CALENDAR_DOC_EDIT', calendar) and calendar.status is not constant('STATUS_CANCELED', calendar) %}
{% if templates|length == 0 %}
+ {% if calendar.isInvited(app.user) and not calendar.isCanceled %}
+ {% set invite = calendar.inviteForUser(app.user) %}
+ {{ 'Id'|trans }}
{{ 'Name'|trans }}
- {{ 'canceledBy'|trans }}
+ {{ 'Canceled by'|trans }}
{{ 'active'|trans }}
{% endblock %}
@@ -40,4 +40,4 @@
{% endblock %}
{% endembed %}
-{% endblock %}
\ No newline at end of file
+{% endblock %}
diff --git a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilder.php b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilder.php
index dfce0548c..bce9b1487 100644
--- a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilder.php
+++ b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilder.php
@@ -19,6 +19,7 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\Service\ShortMessageNotification;
use Chill\CalendarBundle\Entity\Calendar;
+use Chill\CalendarBundle\Entity\CancelReason;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use Symfony\Component\Notifier\Message\SmsMessage;
@@ -57,7 +58,7 @@ class DefaultShortMessageForCalendarBuilder implements ShortMessageForCalendarBu
$this->phoneUtil->format($person->getMobilenumber(), PhoneNumberFormat::E164),
$this->engine->render('@ChillCalendar/CalendarShortMessage/short_message.txt.twig', ['calendar' => $calendar]),
);
- } elseif (Calendar::SMS_CANCEL_PENDING === $calendar->getSmsStatus()) {
+ } elseif (Calendar::SMS_CANCEL_PENDING === $calendar->getSmsStatus() && (null === $calendar->getCancelReason() || CancelReason::CANCELEDBY_PERSON !== $calendar->getCancelReason()->getCanceledBy())) {
$toUsers[] = new SmsMessage(
$this->phoneUtil->format($person->getMobilenumber(), PhoneNumberFormat::E164),
$this->engine->render('@ChillCalendar/CalendarShortMessage/short_message_canceled.txt.twig', ['calendar' => $calendar]),
diff --git a/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml b/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml
index 179597346..8717dcd9f 100644
--- a/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml
+++ b/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml
@@ -31,8 +31,7 @@ Will send SMS: Un SMS de rappel sera envoyé
Will not send SMS: Aucun SMS de rappel ne sera envoyé
SMS already sent: Un SMS a été envoyé
-canceledBy: supprimé par
-Canceled by: supprimé par
+Canceled by: Annulé par
Calendar configuration: Gestion des rendez-vous
crud:
@@ -44,6 +43,14 @@ crud:
title_edit: Modifier le motif d'annulation
chill_calendar:
+ canceled: Annulé
+ cancel_reason: Raison d'annulation
+ cancel_calendar_item: Annuler rendez-vous
+ calendar_canceled: Le rendez-vous a été annulé
+ canceled_by:
+ user: Utilisateur
+ person: Usager
+ other: Autre
Document: Document d'un rendez-vous
form:
The main user is mandatory. He will organize the appointment.: L'utilisateur principal est obligatoire. Il est l'organisateur de l'événement.
diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php
index 19e9014da..ccba39848 100644
--- a/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php
+++ b/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php
@@ -102,7 +102,6 @@ class CRUDController extends AbstractController
Resolver::class => Resolver::class,
SerializerInterface::class => SerializerInterface::class,
FilterOrderHelperFactoryInterface::class => FilterOrderHelperFactoryInterface::class,
- ManagerRegistry::class => ManagerRegistry::class,
]
);
}
@@ -674,7 +673,7 @@ class CRUDController extends AbstractController
protected function getManagerRegistry(): ManagerRegistry
{
- return $this->container->get(ManagerRegistry::class);
+ return $this->container->get('doctrine');
}
/**