diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php index d72c77de1..1f01832fc 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php @@ -80,12 +80,13 @@ class CalendarController extends AbstractController * * @Route("/{_locale}/calendar/{id}/delete", name="chill_calendar_calendar_delete") */ - public function deleteAction(Request $request, int $id) + public function deleteAction(Request $request, Calendar $entity) { $view = null; $em = $this->getDoctrine()->getManager(); - [$user, $accompanyingPeriod] = $this->getEntity($request); + $accompanyingPeriod = $entity->getAccompanyingPeriod(); + $user = null; // TODO legacy code ? remove it ? if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = '@ChillCalendar/Calendar/confirm_deleteByAccompanyingCourse.html.twig'; @@ -93,14 +94,7 @@ class CalendarController extends AbstractController $view = '@ChillCalendar/Calendar/confirm_deleteByUser.html.twig'; } - /** @var Calendar $entity */ - $entity = $em->getRepository(\Chill\CalendarBundle\Entity\Calendar::class)->find($id); - - if (!$entity) { - throw $this->createNotFoundException('Unable to find Calendar entity.'); - } - - $form = $this->createDeleteForm($id, $user, $accompanyingPeriod); + $form = $this->createDeleteForm($entity->getId(), $user, $accompanyingPeriod); if ($request->getMethod() === Request::METHOD_DELETE) { $form->handleRequest($request); @@ -119,7 +113,7 @@ class CalendarController extends AbstractController $params = $this->buildParamsToUrl($user, $accompanyingPeriod); - return $this->redirectToRoute('chill_calendar_calendar_list', $params); + return $this->redirectToRoute('chill_calendar_calendar_list_by_period', $params); } } diff --git a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php index f5d73e34c..6c7e9e503 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php +++ b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php @@ -149,9 +149,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface private ?User $mainUser = null; /** - * @ORM\ManyToMany( - * targetEntity="Chill\PersonBundle\Entity\Person", - * cascade={"persist", "remove", "merge", "detach"}) + * @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person") * @ORM\JoinTable(name="chill_calendar.calendar_to_persons") * @Serializer\Groups({"calendar:read", "read"}) */ @@ -164,9 +162,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface private PrivateCommentEmbeddable $privateComment; /** - * @ORM\ManyToMany( - * targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty", - * cascade={"persist", "remove", "merge", "detach"}) + * @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty") * @ORM\JoinTable(name="chill_calendar.calendar_to_thirdparties") * @Serializer\Groups({"calendar:read", "read"}) */ diff --git a/src/Bundle/ChillCalendarBundle/Messenger/Doctrine/CalendarEntityListener.php b/src/Bundle/ChillCalendarBundle/Messenger/Doctrine/CalendarEntityListener.php index 2fe54cd1d..cbb765fba 100644 --- a/src/Bundle/ChillCalendarBundle/Messenger/Doctrine/CalendarEntityListener.php +++ b/src/Bundle/ChillCalendarBundle/Messenger/Doctrine/CalendarEntityListener.php @@ -13,6 +13,7 @@ namespace Chill\CalendarBundle\Messenger\Doctrine; use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Messenger\Message\CalendarMessage; +use Chill\CalendarBundle\Messenger\Message\CalendarRemovedMessage; use Doctrine\ORM\Event\LifecycleEventArgs; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Security\Core\Security; @@ -44,7 +45,14 @@ class CalendarEntityListener public function postRemove(Calendar $calendar, LifecycleEventArgs $args): void { - // TODO + if (!$calendar->preventEnqueueChanges) { + $this->messageBus->dispatch( + new CalendarRemovedMessage( + $calendar, + $this->security->getUser() + ) + ); + } } public function postUpdate(Calendar $calendar, LifecycleEventArgs $args): void diff --git a/src/Bundle/ChillCalendarBundle/Messenger/Handler/CalendarRangeRemoveToRemoteHandler.php b/src/Bundle/ChillCalendarBundle/Messenger/Handler/CalendarRangeRemoveToRemoteHandler.php index 8f8b724a5..73e190cb6 100644 --- a/src/Bundle/ChillCalendarBundle/Messenger/Handler/CalendarRangeRemoveToRemoteHandler.php +++ b/src/Bundle/ChillCalendarBundle/Messenger/Handler/CalendarRangeRemoveToRemoteHandler.php @@ -39,7 +39,7 @@ class CalendarRangeRemoveToRemoteHandler implements MessageHandlerInterface $this->remoteCalendarConnector->removeCalendarRange( $calendarRangeRemovedMessage->getRemoteId(), $calendarRangeRemovedMessage->getRemoteAttributes(), - $this->userRepository->find($calendarRangeRemovedMessage->getCalendarRangeUserId()) + $this->userRepository->find($calendarRangeRemovedMessage->getCalendarUserId()) ); } } diff --git a/src/Bundle/ChillCalendarBundle/Messenger/Handler/CalendarRemoveHandler.php b/src/Bundle/ChillCalendarBundle/Messenger/Handler/CalendarRemoveHandler.php new file mode 100644 index 000000000..1b2f9a1b7 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Messenger/Handler/CalendarRemoveHandler.php @@ -0,0 +1,49 @@ +remoteCalendarConnector = $remoteCalendarConnector; + $this->calendarRangeRepository = $calendarRangeRepository; + $this->userRepository = $userRepository; + } + + public function __invoke(CalendarRemovedMessage $message) + { + if (null !== $message->getAssociatedCalendarRangeId()) { + $associatedRange = $this->calendarRangeRepository->find($message->getAssociatedCalendarRangeId()); + } else { + $associatedRange = null; + } + + $this->remoteCalendarConnector->removeCalendar( + $message->getRemoteId(), + $message->getRemoteAttributes(), + $this->userRepository->find($message->getCalendarUserId()), + $associatedRange + ); + } +} diff --git a/src/Bundle/ChillCalendarBundle/Messenger/Message/CalendarRemovedMessage.php b/src/Bundle/ChillCalendarBundle/Messenger/Message/CalendarRemovedMessage.php new file mode 100644 index 000000000..1d8132003 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Messenger/Message/CalendarRemovedMessage.php @@ -0,0 +1,73 @@ +remoteId = $calendar->getRemoteId(); + $this->remoteAttributes = $calendar->getRemoteAttributes(); + $this->calendarUserId = $calendar->getMainUser()->getId(); + + if ($calendar->hasCalendarRange()) { + $this->associatedCalendarRangeId = $calendar->getCalendarRange()->getId(); + } + + if (null !== $byUser) { + $this->byUserId = $byUser->getId(); + } + } + + public function getByUserId(): ?int + { + return $this->byUserId; + } + + public function getCalendarUserId(): ?int + { + return $this->calendarUserId; + } + + public function getRemoteAttributes(): array + { + return $this->remoteAttributes; + } + + public function getRemoteId(): string + { + return $this->remoteId; + } + + /** + * @return int|null + */ + public function getAssociatedCalendarRangeId(): ?int + { + return $this->associatedCalendarRangeId; + } + + +} diff --git a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php index 05997bc1c..8ad0bf516 100644 --- a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php +++ b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php @@ -136,6 +136,20 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface } } + public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void + { + if ('' === $remoteId) { + return; + } + + $this->removeEvent($remoteId, $user); + + if (null !== $associatedCalendarRange) { + $this->syncCalendarRange($associatedCalendarRange); + } + } + + public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void { if ('' === $remoteId) { diff --git a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/NullRemoteCalendarConnector.php b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/NullRemoteCalendarConnector.php index a1803d9c1..2a006b6e9 100644 --- a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/NullRemoteCalendarConnector.php +++ b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/NullRemoteCalendarConnector.php @@ -36,6 +36,11 @@ class NullRemoteCalendarConnector implements RemoteCalendarConnectorInterface return []; } + public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void + { + } + + public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void { } diff --git a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/RemoteCalendarConnectorInterface.php b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/RemoteCalendarConnectorInterface.php index 4eebb8863..cf7903beb 100644 --- a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/RemoteCalendarConnectorInterface.php +++ b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/RemoteCalendarConnectorInterface.php @@ -41,6 +41,8 @@ interface RemoteCalendarConnectorInterface public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void; + public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void; + /** * @param array $oldInvites */