Loading module/Application/config/aaa_module.config.php +28 −28 Original line number Diff line number Diff line Loading @@ -173,7 +173,6 @@ $config = [ Service\FichierService::class => Service\FichierService::class, Service\TauxHoraireHETDService::class => Service\TauxHoraireHETDService::class, Assertion\InformationAssertion::class => Assertion\InformationAssertion::class, ORM\Event\Listeners\HistoriqueListener::class => ORM\Event\Listeners\HistoriqueListener::class, ], 'factories' => [ \Laminas\Navigation\Navigation::class => Navigation\NavigationFactory::class, Loading @@ -188,6 +187,7 @@ $config = [ Assertion\InformationAssertion::class => \UnicaenAuth\Assertion\AssertionFactory::class, HostLocalization\HostLocalizationOse::class => HostLocalization\HostLocalizationOseFactory::class, ORM\RouteEntitiesInjector::class => ORM\RouteEntitiesInjectorFactory::class, ORM\Event\Listeners\HistoriqueListener::class => ORM\Event\Listeners\HistoriqueListenerFactory::class, ], 'aliases' => [ 'HostLocalization' => HostLocalization\HostLocalizationOse::class, Loading module/Application/src/ORM/Event/Listeners/HistoriqueListener.php 100644 → 100755 +46 −9 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Application\ORM\Event\Listeners; use Application\Interfaces\ParametreEntityInterface; use Application\Service\Traits\ContextServiceAwareTrait; use Application\Service\Traits\UtilisateurServiceAwareTrait; Loading @@ -9,14 +10,22 @@ use Doctrine\Common\EventSubscriber; use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Events; use RuntimeException; use UnicaenApp\Entity\HistoriqueAwareInterface; /** * Description of HistoriqueListener * * @author Laurent Lécluse <laurent.lecluse at unicaen.fr> */ class HistoriqueListener implements EventSubscriber { use ContextServiceAwareTrait; use UtilisateurServiceAwareTrait; private bool $enabled = true; /** * @param LifecycleEventArgs $args Loading Loading @@ -64,12 +73,14 @@ class HistoriqueListener implements EventSubscriber */ public function prePersist(LifecycleEventArgs $args) { if ($this->isEnabled()) { $entity = $args->getEntity(); // On fait si c'est une HistoriqueAwareInterface, mais pas une ParametreEntityInterface if (($entity instanceof HistoriqueAwareInterface) && (!$entity instanceof ParametreEntityInterface)) { $this->updateHistorique($entity); } } } Loading @@ -78,12 +89,14 @@ class HistoriqueListener implements EventSubscriber */ public function preUpdate(PreUpdateEventArgs $args) { if ($this->isEnabled()) { $entity = $args->getEntity(); // On fait si c'est une HistoriqueAwareInterface, mais pas une ParametreEntityInterface if (($entity instanceof HistoriqueAwareInterface) && (!$entity instanceof ParametreEntityInterface)) { $this->updateHistorique($entity); } } } Loading @@ -94,4 +107,28 @@ class HistoriqueListener implements EventSubscriber { return [Events::prePersist, Events::preUpdate]; } /** * @return bool */ public function isEnabled(): bool { return $this->enabled; } /** * @param bool $enabled * * @return HistoriqueListener */ public function setEnabled(bool $enabled): HistoriqueListener { $this->enabled = $enabled; return $this; } } No newline at end of file module/Application/src/ORM/Event/Listeners/HistoriqueListenerAwareTrait.php 0 → 100755 +40 −0 Original line number Diff line number Diff line <?php namespace Application\ORM\Event\Listeners; /** * Description of HistoriqueListenerAwareTrait * * @author UnicaenCode */ trait HistoriqueListenerAwareTrait { protected ?HistoriqueListener $oRMEventListenersHistoriqueListener = null; /** * @param HistoriqueListener $oRMEventListenersHistoriqueListener * * @return self */ public function setORMEventListenersHistoriqueListener(?HistoriqueListener $oRMEventListenersHistoriqueListener) { $this->oRMEventListenersHistoriqueListener = $oRMEventListenersHistoriqueListener; return $this; } public function getORMEventListenersHistoriqueListener(): ?HistoriqueListener { if (empty($this->oRMEventListenersHistoriqueListener)) { $this->oRMEventListenersHistoriqueListener = \Application::$container->get(HistoriqueListener::class); } return $this->oRMEventListenersHistoriqueListener; } } module/Application/src/ORM/Event/Listeners/HistoriqueListenerFactory.php 0 → 100755 +31 −0 Original line number Diff line number Diff line <?php namespace Application\ORM\Event\Listeners; use Psr\Container\ContainerInterface; /** * Description of HistoriqueListenerFactory * * @author Laurent Lécluse <laurent.lecluse at unicaen.fr> */ class HistoriqueListenerFactory { /** * @param ContainerInterface $container * @param string $requestedName * @param array|null $options * * @return HistoriqueListener */ public function __invoke(ContainerInterface $container, $requestedName, $options = null): HistoriqueListener { $listener = new HistoriqueListener; /* Injectez vos dépendances ICI */ return $listener; } } No newline at end of file module/Application/src/Processus/ContratProcessus.php +6 −17 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ use Application\Entity\Db\Structure; use Application\Entity\Db\TypeVolumeHoraire; use Application\Entity\Db\Validation; use Application\Entity\Db\VolumeHoraire; use Application\ORM\Event\Listeners\HistoriqueListener; use Application\ORM\Event\Listeners\HistoriqueListenerAwareTrait; use Application\Service\Traits\ContextServiceAwareTrait; use Application\Service\Traits\ContratServiceAwareTrait; use Application\Service\Traits\EtatVolumeHoraireServiceAwareTrait; Loading @@ -36,6 +36,7 @@ class ContratProcessus extends AbstractProcessus use TypeValidationServiceAwareTrait; use VolumeHoraireServiceAwareTrait; use ValidationServiceAwareTrait; use HistoriqueListenerAwareTrait; /** Loading Loading @@ -181,14 +182,15 @@ class ContratProcessus extends AbstractProcessus // on récupère les services non contractualisés et on la place les VH correspondants dans le contrat $services = $this->getServices($contrat->getIntervenant(), null, $contrat->getStructure(), false); $this->getORMEventListenersHistoriqueListener()->setEnabled(false); foreach ($services as $service) { foreach ($service->getVolumeHoraire() as $vh) { /* @var $vh VolumeHoraire */ $vh->setContrat($contrat); $this->desactivateHistoriqueListenerEvent(); $this->getEntityManager()->persist($vh); } } $this->getORMEventListenersHistoriqueListener()->setEnabled(true); $this->getEntityManager()->flush(); return $this; Loading @@ -215,12 +217,13 @@ class ContratProcessus extends AbstractProcessus $vhs = $sVH->getList($sVH->finderByContrat($contrat)); // détachement du contrat et des VH $this->desactivateHistoriqueListenerEvent(); $this->getORMEventListenersHistoriqueListener()->setEnabled(false); foreach ($vhs as $vh) { /* @var $vh \Application\Entity\Db\VolumeHoraire */ $vh->setContrat(null); $sVH->save($vh); } $this->getORMEventListenersHistoriqueListener()->setEnabled(true); $this->getServiceContrat()->delete($contrat); return $this; Loading Loading @@ -381,18 +384,4 @@ class ContratProcessus extends AbstractProcessus return $fr->getServiceDu() + $fr->getSolde(); } public function desactivateHistoriqueListenerEvent() { $eventManager = $this->getEntityManager()->getEventManager(); $allListeners = $eventManager->getListeners(); foreach ($allListeners as $event => $listeners) { foreach ($listeners as $listener) { if ($listener instanceof HistoriqueListener) { $this->getEntityManager()->getEventManager()->removeEventListener(['prePersist', 'preUpdate'], $listener); } } } } } No newline at end of file Loading
module/Application/config/aaa_module.config.php +28 −28 Original line number Diff line number Diff line Loading @@ -173,7 +173,6 @@ $config = [ Service\FichierService::class => Service\FichierService::class, Service\TauxHoraireHETDService::class => Service\TauxHoraireHETDService::class, Assertion\InformationAssertion::class => Assertion\InformationAssertion::class, ORM\Event\Listeners\HistoriqueListener::class => ORM\Event\Listeners\HistoriqueListener::class, ], 'factories' => [ \Laminas\Navigation\Navigation::class => Navigation\NavigationFactory::class, Loading @@ -188,6 +187,7 @@ $config = [ Assertion\InformationAssertion::class => \UnicaenAuth\Assertion\AssertionFactory::class, HostLocalization\HostLocalizationOse::class => HostLocalization\HostLocalizationOseFactory::class, ORM\RouteEntitiesInjector::class => ORM\RouteEntitiesInjectorFactory::class, ORM\Event\Listeners\HistoriqueListener::class => ORM\Event\Listeners\HistoriqueListenerFactory::class, ], 'aliases' => [ 'HostLocalization' => HostLocalization\HostLocalizationOse::class, Loading
module/Application/src/ORM/Event/Listeners/HistoriqueListener.php 100644 → 100755 +46 −9 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Application\ORM\Event\Listeners; use Application\Interfaces\ParametreEntityInterface; use Application\Service\Traits\ContextServiceAwareTrait; use Application\Service\Traits\UtilisateurServiceAwareTrait; Loading @@ -9,14 +10,22 @@ use Doctrine\Common\EventSubscriber; use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Events; use RuntimeException; use UnicaenApp\Entity\HistoriqueAwareInterface; /** * Description of HistoriqueListener * * @author Laurent Lécluse <laurent.lecluse at unicaen.fr> */ class HistoriqueListener implements EventSubscriber { use ContextServiceAwareTrait; use UtilisateurServiceAwareTrait; private bool $enabled = true; /** * @param LifecycleEventArgs $args Loading Loading @@ -64,12 +73,14 @@ class HistoriqueListener implements EventSubscriber */ public function prePersist(LifecycleEventArgs $args) { if ($this->isEnabled()) { $entity = $args->getEntity(); // On fait si c'est une HistoriqueAwareInterface, mais pas une ParametreEntityInterface if (($entity instanceof HistoriqueAwareInterface) && (!$entity instanceof ParametreEntityInterface)) { $this->updateHistorique($entity); } } } Loading @@ -78,12 +89,14 @@ class HistoriqueListener implements EventSubscriber */ public function preUpdate(PreUpdateEventArgs $args) { if ($this->isEnabled()) { $entity = $args->getEntity(); // On fait si c'est une HistoriqueAwareInterface, mais pas une ParametreEntityInterface if (($entity instanceof HistoriqueAwareInterface) && (!$entity instanceof ParametreEntityInterface)) { $this->updateHistorique($entity); } } } Loading @@ -94,4 +107,28 @@ class HistoriqueListener implements EventSubscriber { return [Events::prePersist, Events::preUpdate]; } /** * @return bool */ public function isEnabled(): bool { return $this->enabled; } /** * @param bool $enabled * * @return HistoriqueListener */ public function setEnabled(bool $enabled): HistoriqueListener { $this->enabled = $enabled; return $this; } } No newline at end of file
module/Application/src/ORM/Event/Listeners/HistoriqueListenerAwareTrait.php 0 → 100755 +40 −0 Original line number Diff line number Diff line <?php namespace Application\ORM\Event\Listeners; /** * Description of HistoriqueListenerAwareTrait * * @author UnicaenCode */ trait HistoriqueListenerAwareTrait { protected ?HistoriqueListener $oRMEventListenersHistoriqueListener = null; /** * @param HistoriqueListener $oRMEventListenersHistoriqueListener * * @return self */ public function setORMEventListenersHistoriqueListener(?HistoriqueListener $oRMEventListenersHistoriqueListener) { $this->oRMEventListenersHistoriqueListener = $oRMEventListenersHistoriqueListener; return $this; } public function getORMEventListenersHistoriqueListener(): ?HistoriqueListener { if (empty($this->oRMEventListenersHistoriqueListener)) { $this->oRMEventListenersHistoriqueListener = \Application::$container->get(HistoriqueListener::class); } return $this->oRMEventListenersHistoriqueListener; } }
module/Application/src/ORM/Event/Listeners/HistoriqueListenerFactory.php 0 → 100755 +31 −0 Original line number Diff line number Diff line <?php namespace Application\ORM\Event\Listeners; use Psr\Container\ContainerInterface; /** * Description of HistoriqueListenerFactory * * @author Laurent Lécluse <laurent.lecluse at unicaen.fr> */ class HistoriqueListenerFactory { /** * @param ContainerInterface $container * @param string $requestedName * @param array|null $options * * @return HistoriqueListener */ public function __invoke(ContainerInterface $container, $requestedName, $options = null): HistoriqueListener { $listener = new HistoriqueListener; /* Injectez vos dépendances ICI */ return $listener; } } No newline at end of file
module/Application/src/Processus/ContratProcessus.php +6 −17 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ use Application\Entity\Db\Structure; use Application\Entity\Db\TypeVolumeHoraire; use Application\Entity\Db\Validation; use Application\Entity\Db\VolumeHoraire; use Application\ORM\Event\Listeners\HistoriqueListener; use Application\ORM\Event\Listeners\HistoriqueListenerAwareTrait; use Application\Service\Traits\ContextServiceAwareTrait; use Application\Service\Traits\ContratServiceAwareTrait; use Application\Service\Traits\EtatVolumeHoraireServiceAwareTrait; Loading @@ -36,6 +36,7 @@ class ContratProcessus extends AbstractProcessus use TypeValidationServiceAwareTrait; use VolumeHoraireServiceAwareTrait; use ValidationServiceAwareTrait; use HistoriqueListenerAwareTrait; /** Loading Loading @@ -181,14 +182,15 @@ class ContratProcessus extends AbstractProcessus // on récupère les services non contractualisés et on la place les VH correspondants dans le contrat $services = $this->getServices($contrat->getIntervenant(), null, $contrat->getStructure(), false); $this->getORMEventListenersHistoriqueListener()->setEnabled(false); foreach ($services as $service) { foreach ($service->getVolumeHoraire() as $vh) { /* @var $vh VolumeHoraire */ $vh->setContrat($contrat); $this->desactivateHistoriqueListenerEvent(); $this->getEntityManager()->persist($vh); } } $this->getORMEventListenersHistoriqueListener()->setEnabled(true); $this->getEntityManager()->flush(); return $this; Loading @@ -215,12 +217,13 @@ class ContratProcessus extends AbstractProcessus $vhs = $sVH->getList($sVH->finderByContrat($contrat)); // détachement du contrat et des VH $this->desactivateHistoriqueListenerEvent(); $this->getORMEventListenersHistoriqueListener()->setEnabled(false); foreach ($vhs as $vh) { /* @var $vh \Application\Entity\Db\VolumeHoraire */ $vh->setContrat(null); $sVH->save($vh); } $this->getORMEventListenersHistoriqueListener()->setEnabled(true); $this->getServiceContrat()->delete($contrat); return $this; Loading Loading @@ -381,18 +384,4 @@ class ContratProcessus extends AbstractProcessus return $fr->getServiceDu() + $fr->getSolde(); } public function desactivateHistoriqueListenerEvent() { $eventManager = $this->getEntityManager()->getEventManager(); $allListeners = $eventManager->getListeners(); foreach ($allListeners as $event => $listeners) { foreach ($listeners as $listener) { if ($listener instanceof HistoriqueListener) { $this->getEntityManager()->getEventManager()->removeEventListener(['prePersist', 'preUpdate'], $listener); } } } } } No newline at end of file