src/EventSubscriber/EasyAdminSubscriber.php line 82

Open in your IDE?
  1. <?php
  2. # src/EventSubscriber/EasyAdminSubscriber.php
  3. namespace App\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\EventDispatcher\GenericEvent;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\Workflow\Registry;
  8. use App\Entity\Demande;
  9. use App\Controller\DemandeController;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. class EasyAdminSubscriber implements EventSubscriberInterface
  12. {
  13.     protected $em;
  14.     protected $workflows;
  15.     protected $demandeController;
  16.     protected $tokenStorage;
  17.     protected $currentUser;
  18.     public function __construct(EntityManagerInterface $emRegistry $workflowsTokenStorageInterface $tokenStorage)
  19.     {
  20.         $this->tokenStorage $tokenStorage;
  21.         $this->currentUser $this->tokenStorage->getToken()->getUser();
  22.         $this->em                $em;
  23.         $this->workflows         $workflows;
  24.         $this->demandeController = new DemandeController();
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return array(
  29.             'easy_admin.pre_persist' => array('onPrePersistCreateEntity'),
  30.             'easy_admin.post_persist' => array('onPostPersistCreateEntity'),
  31.             'easy_admin.pre_update'  => array('onPrePersistUpdateEntity'),
  32.             'easy_admin.post_update'  => array('onPostPersistUpdateEntity')
  33.         );
  34.     }
  35.     /**
  36.      * Update balance of leave (Before create entity)
  37.      * 
  38.      */
  39.     public function onPrePersistCreateEntity(GenericEvent $event)
  40.     {
  41.         $entity $event->getSubject();   
  42.         if (method_exists($entity'setCreatedBy')) {
  43.             $entity->setCreatedBy($this->currentUser);
  44.         }
  45.     }
  46.     /**
  47.      * Update balance of leave (After create entity)
  48.      * 
  49.      * See workflow transitions in /config/packages/workflow.yaml
  50.      */
  51.     public function onPostPersistCreateEntity(GenericEvent $event)
  52.     {
  53.         $entity $event->getSubject();   
  54.         if ($entity instanceof Demande
  55.         {
  56.             $workflow $this->workflows->get($entity);
  57.             if ($entity->getCurrentPlace() == 'acceptee')
  58.             {
  59.                 // Accept a leave
  60.                 $this->acceptLeave($workflow$entity);
  61.                 $this->em->flush();
  62.             }
  63.         }
  64.     }
  65.   
  66.     /**
  67.      * Update balance of leave (Before update entity)
  68.      * 
  69.      */
  70.     public function onPrePersistUpdateEntity(GenericEvent $event)
  71.     {
  72.         $entity $event->getSubject();
  73.         if (method_exists($entity'setUpdatedBy')) {
  74.             $entity->setUpdatedBy($this->currentUser);
  75.         }
  76.     }
  77.     /**
  78.      * Update balance of leave (After update entity)
  79.      * 
  80.      * See workflow transitions in /config/packages/workflow.yaml
  81.      */
  82.     public function onPostPersistUpdateEntity(GenericEvent $event)
  83.     {
  84.         $entity $event->getSubject();
  85.         if ($entity instanceof Demande)
  86.         {
  87.             global $currentPlaceOrDateOrTypeIsChanged;
  88.             global $dateIsChanged;
  89.             global $currentPlaceChanged;
  90.             global $leaveTypeChanged;
  91.             global $oldLeaveType;
  92.             global $oldPlace;
  93.             // If currentPLace / date / leave type is changed
  94.             if ($currentPlaceOrDateOrTypeIsChanged)
  95.             {
  96.                 // If the leave is not accepted and the state is not changed, we will not continue update the balance  
  97.                 if (!$currentPlaceChanged && $entity->getCurrentPlace() != 'acceptee')
  98.                     return;
  99.                 $workflow $this->workflows->get($entity);
  100.                 // Restore the balance of leave (after the leave is canceled or not accepted)
  101.                 if ($currentPlaceChanged && $oldPlace == 'acceptee')
  102.                 {
  103.                     $currentPlaceBeforeCancel $entity->getCurrentPlace();
  104.                     // Send email if the new state of the leave is refused
  105.                     if($entity->getCurrentPlace() == 'refusee') {
  106.                         // Refuse a leave
  107.                         $this->cancelLeave($workflow$entity'refuse');
  108.                     } else {
  109.                         // Cancel a leave
  110.                         $this->cancelLeave($workflow$entity'cancel');
  111.                         //  Return the old place (before cancel the leave)
  112.                         $entity->setCurrentPlace($currentPlaceBeforeCancel);
  113.                     }
  114.                 }
  115.                 // Accept a leave (if the state is accepted) or (if the state is accepted & the leave type = Congé-Annuel)
  116.                 else if (($currentPlaceChanged && $entity->getCurrentPlace() == 'acceptee') || ($entity->getCurrentPlace() == 'acceptee' && $leaveTypeChanged && $entity->getType() === 'Congé-Annuel'))
  117.                 {
  118.                     // Accept a leave
  119.                     $this->acceptLeave($workflow$entity);
  120.                 }
  121.                 // Send email if the leave is refused (the old state of the leave is not accepted, is for example pending..)
  122.                 else if ($currentPlaceChanged && $entity->getCurrentPlace() == 'refusee')
  123.                 {
  124.                     // Dispatch solde event
  125.                     $entity->setCurrentPlace('en_attente_conge');
  126.                     $workflow->apply($entity'refuser');
  127.                 }
  128.                 // Update the balance of leave (if the leave type <> Congé-Annuel)
  129.                 else if ($leaveTypeChanged && $entity->getType() !== 'Congé-Annuel' && $oldLeaveType === 'Congé-Annuel')
  130.                 {
  131.                     $currentPlaceBeforeCancel $entity->getCurrentPlace();
  132.                     // Dispatch solde event (update balance)
  133.                     $entity->setCurrentPlace('en_attente_annulation'); // Change place (workflow transition : accepter_annulation: from: en_attente_annulation to: annulee)
  134.                     $workflow->apply($entity'accepter_annulation');
  135.                     // Return the old place (before change)
  136.                     $entity->setCurrentPlace($currentPlaceBeforeCancel);
  137.                     // Add google calendar if not exist
  138.                     $this->demandeController->updateEvent($entity);
  139.                 }
  140.                 // Update the balance of leave or date of google event if the date is changed
  141.                 else if ($dateIsChanged)
  142.                 {
  143.                     // Update a leave
  144.                     //$this->updateLeave($workflow, $entity);
  145.                     if (($currentPlaceChanged && $entity->getCurrentPlace() == 'acceptee') || ($leaveTypeChanged && $entity->getCurrentPlace() == 'acceptee' && $entity->getType() === 'Congé-Annuel') || (!$leaveTypeChanged && !$currentPlaceChanged && $entity->getCurrentPlace() == 'acceptee' && $entity->getType() === 'Congé-Annuel'))
  146.                     {
  147.                         // Dispatch solde event (update balance)
  148.                         $entity->setCurrentPlace('en_attente_annulation'); // Change place (workflow transition : accepter_annulation: from: en_attente_annulation to: annulee)
  149.                         $workflow->apply($entity'accepter_annulation');
  150.                         // Accept a leave
  151.                         $entity->setCurrentPlace('en_attente_conge'); // Change place (workflow transition : accepter: from: en_attente_conge to: acceptee)
  152.                         // Dispatch solde event (update balance)
  153.                         $workflow->apply($entity'accepter');
  154.                     }
  155.                     else if (($currentPlaceChanged && $entity->getCurrentPlace() != 'acceptee') || ($leaveTypeChanged && $entity->getType() !== 'Congé-Annuel' && $oldLeaveType === 'Congé-Annuel'))
  156.                     {
  157.                         $currentPlaceBeforeCancel $entity->getCurrentPlace();
  158.                         // Dispatch solde event (update balance)
  159.                         $entity->setCurrentPlace('en_attente_annulation'); // Change place (workflow transition : accepter_annulation: from: en_attente_annulation to: annulee)
  160.                         $workflow->apply($entity'accepter_annulation');
  161.                         // Return the old place (before change)
  162.                         $entity->setCurrentPlace($currentPlaceBeforeCancel);
  163.                     }
  164.                     // Update google calendar date
  165.                     $this->demandeController->updateEvent($entity);
  166.                 }
  167.                 $this->em->flush();
  168.             }
  169.         }
  170.     }
  171.     /**
  172.      * Accept a leave
  173.      */
  174.     private function acceptLeave($workflow$entity)
  175.     {
  176.         // Remove the leave from google calendar (if exist)
  177.         //$this->demandeController->removeFromCalendar($entity);
  178.         $entity->setCurrentPlace('en_attente_conge'); // Change place (workflow transition : accepter: from: en_attente_conge to: acceptee)
  179.         // Dispatch solde event
  180.         $workflow->apply($entity'accepter');
  181.         // Add the leave into google calendar
  182.         //$this->demandeController->addToCalendar($entity);
  183.         // Add google calendar if not exist
  184.         $this->demandeController->updateEvent($entity);
  185.     }
  186.     /**
  187.      * Cancel a leave
  188.      */
  189.     private function cancelLeave($workflow$entity$action)
  190.     {
  191.         if($action == "cancel") {
  192.             $entity->setCurrentPlace('en_attente_annulation'); // Change place (workflow transition : accepter_annulation: from: en_attente_annulation to: annulee)
  193.             // Dispatch solde event
  194.             $workflow->apply($entity'accepter_annulation');
  195.         } else {
  196.             // Dispatch solde event
  197.             $entity->setCurrentPlace('en_attente_conge');
  198.             $workflow->apply($entity'refuser');
  199.         }
  200.         // Remove the leave from google calendar
  201.         $this->demandeController->removeFromCalendar($entity);
  202.     }
  203. }