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 Duplication. |
18
|
|
|
*/ |
19
|
|
|
class TaskDuplicationModel extends Model |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Fields to copy when duplicating a task. |
23
|
|
|
* |
24
|
|
|
* @var string[] |
25
|
|
|
*/ |
26
|
|
|
protected $fieldsToDuplicate = [ |
27
|
|
|
'title', |
28
|
|
|
'description', |
29
|
|
|
'date_due', |
30
|
|
|
'color_id', |
31
|
|
|
'project_id', |
32
|
|
|
'column_id', |
33
|
|
|
'owner_id', |
34
|
|
|
'score', |
35
|
|
|
'priority', |
36
|
|
|
'category_id', |
37
|
|
|
'time_estimated', |
38
|
|
|
'swimlane_id', |
39
|
|
|
'recurrence_status', |
40
|
|
|
'recurrence_trigger', |
41
|
|
|
'recurrence_factor', |
42
|
|
|
'recurrence_timeframe', |
43
|
|
|
'recurrence_basedate', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Duplicate a task to the same project. |
48
|
|
|
* |
49
|
|
|
* @param int $task_id Task id |
50
|
|
|
* |
51
|
|
|
* @return bool|int Duplicated task id |
52
|
|
|
*/ |
53
|
|
|
public function duplicate($task_id) |
54
|
|
|
{ |
55
|
|
|
$new_task_id = $this->save($task_id, $this->copyFields($task_id)); |
56
|
|
|
|
57
|
|
|
if ($new_task_id !== false) { |
58
|
|
|
$this->tagDuplicationModel->duplicateTaskTags($task_id, $new_task_id); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $new_task_id; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if the assignee and the category are available in the destination project. |
66
|
|
|
* |
67
|
|
|
* @param array $values |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function checkDestinationProjectValues(array &$values) |
72
|
|
|
{ |
73
|
|
|
// Check if the assigned user is allowed for the destination project |
74
|
|
|
if ($values['owner_id'] > 0 && !$this->projectPermissionModel->isUserAllowed($values['project_id'], $values['owner_id'])) { |
|
|
|
|
75
|
|
|
$values['owner_id'] = 0; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Check if the category exists for the destination project |
79
|
|
View Code Duplication |
if ($values['category_id'] > 0) { |
|
|
|
|
80
|
|
|
$values['category_id'] = $this->categoryModel->getIdByName( |
|
|
|
|
81
|
|
|
$values['project_id'], |
82
|
|
|
$this->categoryModel->getNameById($values['category_id']) |
|
|
|
|
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Check if the swimlane exists for the destination project |
87
|
|
View Code Duplication |
if ($values['swimlane_id'] > 0) { |
|
|
|
|
88
|
|
|
$values['swimlane_id'] = $this->swimlaneModel->getIdByName( |
|
|
|
|
89
|
|
|
$values['project_id'], |
90
|
|
|
$this->swimlaneModel->getNameById($values['swimlane_id']) |
|
|
|
|
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Check if the column exists for the destination project |
95
|
|
|
if ($values['column_id'] > 0) { |
96
|
|
|
$values['column_id'] = $this->columnModel->getColumnIdByTitle( |
|
|
|
|
97
|
|
|
$values['project_id'], |
98
|
|
|
$this->columnModel->getColumnTitleById($values['column_id']) |
|
|
|
|
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$values['column_id'] = $values['column_id'] ?: $this->columnModel->getFirstColumnId($values['project_id']); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Check if priority exists for destination project |
105
|
|
|
$values['priority'] = $this->projectTaskPriorityModel->getPriorityForProject( |
|
|
|
|
106
|
|
|
$values['project_id'], |
107
|
|
|
empty($values['priority']) ? 0 : $values['priority'] |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
return $values; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Duplicate fields for the new task. |
115
|
|
|
* |
116
|
|
|
* @param int $task_id Task id |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected function copyFields($task_id) |
121
|
|
|
{ |
122
|
|
|
$task = $this->taskFinderModel->getById($task_id); |
|
|
|
|
123
|
|
|
$values = []; |
124
|
|
|
|
125
|
|
|
foreach ($this->fieldsToDuplicate as $field) { |
126
|
|
|
$values[$field] = $task[$field]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $values; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create the new task and duplicate subtasks. |
134
|
|
|
* |
135
|
|
|
* @param int $task_id Task id |
136
|
|
|
* @param array $values Form values |
137
|
|
|
* |
138
|
|
|
* @return bool|int |
139
|
|
|
*/ |
140
|
|
|
protected function save($task_id, array $values) |
141
|
|
|
{ |
142
|
|
|
$new_task_id = $this->taskModel->create($values); |
|
|
|
|
143
|
|
|
|
144
|
|
|
if ($new_task_id !== false) { |
145
|
|
|
$this->subtaskModel->duplicate($task_id, $new_task_id); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $new_task_id; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.