Exemplo n.º 1
0
 public function repeatSessionEvent(SessionEvent $sessionEvent, $iteration, \DateTime $until = null, $duration = null)
 {
     $createdSessionEvents = [];
     $dateInterval = new \DateInterval('P1D');
     $session = $sessionEvent->getSession();
     $description = $sessionEvent->getDescription();
     $location = $sessionEvent->getLocation();
     $locationResource = $sessionEvent->getLocationResource();
     $name = $sessionEvent->getName();
     $eventStartDate = $sessionEvent->getStartDate();
     $endDate = $sessionEvent->getEndDate();
     $tutors = $sessionEvent->getTutors();
     $index = 1;
     $year = intval($eventStartDate->format('Y'));
     $month = intval($eventStartDate->format('m'));
     $day = intval($eventStartDate->format('d'));
     $hour = intval($eventStartDate->format('H'));
     $minute = intval($eventStartDate->format('i'));
     $startDate = new \DateTime();
     $startDate->setTimezone(new \DateTimeZone('GMT'));
     $startDate->setDate($year, $month, $day);
     $startDate->setTime($hour, $minute);
     if (is_null($until) && !is_null($duration) && $duration > 0) {
         $until = clone $startDate;
         $daysToAdd = 7 * $duration;
         $until->add(new \DateInterval('P' . $daysToAdd . 'D'));
     }
     if (!is_null($until)) {
         $untilYear = intval($until->format('Y'));
         $untilMonth = intval($until->format('m'));
         $untilDay = intval($until->format('d'));
         $formattedUntil = new \DateTime();
         $formattedUntil->setTimezone(new \DateTimeZone('GMT'));
         $formattedUntil->setDate($untilYear, $untilMonth, $untilDay);
         $formattedUntil->setTime(23, 59, 59);
         $this->om->startFlushSuite();
         for ($startDate->add($dateInterval); $startDate < $formattedUntil; $startDate->add($dateInterval)) {
             $eventStartDate->add($dateInterval);
             $endDate->add($dateInterval);
             $day = $startDate->format('l');
             if ($iteration[$day]) {
                 $newStartDate = clone $eventStartDate;
                 $newEndDate = clone $endDate;
                 $newSessionEvent = new SessionEvent();
                 $newSessionEvent->setSession($session);
                 $newSessionEvent->setDescription($description);
                 $newSessionEvent->setLocation($location);
                 $newSessionEvent->setLocationResource($locationResource);
                 $newSessionEvent->setStartDate($newStartDate);
                 $newSessionEvent->setEndDate($newEndDate);
                 $newSessionEvent->setName($name . " [{$index}]");
                 foreach ($tutors as $tutor) {
                     $newSessionEvent->addTutor($tutor);
                 }
                 ++$index;
                 $this->persistSessionEvent($newSessionEvent);
                 $createdSessionEvents[] = $newSessionEvent;
                 if ($index % 300 === 0) {
                     $this->om->forceFlush();
                 }
             }
         }
         $this->om->endFlushSuite();
     }
     return $createdSessionEvents;
 }