diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-04-18 14:28:55 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-04-18 14:28:55 -0400 |
commit | 7b7ea56460f755e77194b969c40a38c557ed08f6 (patch) | |
tree | fc309f628da34bf9ed817c01b24a1f2949506de5 /app | |
parent | 94865a7c0343f352e862f7aea232545827922942 (diff) |
Fix issue move column up/down with non consecutive positions
Diffstat (limited to 'app')
-rw-r--r-- | app/Model/Board.php | 58 |
1 files changed, 44 insertions, 14 deletions
diff --git a/app/Model/Board.php b/app/Model/Board.php index 711f3dd9..712a3534 100644 --- a/app/Model/Board.php +++ b/app/Model/Board.php @@ -146,6 +146,46 @@ class Board extends Base } /** + * Get columns with consecutive positions + * + * If you remove a column, the positions are not anymore consecutives + * + * @access public + * @param integer $project_id + * @return array + */ + public function getNormalizedColumnPositions($project_id) + { + $columns = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'position'); + $position = 1; + + foreach ($columns as $column_id => $column_position) { + $columns[$column_id] = $position++; + } + + return $columns; + } + + /** + * Save the new positions for a set of columns + * + * @access public + * @param array $columns Hashmap of column_id/column_position + * @return boolean + */ + public function saveColumnPositions(array $columns) + { + return $this->db->transaction(function ($db) use ($columns) { + + foreach ($columns as $column_id => $position) { + if (! $db->table(self::TABLE)->eq('id', $column_id)->update(array('position' => $position))) { + return false; + } + } + }); + } + + /** * Move a column down, increment the column position value * * @access public @@ -155,7 +195,7 @@ class Board extends Base */ public function moveDown($project_id, $column_id) { - $columns = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'position'); + $columns = $this->getNormalizedColumnPositions($project_id); $positions = array_flip($columns); if (isset($columns[$column_id]) && $columns[$column_id] < count($columns)) { @@ -163,12 +203,7 @@ class Board extends Base $position = ++$columns[$column_id]; $columns[$positions[$position]]--; - $this->db->startTransaction(); - $this->db->table(self::TABLE)->eq('id', $column_id)->update(array('position' => $position)); - $this->db->table(self::TABLE)->eq('id', $positions[$position])->update(array('position' => $columns[$positions[$position]])); - $this->db->closeTransaction(); - - return true; + return $this->saveColumnPositions($columns); } return false; @@ -184,7 +219,7 @@ class Board extends Base */ public function moveUp($project_id, $column_id) { - $columns = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'position'); + $columns = $this->getNormalizedColumnPositions($project_id); $positions = array_flip($columns); if (isset($columns[$column_id]) && $columns[$column_id] > 1) { @@ -192,12 +227,7 @@ class Board extends Base $position = --$columns[$column_id]; $columns[$positions[$position]]++; - $this->db->startTransaction(); - $this->db->table(self::TABLE)->eq('id', $column_id)->update(array('position' => $position)); - $this->db->table(self::TABLE)->eq('id', $positions[$position])->update(array('position' => $columns[$positions[$position]])); - $this->db->closeTransaction(); - - return true; + return $this->saveColumnPositions($columns); } return false; |