/**
* Save a new or update an existing event from the AJAX event detail view.
*
* Request parameters used:
* - event: The event id.
* - cal: The calendar id.
* - targetcalendar: If moving events, the targetcalendar to move to.
* - as_new: Save an existing event as a new event.
* - recur_edit: If editing an instance of a recurring event series,
* how to apply the edit [current|future|all].
* - rstart: If editing an instance of a recurring event series,
* the original start datetime of this instance.
* - rend: If editing an instance of a recurring event series,
* the original ending datetime of this instance.
* - sendupdates: Should updates be sent to attendees?
* - cstart: Start time of the client cache.
* - cend: End time of the client cache.
*/
public function saveEvent()
{
global $injector, $notification, $registry;
$result = $this->_signedResponse($this->vars->targetcalendar);
if (!($kronolith_driver = $this->_getDriver($this->vars->targetcalendar))) {
return $result;
}
if ($this->vars->as_new) {
unset($this->vars->event);
}
if (!$this->vars->event) {
$perms = $injector->getInstance('Horde_Core_Perms');
if ($perms->hasAppPermission('max_events') !== true && $perms->hasAppPermission('max_events') <= Kronolith::countEvents()) {
Horde::permissionDeniedError('kronolith', 'max_events', sprintf(_("You are not allowed to create more than %d events."), $perms->hasAppPermission('max_events')));
return $result;
}
}
if ($this->vars->event && $this->vars->cal && $this->vars->cal != $this->vars->targetcalendar) {
if (strpos($kronolith_driver->calendar, '\\')) {
list($target, $user) = explode('\\', $kronolith_driver->calendar, 2);
} else {
$target = $kronolith_driver->calendar;
$user = $registry->getAuth();
}
$kronolith_driver = $this->_getDriver($this->vars->cal);
// Only delete the event from the source calendar if this user has
// permissions to do so.
try {
$sourceShare = Kronolith::getInternalCalendar($kronolith_driver->calendar);
$share = Kronolith::getInternalCalendar($target);
if ($sourceShare->hasPermission($registry->getAuth(), Horde_Perms::DELETE) && ($user == $registry->getAuth() && $share->hasPermission($registry->getAuth(), Horde_Perms::EDIT) || $user != $registry->getAuth() && $share->hasPermission($registry->getAuth(), Kronolith::PERMS_DELEGATE))) {
$kronolith_driver->move($this->vars->event, $target);
$kronolith_driver = $this->_getDriver($this->vars->targetcalendar);
}
} catch (Exception $e) {
$notification->push(sprintf(_("There was an error moving the event: %s"), $e->getMessage()), 'horde.error');
return $result;
}
}
if ($this->vars->as_new) {
$event = $kronolith_driver->getEvent();
} else {
try {
// Note that when this is a new event, $this->vars->event will
// be empty, so this will create a new event.
$event = $kronolith_driver->getEvent($this->vars->event);
} catch (Horde_Exception_NotFound $e) {
$notification->push(_("The requested event was not found."), 'horde.error');
return $result;
} catch (Exception $e) {
$notification->push($e);
return $result;
}
}
if (!$event->hasPermission(Horde_Perms::EDIT)) {
$notification->push(_("You do not have permission to edit this event."), 'horde.warning');
return $result;
}
$removed_attendees = $old_attendees = array();
if ($this->vars->recur_edit && $this->vars->recur_edit != 'all') {
switch ($this->vars->recur_edit) {
case 'current':
$attributes = new stdClass();
$attributes->rstart = $this->vars->rstart;
$attributes->rend = $this->vars->rend;
$this->_addException($event, $attributes);
// Create a copy of the original event so we can read in the
// new form values for the exception. We also MUST reset the
// recurrence property even though we won't be using it, since
// clone() does not do a deep copy. Otherwise, the original
// event's recurrence will become corrupt.
$newEvent = clone $event;
$newEvent->recurrence = new Horde_Date_Recurrence($event->start);
$newEvent->readForm($event);
// Create an exception event from the new properties.
$exception = $this->_copyEvent($event, $newEvent, $attributes);
$exception->start = $newEvent->start;
$exception->end = $newEvent->end;
// Save the new exception.
$attributes->cstart = $this->vars->cstart;
$attributes->cend = $this->vars->cend;
$result = $this->_saveEvent($exception, $event, $attributes);
break;
case 'future':
$instance = new Horde_Date($this->vars->rstart, $event->timezone);
$exception = clone $instance;
$exception->mday--;
if ($event->end->compareDate($exception) > 0) {
// Same as 'all' since this is the first recurrence.
$this->vars->recur_edit = 'all';
return $this->saveEvent();
} else {
$event->recurrence->setRecurEnd($exception);
$newEvent = $kronolith_driver->getEvent();
$newEvent->readForm();
$newEvent->uid = null;
$result = $this->_saveEvent($newEvent, $event, $this->vars, true);
}
}
} else {
try {
$old_attendees = $event->attendees;
$event->readForm();
$removed_attendees = array_diff(array_keys($old_attendees), array_keys($event->attendees));
$result = $this->_saveEvent($event);
} catch (Exception $e) {
$notification->push($e);
return $result;
}
}
if ($result !== true && $this->vars->sendupdates) {
$type = $event->status == Kronolith::STATUS_CANCELLED ? Kronolith::ITIP_CANCEL : Kronolith::ITIP_REQUEST;
Kronolith::sendITipNotifications($event, $notification, $type);
}
// Send a CANCEL iTip for attendees that have been removed, but only if
// the entire event isn't being marked as cancelled (which would be
// caught above).
if (!empty($removed_attendees)) {
$to_cancel = array();
foreach ($removed_attendees as $email) {
$to_cancel[$email] = $old_attendees[$email];
}
$cancelEvent = clone $event;
Kronolith::sendITipNotifications($cancelEvent, $notification, Kronolith::ITIP_CANCEL, null, null, $to_cancel);
}
Kronolith::notifyOfResourceRejection($event);
return $result;
}