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 DateInterval; |
15
|
|
|
use DateTime; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Task Recurrence. |
19
|
|
|
*/ |
20
|
|
|
class TaskRecurrenceModel extends TaskDuplicationModel |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Return the list user selectable recurrence status. |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getRecurrenceStatusList() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
TaskModel::RECURRING_STATUS_NONE => t('No'), |
31
|
|
|
TaskModel::RECURRING_STATUS_PENDING => t('Yes'), |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return the list recurrence triggers. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function getRecurrenceTriggerList() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
TaskModel::RECURRING_TRIGGER_FIRST_COLUMN => t('When task is moved from first column'), |
44
|
|
|
TaskModel::RECURRING_TRIGGER_LAST_COLUMN => t('When task is moved to last column'), |
45
|
|
|
TaskModel::RECURRING_TRIGGER_CLOSE => t('When task is closed'), |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the list options to calculate recurrence due date. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getRecurrenceBasedateList() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
TaskModel::RECURRING_BASEDATE_DUEDATE => t('Existing due date'), |
58
|
|
|
TaskModel::RECURRING_BASEDATE_TRIGGERDATE => t('Action date'), |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the list recurrence timeframes. |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function getRecurrenceTimeframeList() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
TaskModel::RECURRING_TIMEFRAME_DAYS => t('Day(s)'), |
71
|
|
|
TaskModel::RECURRING_TIMEFRAME_MONTHS => t('Month(s)'), |
72
|
|
|
TaskModel::RECURRING_TIMEFRAME_YEARS => t('Year(s)'), |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Duplicate recurring task. |
78
|
|
|
* |
79
|
|
|
* @param int $task_id Task id |
80
|
|
|
* |
81
|
|
|
* @return bool|int Recurrence task id |
82
|
|
|
*/ |
83
|
|
|
public function duplicateRecurringTask($task_id) |
84
|
|
|
{ |
85
|
|
|
$values = $this->copyFields($task_id); |
86
|
|
|
|
87
|
|
|
if ($values['recurrence_status'] == TaskModel::RECURRING_STATUS_PENDING) { |
88
|
|
|
$values['recurrence_parent'] = $task_id; |
89
|
|
|
$values['column_id'] = $this->columnModel->getFirstColumnId($values['project_id']); |
|
|
|
|
90
|
|
|
$this->calculateRecurringTaskDueDate($values); |
91
|
|
|
|
92
|
|
|
$recurring_task_id = $this->save($task_id, $values); |
93
|
|
|
|
94
|
|
|
if ($recurring_task_id !== false) { |
95
|
|
|
$this->tagDuplicationModel->duplicateTaskTags($task_id, $recurring_task_id); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$parent_update = $this->db |
|
|
|
|
98
|
|
|
->table(TaskModel::TABLE) |
99
|
|
|
->eq('id', $task_id) |
100
|
|
|
->update([ |
101
|
|
|
'recurrence_status' => TaskModel::RECURRING_STATUS_PROCESSED, |
102
|
|
|
'recurrence_child' => $recurring_task_id, |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
if ($parent_update) { |
106
|
|
|
return $recurring_task_id; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Calculate new due date for new recurrence task. |
116
|
|
|
* |
117
|
|
|
* @param array $values Task fields |
118
|
|
|
*/ |
119
|
|
|
public function calculateRecurringTaskDueDate(array &$values) |
120
|
|
|
{ |
121
|
|
|
if (!empty($values['date_due']) && $values['recurrence_factor'] != 0) { |
122
|
|
|
if ($values['recurrence_basedate'] == TaskModel::RECURRING_BASEDATE_TRIGGERDATE) { |
123
|
|
|
$values['date_due'] = time(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$factor = abs($values['recurrence_factor']); |
127
|
|
|
$subtract = $values['recurrence_factor'] < 0; |
128
|
|
|
|
129
|
|
|
switch ($values['recurrence_timeframe']) { |
130
|
|
|
case TaskModel::RECURRING_TIMEFRAME_MONTHS: |
131
|
|
|
$interval = 'P'.$factor.'M'; |
132
|
|
|
break; |
133
|
|
|
case TaskModel::RECURRING_TIMEFRAME_YEARS: |
134
|
|
|
$interval = 'P'.$factor.'Y'; |
135
|
|
|
break; |
136
|
|
|
default: |
137
|
|
|
$interval = 'P'.$factor.'D'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$date_due = new DateTime(); |
141
|
|
|
$date_due->setTimestamp($values['date_due']); |
142
|
|
|
|
143
|
|
|
$subtract ? $date_due->sub(new DateInterval($interval)) : $date_due->add(new DateInterval($interval)); |
144
|
|
|
|
145
|
|
|
$values['date_due'] = $date_due->getTimestamp(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.