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
|
|
|
* Swimlanes. |
18
|
|
|
*/ |
19
|
|
|
class SwimlaneModel extends Model |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* SQL table name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const TABLE = 'swimlanes'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Value for active swimlanes. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
const ACTIVE = 1; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Value for inactive swimlanes. |
37
|
|
|
* |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
const INACTIVE = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get a swimlane by the id. |
44
|
|
|
* |
45
|
|
|
* @param int $swimlane_id Swimlane id |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getById($swimlane_id) |
50
|
|
|
{ |
51
|
|
|
return $this->db->table(self::TABLE)->eq('id', $swimlane_id)->findOne(); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the swimlane name by the id. |
56
|
|
|
* |
57
|
|
|
* @param int $swimlane_id Swimlane id |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getNameById($swimlane_id) |
62
|
|
|
{ |
63
|
|
|
return $this->db->table(self::TABLE)->eq('id', $swimlane_id)->findOneColumn('name') ?: ''; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get a swimlane id by the project and the name. |
68
|
|
|
* |
69
|
|
|
* @param int $project_id Project id |
70
|
|
|
* @param string $name Name |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function getIdByName($project_id, $name) |
75
|
|
|
{ |
76
|
|
|
return (int) $this->db->table(self::TABLE) |
|
|
|
|
77
|
|
|
->eq('project_id', $project_id) |
78
|
|
|
->eq('name', $name) |
79
|
|
|
->findOneColumn('id'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get a swimlane by the project and the name. |
84
|
|
|
* |
85
|
|
|
* @param int $project_id Project id |
86
|
|
|
* @param string $name Swimlane name |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getByName($project_id, $name) |
91
|
|
|
{ |
92
|
|
|
return $this->db->table(self::TABLE) |
|
|
|
|
93
|
|
|
->eq('project_id', $project_id) |
94
|
|
|
->eq('name', $name) |
95
|
|
|
->findOne(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get first active swimlane for a project. |
100
|
|
|
* |
101
|
|
|
* @param int $project_id |
102
|
|
|
* |
103
|
|
|
* @return array|null |
104
|
|
|
*/ |
105
|
|
|
public function getFirstActiveSwimlane($project_id) |
106
|
|
|
{ |
107
|
|
|
$swimlanes = $this->getSwimlanes($project_id); |
108
|
|
|
|
109
|
|
|
if (empty($swimlanes)) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $swimlanes[0]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get default swimlane properties. |
118
|
|
|
* |
119
|
|
|
* @param int $project_id Project id |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getDefault($project_id) |
124
|
|
|
{ |
125
|
|
|
$result = $this->db |
|
|
|
|
126
|
|
|
->table(ProjectModel::TABLE) |
127
|
|
|
->eq('id', $project_id) |
128
|
|
|
->columns('id', 'default_swimlane', 'show_default_swimlane') |
129
|
|
|
->findOne(); |
130
|
|
|
|
131
|
|
|
if ($result['default_swimlane'] === 'Default swimlane') { |
132
|
|
|
$result['default_swimlane'] = t($result['default_swimlane']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get all swimlanes for a given project. |
140
|
|
|
* |
141
|
|
|
* @param int $project_id Project id |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getAll($project_id) |
146
|
|
|
{ |
147
|
|
|
return $this->db |
|
|
|
|
148
|
|
|
->table(self::TABLE) |
149
|
|
|
->eq('project_id', $project_id) |
150
|
|
|
->orderBy('position', 'asc') |
151
|
|
|
->findAll(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the list of swimlanes by status. |
156
|
|
|
* |
157
|
|
|
* @param int $project_id Project id |
158
|
|
|
* @param int $status Status |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function getAllByStatus($project_id, $status = self::ACTIVE) |
163
|
|
|
{ |
164
|
|
|
$query = $this->db |
|
|
|
|
165
|
|
|
->table(self::TABLE) |
166
|
|
|
->eq('project_id', $project_id) |
167
|
|
|
->eq('is_active', $status); |
168
|
|
|
|
169
|
|
|
if ($status == self::ACTIVE) { |
170
|
|
|
$query->asc('position'); |
171
|
|
|
} else { |
172
|
|
|
$query->asc('name'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $query->findAll(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get active swimlanes. |
180
|
|
|
* |
181
|
|
|
* @param int $project_id Project id |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public function getSwimlanes($project_id) |
186
|
|
|
{ |
187
|
|
|
$swimlanes = $this->db |
|
|
|
|
188
|
|
|
->table(self::TABLE) |
189
|
|
|
->columns('id', 'name', 'description') |
190
|
|
|
->eq('project_id', $project_id) |
191
|
|
|
->eq('is_active', self::ACTIVE) |
192
|
|
|
->orderBy('position', 'asc') |
193
|
|
|
->findAll(); |
194
|
|
|
|
195
|
|
|
$defaultSwimlane = $this->db |
|
|
|
|
196
|
|
|
->table(ProjectModel::TABLE) |
197
|
|
|
->eq('id', $project_id) |
198
|
|
|
->eq('show_default_swimlane', 1) |
199
|
|
|
->findOneColumn('default_swimlane'); |
200
|
|
|
|
201
|
|
|
if ($defaultSwimlane) { |
202
|
|
|
if ($defaultSwimlane === 'Default swimlane') { |
203
|
|
|
$defaultSwimlane = t($defaultSwimlane); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
array_unshift($swimlanes, ['id' => 0, 'name' => $defaultSwimlane]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $swimlanes; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get list of all swimlanes. |
214
|
|
|
* |
215
|
|
|
* @param int $project_id Project id |
216
|
|
|
* @param bool $prepend Prepend default value |
217
|
|
|
* @param bool $only_active Return only active swimlanes |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public function getList($project_id, $prepend = false, $only_active = false) |
222
|
|
|
{ |
223
|
|
|
$swimlanes = []; |
224
|
|
|
$default = $this->db->table(ProjectModel::TABLE)->eq('id', $project_id)->eq('show_default_swimlane', 1)->findOneColumn('default_swimlane'); |
|
|
|
|
225
|
|
|
|
226
|
|
|
if ($prepend) { |
227
|
|
|
$swimlanes[-1] = t('All swimlanes'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (!empty($default)) { |
231
|
|
|
$swimlanes[0] = $default === 'Default swimlane' ? t($default) : $default; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $swimlanes + $this->db |
|
|
|
|
235
|
|
|
->hashtable(self::TABLE) |
236
|
|
|
->eq('project_id', $project_id) |
237
|
|
|
->in('is_active', $only_active ? [self::ACTIVE] : [self::ACTIVE, self::INACTIVE]) |
238
|
|
|
->orderBy('position', 'asc') |
239
|
|
|
->getAll('id', 'name'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Add a new swimlane. |
244
|
|
|
* |
245
|
|
|
* @param array $values Form values |
246
|
|
|
* |
247
|
|
|
* @return int|bool |
248
|
|
|
*/ |
249
|
|
|
public function create($values) |
250
|
|
|
{ |
251
|
|
|
if (!$this->projectModel->exists($values['project_id'])) { |
|
|
|
|
252
|
|
|
return 0; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$values['position'] = $this->getLastPosition($values['project_id']); |
256
|
|
|
|
257
|
|
|
return $this->db->table(self::TABLE)->persist($values); |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Update a swimlane. |
262
|
|
|
* |
263
|
|
|
* @param array $values Form values |
264
|
|
|
* |
265
|
|
|
* @return bool |
266
|
|
|
*/ |
267
|
|
|
public function update(array $values) |
268
|
|
|
{ |
269
|
|
|
return $this->db |
|
|
|
|
270
|
|
|
->table(self::TABLE) |
271
|
|
|
->eq('id', $values['id']) |
272
|
|
|
->update($values); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Update the default swimlane. |
277
|
|
|
* |
278
|
|
|
* @param array $values Form values |
279
|
|
|
* |
280
|
|
|
* @return bool |
281
|
|
|
*/ |
282
|
|
|
public function updateDefault(array $values) |
283
|
|
|
{ |
284
|
|
|
return $this->db |
|
|
|
|
285
|
|
|
->table(ProjectModel::TABLE) |
286
|
|
|
->eq('id', $values['id']) |
287
|
|
|
->update([ |
288
|
|
|
'default_swimlane' => $values['default_swimlane'], |
289
|
|
|
'show_default_swimlane' => $values['show_default_swimlane'], |
290
|
|
|
]); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Enable the default swimlane. |
295
|
|
|
* |
296
|
|
|
* @param int $project_id |
297
|
|
|
* |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
|
|
public function enableDefault($project_id) |
301
|
|
|
{ |
302
|
|
|
return $this->db |
|
|
|
|
303
|
|
|
->table(ProjectModel::TABLE) |
304
|
|
|
->eq('id', $project_id) |
305
|
|
|
->update([ |
306
|
|
|
'show_default_swimlane' => 1, |
307
|
|
|
]); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Disable the default swimlane. |
312
|
|
|
* |
313
|
|
|
* @param int $project_id |
314
|
|
|
* |
315
|
|
|
* @return bool |
316
|
|
|
*/ |
317
|
|
|
public function disableDefault($project_id) |
318
|
|
|
{ |
319
|
|
|
return $this->db |
|
|
|
|
320
|
|
|
->table(ProjectModel::TABLE) |
321
|
|
|
->eq('id', $project_id) |
322
|
|
|
->update([ |
323
|
|
|
'show_default_swimlane' => 0, |
324
|
|
|
]); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Get the last position of a swimlane. |
329
|
|
|
* |
330
|
|
|
* @param int $project_id |
331
|
|
|
* |
332
|
|
|
* @return int |
333
|
|
|
*/ |
334
|
|
|
public function getLastPosition($project_id) |
335
|
|
|
{ |
336
|
|
|
return $this->db |
|
|
|
|
337
|
|
|
->table(self::TABLE) |
338
|
|
|
->eq('project_id', $project_id) |
339
|
|
|
->eq('is_active', 1) |
340
|
|
|
->count() + 1; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Disable a swimlane. |
345
|
|
|
* |
346
|
|
|
* @param int $project_id Project id |
347
|
|
|
* @param int $swimlane_id Swimlane id |
348
|
|
|
* |
349
|
|
|
* @return bool |
350
|
|
|
*/ |
351
|
|
|
public function disable($project_id, $swimlane_id) |
352
|
|
|
{ |
353
|
|
|
$result = $this->db |
|
|
|
|
354
|
|
|
->table(self::TABLE) |
355
|
|
|
->eq('id', $swimlane_id) |
356
|
|
|
->update([ |
357
|
|
|
'is_active' => self::INACTIVE, |
358
|
|
|
'position' => 0, |
359
|
|
|
]); |
360
|
|
|
|
361
|
|
|
if ($result) { |
362
|
|
|
// Re-order positions |
363
|
|
|
$this->updatePositions($project_id); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
return $result; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Enable a swimlane. |
371
|
|
|
* |
372
|
|
|
* @param int $project_id Project id |
373
|
|
|
* @param int $swimlane_id Swimlane id |
374
|
|
|
* |
375
|
|
|
* @return bool |
376
|
|
|
*/ |
377
|
|
|
public function enable($project_id, $swimlane_id) |
378
|
|
|
{ |
379
|
|
|
return $this->db |
|
|
|
|
380
|
|
|
->table(self::TABLE) |
381
|
|
|
->eq('id', $swimlane_id) |
382
|
|
|
->update([ |
383
|
|
|
'is_active' => self::ACTIVE, |
384
|
|
|
'position' => $this->getLastPosition($project_id), |
385
|
|
|
]); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Remove a swimlane. |
390
|
|
|
* |
391
|
|
|
* @param int $project_id Project id |
392
|
|
|
* @param int $swimlane_id Swimlane id |
393
|
|
|
* |
394
|
|
|
* @return bool |
395
|
|
|
*/ |
396
|
|
View Code Duplication |
public function remove($project_id, $swimlane_id) |
|
|
|
|
397
|
|
|
{ |
398
|
|
|
$this->db->startTransaction(); |
|
|
|
|
399
|
|
|
|
400
|
|
|
// Tasks should not be assigned anymore to this swimlane |
401
|
|
|
$this->db->table(TaskModel::TABLE)->eq('swimlane_id', $swimlane_id)->update(['swimlane_id' => 0]); |
|
|
|
|
402
|
|
|
|
403
|
|
|
if (!$this->db->table(self::TABLE)->eq('id', $swimlane_id)->remove()) { |
|
|
|
|
404
|
|
|
$this->db->cancelTransaction(); |
|
|
|
|
405
|
|
|
|
406
|
|
|
return false; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
// Re-order positions |
410
|
|
|
$this->updatePositions($project_id); |
411
|
|
|
|
412
|
|
|
$this->db->closeTransaction(); |
|
|
|
|
413
|
|
|
|
414
|
|
|
return true; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Update swimlane positions after disabling or removing a swimlane. |
419
|
|
|
* |
420
|
|
|
* @param int $project_id Project id |
421
|
|
|
* |
422
|
|
|
* @return bool |
423
|
|
|
*/ |
424
|
|
|
public function updatePositions($project_id) |
425
|
|
|
{ |
426
|
|
|
$position = 0; |
427
|
|
|
$swimlanes = $this->db |
|
|
|
|
428
|
|
|
->table(self::TABLE) |
429
|
|
|
->eq('project_id', $project_id) |
430
|
|
|
->eq('is_active', 1) |
431
|
|
|
->asc('position') |
432
|
|
|
->asc('id') |
433
|
|
|
->findAllByColumn('id'); |
434
|
|
|
|
435
|
|
|
if (!$swimlanes) { |
436
|
|
|
return false; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
foreach ($swimlanes as $swimlane_id) { |
440
|
|
|
$this->db->table(self::TABLE) |
|
|
|
|
441
|
|
|
->eq('id', $swimlane_id) |
442
|
|
|
->update(['position' => ++$position]); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
return true; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Change swimlane position. |
450
|
|
|
* |
451
|
|
|
* @param int $project_id |
452
|
|
|
* @param int $swimlane_id |
453
|
|
|
* @param int $position |
454
|
|
|
* |
455
|
|
|
* @return bool |
456
|
|
|
*/ |
457
|
|
View Code Duplication |
public function changePosition($project_id, $swimlane_id, $position) |
|
|
|
|
458
|
|
|
{ |
459
|
|
|
if ($position < 1 || $position > $this->db->table(self::TABLE)->eq('project_id', $project_id)->count()) { |
|
|
|
|
460
|
|
|
return false; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
$swimlane_ids = $this->db->table(self::TABLE) |
|
|
|
|
464
|
|
|
->eq('is_active', 1) |
465
|
|
|
->eq('project_id', $project_id) |
466
|
|
|
->neq('id', $swimlane_id) |
467
|
|
|
->asc('position') |
468
|
|
|
->findAllByColumn('id'); |
469
|
|
|
|
470
|
|
|
$offset = 1; |
471
|
|
|
$results = []; |
472
|
|
|
|
473
|
|
|
foreach ($swimlane_ids as $current_swimlane_id) { |
474
|
|
|
if ($offset == $position) { |
475
|
|
|
$offset++; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
$results[] = $this->db->table(self::TABLE)->eq('id', $current_swimlane_id)->update(['position' => $offset]); |
|
|
|
|
479
|
|
|
$offset++; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
$results[] = $this->db->table(self::TABLE)->eq('id', $swimlane_id)->update(['position' => $position]); |
|
|
|
|
483
|
|
|
|
484
|
|
|
return !in_array(false, $results, true); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Duplicate Swimlane to project. |
489
|
|
|
* |
490
|
|
|
* @param int $project_from Project Template |
491
|
|
|
* @param int $project_to Project that receives the copy |
492
|
|
|
* |
493
|
|
|
* @return int|bool |
494
|
|
|
*/ |
495
|
|
|
public function duplicate($project_from, $project_to) |
496
|
|
|
{ |
497
|
|
|
$swimlanes = $this->getAll($project_from); |
498
|
|
|
|
499
|
|
|
foreach ($swimlanes as $swimlane) { |
500
|
|
|
unset($swimlane['id']); |
501
|
|
|
$swimlane['project_id'] = $project_to; |
502
|
|
|
|
503
|
|
|
if (!$this->db->table(self::TABLE)->save($swimlane)) { |
|
|
|
|
504
|
|
|
return false; |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
$default_swimlane = $this->getDefault($project_from); |
509
|
|
|
$default_swimlane['id'] = $project_to; |
510
|
|
|
|
511
|
|
|
$this->updateDefault($default_swimlane); |
512
|
|
|
|
513
|
|
|
return true; |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
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.