1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Task Position. |
18
|
|
|
*/ |
19
|
|
|
class TaskPositionModel extends Model |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Move a task to another column or to another position. |
23
|
|
|
* |
24
|
|
|
* @param int $project_id Project id |
25
|
|
|
* @param int $task_id Task id |
26
|
|
|
* @param int $column_id Column id |
27
|
|
|
* @param int $position Position (must be >= 1) |
28
|
|
|
* @param int $swimlane_id Swimlane id |
29
|
|
|
* @param bool $fire_events Fire events |
30
|
|
|
* @param bool $onlyOpen Do not move closed tasks |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public function movePosition($project_id, $task_id, $column_id, $position, $swimlane_id = 0, $fire_events = true, $onlyOpen = true) |
35
|
|
|
{ |
36
|
|
|
if ($position < 1) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$task = $this->taskFinderModel->getById($task_id); |
|
|
|
|
41
|
|
|
|
42
|
|
|
if ($onlyOpen && $task['is_active'] == TaskModel::STATUS_CLOSED) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$result = false; |
47
|
|
|
|
48
|
|
|
if ($task['swimlane_id'] != $swimlane_id) { |
49
|
|
|
$result = $this->saveSwimlaneChange($project_id, $task_id, $position, $task['column_id'], $column_id, $task['swimlane_id'], $swimlane_id); |
50
|
|
|
} elseif ($task['column_id'] != $column_id) { |
51
|
|
|
$result = $this->saveColumnChange($project_id, $task_id, $position, $swimlane_id, $task['column_id'], $column_id); |
52
|
|
|
} elseif ($task['position'] != $position) { |
53
|
|
|
$result = $this->savePositionChange($project_id, $task_id, $position, $column_id, $swimlane_id); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($result && $fire_events) { |
57
|
|
|
$this->fireEvents($task, $column_id, $position, $swimlane_id); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Move a task to another swimlane. |
65
|
|
|
* |
66
|
|
|
* @param int $project_id |
67
|
|
|
* @param int $task_id |
68
|
|
|
* @param int $position |
69
|
|
|
* @param int $original_column_id |
70
|
|
|
* @param int $new_column_id |
71
|
|
|
* @param int $original_swimlane_id |
72
|
|
|
* @param int $new_swimlane_id |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
private function saveSwimlaneChange($project_id, $task_id, $position, $original_column_id, $new_column_id, $original_swimlane_id, $new_swimlane_id) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$this->db->startTransaction(); |
|
|
|
|
79
|
|
|
$r1 = $this->saveTaskPositions($project_id, $task_id, 0, $original_column_id, $original_swimlane_id); |
80
|
|
|
$r2 = $this->saveTaskPositions($project_id, $task_id, $position, $new_column_id, $new_swimlane_id); |
81
|
|
|
$r3 = $this->saveTaskTimestamps($task_id); |
82
|
|
|
$this->db->closeTransaction(); |
|
|
|
|
83
|
|
|
|
84
|
|
|
return $r1 && $r2 && $r3; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Move a task to another column. |
89
|
|
|
* |
90
|
|
|
* @param int $project_id |
91
|
|
|
* @param int $task_id |
92
|
|
|
* @param int $position |
93
|
|
|
* @param int $swimlane_id |
94
|
|
|
* @param int $original_column_id |
95
|
|
|
* @param int $new_column_id |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
private function saveColumnChange($project_id, $task_id, $position, $swimlane_id, $original_column_id, $new_column_id) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this->db->startTransaction(); |
|
|
|
|
102
|
|
|
$r1 = $this->saveTaskPositions($project_id, $task_id, 0, $original_column_id, $swimlane_id); |
103
|
|
|
$r2 = $this->saveTaskPositions($project_id, $task_id, $position, $new_column_id, $swimlane_id); |
104
|
|
|
$r3 = $this->saveTaskTimestamps($task_id); |
105
|
|
|
$this->db->closeTransaction(); |
|
|
|
|
106
|
|
|
|
107
|
|
|
return $r1 && $r2 && $r3; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Move a task to another position in the same column. |
112
|
|
|
* |
113
|
|
|
* @param int $project_id |
114
|
|
|
* @param int $task_id |
115
|
|
|
* @param int $position |
116
|
|
|
* @param int $column_id |
117
|
|
|
* @param int $swimlane_id |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
private function savePositionChange($project_id, $task_id, $position, $column_id, $swimlane_id) |
122
|
|
|
{ |
123
|
|
|
$this->db->startTransaction(); |
|
|
|
|
124
|
|
|
$result = $this->saveTaskPositions($project_id, $task_id, $position, $column_id, $swimlane_id); |
125
|
|
|
$this->db->closeTransaction(); |
|
|
|
|
126
|
|
|
|
127
|
|
|
return $result; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Save all task positions for one column. |
132
|
|
|
* |
133
|
|
|
* @param int $project_id |
134
|
|
|
* @param int $task_id |
135
|
|
|
* @param int $position |
136
|
|
|
* @param int $column_id |
137
|
|
|
* @param int $swimlane_id |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
private function saveTaskPositions($project_id, $task_id, $position, $column_id, $swimlane_id) |
142
|
|
|
{ |
143
|
|
|
$tasks_ids = $this->db->table(TaskModel::TABLE) |
|
|
|
|
144
|
|
|
->eq('is_active', 1) |
145
|
|
|
->eq('swimlane_id', $swimlane_id) |
146
|
|
|
->eq('project_id', $project_id) |
147
|
|
|
->eq('column_id', $column_id) |
148
|
|
|
->neq('id', $task_id) |
149
|
|
|
->asc('position') |
150
|
|
|
->asc('id') |
151
|
|
|
->findAllByColumn('id'); |
152
|
|
|
|
153
|
|
|
$offset = 1; |
154
|
|
|
|
155
|
|
|
foreach ($tasks_ids as $current_task_id) { |
156
|
|
|
|
157
|
|
|
// Insert the new task |
158
|
|
|
if ($position == $offset) { |
159
|
|
|
if (!$this->saveTaskPosition($task_id, $offset, $column_id, $swimlane_id)) { |
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
$offset++; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// Rewrite other tasks position |
166
|
|
|
if (!$this->saveTaskPosition($current_task_id, $offset, $column_id, $swimlane_id)) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$offset++; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// Insert the new task at the bottom and normalize bad position |
174
|
|
|
if ($position >= $offset && !$this->saveTaskPosition($task_id, $offset, $column_id, $swimlane_id)) { |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Update task timestamps. |
183
|
|
|
* |
184
|
|
|
* @param int $task_id |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
private function saveTaskTimestamps($task_id) |
189
|
|
|
{ |
190
|
|
|
$now = time(); |
191
|
|
|
|
192
|
|
|
return $this->db->table(TaskModel::TABLE)->eq('id', $task_id)->update([ |
|
|
|
|
193
|
|
|
'date_moved' => $now, |
194
|
|
|
'date_modification' => $now, |
195
|
|
|
]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Save new task position. |
200
|
|
|
* |
201
|
|
|
* @param int $task_id |
202
|
|
|
* @param int $position |
203
|
|
|
* @param int $column_id |
204
|
|
|
* @param int $swimlane_id |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
private function saveTaskPosition($task_id, $position, $column_id, $swimlane_id) |
209
|
|
|
{ |
210
|
|
|
$result = $this->db->table(TaskModel::TABLE)->eq('id', $task_id)->update([ |
|
|
|
|
211
|
|
|
'position' => $position, |
212
|
|
|
'column_id' => $column_id, |
213
|
|
|
'swimlane_id' => $swimlane_id, |
214
|
|
|
]); |
215
|
|
|
|
216
|
|
|
if (!$result) { |
217
|
|
|
$this->db->cancelTransaction(); |
|
|
|
|
218
|
|
|
|
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return true; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Fire events. |
227
|
|
|
* |
228
|
|
|
* @param array $task |
229
|
|
|
* @param int $new_column_id |
230
|
|
|
* @param int $new_position |
231
|
|
|
* @param int $new_swimlane_id |
232
|
|
|
*/ |
233
|
|
|
private function fireEvents(array $task, $new_column_id, $new_position, $new_swimlane_id) |
234
|
|
|
{ |
235
|
|
|
$changes = [ |
236
|
|
|
'project_id' => $task['project_id'], |
237
|
|
|
'position' => $new_position, |
238
|
|
|
'column_id' => $new_column_id, |
239
|
|
|
'swimlane_id' => $new_swimlane_id, |
240
|
|
|
'src_column_id' => $task['column_id'], |
241
|
|
|
'dst_column_id' => $new_column_id, |
242
|
|
|
'date_moved' => $task['date_moved'], |
243
|
|
|
'recurrence_status' => $task['recurrence_status'], |
244
|
|
|
'recurrence_trigger' => $task['recurrence_trigger'], |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
if ($task['swimlane_id'] != $new_swimlane_id) { |
248
|
|
|
$this->queueManager->push($this->taskEventJob->withParams( |
|
|
|
|
249
|
|
|
$task['id'], |
250
|
|
|
[TaskModel::EVENT_MOVE_SWIMLANE], |
251
|
|
|
$changes, |
252
|
|
|
$changes |
253
|
|
|
)); |
254
|
|
|
} elseif ($task['column_id'] != $new_column_id) { |
255
|
|
|
$this->queueManager->push($this->taskEventJob->withParams( |
|
|
|
|
256
|
|
|
$task['id'], |
257
|
|
|
[TaskModel::EVENT_MOVE_COLUMN], |
258
|
|
|
$changes, |
259
|
|
|
$changes |
260
|
|
|
)); |
261
|
|
|
} elseif ($task['position'] != $new_position) { |
262
|
|
|
$this->queueManager->push($this->taskEventJob->withParams( |
|
|
|
|
263
|
|
|
$task['id'], |
264
|
|
|
[TaskModel::EVENT_MOVE_POSITION], |
265
|
|
|
$changes, |
266
|
|
|
$changes |
267
|
|
|
)); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.