<?php
namespace App\EventSubscriber;
use App\Entity\FilmProject;
use App\Entity\FilmProjectChangeHistory;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function onAfterEntityPersistedEvent(AfterEntityPersistedEvent $event): void
{
// $entity = $event->getEntityInstance();
// if (!($entity instanceof Event)) {
// return;
// }
}
public function onBeforeEntityPersistedEvent(BeforeEntityUpdatedEvent $event): void
{
$entity = $event->getEntityInstance(); // Get current data
if (!($entity instanceof FilmProject)) {
return;
}
$jsonChange = $this->generateChangeHistory($entity);
if (!empty($jsonChange)) {
$changeHistory = (new FilmProjectChangeHistory())
->setHistoryChange($jsonChange)
->setCreatedAt(new \DateTime('now'))
->setModifiedAt(new \DateTime('now'))
->setFilmProject($entity);
;
$this->em->persist($changeHistory);
$this->em->flush();
}
}
public static function getSubscribedEvents(): array
{
return [
// AfterEntityPersistedEvent::class => 'onAfterEntityPersistedEvent',
BeforeEntityUpdatedEvent::class => 'onBeforeEntityPersistedEvent',
];
}
private function generateChangeHistory(FilmProject $filmProject): array {
$jsonData = [];
// Get list of changeset to be generated into JSON
$uow = $this->em->getUnitOfWork();
$uow->computeChangeSets();
$filmProjectChangeset = $uow->getEntityChangeSet($filmProject); // get film project changeset
$filmProjectTimelineChangeset = $uow->getEntityChangeSet($filmProject->getFilmProjectTimeline()); // get film project changeset
$filmProjectFundingChangeset = $uow->getEntityChangeSet($filmProject->getFilmProjectFunding()); // get film project changeset
$filmProjectStoryImpactChangeset = $uow->getEntityChangeSet($filmProject->getFilmProjectStoryImpact()); // get film project changeset
$changeset = array_merge($filmProjectChangeset, $filmProjectTimelineChangeset, $filmProjectFundingChangeset, $filmProjectStoryImpactChangeset);
$arrayKeys = ['title', 'creativeTreatment', 'productionStage', 'developmentTimeline', 'productionTimeline', 'postProductionTimeline', 'completionTimeline', 'fundraisingCampaignStartDate', 'fundraisingCampaignEndDate', 'impactCampaignStartDate', 'impactCampaignEndDate', 'fundraisingGoal'];
foreach($arrayKeys as $key) {
if (array_key_exists($key, $changeset)) {
$updatedData = $changeset[$key][1];
if ($updatedData instanceof \DateTime) {
$updatedData = ($changeset[$key][1])->format('d-m-Y');
} elseif (is_float($updatedData)) {
$updatedData = '$'. number_format(($updatedData / 100), 0, '.', ',');
}
// Add conditions here
if ($key == 'productionStage' && $updatedData != 'Film Complete') {
continue; // dont track it if it's not complete
}
array_push($jsonData, [
$key => $updatedData,
]);
}
}
return $jsonData;
}
}