summaryrefslogtreecommitdiff
path: root/app/Model/TaskPosition.php
blob: 091d908d437b21f7b563e2eb40f385d960199166 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php

namespace Kanboard\Model;

use Kanboard\Event\TaskEvent;

/**
 * Task Position
 *
 * @package  model
 * @author   Frederic Guillot
 */
class TaskPosition extends Base
{
    /**
     * Move a task to another column or to another position
     *
     * @access public
     * @param  integer    $project_id        Project id
     * @param  integer    $task_id           Task id
     * @param  integer    $column_id         Column id
     * @param  integer    $position          Position (must be >= 1)
     * @param  integer    $swimlane_id       Swimlane id
     * @param  boolean    $fire_events       Fire events
     * @return boolean
     */
    public function movePosition($project_id, $task_id, $column_id, $position, $swimlane_id = 0, $fire_events = true)
    {
        $result = false;

        if ($position < 1) {
            return $result;
        }

        $task = $this->taskFinder->getById($task_id);

        if ($task['is_active'] == Task::STATUS_CLOSED) {
            return true;
        }

        $this->db->startTransaction();

        if ($task['swimlane_id'] != $swimlane_id || $task['column_id'] != $column_id) {
            $result = $this->moveTaskToAnotherColumn($task, $swimlane_id, $column_id, $position);
        } elseif ($task['position'] != $position) {
            $result = $this->moveTaskWithinSameColumn($task, $position);
        }

        if (! $result) {
            $this->db->cancelTransaction();
            return false;
        }

        $this->db->closeTransaction();

        if ($fire_events) {
            $this->fireEvents($task, $column_id, $position, $swimlane_id);
        }

        return $result;
    }

    /**
     * Move a task to another column/swimlane
     *
     * @access private
     * @param  array   $task
     * @param  integer $swimlane_id
     * @param  integer $column_id
     * @param  integer $position
     * @return boolean
     */
    private function moveTaskToAnotherColumn(array $task, $swimlane_id, $column_id, $position)
    {
        $results = array();
        $max = $this->getQuery($task['project_id'], $swimlane_id, $column_id)->count();
        $position = $max > 0 && $position > $max ? $max + 1 : $position;

        $results[] = $this->getQuery($task['project_id'], $task['swimlane_id'], $task['column_id'])->gt('position', $task['position'])->decrement('position', 1);
        $results[] = $this->getQuery($task['project_id'], $swimlane_id, $column_id)->gte('position', $position)->increment('position', 1);
        $results[] = $this->updateTaskPosition($task['id'], $swimlane_id, $column_id, $position);

        return !in_array(false, $results, true);
    }

    /**
     * Move a task within the same column
     *
     * @access private
     * @param  array   $task
     * @param  integer $position
     * @return boolean
     */
    private function moveTaskWithinSameColumn(array $task, $position)
    {
        $results = array();
        $max = $this->getQuery($task['project_id'], $task['swimlane_id'], $task['column_id'])->count();
        $position = $max > 0 && $position > $max ? $max : $position;

        if ($position >= $max) {
            $results[] = $this->getQuery($task['project_id'], $task['swimlane_id'], $task['column_id'])->lte('position', $position)->decrement('position', 1);
        } else {
            $results[] = $this->getQuery($task['project_id'], $task['swimlane_id'], $task['column_id'])->gte('position', $position)->increment('position', 1);
        }

        $results[] = $this->updateTaskPosition($task['id'], $task['swimlane_id'], $task['column_id'], $position);

        return !in_array(false, $results, true);
    }

    /**
     * Update final task position
     *
     * @access private
     * @param  integer $task_id
     * @param  integer $swimlane_id
     * @param  integer $column_id
     * @param  integer $position
     * @return boolean
     */
    private function updateTaskPosition($task_id, $swimlane_id, $column_id, $position)
    {
        $now = time();

        return $this->db->table(Task::TABLE)
            ->eq('id', $task_id)
            ->eq('is_active', 1)
            ->update(array(
                'position' => $position,
                'column_id' => $column_id,
                'swimlane_id' => $swimlane_id,
                'date_modification' => $now,
                'date_moved' => $now,
            ));
    }

    /**
     * Get common query
     *
     * @access private
     * @param  integer $project_id
     * @param  integer $swimlane_id
     * @param  integer $column_id
     * @return \PicoDb\Table
     */
    private function getQuery($project_id, $swimlane_id, $column_id)
    {
        return $this->db->table(Task::TABLE)
            ->eq('project_id', $project_id)
            ->eq('swimlane_id', $swimlane_id)
            ->eq('column_id', $column_id)
            ->eq('is_active', 1);
    }

    /**
     * Fire events
     *
     * @access private
     * @param  array     $task
     * @param  integer   $new_column_id
     * @param  integer   $new_position
     * @param  integer   $new_swimlane_id
     */
    private function fireEvents(array $task, $new_column_id, $new_position, $new_swimlane_id)
    {
        $event_data = array(
            'task_id' => $task['id'],
            'project_id' => $task['project_id'],
            'position' => $new_position,
            'column_id' => $new_column_id,
            'swimlane_id' => $new_swimlane_id,
            'src_column_id' => $task['column_id'],
            'dst_column_id' => $new_column_id,
            'date_moved' => $task['date_moved'],
            'recurrence_status' => $task['recurrence_status'],
            'recurrence_trigger' => $task['recurrence_trigger'],
        );

        if ($task['swimlane_id'] != $new_swimlane_id) {
            $this->logger->debug('Event fired: '.Task::EVENT_MOVE_SWIMLANE);
            $this->dispatcher->dispatch(Task::EVENT_MOVE_SWIMLANE, new TaskEvent($event_data));
        } elseif ($task['column_id'] != $new_column_id) {
            $this->logger->debug('Event fired: '.Task::EVENT_MOVE_COLUMN);
            $this->dispatcher->dispatch(Task::EVENT_MOVE_COLUMN, new TaskEvent($event_data));
        } elseif ($task['position'] != $new_position) {
            $this->logger->debug('Event fired: '.Task::EVENT_MOVE_POSITION);
            $this->dispatcher->dispatch(Task::EVENT_MOVE_POSITION, new TaskEvent($event_data));
        }
    }
}