Exemple #1
0
 private function importEvents(Calendar $calendar, VCalendar $document)
 {
     // Prepare by deleting all existing events
     foreach ($calendar->getEvents() as $event) {
         $this->getEntityManager()->remove($event);
     }
     $calendar->getEvents()->clear();
     // Abort if there is no events at all
     if (!$document->VEVENT) {
         return;
     }
     // First, import only originals, because we want to save RRULE before it is destroyed when expanding
     $originals = [];
     foreach ($document->VEVENT as $vevent) {
         if (!$vevent->__get('RECURRENCE-ID')) {
             $event = $this->importEvent($calendar, $vevent);
             $originals[$event->getUid()] = $event;
         }
     }
     // Expand repetitions 2 month in the past and 1 year in the future
     $now = Utility::getNow();
     $start = $now->sub(new \DateInterval('P2M'));
     $end = $now->add(new \DateInterval('P1Y'));
     // Then we expand recurent rules
     $expandedDocument = $document->expand($start, $end);
     // Abort if there is no repetitions at all
     if (!$expandedDocument->VEVENT) {
         return;
     }
     // Finally re-import, but this time only the recurrent things
     foreach ($expandedDocument->VEVENT as $vevent) {
         $original = $this->findOriginal($originals, $vevent);
         if ($original) {
             $repetition = $this->importEvent($calendar, $vevent);
             $repetition->setOriginal($original);
         }
     }
 }