Kronolith_FreeBusy::generate PHP Method

generate() public static method

Generates the free/busy text for $calendars.
public static generate ( string | array $calendars, integer $startstamp = null, integer $endstamp = null, boolean $returnObj = false, string $user = null ) : string
$calendars string | array The calendar to view free/busy slots for.
$startstamp integer The start of the time period to retrieve.
$endstamp integer The end of the time period to retrieve.
$returnObj boolean Default false. Return a vFreebusy object instead of text.
$user string Set organizer to this user.
return string The free/busy text.
    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();
    }

Usage Example

示例#1
0
文件: Api.php 项目: horde/horde
 /**
  * Generates free/busy information for a given time period.
  *
  * @param integer $startstamp  The start of the time period to retrieve.
  * @param integer $endstamp    The end of the time period to retrieve.
  * @param string $calendar     The calendar to view free/busy slots for.
  *                             Defaults to the user's default calendar.
  *
  * @return Horde_Icalendar_Vfreebusy  A freebusy object that covers the
  *                                    specified time period.
  * @throws Kronolith_Exception
  */
 public function getFreeBusy($startstamp = null, $endstamp = null, $calendar = null)
 {
     if (is_null($calendar)) {
         $calendar = Kronolith::getDefaultCalendar();
     }
     // Free/Busy information is globally available; no permission
     // check is needed.
     return Kronolith_FreeBusy::generate($calendar, $startstamp, $endstamp, true);
 }
All Usage Examples Of Kronolith_FreeBusy::generate