mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
96 lines
3.5 KiB
PHP
96 lines
3.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\AccompanyingPeriod\Events;
|
|
|
|
use Chill\MainBundle\Entity\Address;
|
|
use Chill\MainBundle\Entity\Notification;
|
|
use Chill\MainBundle\Notification\NotificationPersisterInterface;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
|
use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent;
|
|
use DateTimeImmutable;
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
use Symfony\Component\Security\Core\Security;
|
|
use Symfony\Component\Templating\EngineInterface;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
class PersonAddressMoveEventSubscriber implements EventSubscriberInterface
|
|
{
|
|
private EngineInterface $engine;
|
|
|
|
private NotificationPersisterInterface $notificationPersister;
|
|
|
|
private Security $security;
|
|
|
|
private TranslatorInterface $translator;
|
|
|
|
public function __construct(
|
|
EngineInterface $engine,
|
|
NotificationPersisterInterface $notificationPersister,
|
|
Security $security,
|
|
TranslatorInterface $translator
|
|
) {
|
|
$this->engine = $engine;
|
|
$this->notificationPersister = $notificationPersister;
|
|
$this->security = $security;
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
public static function getSubscribedEvents(): array
|
|
{
|
|
return [
|
|
PersonAddressMoveEvent::class => 'resetPeriodLocation',
|
|
];
|
|
}
|
|
|
|
public function resetPeriodLocation(PersonAddressMoveEvent $event)
|
|
{
|
|
if ($event->getPreviousAddress() !== $event->getNextAddress()
|
|
&& null !== $event->getPreviousAddress()
|
|
) {
|
|
$person = $event->getPerson();
|
|
|
|
foreach ($person->getCurrentAccompanyingPeriods() as $period) {
|
|
if ($period->getStep() === AccompanyingPeriod::STEP_DRAFT) {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
$period->getPersonLocation() === $person
|
|
&& (
|
|
$event->getMoveDate() >= $period->getLastLocationHistory()->getStartDate()
|
|
|| $event->willChangeBeActiveAt(new DateTimeImmutable('now'))
|
|
)
|
|
&& null !== $period->getUser()
|
|
&& $period->getUser() !== $this->security->getUser()
|
|
) {
|
|
// reset the location, back to an address
|
|
$period->setPersonLocation(null);
|
|
$period->setAddressLocation(Address::createFromAddress($event->getPreviousAddress()));
|
|
|
|
$notification = new Notification();
|
|
$notification
|
|
->addAddressee($period->getUser())
|
|
->setTitle($this->translator->trans('period_notification.Person locating period has moved'))
|
|
->setRelatedEntityClass(AccompanyingPeriod::class)
|
|
->setRelatedEntityId($period->getId())
|
|
->setMessage($this->engine->render('@ChillPerson/AccompanyingPeriod/notification_location_user_on_period_has_moved.fr.txt.twig', [
|
|
'oldPersonLocation' => $person,
|
|
'period' => $period,
|
|
]));
|
|
|
|
$this->notificationPersister->persist($notification);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|