From f10d762e592bc327b640fa2d7dc6775721bf0330 Mon Sep 17 00:00:00 2001 From: LenaertsJ Date: Mon, 6 Dec 2021 13:57:10 +0000 Subject: [PATCH] Eventlistener to create accompanyingPeriodWork --- CHANGELOG.md | 1 + .../ChillActivityExtension.php | 1 + .../EntityListener/ActivityEntityListener.php | 77 +++++++++++++++++++ .../ChillActivityBundle/Form/ActivityType.php | 14 ++-- .../services/doctrine.entitylistener.yaml | 15 ++++ 5 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 src/Bundle/ChillActivityBundle/EntityListener/ActivityEntityListener.php create mode 100644 src/Bundle/ChillActivityBundle/config/services/doctrine.entitylistener.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 337be5fe9..5107184b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to * [person search] fix bug when using birthdate after and birthdate before * [person search] increase pertinence when lastname begins with search pattern +* [activity] create work if a work with same social action is not associated to the activity ## Test releases diff --git a/src/Bundle/ChillActivityBundle/DependencyInjection/ChillActivityExtension.php b/src/Bundle/ChillActivityBundle/DependencyInjection/ChillActivityExtension.php index e1c657947..39c7eab36 100644 --- a/src/Bundle/ChillActivityBundle/DependencyInjection/ChillActivityExtension.php +++ b/src/Bundle/ChillActivityBundle/DependencyInjection/ChillActivityExtension.php @@ -41,6 +41,7 @@ class ChillActivityExtension extends Extension implements PrependExtensionInterf $loader->load('services/form.yaml'); $loader->load('services/templating.yaml'); $loader->load('services/accompanyingPeriodConsistency.yaml'); + $loader->load('services/doctrine.entitylistener.yaml'); } public function prepend(ContainerBuilder $container) diff --git a/src/Bundle/ChillActivityBundle/EntityListener/ActivityEntityListener.php b/src/Bundle/ChillActivityBundle/EntityListener/ActivityEntityListener.php new file mode 100644 index 000000000..ab370e89e --- /dev/null +++ b/src/Bundle/ChillActivityBundle/EntityListener/ActivityEntityListener.php @@ -0,0 +1,77 @@ +em = $em; + $this->workRepository = $workRepository; + } + + public function persistActionToCourse(Activity $activity) + { + if ($activity->getAccompanyingPeriod() instanceof AccompanyingPeriod) { + $period = $activity->getAccompanyingPeriod(); + + $accompanyingCourseWorks = $this->workRepository->findByAccompanyingPeriod($period); + $periodActions = []; + $now = new DateTimeImmutable(); + + foreach ($accompanyingCourseWorks as $key => $work) { + // take only the actions which are still opened + if ($work->getEndDate() === null || $work->getEndDate() > ($activity->getDate() ?? $now)) { + $periodActions[$key] = spl_object_hash($work->getSocialAction()); + } + } + + $associatedPersons = $activity->getPersonsAssociated(); + $associatedThirdparties = $activity->getThirdParties(); + + foreach ($activity->getSocialActions() as $action) { + if (in_array(spl_object_hash($action), $periodActions, true)) { + continue; + } + $newAction = new AccompanyingPeriodWork(); + $newAction->setSocialAction($action); + $period->addWork($newAction); + + $date = DateTimeImmutable::createFromMutable($activity->getDate()); + $newAction->setStartDate($date); + + foreach ($associatedPersons as $person) { + $newAction->addPerson($person); + } + + foreach ($associatedThirdparties as $thirdparty) { + $newAction->setHandlingThierparty($thirdparty); + } + $this->em->persist($newAction); + $this->em->flush(); + } + } + } +} diff --git a/src/Bundle/ChillActivityBundle/Form/ActivityType.php b/src/Bundle/ChillActivityBundle/Form/ActivityType.php index d332acdcb..7ed80ab00 100644 --- a/src/Bundle/ChillActivityBundle/Form/ActivityType.php +++ b/src/Bundle/ChillActivityBundle/Form/ActivityType.php @@ -143,7 +143,7 @@ class ActivityType extends AbstractType return array_map( fn (string $id): ?SocialIssue => $this->om->getRepository(SocialIssue::class)->findOneBy(['id' => (int) $id]), - explode(',', $socialIssuesAsString) + explode(',', (string) $socialIssuesAsString) ); } )); @@ -169,7 +169,7 @@ class ActivityType extends AbstractType return array_map( fn (string $id): ?SocialAction => $this->om->getRepository(SocialAction::class)->findOneBy(['id' => (int) $id]), - explode(',', $socialActionsAsString) + explode(',', (string) $socialActionsAsString) ); } )); @@ -262,7 +262,7 @@ class ActivityType extends AbstractType function (?string $personsAsString): array { return array_map( fn (string $id): ?Person => $this->om->getRepository(Person::class)->findOneBy(['id' => (int) $id]), - explode(',', $personsAsString) + explode(',', (string) $personsAsString) ); } )); @@ -284,7 +284,7 @@ class ActivityType extends AbstractType function (?string $thirdpartyAsString): array { return array_map( fn (string $id): ?ThirdParty => $this->om->getRepository(ThirdParty::class)->findOneBy(['id' => (int) $id]), - explode(',', $thirdpartyAsString) + explode(',', (string) $thirdpartyAsString) ); } )); @@ -317,7 +317,7 @@ class ActivityType extends AbstractType function (?string $usersAsString): array { return array_map( fn (string $id): ?User => $this->om->getRepository(User::class)->findOneBy(['id' => (int) $id]), - explode(',', $usersAsString) + explode(',', (string) $usersAsString) ); } )); @@ -332,7 +332,7 @@ class ActivityType extends AbstractType return ''; } - return $location->getId(); + return (string) $location->getId(); }, function (?string $id): ?Location { return $this->om->getRepository(Location::class)->findOneBy(['id' => (int) $id]); @@ -379,7 +379,7 @@ class ActivityType extends AbstractType $timezoneUTC = new DateTimeZone('GMT'); /** @var DateTime $data */ $data = $formEvent->getData() === null ? - DateTime::createFromFormat('U', 300) : + DateTime::createFromFormat('U', '300') : $formEvent->getData(); $seconds = $data->getTimezone()->getOffset($data); $data->setTimeZone($timezoneUTC); diff --git a/src/Bundle/ChillActivityBundle/config/services/doctrine.entitylistener.yaml b/src/Bundle/ChillActivityBundle/config/services/doctrine.entitylistener.yaml new file mode 100644 index 000000000..3c439153b --- /dev/null +++ b/src/Bundle/ChillActivityBundle/config/services/doctrine.entitylistener.yaml @@ -0,0 +1,15 @@ +services: + Chill\ActivityBundle\EntityListener\ActivityEntityListener: + autowire: true + autoconfigure: true + tags: + - + name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'Chill\ActivityBundle\Entity\Activity' + method: 'persistActionToCourse' + - + name: 'doctrine.orm.entity_listener' + event: 'postUpdate' + entity: 'Chill\ActivityBundle\Entity\Activity' + method: 'persistActionToCourse'