src/EventSubscriber/CalendarSubscriber.php line 43

  1. <?php
  2. namespace App\EventSubscriber;
  3. use CalendarBundle\CalendarEvents;
  4. use CalendarBundle\Entity\Event;
  5. use CalendarBundle\Event\CalendarEvent;
  6. use Symfony\Bundle\SecurityBundle\Security;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. //use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;
  10. use App\Repository\TheCaseActionsRepository;
  11. class CalendarSubscriber implements EventSubscriberInterface
  12. {
  13.     // private UrlGeneratorInterface $router;
  14.     private TheCaseActionsRepository $TheCaseActionsRepository;
  15.     private Encryptor $encryptor;
  16.     private Security $security;
  17.     public function __construct(
  18.         TheCaseActionsRepository $TheCaseActionsRepository,
  19.         /*  UrlGeneratorInterface $router,*/
  20.         Encryptor $encryptor,
  21.         Security $security
  22.     ) {
  23.         $this->TheCaseActionsRepository = $TheCaseActionsRepository;
  24.         // $this->router = $router;
  25.         $this->encryptor = $encryptor;
  26.         $this->security = $security;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  32.         ];
  33.     }
  34.     public function onCalendarSetData(CalendarEvent $calendar): void
  35.     {
  36.         $LawOffice = $this->security->getUser()->getLawOffice();
  37.         $start = $calendar->getStart();
  38.         $end = $calendar->getEnd();
  39.         $filters = $calendar->getFilters();
  40.         //dd($calendar);
  41.         //Get CaseAction by LawOffice Repository
  42.         $CaseActionsAt = $this->TheCaseActionsRepository->
  43.         createQueryBuilder('TheCaseActions')
  44.             ->where('TheCaseActions.ActionAt BETWEEN :StartAt and :EndAt')
  45.             ->innerJoin('TheCaseActions.theCase', 'theCase', 'WITH', 'TheCaseActions.theCase = theCase.id')
  46.             ->andWhere('theCase.LawOffice = :LawOffice')
  47.             ->andWhere('theCase.FileStatus not in (:FileStatus)')
  48.             ->andWhere('TheCaseActions.IsDeleted <> :IsDeleted')
  49.             ->Select('COUNT(TheCaseActions.id) NbreActions','TheCaseActions.ActionAt')
  50.             ->setParameter('StartAt', $start->format('Y-m-d H:i:s'))
  51.             ->setParameter('EndAt', $end->format('Y-m-d H:i:s'))
  52.             ->setParameter('LawOffice', $LawOffice)
  53.             ->setParameter('IsDeleted', true)
  54.             ->setParameter('FileStatus',['2000'])
  55.             ->groupBy('TheCaseActions.ActionAt')
  56.             ->orderBy('TheCaseActions.ActionAt', 'DESC');
  57.         $CaseActionsAt = $CaseActionsAt->getQuery()->getResult();
  58.         //dd($CaseActionsAt);
  59.         foreach ($CaseActionsAt as $CaseAction) {
  60.             //dd($CaseAction['NbreActions']);
  61.             //case $CaseAction['NbreActions'] case 1,2,3
  62.             $Title = $this->TitleCalendar($CaseAction['NbreActions']);
  63.             $CaseActionAtEvent = new Event(
  64.                 $Title,
  65.                 $CaseAction['ActionAt'],
  66.                 null // If the end date is null or not defined, an all day event is created.
  67.             );
  68.             /*
  69.              * Add custom options to events
  70.              *
  71.              * For more information see: https://fullcalendar.io/docs/event-object
  72.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  73.              */
  74.             $CaseActionAtEvent->setOptions([
  75.                 'backgroundColor' => 'red',
  76.                 'borderColor' => 'red',
  77.             ]);
  78.             $CaseActionAtEvent->addOption('id', $this->encryptor->encrypt($CaseAction['ActionAt']->format('Y-m-d H:i:s')));
  79.             // finally, add the event to the CalendarEvent to fill the calendar
  80.             $calendar->addEvent($CaseActionAtEvent);
  81.         }
  82.         $CaseActionsNext = $this->TheCaseActionsRepository->
  83.         createQueryBuilder('TheCaseActions')
  84.             ->where('TheCaseActions.ActionNextAt >= :DateNow')
  85.             ->innerJoin('TheCaseActions.theCase', 'theCase', 'WITH', 'TheCaseActions.theCase = theCase.id')
  86.             ->andWhere('theCase.LawOffice = :LawOffice')
  87.             ->andWhere('TheCaseActions.IsDeleted <> :IsDeleted')
  88.             ->select('COUNT(TheCaseActions.id) NbreActions','TheCaseActions.ActionNextAt')
  89.             ->andWhere('TheCaseActions.ActionNextAt BETWEEN :StartAt and :EndAt')
  90.             ->setParameter('StartAt', $start->format('Y-m-d H:i:s'))
  91.             ->setParameter('EndAt', $end->format('Y-m-d H:i:s'))
  92.             ->setParameter('DateNow', new \DateTime('now'))
  93.             ->setParameter('LawOffice', $LawOffice)
  94.             ->setParameter('IsDeleted', true)
  95.             ->groupBy('TheCaseActions.ActionNextAt')
  96.             ->orderBy('TheCaseActions.ActionNextAt', 'DESC')
  97.             ->getQuery()
  98.             ->getResult();
  99.         foreach ($CaseActionsNext as $CaseAction) {
  100.             $Title = $this->TitleCalendar($CaseAction['NbreActions']);
  101.             $CaseActionNextAtEvent = new Event(
  102.                 $Title,
  103.                 $CaseAction['ActionNextAt'],
  104.                 null // If the end date is null or not defined, an all day event is created.
  105.             );
  106.             $CaseActionNextAtEvent->setOptions([
  107.                 'backgroundColor' => 'green',
  108.                 'borderColor' => 'green',
  109.             ]);
  110.             $CaseActionNextAtEvent->addOption('id', $this->encryptor->encrypt($CaseAction['ActionNextAt']->format('Y-m-d H:i:s')));
  111.             $calendar->addEvent($CaseActionNextAtEvent);
  112.         }
  113.     }
  114.     public function TitleCalendar($Number): string
  115.     {
  116.         $Title = $Number;
  117.         if ($Number == 1) {
  118.             $Title .= 'جلسة';
  119.         }
  120.         elseif ($Number == 2) {
  121.             $Title = 'جلستين';
  122.         }elseif ($Number <= 10) {
  123.             $Title .= ' جلسات';
  124.         }elseif ($Number >= 11) {
  125.             $Title .= ' جلسة';
  126.         }
  127.         return $Title;
  128.     }
  129. }