diff options
Diffstat (limited to 'app/Model')
-rw-r--r-- | app/Model/Category.php | 13 | ||||
-rw-r--r-- | app/Model/Task.php | 96 |
2 files changed, 60 insertions, 49 deletions
diff --git a/app/Model/Category.php b/app/Model/Category.php index 58841f9a..4f296944 100644 --- a/app/Model/Category.php +++ b/app/Model/Category.php @@ -21,6 +21,19 @@ class Category extends Base const TABLE = 'project_has_categories'; /** + * Return true if a category exists for a given project + * + * @access public + * @param integer $category_id Category id + * @param integer $project_id Project id + * @return boolean + */ + public function exists($category_id, $project_id) + { + return $this->db->table(self::TABLE)->eq('id', $category_id)->eq('project_id', $project_id)->count() > 0; + } + + /** * Get a category by the id * * @access public diff --git a/app/Model/Task.php b/app/Model/Task.php index 30c1781b..6a20f4d0 100644 --- a/app/Model/Task.php +++ b/app/Model/Task.php @@ -263,55 +263,20 @@ class Task extends Base } /** - * Duplicate a task + * Generic method to duplicate a task * * @access public - * @param integer $task_id Task id - * @return boolean + * @param array $task Task data + * @param array $override Task properties to override + * @return integer|boolean */ - public function duplicate($task_id) + public function copy(array $task, array $override = array()) { - $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['is_active'] = 1; - $task['position'] = $this->countByColumnId($task['project_id'], $task['column_id']); - - // Save task - if (! $this->db->table(self::TABLE)->save($task)) { - $this->db->cancelTransaction(); - return false; + // Values to override + if (! empty($override)) { + $task = $override + $task; } - $task_id = $this->db->getConnection()->getLastId(); - - $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); - - return $task_id; - } - - /** - * Duplicate a task to another project (always copy to the first column) - * - * @access public - * @param integer $project_id Destination project id - * @param array $task Task data - * @return boolean - */ - public function duplicateToAnotherProject($project_id, array $task) - { $this->db->startTransaction(); // Assign new values @@ -322,19 +287,24 @@ class Task extends Base $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['project_id'] = $task['project_id']; + $values['column_id'] = $task['column_id']; $values['owner_id'] = 0; $values['creator_id'] = $task['creator_id']; - $values['position'] = $this->countByColumnId($project_id, $values['column_id']); + $values['position'] = $this->countByColumnId($values['project_id'], $values['column_id']) + 1; $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'])) { + if ($task['owner_id'] && $this->project->isUserAllowed($values['project_id'], $task['owner_id'])) { $values['owner_id'] = $task['owner_id']; } + // Check if the category exists + if ($task['category_id'] && $this->category->exists($task['category_id'], $task['project_id'])) { + $values['category_id'] = $task['category_id']; + } + // Save task if (! $this->db->table(self::TABLE)->save($values)) { $this->db->cancelTransaction(); @@ -359,6 +329,34 @@ class Task extends Base } /** + * Duplicate a task to the same project + * + * @access public + * @param array $task Task data + * @return integer|boolean + */ + public function duplicateSameProject($task) + { + return $this->copy($task); + } + + /** + * Duplicate a task to another project (always copy to the first column) + * + * @access public + * @param integer $project_id Destination project id + * @param array $task Task data + * @return integer|boolean + */ + public function duplicateToAnotherProject($project_id, array $task) + { + return $this->copy($task, array( + 'project_id' => $project_id, + 'column_id' => $this->board->getFirstColumn($project_id), + )); + } + + /** * Prepare data before task creation or modification * * @access public @@ -399,7 +397,7 @@ class Task extends Base $this->prepare($values); $values['date_creation'] = time(); $values['date_modification'] = $values['date_creation']; - $values['position'] = $this->countByColumnId($values['project_id'], $values['column_id']); + $values['position'] = $this->countByColumnId($values['project_id'], $values['column_id']) + 1; // Save task if (! $this->db->table(self::TABLE)->save($values)) { @@ -592,7 +590,7 @@ class Task extends Base // We use the first column of the new project $values['column_id'] = $this->board->getFirstColumn($project_id); - $values['position'] = $this->countByColumnId($project_id, $values['column_id']); + $values['position'] = $this->countByColumnId($project_id, $values['column_id']) + 1; $values['project_id'] = $project_id; if ($this->db->table(self::TABLE)->eq('id', $task['id'])->update($values)) { |