<?php
namespace App\EventListener;
use App\Repository\CourseOccurrenceTimeRepository;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
class CalendarSubscriber implements EventSubscriberInterface
{
private $cotRepo;
private $security;
public function __construct(CourseOccurrenceTimeRepository $cotRepo, Security $security)
{
$this->cotRepo = $cotRepo;
$this->security = $security;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
CalendarEvents::SET_DATA => 'onCalendarSetData',
];
}
/**
* @return void
*/
public function onCalendarSetData(CalendarEvent $calendar)
{
$filters = $calendar->getFilters();
$filters['start'] = $calendar->getStart();
$filters['end'] = $calendar->getEnd();
$times = $this->cotRepo->getTimesByFilter($filters);
$roles = $this->security->getUser()->getRoles();
$seekSpeaker = (in_array('ROLE_SPEAKER', $roles)) ? $this->security->getUser()->getId() : null;
$speakerid = $this->security->getUser()->getId();
foreach ($times as $time) {
$info = [];
$info['courseId'] = $time->getOccurrence()->getCourse()->getId();
$info['title'] = $time->getOccurrence()->getCourse()->getTitle();
$info['occurrenceId'] = $time->getOccurrence()->getId();
$info['venue'] = ($time->getOccurrence()->getVenue() !== null) ? $time->getOccurrence()->getVenue()->getName() : null;
$info['venueId'] = ($time->getOccurrence()->getVenue() !== null) ? $time->getOccurrence()->getVenue()->getId() : null;
$info['room'] = ($time->getOccurrence()->getVenueRoom() !== null) ? $time->getOccurrence()->getVenueRoom()->getName() : null;
$info['speakers'] = [];
$info['slots'] = $time->getOccurrence()->getSlots();
$info['bookedSlots'] = $time->getOccurrence()->getBookedSlots();
$foundSpeaker = false;
foreach ($time->getOccurrence()->getSpeakers() as $speaker) {
if ($seekSpeaker and !$foundSpeaker and $speaker->getPerson()->getUser()->getId() == $seekSpeaker) {
$foundSpeaker = true;
}
$arrElem = [];
$arrElem['id'] = $speaker->getId();
$arrElem['name'] = 'Referent: ' .$speaker->getFullname() ;
$info['speakers'][] = $arrElem;
}
$info['participants'] = [];
if (in_array('ROLE_ADMIN', $roles) or (in_array('ROLE_SPEAKER', $roles) and $_ENV['SPEAKER_CALENDAR'] == '1') ) {
foreach ($time->getOccurrence()->getOrderItems() as $orderItem) {
foreach ($orderItem->getParticipants() as $participant) {
$arrElem = [];
if ($participant->isCancelled()) {
continue;
}
if ($participant->getOrderItem()->getOrder()->getStatus() == 'cancelled') {
continue;
}
$arrElem['customerId'] = $participant->getOrderItem()->getOrder()->getCustomer()->getId();
$arrElem['name'] = $participant->getFullname();
$info['participants'][] = $arrElem;
}
}
}
$options = [];
$options['extendedProps'] = $info;
$calendar->addEvent(new Event(
$time->getOccurrence()->getCourse()->getTitle(),
$time->getStart(),
$time->getEnd(),
null,
$options
));
}
}
}