/**
* TODO
*/
public function saveCalendar()
{
global $calendar_manager, $injector, $notification, $prefs, $registry, $session;
$calendar_id = $this->vars->calendar;
$result = new stdClass();
switch ($this->vars->type) {
case 'internal':
$info = array();
foreach (array('name', 'color', 'description', 'tags', 'system') as $key) {
$info[$key] = $this->vars->{$key};
}
// Create a calendar.
if (!$calendar_id) {
if (!$registry->getAuth() || $prefs->isLocked('default_share')) {
return $result;
}
try {
$calendar = Kronolith::addShare($info);
Kronolith::readPermsForm($calendar);
if ($calendar->hasPermission($registry->getAuth(), Horde_Perms::SHOW)) {
$wrapper = new Kronolith_Calendar_Internal(array('share' => $calendar));
$result->saved = true;
$result->id = $calendar->getName();
$result->calendar = $wrapper->toHash();
}
} catch (Exception $e) {
$notification->push($e, 'horde.error');
return $result;
}
$notification->push(sprintf(_("The calendar \"%s\" has been created."), $info['name']), 'horde.success');
break;
}
// Update a calendar.
try {
$calendar = $injector->getInstance('Kronolith_Shares')->getShare($calendar_id);
$original_name = $calendar->get('name');
$original_owner = $calendar->get('owner');
Kronolith::updateShare($calendar, $info);
Kronolith::readPermsForm($calendar);
if (!$info['system'] && $calendar->get('owner') != $original_owner || $info['system'] && !is_null($original_owner)) {
$result->deleted = true;
}
if ($calendar->hasPermission($registry->getAuth(), Horde_Perms::SHOW) || is_null($calendar->get('owner')) && $registry->isAdmin()) {
$wrapper = new Kronolith_Calendar_Internal(array('share' => $calendar));
$result->saved = true;
$result->id = $calendar->getName();
$result->calendar = $wrapper->toHash();
}
} catch (Exception $e) {
$notification->push($e, 'horde.error');
return $result;
}
if ($calendar->get('name') != $original_name) {
$notification->push(sprintf(_("The calendar \"%s\" has been renamed to \"%s\"."), $original_name, $calendar->get('name')), 'horde.success');
} else {
$notification->push(sprintf(_("The calendar \"%s\" has been saved."), $original_name), 'horde.success');
}
break;
case 'tasklists':
$calendar = array();
foreach (array('name', 'color', 'description') as $key) {
$calendar[$key] = $this->vars->{$key};
}
// Create a task list.
if (!$calendar_id) {
if (!$registry->getAuth() || $prefs->isLocked('default_share')) {
return $result;
}
try {
$tasklistId = $registry->tasks->addTasklist($calendar['name'], $calendar['description'], $calendar['color']);
$tasklists = $registry->tasks->listTasklists(true);
if (!isset($tasklists[$tasklistId])) {
$notification->push(_("Added task list not found."), 'horde.error');
return $result;
}
$tasklist = $tasklists[$tasklistId];
Kronolith::readPermsForm($tasklist);
if ($tasklist->hasPermission($registry->getAuth(), Horde_Perms::SHOW)) {
$wrapper = new Kronolith_Calendar_External_Tasks(array('api' => 'tasks', 'name' => $tasklistId, 'share' => $tasklist));
// Update external calendars caches.
$all_external = $session->get('kronolith', 'all_external_calendars');
$all_external[] = array('a' => 'tasks', 'n' => $tasklistId, 'd' => $tasklist->get('name'));
$session->set('kronolith', 'all_external_calendars', $all_external);
$display_external = $calendar_manager->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS);
$display_external[] = 'tasks/' . $tasklistId;
$calendar_manager->set(Kronolith::DISPLAY_EXTERNAL_CALENDARS, $display_external);
$prefs->setValue('display_external_cals', serialize($display_external));
$all_external = $calendar_manager->get(Kronolith::ALL_EXTERNAL_CALENDARS);
$all_external['tasks/' . $tasklistId] = $wrapper;
$calendar_manager->set(Kronolith::ALL_EXTERNAL_CALENDARS, $all_external);
$result->saved = true;
$result->id = 'tasks/' . $tasklistId;
$result->calendar = $wrapper->toHash();
}
} catch (Exception $e) {
$notification->push($e, 'horde.error');
return $result;
}
$notification->push(sprintf(_("The task list \"%s\" has been created."), $calendar['name']), 'horde.success');
break;
}
// Update a task list.
$calendar_id = substr($calendar_id, 6);
try {
$registry->tasks->updateTasklist($calendar_id, $calendar);
$tasklists = $registry->tasks->listTasklists(true, Horde_Perms::EDIT);
$tasklist = $tasklists[$calendar_id];
$original_owner = $tasklist->get('owner');
Kronolith::readPermsForm($tasklist);
if ($tasklist->get('owner') != $original_owner) {
$result->deleted = true;
}
if ($tasklist->hasPermission($registry->getAuth(), Horde_Perms::SHOW)) {
$wrapper = new Kronolith_Calendar_External_Tasks(array('api' => 'tasks', 'name' => $calendar_id, 'share' => $tasklist));
$result->saved = true;
$result->calendar = $wrapper->toHash();
}
} catch (Exception $e) {
$notification->push($e, 'horde.error');
return $result;
}
if ($tasklist->get('name') != $calendar['name']) {
$notification->push(sprintf(_("The task list \"%s\" has been renamed to \"%s\"."), $tasklist->get('name'), $calendar['name']), 'horde.success');
} else {
$notification->push(sprintf(_("The task list \"%s\" has been saved."), $tasklist->get('name')), 'horde.success');
}
break;
case 'remote':
$calendar = array();
foreach (array('name', 'desc', 'url', 'color', 'user', 'password') as $key) {
$calendar[$key] = $this->vars->{$key};
}
try {
Kronolith::subscribeRemoteCalendar($calendar, $calendar_id);
} catch (Exception $e) {
$notification->push($e, 'horde.error');
return $result;
}
if ($calendar_id) {
$notification->push(sprintf(_("The calendar \"%s\" has been saved."), $calendar['name']), 'horde.success');
} else {
$notification->push(sprintf(_("You have been subscribed to \"%s\" (%s)."), $calendar['name'], $calendar['url']), 'horde.success');
$result->id = $calendar['url'];
}
$wrapper = new Kronolith_Calendar_Remote($calendar);
$result->saved = true;
$result->calendar = $wrapper->toHash();
break;
case 'resource':
foreach (array('name', 'desc', 'response_type') as $key) {
$info[$key] = $this->vars->{$key};
}
if (!$calendar_id) {
// New resource
if (!$registry->isAdmin() && !$injector->getInstance('Horde_Core_Perms')->hasAppPermission('resource_management')) {
$notification->push(_("You are not allowed to create new resources."), 'horde.error');
return $result;
}
$resource = Kronolith_Resource::addResource($info);
Kronolith::readPermsForm($resource);
$resource->save();
} else {
try {
$rdriver = Kronolith::getDriver('Resource');
$resource = $rdriver->getResource($rdriver->getResourceIdByCalendar($calendar_id));
if (!$resource->hasPermission($registry->getAuth(), Horde_Perms::EDIT)) {
$notification->push(_("You are not allowed to edit this resource."), 'horde.error');
return $result;
}
foreach (array('name', 'desc', 'response_type', 'email') as $key) {
$resource->set($key, $this->vars->{$key});
}
Kronolith::readPermsForm($resource);
$resource->save();
} catch (Kronolith_Exception $e) {
$notification->push($e->getMessage(), 'horde.error');
return $result;
}
}
$wrapper = new Kronolith_Calendar_Resource(array('resource' => $resource));
$result->calendar = $wrapper->toHash();
$result->saved = true;
$result->id = $resource->get('calendar');
$notification->push(sprintf(_("The resource \"%s\" has been saved."), $resource->get('name'), 'horde.success'));
break;
case 'resourcegroup':
$info = array('group' => true);
foreach (array('name', 'desc', 'members') as $key) {
$info[$key] = $this->vars->{$key};
}
if (empty($calendar_id)) {
// New resource group.
$resource = Kronolith_Resource::addResource($info);
} else {
$driver = Kronolith::getDriver('Resource');
$resource = $driver->getResource($calendar_id);
$resource->set('name', $this->vars->name);
$resource->set('desc', $this->vars->description);
$resource->set('members', $this->vars->members);
$resource->save();
}
$wrapper = new Kronolith_Calendar_ResourceGroup(array('resource' => $resource));
$result->calendar = $wrapper->toHash();
$result->saved = true;
$result->id = $resource->get('calendar');
$notification->push(sprintf(_("The resource group \"%s\" has been saved."), $resource->get('name'), 'horde.success'));
break;
}
return $result;
}