src/EventSubscriber/EasyAdminSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\FilmProject;
  4. use App\Entity\FilmProjectChangeHistory;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class EasyAdminSubscriber implements EventSubscriberInterface
  10. {
  11.     private $em;
  12.     public function __construct(EntityManagerInterface $em)
  13.     {
  14.         $this->em $em;
  15.     }
  16.     public function onAfterEntityPersistedEvent(AfterEntityPersistedEvent $event): void
  17.     {
  18.         // $entity = $event->getEntityInstance();
  19.         // if (!($entity instanceof Event)) {
  20.         //     return;
  21.         // }
  22.     }
  23.     public function onBeforeEntityPersistedEvent(BeforeEntityUpdatedEvent $event): void
  24.     {
  25.         $entity $event->getEntityInstance(); // Get current data 
  26.         if (!($entity instanceof FilmProject)) {
  27.             return;
  28.         }
  29.         $jsonChange $this->generateChangeHistory($entity);
  30.         if (!empty($jsonChange)) {
  31.             $changeHistory = (new FilmProjectChangeHistory())
  32.                 ->setHistoryChange($jsonChange)
  33.                 ->setCreatedAt(new \DateTime('now'))
  34.                 ->setModifiedAt(new \DateTime('now'))
  35.                 ->setFilmProject($entity);
  36.             ;
  37.             $this->em->persist($changeHistory);
  38.             $this->em->flush();
  39.         }
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             // AfterEntityPersistedEvent::class => 'onAfterEntityPersistedEvent',
  45.             BeforeEntityUpdatedEvent::class => 'onBeforeEntityPersistedEvent',
  46.         ];
  47.     }
  48.     private function generateChangeHistory(FilmProject $filmProject): array {
  49.         $jsonData = [];
  50.         // Get list of changeset to be generated into JSON
  51.         $uow $this->em->getUnitOfWork();
  52.         $uow->computeChangeSets();
  53.         $filmProjectChangeset $uow->getEntityChangeSet($filmProject); // get film project changeset
  54.         $filmProjectTimelineChangeset $uow->getEntityChangeSet($filmProject->getFilmProjectTimeline()); // get film project changeset
  55.         $filmProjectFundingChangeset $uow->getEntityChangeSet($filmProject->getFilmProjectFunding()); // get film project changeset
  56.         $filmProjectStoryImpactChangeset $uow->getEntityChangeSet($filmProject->getFilmProjectStoryImpact()); // get film project changeset
  57.         $changeset array_merge($filmProjectChangeset$filmProjectTimelineChangeset$filmProjectFundingChangeset$filmProjectStoryImpactChangeset);
  58.         $arrayKeys = ['title''creativeTreatment''productionStage''developmentTimeline''productionTimeline''postProductionTimeline''completionTimeline''fundraisingCampaignStartDate''fundraisingCampaignEndDate''impactCampaignStartDate''impactCampaignEndDate''fundraisingGoal'];
  59.         foreach($arrayKeys as $key) {
  60.             if (array_key_exists($key$changeset)) {
  61.                 $updatedData $changeset[$key][1];
  62.                 if ($updatedData instanceof \DateTime) {
  63.                     $updatedData = ($changeset[$key][1])->format('d-m-Y');
  64.                 } elseif (is_float($updatedData)) {
  65.                     $updatedData '$'number_format(($updatedData 100), 0'.'',');
  66.                 }
  67.                 // Add conditions here
  68.                 if ($key == 'productionStage' && $updatedData != 'Film Complete') {
  69.                     continue; // dont track it if it's not complete
  70.                 }
  71.                 array_push($jsonData, [
  72.                     $key => $updatedData,
  73.                 ]);
  74.             }
  75.         }
  76.         return $jsonData;
  77.     }
  78. }