/** * Returns an alarm hash of this event suitable for Horde_Alarm. * * @param Horde_Date $time Time of alarm. * @param string $user The user to return alarms for. * @param Prefs $prefs A Prefs instance. * * @return array Alarm hash or null. */ public function toAlarm($time, $user = null, $prefs = null) { if (!$this->alarm || $this->status == Kronolith::STATUS_CANCELLED) { return; } if ($this->recurs()) { $eventDate = $this->recurrence->nextRecurrence($time); if (!$eventDate || $eventDate && $this->recurrence->hasException($eventDate->year, $eventDate->month, $eventDate->mday)) { return; } $start = clone $eventDate; $diff = Date_Calc::dateDiff($this->start->mday, $this->start->month, $this->start->year, $this->end->mday, $this->end->month, $this->end->year); if ($diff == -1) { $diff = 0; } $end = new Horde_Date(array('year' => $start->year, 'month' => $start->month, 'mday' => $start->mday + $diff, 'hour' => $this->end->hour, 'min' => $this->end->min, 'sec' => $this->end->sec)); } else { $start = clone $this->start; $end = clone $this->end; } $serverName = $_SERVER['SERVER_NAME']; $serverConf = $GLOBALS['conf']['server']['name']; if (!empty($GLOBALS['conf']['reminder']['server_name'])) { $_SERVER['SERVER_NAME'] = $GLOBALS['conf']['server']['name'] = $GLOBALS['conf']['reminder']['server_name']; } if (empty($user)) { $user = $GLOBALS['registry']->getAuth(); } if (empty($prefs)) { $prefs = $GLOBALS['prefs']; } $methods = !empty($this->methods) ? $this->methods : @unserialize($prefs->getValue('event_alarms')); if (isset($methods['notify'])) { $methods['notify']['show'] = array('__app' => $GLOBALS['registry']->getApp(), 'event' => $this->id, 'calendar' => $this->calendar); $methods['notify']['ajax'] = 'event:' . $this->calendarType . '|' . $this->calendar . ':' . $this->id . ':' . $start->dateString(); if (!empty($methods['notify']['sound'])) { if ($methods['notify']['sound'] == 'on') { // Handle boolean sound preferences. $methods['notify']['sound'] = (string) Horde_Themes::sound('theetone.wav'); } else { // Else we know we have a sound name that can be // served from Horde. $methods['notify']['sound'] = (string) Horde_Themes::sound($methods['notify']['sound']); } } if ($this->isAllDay()) { if ($start->compareDate($end) == 0) { $methods['notify']['subtitle'] = sprintf(_("On %s"), '<strong>' . $start->strftime($prefs->getValue('date_format')) . '</strong>'); } else { $methods['notify']['subtitle'] = sprintf(_("From %s to %s"), '<strong>' . $start->strftime($prefs->getValue('date_format')) . '</strong>', '<strong>' . $end->strftime($prefs->getValue('date_format')) . '</strong>'); } } else { $methods['notify']['subtitle'] = sprintf(_("From %s at %s to %s at %s"), '<strong>' . $start->strftime($prefs->getValue('date_format')), $start->format($prefs->getValue('twentyFour') ? 'H:i' : 'h:ia') . '</strong>', '<strong>' . $end->strftime($prefs->getValue('date_format')), $this->end->format($prefs->getValue('twentyFour') ? 'H:i' : 'h:ia') . '</strong>'); } } if (isset($methods['mail'])) { $image = Kronolith::getImagePart('big_alarm.png'); $view = new Horde_View(array('templatePath' => KRONOLITH_TEMPLATES . '/alarm', 'encoding' => 'UTF-8')); new Horde_View_Helper_Text($view); $view->event = $this; $view->imageId = $image->getContentId(); $view->user = $user; $view->dateFormat = $prefs->getValue('date_format'); $view->timeFormat = $prefs->getValue('twentyFour') ? 'H:i' : 'h:ia'; $view->start = $start; if (!$prefs->isLocked('event_reminder')) { $view->prefsUrl = Horde::url($GLOBALS['registry']->getServiceLink('prefs', 'kronolith'), true)->remove(session_name()); } if ($this->attendees) { $view->attendees = Kronolith::getAttendeeEmailList($this->attendees)->addresses; } $methods['mail']['mimepart'] = Kronolith::buildMimeMessage($view, 'mail', $image); } if (isset($methods['desktop'])) { if ($this->isAllDay()) { if ($this->start->compareDate($this->end) == 0) { $methods['desktop']['subtitle'] = sprintf(_("On %s"), $start->strftime($prefs->getValue('date_format'))); } else { $methods['desktop']['subtitle'] = sprintf(_("From %s to %s"), $start->strftime($prefs->getValue('date_format')), $end->strftime($prefs->getValue('date_format'))); } } else { $methods['desktop']['subtitle'] = sprintf(_("From %s at %s to %s at %s"), $start->strftime($prefs->getValue('date_format')), $start->format($prefs->getValue('twentyFour') ? 'H:i' : 'h:ia'), $end->strftime($prefs->getValue('date_format')), $this->end->format($prefs->getValue('twentyFour') ? 'H:i' : 'h:ia')); } $methods['desktop']['url'] = strval($this->getViewUrl(array(), true, false)); } $alarmStart = clone $start; $alarmStart->min -= $this->alarm; $alarm = array('id' => $this->uid, 'user' => $user, 'start' => $alarmStart, 'end' => $end, 'methods' => array_keys($methods), 'params' => $methods, 'title' => $this->getTitle($user), 'text' => $this->description, 'instanceid' => $this->recurs() ? $eventDate->dateString() : null); $_SERVER['SERVER_NAME'] = $serverName; $GLOBALS['conf']['server']['name'] = $serverConf; return $alarm; }
/** * Retrieve the busy times from this event within the given timeframe. This * is trivial for non-recurring events but recurring events need to be * expanded. * * @param Horde_Date $startDate The start point. * @param Horde_Date $endDate The end point. * * @return array The list of busy times (only the start times of the event). */ public function getBusyTimes(Horde_Date $startDate, Horde_Date $endDate) { if (!$this->recurs()) { if ($startDate->compareDateTime($this->_start) > 0 || $endDate->compareDateTime($this->_start) < 0) { return array(); } return array($this->_start->timestamp()); } else { $result = array(); $next = $this->_recurrence->nextRecurrence($startDate); while ($next) { if ($endDate->compareDateTime($next) < 0) { break; } if (!$this->_recurrence->hasException($next->year, $next->month, $next->mday)) { $result[] = $next->timestamp(); } $next->mday++; $next = $this->_recurrence->nextRecurrence($next); } return $result; } }
/** */ public function testCompletions() { $r = new Horde_Date_Recurrence(new Horde_Date(1970, 1, 1)); $r->setRecurType(Horde_Date_Recurrence::RECUR_DAILY); $r->addCompletion(1970, 1, 2); $this->assertTrue($r->hasCompletion(1970, 1, 2)); $this->assertEquals(1, count($r->getCompletions())); $r->addCompletion(1970, 1, 4); $this->assertEquals(2, count($r->getCompletions())); $r->deleteCompletion(1970, 1, 2); $this->assertEquals(1, count($r->getCompletions())); $this->assertFalse($r->hasCompletion(1970, 1, 2)); $r->addCompletion(1970, 1, 2); $r->addException(1970, 1, 1); $r->addException(1970, 1, 3); $next = $r->nextRecurrence(new Horde_Date($r->start)); $this->assertEquals(1, $next->mday); $this->assertTrue($r->hasException($next->year, $next->month, $next->mday)); $next->mday++; $next = $r->nextRecurrence($next); $this->assertTrue($r->hasCompletion($next->year, $next->month, $next->mday)); $next->mday++; $next = $r->nextRecurrence($next); $this->assertTrue($r->hasException($next->year, $next->month, $next->mday)); $next->mday++; $next = $r->nextRecurrence($next); $this->assertTrue($r->hasCompletion($next->year, $next->month, $next->mday)); $r->setRecurEnd(new Horde_Date(1970, 1, 4)); $this->assertTrue($r->hasRecurEnd()); $this->assertFalse($r->hasActiveRecurrence()); $s = new Horde_Date_Recurrence(new Horde_Date(1970, 1, 1)); $s->fromKolab($r->toKolab()); $this->assertTrue($s->hasRecurEnd()); $next = $s->nextRecurrence(new Horde_Date($s->start)); $this->assertEquals(1, $next->mday); $this->assertTrue($s->hasException($next->year, $next->month, $next->mday)); $next->mday++; $next = $s->nextRecurrence($next); $this->assertTrue($s->hasCompletion($next->year, $next->month, $next->mday)); $next->mday++; $next = $s->nextRecurrence($next); $this->assertTrue($s->hasException($next->year, $next->month, $next->mday)); $next->mday++; $next = $s->nextRecurrence($next); $this->assertTrue($s->hasCompletion($next->year, $next->month, $next->mday)); $this->assertEquals(2, count($s->getCompletions())); $this->assertEquals(2, count($s->getExceptions())); $this->assertFalse($s->hasActiveRecurrence()); $this->assertEquals(2, count($s->getCompletions())); $s->deleteCompletion(1970, 1, 2); $this->assertEquals(1, count($s->getCompletions())); $s->deleteCompletion(1970, 1, 4); $this->assertEquals(0, count($s->getCompletions())); }