/** * Returns all events from the calendar between two timestamps * * Note that the events returned needs only slightly overlap. * * @param SG_iCalReader|array $ical The calendar to query * @param int $start * @param int $end * @return SG_iCal_VEvent[] */ public static function Between($ical, $start, $end) { if ($ical instanceof SG_iCalReader) { $ical = $ical->getEvents(); } if (!is_array($ical)) { throw new Exception('SG_iCal_Query::Between called with invalid input!'); } $rtn = array(); foreach ($ical as $e) { if ($start <= $e->getStart() && $e->getStart() < $end || $start < $e->getRangeEnd() && $e->getRangeEnd() <= $end) { $rtn[] = $e; } else { if (isset($e->recurrence)) { $freq = new SG_iCal_Freq($e->recurrence->rrule, $e->getStart()); $occurrences = $freq->getOccurrences(50); foreach ($occurrences as $oc) { $oc_end = $oc + $e->getDuration(); if ($start <= $oc && $oc < $end || $start < $oc_end && $oc_end <= $end) { $rtn[] = $e; break; } } } } } return $rtn; }
/** * Determines which of the daylight or standard is the active * setting. * The call is cached for a given timestamp, so a call to * getOffset and getTimeZoneName with the same ts won't calculate * the answer twice. * @param int $ts * @return string standard|daylight */ private function getActive($ts) { if (isset($this->cache[$ts])) { return $this->cache[$ts]; } $daylight_freq = new SG_iCal_Freq($this->daylight['rrule'], strtotime($this->daylight['dtstart'])); $standard_freq = new SG_iCal_Freq($this->standard['rrule'], strtotime($this->standard['dtstart'])); $last_standard = $standard_freq->previousOccurrence($ts); $last_dst = $daylight_freq->previousOccurrence($ts); if ($last_dst > $last_standard) { $this->cache[$ts] = 'daylight'; } else { $this->cache[$ts] = 'standard'; } return $this->cache[$ts]; }
/** * Determines which of the daylight or standard is the active * setting. * The call is cached for a given timestamp, so a call to * getOffset and getTimeZoneName with the same ts won't calculate * the answer twice. * @param int $ts * @return string standard|daylight */ private function getActive($ts) { if (class_exists('DateTimeZone')) { //PHP >= 5.2 $tz = new DateTimeZone($this->tzid); $date = new DateTime("@{$ts}", $tz); return $date->format('I') == 1 ? 'daylight' : 'standard'; } else { if (isset($this->cache[$ts])) { return $this->cache[$ts]; } $daylight_freq = new SG_iCal_Freq($this->daylight['rrule'], strtotime($this->daylight['dtstart'])); $standard_freq = new SG_iCal_Freq($this->standard['rrule'], strtotime($this->standard['dtstart'])); $last_standard = $standard_freq->previousOccurrence($ts); $last_dst = $daylight_freq->previousOccurrence($ts); if ($last_dst > $last_standard) { $this->cache[$ts] = 'daylight'; } else { $this->cache[$ts] = 'standard'; } return $this->cache[$ts]; } }
/** * Fonction qui insère en base de données les évènements du fichier iCal * * @param int $agendaId Identifiant de l'agenda dans lequel doivent être insérés les évènements * @param SG_iCal_Freq $event Evenement à enregistrer * * @return Recorde Evenement à sauvegarder */ public static function createEvent($agendaId, $event) { $record = _record('event'); $record->id_agenda = $agendaId; $record->title_event = $event->getSummary(); $record->desc_event = $event->getDescription(); $record->place_event = $event->getLocation(); $record->datedeb_event = date('Ymd', $event->getStart()); $record->heuredeb_event = date('H:i', $event->getStart()); $record->heurefin_event = date('H:i', $event->getEnd()); $record->alldaylong_event = $event->isWholeDay() ? 1 : 0; $record->everyday_event = 0; $record->everyweek_event = 0; $record->everymonth_event = 0; $record->everyyear_event = 0; $record->endrepeatdate_event = null; if ($event->isWholeDay()) { $record->datefin_event = date('Ymd', strtotime('-1 day', $event->getEnd())); } else { $record->datefin_event = date('Ymd', $event->getEnd()); } // Récupération de la fréquence de l'événement if ($eventRecurrence = $event->getProperty('recurrence')) { switch ($eventRecurrence->getFreq()) { case 'DAILY': $record->everyday_event = 1; break; case 'WEEKLY': $record->everyweek_event = 1; break; case 'MONTHLY': $record->everymonth_event = 1; break; case 'YEARLY': $record->everyyear_event = 1; break; } // Si l'évenement s'arrête après un certain nombre de fois, on récupère la dernière date if ($eventFrequence = $event->getProperty('freq')) { $record->endrepeatdate_event = date('Ymd', $eventFrequence->lastOccurrence()); } elseif ($eventRecurrence->getUntil()) { // Sinon on récupérère la date de dernière récurrence, on vérifie que l'attribut rrule possède la date if (preg_match('/UNTIL/', $eventRecurrence->rrule) > 0) { $record->endrepeatdate_event = date_create_from_format('Ymd\\THisO', $eventRecurrence->getUntil())->format('Ymd'); } else { // Sinon on ne récupère pas la date donné par SG_iCal, car l'évenement ne se termine pas et SG_iCal l'ajoute forcement à 3ans plus tard $record->endrepeatdate_event = '99999999'; } } else { $record->endrepeatdate_event = '99999999'; } } return $record; }
/** * generate_dates_array_from_ics_rule function * * @return array **/ function generate_dates_array_from_ics_rule($start, $ics_rule) { $freq = new SG_iCal_Freq($ics_rule, $start, array(), array(), true); return $freq->getAllOccurrences(); }
private function assertRule($rule, $start, $dateset) { $freq = new SG_iCal_Freq($rule, $start); reset($dateset); $n = $start - 1; do { $n = $freq->findNext($n); //echo date('Y-m-d H:i:sO ',$n); $e = current($dateset) != -1 ? current($dateset) : false; $this->assertEquals($e, $n); } while (next($dateset) !== false); }