Skip to content

Instantly share code, notes, and snippets.

@evert
Created April 13, 2012 09:32
Show Gist options
  • Save evert/2375417 to your computer and use it in GitHub Desktop.
Save evert/2375417 to your computer and use it in GitHub Desktop.
Expanding events manually based on alarm range
<?php
function alarmExpand(Sabre_VObject_Component_VCalendar $calendar, DateTime $start, DateTime $end) {
// You should adjust these to be more reasonable
$startScan = new DateTime('1995-01-01');
$endScan = new DateTime('2030-01-01');
$newEvents = array();
foreach($calendar->select('VEVENT') as $key=>$vevent) {
if (isset($vevent->{'RECURRENCE-ID'})) {
// Overridden event
continue;
}
if (!$vevent->rrule) {
$newEvents[] = $vevent;
continue;
}
$uid = (string)$vevent->uid;
if (!$uid) {
throw new LogicException('Event did not have a UID!');
}
$it = new Sabre_VObject_RecurrenceIterator($calendar, $vevent->uid);
$it->fastForward($startScan);
while($it->valid() && $it->getDTStart() < $endScan) {
$newEvents[] = $it->getEventObject();
$it->next();
}
unset($calendar->children[$key]);
}
// Removing all existing events
unset($calendar->VEVENT);
foreach($newEvents as $newEvent) {
$inRange = false;
foreach($newEvent->VALARM as $alarm) {
if ($alarm->isInTimeRange($start, $end)) {
$inRange = true;
break;
}
}
if (!$inRange) continue;
// Note: If you don't care to normalize everything to UTC, you can skip this step.
foreach($newEvent->children as $child) {
if ($child instanceof Sabre_VObject_Property_DateTime &&
$child->getDateType() == Sabre_VObject_Property_DateTime::LOCALTZ) {
$child->setDateTime($child->getDateTime(),Sabre_VObject_Property_DateTime::UTC);
}
}
$calendar->add($newEvent);
}
// Removing all VTIMEZONE components
unset($calendar->VTIMEZONE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment