Add the possibility to duplicate a task to another project

This commit is contained in:
Frédéric Guillot
2014-09-01 19:36:40 -08:00
parent e496554654
commit e6d0658a0e
13 changed files with 205 additions and 45 deletions

View File

@@ -121,13 +121,12 @@ class SubTask extends Base
}
/**
* Create
* Prepare data before insert/update
*
* @access public
* @param array $values Form values
* @return bool
*/
public function create(array $values)
public function prepare(array &$values)
{
if (isset($values['another_subtask'])) {
unset($values['another_subtask']);
@@ -140,7 +139,18 @@ class SubTask extends Base
if (isset($values['time_spent']) && empty($values['time_spent'])) {
$values['time_spent'] = 0;
}
}
/**
* Create
*
* @access public
* @param array $values Form values
* @return bool
*/
public function create(array $values)
{
$this->prepare($values);
$result = $this->db->table(self::TABLE)->save($values);
if ($result) {
@@ -160,14 +170,7 @@ class SubTask extends Base
*/
public function update(array $values)
{
if (isset($values['time_estimated']) && empty($values['time_estimated'])) {
$values['time_estimated'] = 0;
}
if (isset($values['time_spent']) && empty($values['time_spent'])) {
$values['time_spent'] = 0;
}
$this->prepare($values);
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->save($values);
if ($result) {
@@ -189,6 +192,34 @@ class SubTask extends Base
return $this->db->table(self::TABLE)->eq('id', $subtask_id)->remove();
}
/**
* Duplicate all subtasks to another task
*
* @access public
* @param integer $src_task_id Source task id
* @param integer $dst_task_id Destination task id
* @return bool
*/
public function duplicate($src_task_id, $dst_task_id)
{
$subtasks = $this->db->table(self::TABLE)
->columns('title', 'time_estimated')
->eq('task_id', $src_task_id)
->findAll();
foreach ($subtasks as &$subtask) {
$subtask['task_id'] = $dst_task_id;
$subtask['time_spent'] = 0;
if (! $this->db->table(self::TABLE)->save($subtask)) {
return false;
}
}
return true;
}
/**
* Validate creation/modification
*

View File

@@ -306,43 +306,54 @@ class Task extends Base
* Duplicate a task to another project (always copy to the first column)
*
* @access public
* @param integer $task_id Task id
* @param integer $project_id Destination project id
* @param array $task Task data
* @return boolean
*/
public function duplicateToAnotherProject($task_id, $project_id)
public function duplicateToAnotherProject($project_id, array $task)
{
$this->db->startTransaction();
// Get the original task
$task = $this->getById($task_id);
// Cleanup data
unset($task['id']);
unset($task['date_completed']);
// Assign new values
$task['date_creation'] = time();
$task['owner_id'] = 0;
$task['category_id'] = 0;
$task['is_active'] = 1;
$task['column_id'] = $this->board->getFirstColumn($project_id);
$task['project_id'] = $project_id;
$task['position'] = $this->countByColumnId($task['project_id'], $task['column_id']);
$values = array();
$values['title'] = $task['title'];
$values['description'] = $task['description'];
$values['date_creation'] = time();
$values['date_modification'] = $values['date_creation'];
$values['date_due'] = $task['date_due'];
$values['color_id'] = $task['color_id'];
$values['project_id'] = $project_id;
$values['column_id'] = $this->board->getFirstColumn($project_id);
$values['owner_id'] = 0;
$values['creator_id'] = $task['creator_id'];
$values['position'] = $this->countByColumnId($project_id, $values['column_id']);
$values['score'] = $task['score'];
$values['category_id'] = 0;
// Check if the assigned user is allowed for the new project
if ($task['owner_id'] && $this->project->isUserAllowed($project_id, $task['owner_id'])) {
$values['owner_id'] = $task['owner_id'];
}
// Save task
if (! $this->db->table(self::TABLE)->save($task)) {
if (! $this->db->table(self::TABLE)->save($values)) {
$this->db->cancelTransaction();
return false;
}
$task_id = $this->db->getConnection()->getLastId();
// Duplicate subtasks
if (! $this->subTask->duplicate($task['id'], $task_id)) {
$this->db->cancelTransaction();
return false;
}
$this->db->closeTransaction();
// Trigger events
$this->event->trigger(self::EVENT_CREATE_UPDATE, array('task_id' => $task_id) + $task);
$this->event->trigger(self::EVENT_CREATE, array('task_id' => $task_id) + $task);
$this->event->trigger(self::EVENT_CREATE_UPDATE, array('task_id' => $task_id) + $values);
$this->event->trigger(self::EVENT_CREATE, array('task_id' => $task_id) + $values);
return $task_id;
}
@@ -584,7 +595,11 @@ class Task extends Base
$values['position'] = $this->countByColumnId($project_id, $values['column_id']);
$values['project_id'] = $project_id;
return $this->db->table(self::TABLE)->eq('id', $task['id'])->update($values);
if ($this->db->table(self::TABLE)->eq('id', $task['id'])->update($values)) {
return $task['id'];
}
return false;
}
/**