public static function generate($calendars, $startstamp = null, $endstamp = null, $returnObj = false, $user = null)
{
if (!is_array($calendars)) {
$calendars = array($calendars);
}
if (!$user) {
$kronolith_shares = $GLOBALS['injector']->getInstance('Kronolith_Shares');
/* Find a share and retrieve owner. */
foreach ($calendars as $calendar) {
if (strpos($calendar, 'internal_') !== 0) {
continue;
}
$calendar = substr($calendar, 9);
try {
$share = $kronolith_shares->getShare($calendar);
$user = $share->get('owner');
break;
} catch (Horde_Exception $e) {
}
}
}
/* Default the start date to today. */
if (is_null($startstamp)) {
$startstamp = mktime(0, 0, 0);
}
/* Default the end date to the start date + freebusy_days. */
if (is_null($endstamp) || $endstamp < $startstamp) {
$enddate = new Horde_Date($startstamp);
$enddate->mday += $GLOBALS['prefs']->getValue('freebusy_days');
$endstamp = $enddate->timestamp();
} else {
$enddate = new Horde_Date($endstamp);
}
/* Get the Identity for the owner of the share. */
$identity = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Identity')->create($user);
$email = $identity->getValue('from_addr');
$cn = $identity->getValue('fullname');
if (empty($email) && empty($cn)) {
$cn = $user;
}
/* Fetch events. */
$busy = array();
foreach ($calendars as $calendar) {
if (strpos($calendar, '_')) {
@(list($type, $calendar) = explode('_', $calendar, 2));
} else {
$type = 'internal';
}
try {
$GLOBALS['injector']->getInstance('Kronolith_Shares')->getShare($calendar);
} catch (Horde_Exception $e) {
throw new Kronolith_Exception('Share not found.');
}
try {
$driver = Kronolith::getDriver($type, $calendar);
$events = $driver->listEvents(new Horde_Date($startstamp), $enddate, array('show_recurrence' => true));
Kronolith::mergeEvents($busy, $events);
} catch (Exception $e) {
}
}
/* Create the new iCalendar. */
$vCal = new Horde_Icalendar();
$vCal->setAttribute('PRODID', '-//The Horde Project//Kronolith ' . $GLOBALS['registry']->getVersion() . '//EN');
$vCal->setAttribute('METHOD', 'PUBLISH');
/* Create new vFreebusy. */
$vFb = Horde_Icalendar::newComponent('vfreebusy', $vCal);
$params = array();
if (!empty($cn)) {
$params['CN'] = $cn;
}
if (!empty($email)) {
$vFb->setAttribute('ORGANIZER', 'mailto:' . $email, $params);
} else {
$vFb->setAttribute('ORGANIZER', '', $params);
}
$vFb->setAttribute('DTSTAMP', $_SERVER['REQUEST_TIME']);
$vFb->setAttribute('DTSTART', $startstamp);
$vFb->setAttribute('DTEND', $endstamp);
$vFb->setAttribute('URL', Horde::url('fb.php?u=' . $user, true, -1));
/* Add all the busy periods. */
foreach ($busy as $events) {
foreach ($events as $event) {
if ($event->status == Kronolith::STATUS_FREE) {
continue;
}
if ($event->status == Kronolith::STATUS_CANCELLED) {
continue;
}
/* Horde_Icalendar_Vfreebusy only supports timestamps at the
* moment. */
$vFb->addBusyPeriod('BUSY', $event->start->timestamp(), null, $event->end->timestamp() - $event->start->timestamp());
}
}
/* Remove the overlaps. */
$vFb->simplify();
$vCal->addComponent($vFb);
/* Return the vFreebusy object if requested. */
if ($returnObj) {
return $vFb;
}
/* Generate the vCal file. */
return $vCal->exportvCalendar();
}