<?php
# src/EventSubscriber/EasyAdminSubscriber.php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Workflow\Registry;
use App\Entity\Demande;
use App\Controller\DemandeController;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
protected $em;
protected $workflows;
protected $demandeController;
protected $tokenStorage;
protected $currentUser;
public function __construct(EntityManagerInterface $em, Registry $workflows, TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
$this->currentUser = $this->tokenStorage->getToken()->getUser();
$this->em = $em;
$this->workflows = $workflows;
$this->demandeController = new DemandeController();
}
public static function getSubscribedEvents()
{
return array(
'easy_admin.pre_persist' => array('onPrePersistCreateEntity'),
'easy_admin.post_persist' => array('onPostPersistCreateEntity'),
'easy_admin.pre_update' => array('onPrePersistUpdateEntity'),
'easy_admin.post_update' => array('onPostPersistUpdateEntity')
);
}
/**
* Update balance of leave (Before create entity)
*
*/
public function onPrePersistCreateEntity(GenericEvent $event)
{
$entity = $event->getSubject();
if (method_exists($entity, 'setCreatedBy')) {
$entity->setCreatedBy($this->currentUser);
}
}
/**
* Update balance of leave (After create entity)
*
* See workflow transitions in /config/packages/workflow.yaml
*/
public function onPostPersistCreateEntity(GenericEvent $event)
{
$entity = $event->getSubject();
if ($entity instanceof Demande)
{
$workflow = $this->workflows->get($entity);
if ($entity->getCurrentPlace() == 'acceptee')
{
// Accept a leave
$this->acceptLeave($workflow, $entity);
$this->em->flush();
}
}
}
/**
* Update balance of leave (Before update entity)
*
*/
public function onPrePersistUpdateEntity(GenericEvent $event)
{
$entity = $event->getSubject();
if (method_exists($entity, 'setUpdatedBy')) {
$entity->setUpdatedBy($this->currentUser);
}
}
/**
* Update balance of leave (After update entity)
*
* See workflow transitions in /config/packages/workflow.yaml
*/
public function onPostPersistUpdateEntity(GenericEvent $event)
{
$entity = $event->getSubject();
if ($entity instanceof Demande)
{
global $currentPlaceOrDateOrTypeIsChanged;
global $dateIsChanged;
global $currentPlaceChanged;
global $leaveTypeChanged;
global $oldLeaveType;
global $oldPlace;
// If currentPLace / date / leave type is changed
if ($currentPlaceOrDateOrTypeIsChanged)
{
// If the leave is not accepted and the state is not changed, we will not continue update the balance
if (!$currentPlaceChanged && $entity->getCurrentPlace() != 'acceptee')
return;
$workflow = $this->workflows->get($entity);
// Restore the balance of leave (after the leave is canceled or not accepted)
if ($currentPlaceChanged && $oldPlace == 'acceptee')
{
$currentPlaceBeforeCancel = $entity->getCurrentPlace();
// Send email if the new state of the leave is refused
if($entity->getCurrentPlace() == 'refusee') {
// Refuse a leave
$this->cancelLeave($workflow, $entity, 'refuse');
} else {
// Cancel a leave
$this->cancelLeave($workflow, $entity, 'cancel');
// Return the old place (before cancel the leave)
$entity->setCurrentPlace($currentPlaceBeforeCancel);
}
}
// Accept a leave (if the state is accepted) or (if the state is accepted & the leave type = Congé-Annuel)
else if (($currentPlaceChanged && $entity->getCurrentPlace() == 'acceptee') || ($entity->getCurrentPlace() == 'acceptee' && $leaveTypeChanged && $entity->getType() === 'Congé-Annuel'))
{
// Accept a leave
$this->acceptLeave($workflow, $entity);
}
// Send email if the leave is refused (the old state of the leave is not accepted, is for example pending..)
else if ($currentPlaceChanged && $entity->getCurrentPlace() == 'refusee')
{
// Dispatch solde event
$entity->setCurrentPlace('en_attente_conge');
$workflow->apply($entity, 'refuser');
}
// Update the balance of leave (if the leave type <> Congé-Annuel)
else if ($leaveTypeChanged && $entity->getType() !== 'Congé-Annuel' && $oldLeaveType === 'Congé-Annuel')
{
$currentPlaceBeforeCancel = $entity->getCurrentPlace();
// Dispatch solde event (update balance)
$entity->setCurrentPlace('en_attente_annulation'); // Change place (workflow transition : accepter_annulation: from: en_attente_annulation to: annulee)
$workflow->apply($entity, 'accepter_annulation');
// Return the old place (before change)
$entity->setCurrentPlace($currentPlaceBeforeCancel);
// Add google calendar if not exist
$this->demandeController->updateEvent($entity);
}
// Update the balance of leave or date of google event if the date is changed
else if ($dateIsChanged)
{
// Update a leave
//$this->updateLeave($workflow, $entity);
if (($currentPlaceChanged && $entity->getCurrentPlace() == 'acceptee') || ($leaveTypeChanged && $entity->getCurrentPlace() == 'acceptee' && $entity->getType() === 'Congé-Annuel') || (!$leaveTypeChanged && !$currentPlaceChanged && $entity->getCurrentPlace() == 'acceptee' && $entity->getType() === 'Congé-Annuel'))
{
// Dispatch solde event (update balance)
$entity->setCurrentPlace('en_attente_annulation'); // Change place (workflow transition : accepter_annulation: from: en_attente_annulation to: annulee)
$workflow->apply($entity, 'accepter_annulation');
// Accept a leave
$entity->setCurrentPlace('en_attente_conge'); // Change place (workflow transition : accepter: from: en_attente_conge to: acceptee)
// Dispatch solde event (update balance)
$workflow->apply($entity, 'accepter');
}
else if (($currentPlaceChanged && $entity->getCurrentPlace() != 'acceptee') || ($leaveTypeChanged && $entity->getType() !== 'Congé-Annuel' && $oldLeaveType === 'Congé-Annuel'))
{
$currentPlaceBeforeCancel = $entity->getCurrentPlace();
// Dispatch solde event (update balance)
$entity->setCurrentPlace('en_attente_annulation'); // Change place (workflow transition : accepter_annulation: from: en_attente_annulation to: annulee)
$workflow->apply($entity, 'accepter_annulation');
// Return the old place (before change)
$entity->setCurrentPlace($currentPlaceBeforeCancel);
}
// Update google calendar date
$this->demandeController->updateEvent($entity);
}
$this->em->flush();
}
}
}
/**
* Accept a leave
*/
private function acceptLeave($workflow, $entity)
{
// Remove the leave from google calendar (if exist)
//$this->demandeController->removeFromCalendar($entity);
$entity->setCurrentPlace('en_attente_conge'); // Change place (workflow transition : accepter: from: en_attente_conge to: acceptee)
// Dispatch solde event
$workflow->apply($entity, 'accepter');
// Add the leave into google calendar
//$this->demandeController->addToCalendar($entity);
// Add google calendar if not exist
$this->demandeController->updateEvent($entity);
}
/**
* Cancel a leave
*/
private function cancelLeave($workflow, $entity, $action)
{
if($action == "cancel") {
$entity->setCurrentPlace('en_attente_annulation'); // Change place (workflow transition : accepter_annulation: from: en_attente_annulation to: annulee)
// Dispatch solde event
$workflow->apply($entity, 'accepter_annulation');
} else {
// Dispatch solde event
$entity->setCurrentPlace('en_attente_conge');
$workflow->apply($entity, 'refuser');
}
// Remove the leave from google calendar
$this->demandeController->removeFromCalendar($entity);
}
}