summaryrefslogtreecommitdiff
path: root/app/Model/TaskReorderModel.php
blob: c7fc6f6d1d87d3f87a162d5087fa3479811c10be (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

namespace Kanboard\Model;

use Kanboard\Core\Base;

class TaskReorderModel extends Base
{
    public function reorderByPriority($projectID, $swimlaneID, $columnID, $direction)
    {
        $this->db->startTransaction();

        $taskIDs = $this->db->table(TaskModel::TABLE)
            ->eq('project_id', $projectID)
            ->eq('swimlane_id', $swimlaneID)
            ->eq('column_id', $columnID)
            ->orderBy('priority', $direction)
            ->asc('id')
            ->findAllByColumn('id');

        $this->reorderTasks($taskIDs);

        $this->db->closeTransaction();
    }

    public function reorderByAssigneeAndPriority($projectID, $swimlaneID, $columnID, $direction)
    {
        $this->db->startTransaction();

        $taskIDs = $this->db->table(TaskModel::TABLE)
            ->eq('tasks.project_id', $projectID)
            ->eq('tasks.swimlane_id', $swimlaneID)
            ->eq('tasks.column_id', $columnID)
            ->asc('u.name')
            ->asc('u.username')
            ->orderBy('tasks.priority', $direction)
            ->left(UserModel::TABLE, 'u', 'id', TaskModel::TABLE, 'owner_id')
            ->findAllByColumn('tasks.id');

        $this->reorderTasks($taskIDs);

        $this->db->closeTransaction();
    }

    public function reorderByAssignee($projectID, $swimlaneID, $columnID, $direction)
    {
        $this->db->startTransaction();

        $taskIDs = $this->db->table(TaskModel::TABLE)
            ->eq('tasks.project_id', $projectID)
            ->eq('tasks.swimlane_id', $swimlaneID)
            ->eq('tasks.column_id', $columnID)
            ->orderBy('u.name', $direction)
            ->orderBy('u.username', $direction)
            ->orderBy('u.id', $direction)
            ->left(UserModel::TABLE, 'u', 'id', TaskModel::TABLE, 'owner_id')
            ->findAllByColumn('tasks.id');

        $this->reorderTasks($taskIDs);

        $this->db->closeTransaction();
    }

    public function reorderByDueDate($projectID, $swimlaneID, $columnID, $direction)
    {
        $this->db->startTransaction();

        $taskIDs = $this->db->table(TaskModel::TABLE)
            ->eq('project_id', $projectID)
            ->eq('swimlane_id', $swimlaneID)
            ->eq('column_id', $columnID)
            ->orderBy('date_due', $direction)
            ->asc('id')
            ->findAllByColumn('id');

        $this->reorderTasks($taskIDs);

        $this->db->closeTransaction();
    }

    protected function reorderTasks(array $taskIDs)
    {
        $i = 1;
        foreach ($taskIDs as $taskID) {
            $this->db->table(TaskModel::TABLE)
                ->eq('id', $taskID)
                ->update(['position' => $i]);
            $i++;
        }
    }
}