summaryrefslogtreecommitdiff
path: root/app/Model/TaskPosition.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Model/TaskPosition.php')
-rw-r--r--app/Model/TaskPosition.php67
1 files changed, 52 insertions, 15 deletions
diff --git a/app/Model/TaskPosition.php b/app/Model/TaskPosition.php
index c23bc3b5..9a9642e5 100644
--- a/app/Model/TaskPosition.php
+++ b/app/Model/TaskPosition.php
@@ -18,20 +18,25 @@ class TaskPosition extends Base
* @param integer $task_id Task id
* @param integer $column_id Column id
* @param integer $position Position (must be >= 1)
+ * @param integer $swimlane_id Swimlane id
* @return boolean
*/
- public function movePosition($project_id, $task_id, $column_id, $position)
+ public function movePosition($project_id, $task_id, $column_id, $position, $swimlane_id = 0)
{
$original_task = $this->taskFinder->getById($task_id);
- $positions = $this->calculatePositions($project_id, $task_id, $column_id, $position);
- if ($positions === false || ! $this->savePositions($positions)) {
- return false;
- }
+ $result = $this->calculateAndSave($project_id, $task_id, $column_id, $position, $swimlane_id);
- $this->fireEvents($original_task, $column_id, $position);
+ if ($result) {
- return true;
+ if ($original_task['swimlane_id'] != $swimlane_id) {
+ $this->calculateAndSave($project_id, 0, $column_id, 1, $original_task['swimlane_id']);
+ }
+
+ $this->fireEvents($original_task, $column_id, $position, $swimlane_id);
+ }
+
+ return $result;
}
/**
@@ -42,9 +47,10 @@ class TaskPosition extends Base
* @param integer $task_id Task id
* @param integer $column_id Column id
* @param integer $position Position (must be >= 1)
+ * @param integer $swimlane_id Swimlane id
* @return array|boolean
*/
- public function calculatePositions($project_id, $task_id, $column_id, $position)
+ public function calculatePositions($project_id, $task_id, $column_id, $position, $swimlane_id = 0)
{
// The position can't be lower than 1
if ($position < 1) {
@@ -59,6 +65,7 @@ class TaskPosition extends Base
$columns[$board_column_id] = $this->db->table(Task::TABLE)
->eq('is_active', 1)
+ ->eq('swimlane_id', $swimlane_id)
->eq('project_id', $project_id)
->eq('column_id', $board_column_id)
->neq('id', $task_id)
@@ -72,7 +79,9 @@ class TaskPosition extends Base
}
// We put our task to the new position
- array_splice($columns[$column_id], $position - 1, 0, $task_id);
+ if ($task_id) {
+ array_splice($columns[$column_id], $position - 1, 0, $task_id);
+ }
return $columns;
}
@@ -84,9 +93,9 @@ class TaskPosition extends Base
* @param array $columns Sorted tasks
* @return boolean
*/
- private function savePositions(array $columns)
+ private function savePositions(array $columns, $swimlane_id)
{
- return $this->db->transaction(function ($db) use ($columns) {
+ return $this->db->transaction(function ($db) use ($columns, $swimlane_id) {
foreach ($columns as $column_id => $column) {
@@ -96,7 +105,8 @@ class TaskPosition extends Base
$result = $db->table(Task::TABLE)->eq('id', $task_id)->update(array(
'position' => $position,
- 'column_id' => $column_id
+ 'column_id' => $column_id,
+ 'swimlane_id' => $swimlane_id,
));
if (! $result) {
@@ -112,25 +122,52 @@ class TaskPosition extends Base
/**
* Fire events
*
- * @access public
+ * @access private
* @param array $task
* @param integer $new_column_id
* @param integer $new_position
+ * @param integer $new_swimlane_id
*/
- public function fireEvents(array $task, $new_column_id, $new_position)
+ private function fireEvents(array $task, $new_column_id, $new_position, $new_swimlane_id)
{
$event_data = array(
'task_id' => $task['id'],
'project_id' => $task['project_id'],
'position' => $new_position,
'column_id' => $new_column_id,
+ 'swimlane_id' => $new_swimlane_id,
);
- if ($task['column_id'] != $new_column_id) {
+ if ($task['swimlane_id'] != $new_swimlane_id) {
+ $this->event->trigger(Task::EVENT_MOVE_SWIMLANE, $event_data);
+ }
+ else if ($task['column_id'] != $new_column_id) {
$this->event->trigger(Task::EVENT_MOVE_COLUMN, $event_data);
}
else if ($task['position'] != $new_position) {
$this->event->trigger(Task::EVENT_MOVE_POSITION, $event_data);
}
}
+
+ /**
+ * Calculate the new position of all tasks
+ *
+ * @access private
+ * @param integer $project_id Project id
+ * @param integer $task_id Task id
+ * @param integer $column_id Column id
+ * @param integer $position Position (must be >= 1)
+ * @param integer $swimlane_id Swimlane id
+ * @return boolean
+ */
+ private function calculateAndSave($project_id, $task_id, $column_id, $position, $swimlane_id)
+ {
+ $positions = $this->calculatePositions($project_id, $task_id, $column_id, $position, $swimlane_id);
+
+ if ($positions === false || ! $this->savePositions($positions, $swimlane_id)) {
+ return false;
+ }
+
+ return true;
+ }
}