Create new class SubtaskPositionModel

This commit is contained in:
Frederic Guillot
2016-07-23 21:14:33 -04:00
parent 220bc9cdcc
commit 2a7ca0405c
7 changed files with 127 additions and 99 deletions

View File

@@ -272,39 +272,6 @@ class SubtaskModel extends Base
return $this->db->table(self::TABLE)->eq('task_id', $task_id)->update(array('status' => self::STATUS_DONE));
}
/**
* Save 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(self::TABLE)->eq('task_id', $task_id)->count()) {
return false;
}
$subtask_ids = $this->db->table(self::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(self::TABLE)->eq('id', $current_subtask_id)->update(array('position' => $offset));
$offset++;
}
$results[] = $this->db->table(self::TABLE)->eq('id', $subtask_id)->update(array('position' => $position));
return !in_array(false, $results, true);
}
/**
* Change the status of subtask
*