Skip to content

Commit

Permalink
Add Kronolith_FreeBusy::getForUser().
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jul 13, 2016
1 parent 5256bf1 commit 82b7ce1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
29 changes: 5 additions & 24 deletions kronolith/fb.php
Expand Up @@ -27,31 +27,12 @@
$key = 'kronolith.fb.' . ($user ? 'u.' . $user : 'c.' . $cal);
$fb = $cache->get($key, 360);
if (!$fb) {
if ($user) {
$prefs = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Prefs')->create('kronolith', array(
'cache' => false,
'user' => $user
));
$registry->setTimeZone();
$cal = @unserialize($prefs->getValue('fb_cals'));
if (is_array($cal)) {
$cal = implode('|', $cal);
}

// If the free/busy calendars preference is empty, default to
// the user's default_share preference, and if that's empty,
// to their username.
if (!$cal) {
$cal = $prefs->getValue('default_share');
if (!$cal) {
$cal = $user;
}
$cal = 'internal_' . $cal;
}
}

try {
$fb = Kronolith_FreeBusy::generate(explode('|', $cal), null, null, false, $user);
if ($user) {
$fb = Kronolith_FreeBusy::getForUser($user)->exportvCalendar();
} else {
$fb = Kronolith_FreeBusy::generate(explode('|', $cal), null, null, false, $user);
}
} catch (Exception $e) {
Horde::log($e, 'ERR');
exit;
Expand Down
51 changes: 48 additions & 3 deletions kronolith/lib/FreeBusy.php
Expand Up @@ -8,8 +8,7 @@
class Kronolith_FreeBusy
{
/**
* Generates the free/busy text for $calendars. Cache it for at least an
* hour, as well.
* Generates the free/busy text for $calendars.
*
* @param string|array $calendars The calendar to view free/busy slots for.
* @param integer $startstamp The start of the time period to retrieve.
Expand Down Expand Up @@ -140,6 +139,52 @@ public static function generate($calendars, $startstamp = null,
return $vCal->exportvCalendar();
}

/**
* Retrieves the free/busy information for a given user.
*
* @since Kronolith 4.3.0
*
* @param string $user The user to look for.
* @param array $opts Options:
* - json: (boolean) Whether to return the free/busy
* data as a simple object suitable to be
* transferred as json. DEFAULT: false
* - start: (integer) The start of the time period to
* retrieve. DEFAULT: now
* - end: (integer) The end of the time period to
* retrieve. DEFAULT: now + freebusy_days pref
*
* @return Horde_Icalendar_Vfreebusy|object Free/busy component.
* @throws Kronolith_Exception
*/
public static function getForUser($user, $opts = array())
{
global $injector, $registry;

$opts = array_merge(
array('json' => false, 'start' => null, 'end' => null),
$opts
);
$prefs = $injector->getInstance('Horde_Core_Factory_Prefs')
->create('kronolith', array('cache' => false, 'user' => $user));
$registry->setTimeZone();
$cals = @unserialize($prefs->getValue('fb_cals'));

// If the free/busy calendars preference is empty, default to the
// user's default_share preference, and if that's empty, to their
// username.
if (!$cals) {
$cal = $prefs->getValue('default_share');
if (!$cal) {
$cal = $user;
}
$cals = array('internal_' . $cal);
}
$fb = self::generate($cals, $opts['start'], $opts['end'], true, $user);

return $opts['json'] ? self::toJson($fb) : $fb;
}

/**
* Retrieves the free/busy information for a given email address, if any
* information is available.
Expand All @@ -148,7 +193,7 @@ public static function generate($calendars, $startstamp = null,
* @param boolean $json Whether to return the free/busy data as a simple
* object suitable to be transferred as json.
*
* @return Horde_Icalendar_Vfreebusy Free/busy component.
* @return Horde_Icalendar_Vfreebusy|object Free/busy component.
* @throws Kronolith_Exception
*/
public static function get($email, $json = false)
Expand Down

0 comments on commit 82b7ce1

Please sign in to comment.