summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-04-18 15:10:40 -0400
committerFrederic Guillot <fred@kanboard.net>2015-04-18 15:10:40 -0400
commit54449d48c45b87699a796ca71b42c42a95dd9b8d (patch)
tree54203609ffa90ff9a942e50478bceeba42df7acc /app
parent7b7ea56460f755e77194b969c40a38c557ed08f6 (diff)
Fix bug when moving subtasks with non consecutive positions
Diffstat (limited to 'app')
-rw-r--r--app/Model/Subtask.php58
-rw-r--r--app/Template/subtask/show.php8
2 files changed, 50 insertions, 16 deletions
diff --git a/app/Model/Subtask.php b/app/Model/Subtask.php
index 492f3a77..f7929af3 100644
--- a/app/Model/Subtask.php
+++ b/app/Model/Subtask.php
@@ -228,6 +228,46 @@ class Subtask extends Base
}
/**
+ * Get subtasks with consecutive positions
+ *
+ * If you remove a subtask, the positions are not anymore consecutives
+ *
+ * @access public
+ * @param integer $task_id
+ * @return array
+ */
+ public function getNormalizedPositions($task_id)
+ {
+ $subtasks = $this->db->hashtable(self::TABLE)->eq('task_id', $task_id)->asc('position')->getAll('id', 'position');
+ $position = 1;
+
+ foreach ($subtasks as $subtask_id => $subtask_position) {
+ $subtasks[$subtask_id] = $position++;
+ }
+
+ return $subtasks;
+ }
+
+ /**
+ * Save the new positions for a set of subtasks
+ *
+ * @access public
+ * @param array $subtasks Hashmap of column_id/column_position
+ * @return boolean
+ */
+ public function savePositions(array $subtasks)
+ {
+ return $this->db->transaction(function ($db) use ($subtasks) {
+
+ foreach ($subtasks as $subtask_id => $position) {
+ if (! $db->table(self::TABLE)->eq('id', $subtask_id)->update(array('position' => $position))) {
+ return false;
+ }
+ }
+ });
+ }
+
+ /**
* Move a subtask down, increment the position value
*
* @access public
@@ -237,7 +277,7 @@ class Subtask extends Base
*/
public function moveDown($task_id, $subtask_id)
{
- $subtasks = $this->db->hashtable(self::TABLE)->eq('task_id', $task_id)->asc('position')->getAll('id', 'position');
+ $subtasks = $this->getNormalizedPositions($task_id);
$positions = array_flip($subtasks);
if (isset($subtasks[$subtask_id]) && $subtasks[$subtask_id] < count($subtasks)) {
@@ -245,12 +285,7 @@ class Subtask extends Base
$position = ++$subtasks[$subtask_id];
$subtasks[$positions[$position]]--;
- $this->db->startTransaction();
- $this->db->table(self::TABLE)->eq('id', $subtask_id)->update(array('position' => $position));
- $this->db->table(self::TABLE)->eq('id', $positions[$position])->update(array('position' => $subtasks[$positions[$position]]));
- $this->db->closeTransaction();
-
- return true;
+ return $this->savePositions($subtasks);
}
return false;
@@ -266,7 +301,7 @@ class Subtask extends Base
*/
public function moveUp($task_id, $subtask_id)
{
- $subtasks = $this->db->hashtable(self::TABLE)->eq('task_id', $task_id)->asc('position')->getAll('id', 'position');
+ $subtasks = $this->getNormalizedPositions($task_id);
$positions = array_flip($subtasks);
if (isset($subtasks[$subtask_id]) && $subtasks[$subtask_id] > 1) {
@@ -274,12 +309,7 @@ class Subtask extends Base
$position = --$subtasks[$subtask_id];
$subtasks[$positions[$position]]++;
- $this->db->startTransaction();
- $this->db->table(self::TABLE)->eq('id', $subtask_id)->update(array('position' => $position));
- $this->db->table(self::TABLE)->eq('id', $positions[$position])->update(array('position' => $subtasks[$positions[$position]]));
- $this->db->closeTransaction();
-
- return true;
+ return $this->savePositions($subtasks);
}
return false;
diff --git a/app/Template/subtask/show.php b/app/Template/subtask/show.php
index c7ac652a..e5368265 100644
--- a/app/Template/subtask/show.php
+++ b/app/Template/subtask/show.php
@@ -1,4 +1,8 @@
<?php if (! empty($subtasks)): ?>
+
+<?php $first_position = $subtasks[0]['position']; ?>
+<?php $last_position = $subtasks[count($subtasks) - 1]['position']; ?>
+
<div id="subtasks" class="task-show-section">
<div class="page-header">
@@ -40,12 +44,12 @@
<?php if (! isset($not_editable)): ?>
<td>
<ul>
- <?php if ($subtask['position'] > 1): ?>
+ <?php if ($subtask['position'] != $first_position): ?>
<li>
<?= $this->a(t('Move Up'), 'subtask', 'movePosition', array('project_id' => $project['id'], 'task_id' => $subtask['task_id'], 'subtask_id' => $subtask['id'], 'direction' => 'up'), true) ?>
</li>
<?php endif ?>
- <?php if ($subtask['position'] != 0 && $subtask['position'] != count($subtasks)): ?>
+ <?php if ($subtask['position'] != $last_position): ?>
<li>
<?= $this->a(t('Move Down'), 'subtask', 'movePosition', array('project_id' => $project['id'], 'task_id' => $subtask['task_id'], 'subtask_id' => $subtask['id'], 'direction' => 'down'), true) ?>
</li>