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 Model. |
18
|
|
|
*/ |
19
|
|
|
class TaskModel extends Model |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* SQL table name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const TABLE = 'tasks'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Task status. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
const STATUS_OPEN = 1; |
34
|
|
|
const STATUS_CLOSED = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Events. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
const EVENT_MOVE_PROJECT = 'task.move.project'; |
42
|
|
|
const EVENT_MOVE_COLUMN = 'task.move.column'; |
43
|
|
|
const EVENT_MOVE_POSITION = 'task.move.position'; |
44
|
|
|
const EVENT_MOVE_SWIMLANE = 'task.move.swimlane'; |
45
|
|
|
const EVENT_UPDATE = 'task.update'; |
46
|
|
|
const EVENT_CREATE = 'task.create'; |
47
|
|
|
const EVENT_CLOSE = 'task.close'; |
48
|
|
|
const EVENT_OPEN = 'task.open'; |
49
|
|
|
const EVENT_CREATE_UPDATE = 'task.create_update'; |
50
|
|
|
const EVENT_ASSIGNEE_CHANGE = 'task.assignee_change'; |
51
|
|
|
const EVENT_OVERDUE = 'task.overdue'; |
52
|
|
|
const EVENT_USER_MENTION = 'task.user.mention'; |
53
|
|
|
const EVENT_DAILY_CRONJOB = 'task.cronjob.daily'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Recurrence: status. |
57
|
|
|
* |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
const RECURRING_STATUS_NONE = 0; |
61
|
|
|
const RECURRING_STATUS_PENDING = 1; |
62
|
|
|
const RECURRING_STATUS_PROCESSED = 2; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Recurrence: trigger. |
66
|
|
|
* |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
const RECURRING_TRIGGER_FIRST_COLUMN = 0; |
70
|
|
|
const RECURRING_TRIGGER_LAST_COLUMN = 1; |
71
|
|
|
const RECURRING_TRIGGER_CLOSE = 2; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Recurrence: timeframe. |
75
|
|
|
* |
76
|
|
|
* @var int |
77
|
|
|
*/ |
78
|
|
|
const RECURRING_TIMEFRAME_DAYS = 0; |
79
|
|
|
const RECURRING_TIMEFRAME_MONTHS = 1; |
80
|
|
|
const RECURRING_TIMEFRAME_YEARS = 2; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Recurrence: base date used to calculate new due date. |
84
|
|
|
* |
85
|
|
|
* @var int |
86
|
|
|
*/ |
87
|
|
|
const RECURRING_BASEDATE_DUEDATE = 0; |
88
|
|
|
const RECURRING_BASEDATE_TRIGGERDATE = 1; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create a task. |
92
|
|
|
* |
93
|
|
|
* @param array $values Form values |
94
|
|
|
* |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
public function create(array $values) |
98
|
|
|
{ |
99
|
|
|
$position = empty($values['position']) ? 0 : $values['position']; |
100
|
|
|
$tags = []; |
101
|
|
|
|
102
|
|
|
if (isset($values['tags'])) { |
103
|
|
|
$tags = $values['tags']; |
104
|
|
|
unset($values['tags']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->prepare($values); |
108
|
|
|
$task_id = $this->db->table(self::TABLE)->persist($values); |
|
|
|
|
109
|
|
|
|
110
|
|
|
if ($task_id !== false) { |
111
|
|
|
if ($position > 0 && $values['position'] > 1) { |
112
|
|
|
$this->taskPositionModel->movePosition($values['project_id'], $task_id, $values['column_id'], $position, $values['swimlane_id'], false); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!empty($tags)) { |
116
|
|
|
$this->taskTagModel->save($values['project_id'], $task_id, $tags); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->queueManager->push($this->taskEventJob->withParams( |
|
|
|
|
120
|
|
|
$task_id, |
121
|
|
|
[self::EVENT_CREATE_UPDATE, self::EVENT_CREATE] |
122
|
|
|
)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return (int) $task_id; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Update a task. |
130
|
|
|
* |
131
|
|
|
* @param array $values |
132
|
|
|
* @param bool $fire_events |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function update(array $values, $fire_events = true) |
137
|
|
|
{ |
138
|
|
|
$task = $this->taskFinderModel->getById($values['id']); |
|
|
|
|
139
|
|
|
|
140
|
|
|
if (isset($values['tags'])) { |
141
|
|
|
$this->taskTagModel->save($task['project_id'], $values['id'], $values['tags']); |
|
|
|
|
142
|
|
|
unset($values['tags']); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$values = $this->dateParser->convert($values, ['date_due']); |
|
|
|
|
146
|
|
|
$values = $this->dateParser->convert($values, ['date_started'], true); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$this->helper->model->removeFields($values, ['id']); |
|
|
|
|
149
|
|
|
$this->helper->model->resetFields($values, ['date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent']); |
|
|
|
|
150
|
|
|
$this->helper->model->convertIntegerFields($values, ['priority', 'is_active', 'recurrence_status', 'recurrence_trigger', 'recurrence_factor', 'recurrence_timeframe', 'recurrence_basedate']); |
|
|
|
|
151
|
|
|
|
152
|
|
|
$values['date_modification'] = time(); |
153
|
|
|
|
154
|
|
|
$this->hook->reference('model:task:modification:prepare', $values); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$result = $this->db->table(self::TABLE)->eq('id', $task['id'])->update($values); |
|
|
|
|
157
|
|
|
|
158
|
|
|
if ($fire_events && $result) { |
159
|
|
|
$events = []; |
160
|
|
|
|
161
|
|
|
$diff = array_diff_assoc($values, $task); |
162
|
|
|
unset($diff['date_modification']); |
163
|
|
|
|
164
|
|
|
if (isset($values['owner_id']) && $task['owner_id'] != $values['owner_id'] && count($diff) === 1) { |
165
|
|
|
$events[] = self::EVENT_ASSIGNEE_CHANGE; |
166
|
|
|
} elseif (count($diff) > 0) { |
167
|
|
|
$events[] = self::EVENT_CREATE_UPDATE; |
168
|
|
|
$events[] = self::EVENT_UPDATE; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (!empty($events)) { |
172
|
|
|
$this->queueManager->push($this->taskEventJob |
|
|
|
|
173
|
|
|
->withParams($task['id'], $events, $values, [], $task) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $result; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Remove a task. |
183
|
|
|
* |
184
|
|
|
* @param int $task_id Task id |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public function remove($task_id) |
189
|
|
|
{ |
190
|
|
|
if (!$this->taskFinderModel->exists($task_id)) { |
|
|
|
|
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->taskFileModel->removeAll($task_id); |
|
|
|
|
195
|
|
|
|
196
|
|
|
return $this->db->table(self::TABLE)->eq('id', $task_id)->remove(); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get a the task id from a text. |
201
|
|
|
* |
202
|
|
|
* Example: "Fix bug #1234" will return 1234 |
203
|
|
|
* |
204
|
|
|
* @param string $message Text |
205
|
|
|
* |
206
|
|
|
* @return int |
207
|
|
|
*/ |
208
|
|
|
public function getTaskIdFromText($message) |
209
|
|
|
{ |
210
|
|
|
if (preg_match('!#(\d+)!i', $message, $matches) && isset($matches[1])) { |
211
|
|
|
return $matches[1]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return 0; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get task progress. |
219
|
|
|
* |
220
|
|
|
* @param array $task |
221
|
|
|
* @param array $columns |
222
|
|
|
* |
223
|
|
|
* @return int |
224
|
|
|
*/ |
225
|
|
|
public function getProgress(array $task, array $columns) |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
if ($task['is_active'] == self::STATUS_CLOSED) { |
228
|
|
|
return 100; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $task['progress'] ?: 0; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Prepare data. |
236
|
|
|
* |
237
|
|
|
* @param array $values Form values |
238
|
|
|
*/ |
239
|
|
|
protected function prepare(array &$values) |
240
|
|
|
{ |
241
|
|
|
$values = $this->dateParser->convert($values, ['date_due']); |
|
|
|
|
242
|
|
|
$values = $this->dateParser->convert($values, ['date_started'], true); |
|
|
|
|
243
|
|
|
|
244
|
|
|
$this->helper->model->removeFields($values, ['another_task', 'duplicate_multiple_projects']); |
|
|
|
|
245
|
|
|
$this->helper->model->resetFields($values, ['creator_id', 'owner_id', 'swimlane_id', 'date_due', 'date_started', 'score', 'progress', 'category_id', 'time_estimated', 'time_spent']); |
|
|
|
|
246
|
|
|
|
247
|
|
|
if (empty($values['column_id'])) { |
248
|
|
|
$values['column_id'] = $this->columnModel->getFirstColumnId($values['project_id']); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if (empty($values['color_id'])) { |
252
|
|
|
$values['color_id'] = $this->colorModel->getDefaultColor(); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
if (empty($values['title'])) { |
256
|
|
|
$values['title'] = t('Untitled'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if ($this->userSession->isLogged()) { |
|
|
|
|
260
|
|
|
$values['creator_id'] = $this->userSession->getId(); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$values['swimlane_id'] = empty($values['swimlane_id']) ? 0 : $values['swimlane_id']; |
264
|
|
|
$values['date_creation'] = time(); |
265
|
|
|
$values['date_modification'] = $values['date_creation']; |
266
|
|
|
$values['date_moved'] = $values['date_creation']; |
267
|
|
|
$values['position'] = $this->taskFinderModel->countByColumnAndSwimlaneId($values['project_id'], $values['column_id'], $values['swimlane_id']) + 1; |
|
|
|
|
268
|
|
|
|
269
|
|
|
$this->hook->reference('model:task:creation:prepare', $values); |
|
|
|
|
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
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.