summaryrefslogtreecommitdiff
path: root/app/Model/SubtaskPositionModel.php
diff options
context:
space:
mode:
authorJLGC @monolinux <monolinux@junglacode.org>2016-08-15 23:13:16 -0500
committerJLGC @monolinux <monolinux@junglacode.org>2016-08-15 23:13:16 -0500
commit683c0464093f6a7976236c68653c2a2cc5dae280 (patch)
treebf176ecd82415cc4952eea071b7d264dd5fd68b4 /app/Model/SubtaskPositionModel.php
parentb1e795fc5b45369f7b9b565b1e106d2673361977 (diff)
parent5f82a942c0011bf91947b2c1d627c0907bda0c92 (diff)
Merge https://github.com/kanboard/kanboard
Diffstat (limited to 'app/Model/SubtaskPositionModel.php')
-rw-r--r--app/Model/SubtaskPositionModel.php47
1 files changed, 47 insertions, 0 deletions
diff --git a/app/Model/SubtaskPositionModel.php b/app/Model/SubtaskPositionModel.php
new file mode 100644
index 00000000..3c26465d
--- /dev/null
+++ b/app/Model/SubtaskPositionModel.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Kanboard\Model;
+
+use Kanboard\Core\Base;
+
+/**
+ * Class SubtaskPositionModel
+ *
+ * @package Kanboard\Model
+ * @author Frederic Guillot
+ */
+class SubtaskPositionModel extends Base
+{
+ /**
+ * Change subtask position
+ *
+ * @access public
+ * @param integer $task_id
+ * @param integer $subtask_id
+ * @param integer $position
+ * @return boolean
+ */
+ public function changePosition($task_id, $subtask_id, $position)
+ {
+ if ($position < 1 || $position > $this->db->table(SubtaskModel::TABLE)->eq('task_id', $task_id)->count()) {
+ return false;
+ }
+
+ $subtask_ids = $this->db->table(SubtaskModel::TABLE)->eq('task_id', $task_id)->neq('id', $subtask_id)->asc('position')->findAllByColumn('id');
+ $offset = 1;
+ $results = array();
+
+ foreach ($subtask_ids as $current_subtask_id) {
+ if ($offset == $position) {
+ $offset++;
+ }
+
+ $results[] = $this->db->table(SubtaskModel::TABLE)->eq('id', $current_subtask_id)->update(array('position' => $offset));
+ $offset++;
+ }
+
+ $results[] = $this->db->table(SubtaskModel::TABLE)->eq('id', $subtask_id)->update(array('position' => $position));
+
+ return !in_array(false, $results, true);
+ }
+}