summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorFrédéric Guillot <fred@kanboard.net>2014-09-04 20:14:26 -0700
committerFrédéric Guillot <fred@kanboard.net>2014-09-04 20:14:26 -0700
commit954bed954f6c81cbcdb217966dcc9e008e7dd149 (patch)
tree3a3b54ed309a151476f2a0e77bbf35baaa078765 /app
parent749136361e6eedbc868778db17bdc67aa0f3b677 (diff)
Task move position refactoring
Diffstat (limited to 'app')
-rw-r--r--app/Controller/Board.php34
-rw-r--r--app/Model/Board.php26
-rw-r--r--app/Model/Task.php112
3 files changed, 109 insertions, 63 deletions
diff --git a/app/Controller/Board.php b/app/Controller/Board.php
index e2c10f58..4724cae5 100644
--- a/app/Controller/Board.php
+++ b/app/Controller/Board.php
@@ -396,27 +396,31 @@ class Board extends Base
*/
public function save()
{
- if ($this->request->isAjax()) {
+ $project_id = $this->request->getIntegerParam('project_id');
- $project_id = $this->request->getIntegerParam('project_id');
- $values = $this->request->getValues();
+ if ($project_id > 0 && $this->request->isAjax()) {
- if ($project_id > 0 && ! $this->project->isUserAllowed($project_id, $this->acl->getUserId())) {
- $this->response->text('Not Authorized', 401);
+ if (! $this->project->isUserAllowed($project_id, $this->acl->getUserId())) {
+ $this->response->status(401);
}
- if (isset($values['positions'])) {
- $this->board->saveTasksPosition($values['positions'], $values['selected_task_id']);
+ $values = $this->request->getValues();
+
+ if ($this->task->movePosition($project_id, $values['task_id'], $values['column_id'], $values['position'])) {
+
+ $this->response->html(
+ $this->template->load('board_show', array(
+ 'current_project_id' => $project_id,
+ 'board' => $this->board->get($project_id),
+ 'categories' => $this->category->getList($project_id, false),
+ )),
+ 201
+ );
}
+ else {
- $this->response->html(
- $this->template->load('board_show', array(
- 'current_project_id' => $project_id,
- 'board' => $this->board->get($project_id),
- 'categories' => $this->category->getList($project_id, false),
- )),
- 201
- );
+ $this->response->status(400);
+ }
}
else {
$this->response->status(401);
diff --git a/app/Model/Board.php b/app/Model/Board.php
index 8fb30e70..07020600 100644
--- a/app/Model/Board.php
+++ b/app/Model/Board.php
@@ -21,32 +21,6 @@ class Board extends Base
const TABLE = 'columns';
/**
- * Save task positions for each column
- *
- * @access public
- * @param array $positions [['task_id' => X, 'column_id' => X, 'position' => X], ...]
- * @param integer $selected_task_id The selected task id
- * @return boolean
- */
- public function saveTasksPosition(array $positions, $selected_task_id)
- {
- $this->db->startTransaction();
-
- foreach ($positions as $value) {
-
- // We trigger events only for the selected task
- if (! $this->task->movePosition($value['task_id'], $value['column_id'], $value['position'], $value['task_id'] == $selected_task_id)) {
- $this->db->cancelTransaction();
- return false;
- }
- }
-
- $this->db->closeTransaction();
-
- return true;
- }
-
- /**
* Create a board with default columns, must be executed inside a transaction
*
* @access public
diff --git a/app/Model/Task.php b/app/Model/Task.php
index df6e4426..07a2a2fd 100644
--- a/app/Model/Task.php
+++ b/app/Model/Task.php
@@ -421,10 +421,9 @@ class Task extends Base
*
* @access public
* @param array $values Form values
- * @param boolean $trigger_events Flag to trigger events
* @return boolean
*/
- public function update(array $values, $trigger_events = true)
+ public function update(array $values)
{
// Fetch original task
$original_task = $this->getById($values['id']);
@@ -436,21 +435,14 @@ class Task extends Base
// Prepare data
$this->prepare($values);
$updated_task = $values;
+ $updated_task['date_modification'] = time();
unset($updated_task['id']);
- // We update the modification date only for the selected task to highlight recent moves
- if ($trigger_events) {
- $updated_task['date_modification'] = time();
- }
-
- $result = $this->db->table(self::TABLE)->eq('id', $values['id'])->update($updated_task);
-
- // Trigger events
- if ($result && $trigger_events) {
+ if ($this->db->table(self::TABLE)->eq('id', $values['id'])->update($updated_task)) {
$this->triggerUpdateEvents($original_task, $updated_task);
}
- return $result;
+ return true;
}
/**
@@ -548,23 +540,99 @@ class Task extends Base
* Move a task to another column or to another position
*
* @access public
+ * @param integer $project_id Project id
* @param integer $task_id Task id
* @param integer $column_id Column id
- * @param integer $position Position (must be greater than 1)
- * @param boolean $trigger_events Flag to trigger events
+ * @param integer $position Position (must be >= 1)
* @return boolean
*/
- public function movePosition($task_id, $column_id, $position, $trigger_events = true)
+ public function movePosition($project_id, $task_id, $column_id, $position)
{
- $this->event->clearTriggeredEvents();
+ // The position can't be lower than 1
+ if ($position < 1) {
+ return false;
+ }
- $values = array(
- 'id' => $task_id,
- 'column_id' => $column_id,
- 'position' => $position,
- );
+ // We fetch all tasks on the board
+ $tasks = $this->db->table(self::TABLE)
+ ->columns('id', 'column_id', 'position')
+ ->eq('is_active', 1)
+ ->eq('project_id', $project_id)
+ ->findAll();
+
+ // We sort everything by column and position
+ $board = $this->board->getColumnsList($project_id);
+ $columns = array();
+
+ foreach ($board as $board_column_id => $board_column_name) {
+ $columns[$board_column_id] = array();
+ }
+
+ foreach ($tasks as &$task) {
+ if ($task['id'] != $task_id) {
+ $columns[$task['column_id']][$task['position'] - 1] = (int) $task['id'];
+ }
+ }
+
+ // The column must exists
+ if (! isset($columns[$column_id])) {
+ return false;
+ }
+
+ // We put our task to the new position
+ array_splice($columns[$column_id], $position - 1, 0, $task_id); // print_r($columns);
+
+ // We save the new positions for all tasks
+ return $this->savePositions($task_id, $columns);
+ }
+
+ /**
+ * Save task positions
+ *
+ * @access private
+ * @param integer $moved_task_id Id of the moved task
+ * @param array $columns Sorted tasks
+ * @return boolean
+ */
+ private function savePositions($moved_task_id, array $columns)
+ {
+ $this->db->startTransaction();
+
+ foreach ($columns as $column_id => $column) {
+
+ $position = 1;
+ ksort($column);
+
+ foreach ($column as $task_id) {
+
+ if ($task_id == $moved_task_id) {
+
+ // Events will be triggered only for that task
+ $result = $this->update(array(
+ 'id' => $task_id,
+ 'position' => $position,
+ 'column_id' => $column_id
+ ));
+ }
+ else {
+ $result = $this->db->table(self::TABLE)->eq('id', $task_id)->update(array(
+ 'position' => $position,
+ 'column_id' => $column_id
+ ));
+ }
+
+ $position++;
+
+ if (! $result) {
+ $this->db->cancelTransaction();
+ return false;
+ }
+ }
+ }
+
+ $this->db->closeTransaction();
- return $this->update($values, $trigger_events);
+ return true;
}
/**