summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorFrédéric Guillot <fred@kanboard.net>2014-11-23 18:23:20 -0500
committerFrédéric Guillot <fred@kanboard.net>2014-11-23 18:23:20 -0500
commit81df22de238bc36d810b95ad67a4426db095fbab (patch)
tree53231a3d46b6b7c54eaec515016247984cf7a4e4 /app
parentf684602ebedc0adc4a329693ba524ad46d5cd8b0 (diff)
Create TaskDuplication model
Diffstat (limited to 'app')
-rw-r--r--app/Action/Base.php1
-rw-r--r--app/Action/TaskDuplicateAnotherProject.php4
-rw-r--r--app/Action/TaskMoveAnotherProject.php4
-rw-r--r--app/Controller/Base.php1
-rw-r--r--app/Controller/Task.php47
-rw-r--r--app/Model/Category.php28
-rw-r--r--app/Model/SubTask.php24
-rw-r--r--app/Model/Task.php130
-rw-r--r--app/Model/TaskDuplication.php145
9 files changed, 222 insertions, 162 deletions
diff --git a/app/Action/Base.php b/app/Action/Base.php
index b7255371..ae5c2e03 100644
--- a/app/Action/Base.php
+++ b/app/Action/Base.php
@@ -17,6 +17,7 @@ use Core\Tool;
* @property \Model\Task $task
* @property \Model\TaskCreation $taskCreation
* @property \Model\TaskModification $taskModification
+ * @property \Model\TaskDuplication $taskDuplication
* @property \Model\TaskFinder $taskFinder
* @property \Model\TaskStatus $taskStatus
*/
diff --git a/app/Action/TaskDuplicateAnotherProject.php b/app/Action/TaskDuplicateAnotherProject.php
index 4ab88534..55ebc76e 100644
--- a/app/Action/TaskDuplicateAnotherProject.php
+++ b/app/Action/TaskDuplicateAnotherProject.php
@@ -64,9 +64,7 @@ class TaskDuplicateAnotherProject extends Base
*/
public function doAction(array $data)
{
- $task = $this->taskFinder->getById($data['task_id']);
- $this->task->duplicateToAnotherProject($this->getParam('project_id'), $task);
- return true;
+ return (bool) $this->taskDuplication->duplicateToProject($data['task_id'], $this->getParam('project_id'));
}
/**
diff --git a/app/Action/TaskMoveAnotherProject.php b/app/Action/TaskMoveAnotherProject.php
index d852f56d..ee212998 100644
--- a/app/Action/TaskMoveAnotherProject.php
+++ b/app/Action/TaskMoveAnotherProject.php
@@ -64,9 +64,7 @@ class TaskMoveAnotherProject extends Base
*/
public function doAction(array $data)
{
- $task = $this->taskFinder->getById($data['task_id']);
- $this->task->moveToAnotherProject($this->getParam('project_id'), $task);
- return true;
+ return $this->taskDuplication->moveToProject($data['task_id'], $this->getParam('project_id'));
}
/**
diff --git a/app/Controller/Base.php b/app/Controller/Base.php
index 8dd96c56..9b8ca0ec 100644
--- a/app/Controller/Base.php
+++ b/app/Controller/Base.php
@@ -35,6 +35,7 @@ use Model\LastLogin;
* @property \Model\Task $task
* @property \Model\TaskCreation $taskCreation
* @property \Model\TaskModification $taskModification
+ * @property \Model\TaskDuplication $taskDuplication
* @property \Model\TaskHistory $taskHistory
* @property \Model\TaskExport $taskExport
* @property \Model\TaskFinder $taskFinder
diff --git a/app/Controller/Task.php b/app/Controller/Task.php
index 6c6eaf90..8dbd8429 100644
--- a/app/Controller/Task.php
+++ b/app/Controller/Task.php
@@ -352,7 +352,7 @@ class Task extends Base
if ($this->request->getStringParam('confirmation') === 'yes') {
$this->checkCSRFParam();
- $task_id = $this->task->duplicateToSameProject($task);
+ $task_id = $this->taskDuplication->duplicate($task['id']);
if ($task_id) {
$this->session->flash(t('Task created successfully.'));
@@ -428,7 +428,36 @@ class Task extends Base
*/
public function move()
{
- $this->toAnotherProject('move');
+ $task = $this->getTask();
+ $values = $task;
+ $errors = array();
+ $projects_list = $this->projectPermission->getMemberProjects($this->acl->getUserId());
+
+ unset($projects_list[$task['project_id']]);
+
+ if ($this->request->isPost()) {
+
+ $values = $this->request->getValues();
+ list($valid, $errors) = $this->taskValidator->validateProjectModification($values);
+
+ if ($valid) {
+
+ if ($this->taskDuplication->moveToProject($task['id'], $values['project_id'])) {
+ $this->session->flash(t('Task updated successfully.'));
+ $this->response->redirect('?controller=task&action=show&task_id='.$task['id']);
+ }
+ else {
+ $this->session->flashError(t('Unable to update your task.'));
+ }
+ }
+ }
+
+ $this->response->html($this->taskLayout('task_move_project', array(
+ 'values' => $values,
+ 'errors' => $errors,
+ 'task' => $task,
+ 'projects_list' => $projects_list,
+ )));
}
/**
@@ -438,16 +467,6 @@ class Task extends Base
*/
public function copy()
{
- $this->toAnotherProject('duplicate');
- }
-
- /**
- * Common methods between the actions "move" and "copy"
- *
- * @access private
- */
- private function toAnotherProject($action)
- {
$task = $this->getTask();
$values = $task;
$errors = array();
@@ -461,7 +480,7 @@ class Task extends Base
list($valid, $errors) = $this->taskValidator->validateProjectModification($values);
if ($valid) {
- $task_id = $this->task->{$action.'ToAnotherProject'}($values['project_id'], $task);
+ $task_id = $this->taskDuplication->duplicateToProject($task['id'], $values['project_id']);
if ($task_id) {
$this->session->flash(t('Task created successfully.'));
$this->response->redirect('?controller=task&action=show&task_id='.$task_id);
@@ -472,7 +491,7 @@ class Task extends Base
}
}
- $this->response->html($this->taskLayout('task_'.$action.'_project', array(
+ $this->response->html($this->taskLayout('task_duplicate_project', array(
'values' => $values,
'errors' => $errors,
'task' => $task,
diff --git a/app/Model/Category.php b/app/Model/Category.php
index 7fed50a3..65fd0c56 100644
--- a/app/Model/Category.php
+++ b/app/Model/Category.php
@@ -46,6 +46,34 @@ class Category extends Base
}
/**
+ * Get the category name by the id
+ *
+ * @access public
+ * @param integer $category_id Category id
+ * @return string
+ */
+ public function getNameById($category_id)
+ {
+ return $this->db->table(self::TABLE)->eq('id', $category_id)->findOneColumn('name') ?: '';
+ }
+
+ /**
+ * Get a category id by the project and the name
+ *
+ * @access public
+ * @param integer $project_id Project id
+ * @param string $category_name Category name
+ * @return integer
+ */
+ public function getIdByName($project_id, $category_name)
+ {
+ return (int) $this->db->table(self::TABLE)
+ ->eq('project_id', $project_id)
+ ->eq('name', $category_name)
+ ->findOneColumn('id');
+ }
+
+ /**
* Return the list of all categories
*
* @access public
diff --git a/app/Model/SubTask.php b/app/Model/SubTask.php
index 25979eda..87820c69 100644
--- a/app/Model/SubTask.php
+++ b/app/Model/SubTask.php
@@ -193,22 +193,22 @@ class SubTask extends Base
*/
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();
+ return $this->db->transaction(function ($db) use ($src_task_id, $dst_task_id) {
- foreach ($subtasks as &$subtask) {
+ $subtasks = $db->table(self::TABLE)
+ ->columns('title', 'time_estimated')
+ ->eq('task_id', $src_task_id)
+ ->findAll();
- $subtask['task_id'] = $dst_task_id;
- $subtask['time_spent'] = 0;
+ foreach ($subtasks as &$subtask) {
- if (! $this->db->table(self::TABLE)->save($subtask)) {
- return false;
- }
- }
+ $subtask['task_id'] = $dst_task_id;
- return true;
+ if (! $db->table(self::TABLE)->save($subtask)) {
+ return false;
+ }
+ }
+ });
}
/**
diff --git a/app/Model/Task.php b/app/Model/Task.php
index f382b604..a745f30f 100644
--- a/app/Model/Task.php
+++ b/app/Model/Task.php
@@ -58,136 +58,6 @@ class Task extends Base
}
/**
- * Move a task to another project
- *
- * @access public
- * @param integer $project_id Project id
- * @param array $task Task data
- * @return boolean
- */
- public function moveToAnotherProject($project_id, array $task)
- {
- $values = array();
-
- // Clear values (categories are different for each project)
- $values['category_id'] = 0;
- $values['owner_id'] = 0;
-
- // Check if the assigned user is allowed for the new project
- if ($task['owner_id'] && $this->projectPermission->isUserAllowed($project_id, $task['owner_id'])) {
- $values['owner_id'] = $task['owner_id'];
- }
-
- // We use the first column of the new project
- $values['column_id'] = $this->board->getFirstColumn($project_id);
- $values['position'] = $this->taskFinder->countByColumnId($project_id, $values['column_id']) + 1;
- $values['project_id'] = $project_id;
-
- // The task will be open (close event binding)
- $values['is_active'] = 1;
-
- if ($this->db->table(self::TABLE)->eq('id', $task['id'])->update($values)) {
- return $task['id'];
- }
-
- return false;
- }
-
- /**
- * Generic method to duplicate a task
- *
- * @access public
- * @param array $task Task data
- * @param array $override Task properties to override
- * @return integer|boolean
- */
- public function copy(array $task, array $override = array())
- {
- // Values to override
- if (! empty($override)) {
- $task = $override + $task;
- }
-
- $this->db->startTransaction();
-
- // Assign new values
- $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'] = $task['project_id'];
- $values['column_id'] = $task['column_id'];
- $values['owner_id'] = 0;
- $values['creator_id'] = $task['creator_id'];
- $values['position'] = $this->taskFinder->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->projectPermission->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(Task::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(Task::EVENT_CREATE_UPDATE, array('task_id' => $task_id) + $values);
- $this->event->trigger(Task::EVENT_CREATE, array('task_id' => $task_id) + $values);
-
- return $task_id;
- }
-
- /**
- * Duplicate a task to the same project
- *
- * @access public
- * @param array $task Task data
- * @return integer|boolean
- */
- public function duplicateToSameProject($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),
- ));
- }
-
- /**
* Get a the task id from a text
*
* Example: "Fix bug #1234" will return 1234
diff --git a/app/Model/TaskDuplication.php b/app/Model/TaskDuplication.php
new file mode 100644
index 00000000..ab7a57f1
--- /dev/null
+++ b/app/Model/TaskDuplication.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace Model;
+
+/**
+ * Task Duplication
+ *
+ * @package model
+ * @author Frederic Guillot
+ */
+class TaskDuplication extends Base
+{
+ /**
+ * Fields to copy when duplicating a task
+ *
+ * @access private
+ * @var array
+ */
+ private $fields_to_duplicate = array(
+ 'title',
+ 'description',
+ 'date_due',
+ 'color_id',
+ 'project_id',
+ 'column_id',
+ 'owner_id',
+ 'score',
+ 'category_id',
+ 'time_estimated',
+ );
+
+ /**
+ * Duplicate a task to the same project
+ *
+ * @access public
+ * @param integer $task_id Task id
+ * @return boolean|integer Duplicated task id
+ */
+ public function duplicate($task_id)
+ {
+ return $this->save($task_id, $this->copyFields($task_id));
+ }
+
+ /**
+ * Duplicate a task to another project
+ *
+ * @access public
+ * @param integer $task_id Task id
+ * @param integer $project_id Project id
+ * @return boolean|integer Duplicated task id
+ */
+ public function duplicateToProject($task_id, $project_id)
+ {
+ $values = $this->copyFields($task_id);
+ $values['project_id'] = $project_id;
+ $values['column_id'] = $this->board->getFirstColumn($project_id);
+
+ $this->checkDestinationProjectValues($values);
+
+ return $this->save($task_id, $values);
+ }
+
+ /**
+ * Move a task to another project
+ *
+ * @access public
+ * @param integer $task_id Task id
+ * @param integer $project_id Project id
+ * @return boolean
+ */
+ public function moveToProject($task_id, $project_id)
+ {
+ $task = $this->taskFinder->getById($task_id);
+
+ $values = array();
+ $values['is_active'] = 1;
+ $values['project_id'] = $project_id;
+ $values['column_id'] = $this->board->getFirstColumn($project_id);
+ $values['position'] = $this->taskFinder->countByColumnId($project_id, $values['column_id']) + 1;
+ $values['owner_id'] = $task['owner_id'];
+ $values['category_id'] = $task['category_id'];
+
+ $this->checkDestinationProjectValues($values);
+
+ return $this->db->table(Task::TABLE)->eq('id', $task['id'])->update($values);
+ }
+
+ /**
+ * Check if the assignee and the category are available in the destination project
+ *
+ * @access private
+ * @param array $values
+ */
+ private function checkDestinationProjectValues(&$values)
+ {
+ // Check if the assigned user is allowed for the destination project
+ if ($values['owner_id'] > 0 && ! $this->projectPermission->isUserAllowed($values['project_id'], $values['owner_id'])) {
+ $values['owner_id'] = 0;
+ }
+
+ // Check if the category exists for the destination project
+ if ($values['category_id'] > 0) {
+ $category_name = $this->category->getNameById($values['category_id']);
+ $values['category_id'] = $this->category->getIdByName($values['project_id'], $category_name);
+ }
+ }
+
+ /**
+ * Duplicate fields for the new task
+ *
+ * @access private
+ * @param integer $task_id Task id
+ * @return array
+ */
+ private function copyFields($task_id)
+ {
+ $task = $this->taskFinder->getById($task_id);
+ $values = array();
+
+ foreach ($this->fields_to_duplicate as $field) {
+ $values[$field] = $task[$field];
+ }
+
+ return $values;
+ }
+
+ /**
+ * Create the new task and duplicate subtasks
+ *
+ * @access private
+ * @param integer $task_id Task id
+ * @param array $values Form values
+ * @return boolean|integer
+ */
+ private function save($task_id, array $values)
+ {
+ $new_task_id = $this->taskCreation->create($values);
+
+ if ($new_task_id) {
+ $this->subTask->duplicate($task_id, $new_task_id);
+ }
+
+ return $new_task_id;
+ }
+}