summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/Model/TaskPosition.php180
1 files changed, 64 insertions, 116 deletions
diff --git a/app/Model/TaskPosition.php b/app/Model/TaskPosition.php
index da363cb3..0a8f6c4c 100644
--- a/app/Model/TaskPosition.php
+++ b/app/Model/TaskPosition.php
@@ -26,28 +26,34 @@ class TaskPosition extends Base
*/
public function movePosition($project_id, $task_id, $column_id, $position, $swimlane_id = 0, $fire_events = true)
{
+ $result = false;
+
if ($position < 1) {
- return false;
+ return $result;
}
$task = $this->taskFinder->getById($task_id);
- // Ignore closed tasks
if ($task['is_active'] == Task::STATUS_CLOSED) {
return true;
}
- $result = false;
+ $this->db->startTransaction();
- if ($task['swimlane_id'] != $swimlane_id) {
- $result = $this->saveSwimlaneChange($project_id, $task_id, $position, $task['column_id'], $column_id, $task['swimlane_id'], $swimlane_id);
- } elseif ($task['column_id'] != $column_id) {
- $result = $this->saveColumnChange($project_id, $task_id, $position, $swimlane_id, $task['column_id'], $column_id);
+ if ($task['swimlane_id'] != $swimlane_id || $task['column_id'] != $column_id) {
+ $result = $this->moveTaskToAnotherColumn($task, $swimlane_id, $column_id, $position);
} elseif ($task['position'] != $position) {
- $result = $this->savePositionChange($project_id, $task_id, $position, $column_id, $swimlane_id);
+ $result = $this->moveTaskWithinSameColumn($task, $position);
}
- if ($result && $fire_events) {
+ if (! $result) {
+ $this->db->cancelTransaction();
+ return false;
+ }
+
+ $this->db->closeTransaction();
+
+ if ($fire_events) {
$this->fireEvents($task, $column_id, $position, $swimlane_id);
}
@@ -55,145 +61,87 @@ class TaskPosition extends Base
}
/**
- * Move a task to another swimlane
+ * Move a task to another column/swimlane
*
* @access private
- * @param integer $project_id
- * @param integer $task_id
- * @param integer $position
- * @param integer $original_column_id
- * @param integer $new_column_id
- * @param integer $original_swimlane_id
- * @param integer $new_swimlane_id
+ * @param array $task
+ * @param integer $swimlane_id
+ * @param integer $column_id
+ * @param integer $position
* @return boolean
*/
- private function saveSwimlaneChange($project_id, $task_id, $position, $original_column_id, $new_column_id, $original_swimlane_id, $new_swimlane_id)
+ private function moveTaskToAnotherColumn(array $task, $swimlane_id, $column_id, $position)
{
- $this->db->startTransaction();
- $r1 = $this->saveTaskPositions($project_id, $task_id, 0, $original_column_id, $original_swimlane_id);
- $r2 = $this->saveTaskPositions($project_id, $task_id, $position, $new_column_id, $new_swimlane_id);
- $this->db->closeTransaction();
+ $results = array();
+ $max = $this->getQuery($task['project_id'], $swimlane_id, $column_id)->count();
+ $position = $max > 0 && $position > $max ? $max + 1 : $position;
- return $r1 && $r2;
- }
+ $results[] = $this->getQuery($task['project_id'], $task['swimlane_id'], $task['column_id'])->gt('position', $task['position'])->decrement('position', 1);
+ $results[] = $this->getQuery($task['project_id'], $swimlane_id, $column_id)->gte('position', $position)->increment('position', 1);
+ $results[] = $this->updateTaskPosition($task['id'], $swimlane_id, $column_id, $position);
- /**
- * Move a task to another column
- *
- * @access private
- * @param integer $project_id
- * @param integer $task_id
- * @param integer $position
- * @param integer $swimlane_id
- * @param integer $original_column_id
- * @param integer $new_column_id
- * @return boolean
- */
- private function saveColumnChange($project_id, $task_id, $position, $swimlane_id, $original_column_id, $new_column_id)
- {
- $this->db->startTransaction();
- $r1 = $this->saveTaskPositions($project_id, $task_id, 0, $original_column_id, $swimlane_id);
- $r2 = $this->saveTaskPositions($project_id, $task_id, $position, $new_column_id, $swimlane_id);
- $this->db->closeTransaction();
-
- return $r1 && $r2;
+ return !in_array(false, $results, true);
}
/**
- * Move a task to another position in the same column
+ * Move a task within the same column
*
* @access private
- * @param integer $project_id
- * @param integer $task_id
- * @param integer $position
- * @param integer $column_id
- * @param integer $swimlane_id
+ * @param array $task
+ * @param integer $position
* @return boolean
*/
- private function savePositionChange($project_id, $task_id, $position, $column_id, $swimlane_id)
+ private function moveTaskWithinSameColumn(array $task, $position)
{
- $this->db->startTransaction();
- $result = $this->saveTaskPositions($project_id, $task_id, $position, $column_id, $swimlane_id);
- $this->db->closeTransaction();
+ $results = array();
+ $max = $this->getQuery($task['project_id'], $task['swimlane_id'], $task['column_id'])->count();
+ $position = $max > 0 && $position > $max ? $max : $position;
+
+ if ($position >= $max) {
+ $results[] = $this->getQuery($task['project_id'], $task['swimlane_id'], $task['column_id'])->lte('position', $position)->decrement('position', 1);
+ } else {
+ $results[] = $this->getQuery($task['project_id'], $task['swimlane_id'], $task['column_id'])->gte('position', $position)->increment('position', 1);
+ }
- return $result;
+ $results[] = $this->updateTaskPosition($task['id'], $task['swimlane_id'], $task['column_id'], $position);
+
+ return !in_array(false, $results, true);
}
/**
- * Save all task positions for one column
+ * Update final task position
*
* @access private
- * @param integer $project_id
- * @param integer $task_id
- * @param integer $position
- * @param integer $column_id
- * @param integer $swimlane_id
+ * @param integer $task_id
+ * @param integer $swimlane_id
+ * @param integer $column_id
+ * @param integer $position
* @return boolean
*/
- private function saveTaskPositions($project_id, $task_id, $position, $column_id, $swimlane_id)
+ private function updateTaskPosition($task_id, $swimlane_id, $column_id, $position)
{
- $tasks_ids = $this->db->table(Task::TABLE)
+ return $this->db->table(Task::TABLE)
+ ->eq('id', $task_id)
->eq('is_active', 1)
- ->eq('swimlane_id', $swimlane_id)
- ->eq('project_id', $project_id)
- ->eq('column_id', $column_id)
- ->neq('id', $task_id)
- ->asc('position')
- ->asc('id')
- ->findAllByColumn('id');
-
- $offset = 1;
-
- foreach ($tasks_ids as $current_task_id) {
-
- // Insert the new task
- if ($position == $offset) {
- if (! $this->saveTaskPosition($task_id, $offset, $column_id, $swimlane_id)) {
- return false;
- }
- $offset++;
- }
-
- // Rewrite other tasks position
- if (! $this->saveTaskPosition($current_task_id, $offset, $column_id, $swimlane_id)) {
- return false;
- }
-
- $offset++;
- }
-
- // Insert the new task at the bottom and normalize bad position
- if ($position >= $offset && ! $this->saveTaskPosition($task_id, $offset, $column_id, $swimlane_id)) {
- return false;
- }
-
- return true;
+ ->update(array('position' => $position, 'column_id' => $column_id, 'swimlane_id' => $swimlane_id));
}
/**
- * Save new task position
+ * Get common query
*
* @access private
- * @param integer $task_id
- * @param integer $position
- * @param integer $column_id
- * @param integer $swimlane_id
- * @return boolean
+ * @param integer $project_id
+ * @param integer $swimlane_id
+ * @param integer $column_id
+ * @return \PicoDb\Table
*/
- private function saveTaskPosition($task_id, $position, $column_id, $swimlane_id)
+ private function getQuery($project_id, $swimlane_id, $column_id)
{
- $result = $this->db->table(Task::TABLE)->eq('id', $task_id)->update(array(
- 'position' => $position,
- 'column_id' => $column_id,
- 'swimlane_id' => $swimlane_id,
- ));
-
- if (! $result) {
- $this->db->cancelTransaction();
- return false;
- }
-
- return true;
+ return $this->db->table(Task::TABLE)
+ ->eq('project_id', $project_id)
+ ->eq('swimlane_id', $swimlane_id)
+ ->eq('column_id', $column_id)
+ ->eq('is_active', 1);
}
/**