protected function onModelSaved($model) { $selectedParent = $this->byId($this->_getParam('parent'), 'UserGroup'); if ($selectedParent != null) { try { $this->groupService->addToGroup($selectedParent, $model); } catch (RecursiveGroupException $rge) { $this->flash("Cannot set a child group as the parent"); } } $this->redirect('admin', 'grouplist'); }
/** * Save a task. * * Saves the task object, then updates any task assignments that need * attention. * * @param Task $taskToSave The task to save * @param boolean $updateGroups Whether to update group membership on save. Defaults * to false to prevent too much db access * @param boolean $updateAssignees Whether the assignees should be updated. * make this false if you know the assigned users haven't changed */ public function saveTask($taskToSave, $updateGroups = false, $updateAssignees = true) { $task = null; try { $this->dbService->beginTransaction(); $oldId = null; $projectid = 0; $title = ''; if (is_array($taskToSave)) { $title = ifset($taskToSave, 'title', 'Untitled Task'); if (ifset($taskToSave, 'complete', false)) { $taskToSave['completedate'] = date('Y-m-d H:i:s'); } if (isset($taskToSave['id'])) { $oldId = $taskToSave['id']; } } else { $title = $taskToSave->title; if ($taskToSave->complete) { $taskToSave->completedate = date('Y-m-d H:i:s'); } if ($taskToSave->id) { $oldId = $taskToSave->id; } } // if there's an OLD ID, get that task so we know what dependencies // will need to be updated after saving $dependency = null; $oldState = null; if ($oldId) { $oldState = $this->getTask($oldId); $dependency = $oldState->getDependencyId(); } else { // no previous id, so must be creating from scratch $this->trackerService->track('create-task', $title); } $task = $this->dbService->saveObject($taskToSave, 'Task'); if ($task == null) { throw new Exception("Failed creating task for parameters " . print_r($taskToSave, true)); } $projectid = $task->projectid ? $task->projectid : 0; if (!$projectid) { $unassignedProject = $this->getUnassignedTaskProject(); $task->projectid = $unassignedProject->id; $this->dbService->saveObject($task); } // If the task's dependency is different, or its dates have changed, // update all dependants if ($oldState != null) { if (date('Y-m-d', strtotime($oldState->due)) != date('Y-m-d', strtotime($task->due)) || $dependency != $task->getDependencyId()) { // go and update the dependency $this->updateTaskDependants($task, $dependency); } } // Get the project because we'll be adding users // to the project in a moment $project = $this->getProject($task->projectid); if (!is_array($task->userid)) { $task->userid = array(); } if ($updateAssignees) { // Delete all the old user/task assignments $this->dbService->delete('usertaskassignment', 'taskid=' . $task->id); foreach ($task->userid as $username) { // create a new assignment $params = array('taskid' => $task->id, 'userid' => $username); $user = $this->userService->getByName($username); if ($user && $updateGroups) { $groups = $this->groupService->getGroupsForUser($user, false); // Note here that ownerid == the group that owns this project if ($project->ownerid && !isset($groups[$project->ownerid])) { $group = $this->groupService->getGroup($project->ownerid); if ($group) { // Add the user to this group $this->groupService->addToGroup($group, $user); $this->log->debug(__CLASS__ . ':' . __LINE__ . ": User {$user->username} added to {$group->title}"); } else { $this->log->warn(__CLASS__ . ':' . __LINE__ . ": Group not found for {$project->ownerid}"); } } else { if ($project->ownerid) { $this->log->debug(__CLASS__ . ':' . __LINE__ . ": User {$user->username} is already in group " . $groups[$project->ownerid]->title); } else { $this->log->debug(__CLASS__ . ':' . __LINE__ . ": Project does not have an owner for assigning a group"); } } } $this->dbService->saveObject($params, 'UserTaskAssignment'); } } $this->updateAffectedLinkedItems($task); $this->dbService->commit(); } catch (InvalidModelException $ime) { $this->log->err("Failed saving task because of invalid data: " . print_r($ime->getMessages(), true)); $this->log->err($ime->getTraceAsString()); $this->dbService->rollback(); throw $ime; } catch (Exception $e) { $this->log->err("Failed saving task " . $e->getMessage()); $this->log->err($e->getTraceAsString()); $this->dbService->rollback(); throw $e; } return $task; }