summaryrefslogtreecommitdiff
path: root/app/Model/SubtaskPositionModel.php
blob: 3c26465de3c42dfe2af685c71723d3b10646fbdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
    }
}