@@ -28,17 +28,24 @@ class NewTaskModificationModel extends Base
28
28
public function update (array $ values , $ fire_events = true )
29
29
{
30
30
$ task = $ this ->taskFinderModel ->getById ($ values ['id ' ]);
31
-
31
+
32
32
$ this ->updateTags ($ values , $ task );
33
- if ( $ this -> checkKeys ( $ values )) $ this -> updateMeta ( $ values , $ task );
33
+
34
34
$ this ->prepare ($ values );
35
- $ result = $ this ->db ->table (TaskModel::TABLE )->eq ('id ' , $ task ['id ' ])->update ($ values );
36
-
37
- if ($ fire_events && $ result ) {
38
- $ this ->fireEvents ($ task , $ values );
39
- }
40
-
41
- return $ result ;
35
+
36
+ $ values = array_filter ($ values );
37
+ $ origValues = array_merge (array (), $ values );
38
+
39
+ if ($ this ->checkKeys ($ values ))
40
+ $ this ->updateMeta ($ values , $ task );
41
+
42
+ $ result = $ this ->db ->table (TaskModel::TABLE )->eq ('id ' , $ task ['id ' ])->update ($ values );
43
+
44
+ if ($ fire_events && $ result ) {
45
+ $ this ->fireEvents ($ task , $ origValues );
46
+ }
47
+
48
+ return $ result ;
42
49
}
43
50
44
51
/**
@@ -50,17 +57,17 @@ public function update(array $values, $fire_events = true)
50
57
*/
51
58
public function checkKeys ($ values )
52
59
{
53
-
60
+
54
61
if (count (preg_grep ('/^metamagikkey[\d]*/ ' , array_keys ($ values ))) > 0 )
55
62
{
56
- return true ;
63
+ return true ;
57
64
}
58
65
else
59
66
{
60
- return false ;
67
+ return false ;
61
68
}
62
69
}
63
-
70
+
64
71
/**
65
72
* Fire events
66
73
*
@@ -71,21 +78,21 @@ public function checkKeys($values)
71
78
protected function fireEvents (array $ task , array $ changes )
72
79
{
73
80
$ events = array ();
74
-
81
+
75
82
if ($ this ->isAssigneeChanged ($ task , $ changes )) {
76
83
$ events [] = TaskModel::EVENT_ASSIGNEE_CHANGE ;
77
84
} elseif ($ this ->isModified ($ task , $ changes )) {
78
85
$ events [] = TaskModel::EVENT_CREATE_UPDATE ;
79
86
$ events [] = TaskModel::EVENT_UPDATE ;
80
87
}
81
-
88
+
82
89
if (! empty ($ events )) {
83
90
$ this ->queueManager ->push ($ this ->taskEventJob
84
91
->withParams ($ task ['id ' ], $ events , $ changes , array (), $ task )
85
- );
92
+ );
86
93
}
87
94
}
88
-
95
+
89
96
/**
90
97
* Return true if the task have been modified
91
98
*
@@ -100,7 +107,7 @@ protected function isModified(array $task, array $changes)
100
107
unset($ diff ['date_modification ' ]);
101
108
return count ($ diff ) > 0 ;
102
109
}
103
-
110
+
104
111
/**
105
112
* Return true if the field is the only modified value
106
113
*
@@ -115,7 +122,7 @@ protected function isAssigneeChanged(array $task, array $changes)
115
122
unset($ diff ['date_modification ' ]);
116
123
return isset ($ changes ['owner_id ' ]) && $ task ['owner_id ' ] != $ changes ['owner_id ' ] && count ($ diff ) === 1 ;
117
124
}
118
-
125
+
119
126
/**
120
127
* Prepare data before task modification
121
128
*
@@ -126,16 +133,16 @@ protected function prepare(array &$values)
126
133
{
127
134
$ values = $ this ->dateParser ->convert ($ values , array ('date_due ' ), true );
128
135
$ values = $ this ->dateParser ->convert ($ values , array ('date_started ' ), true );
129
-
136
+
130
137
$ this ->helper ->model ->removeFields ($ values , array ('id ' ));
131
138
$ this ->helper ->model ->resetFields ($ values , array ('date_due ' , 'date_started ' , 'score ' , 'category_id ' , 'time_estimated ' , 'time_spent ' ));
132
139
$ this ->helper ->model ->convertIntegerFields ($ values , array ('priority ' , 'is_active ' , 'recurrence_status ' , 'recurrence_trigger ' , 'recurrence_factor ' , 'recurrence_timeframe ' , 'recurrence_basedate ' ));
133
-
140
+
134
141
$ values ['date_modification ' ] = time ();
135
-
142
+
136
143
$ this ->hook ->reference ('model:task:modification:prepare ' , $ values );
137
144
}
138
-
145
+
139
146
/**
140
147
* Update tags
141
148
*
@@ -154,9 +161,10 @@ protected function updateTags(array &$values, array $original_task)
154
161
protected function updateMeta (array &$ values , array $ original_task )
155
162
{
156
163
$ metadoublecheck = $ this ->metadataTypeModel ->getAll ();
164
+
157
165
foreach ($ metadoublecheck as $ check ) {
158
166
$ exists = array_key_exists ('metamagikkey_ ' . $ check ['human_name ' ], $ values );
159
- if (!$ exists ) {
167
+ if (!$ exists ) {
160
168
$ existsdoublecheck = array_key_exists ('metamagikkey_ ' . $ check ['human_name ' ] . '[] ' , $ values );
161
169
if (!$ existsdoublecheck ) { $ values ['metamagikkey_ ' . $ check ['human_name ' ]] = '' ; }
162
170
}
@@ -171,19 +179,19 @@ protected function updateMeta(array &$values, array $original_task)
171
179
$ keyval = $ values [$ key ];
172
180
if (empty ($ keyval )) { $ keyval = '' ; }
173
181
if (!is_array ($ keyval )) {
174
- $ this ->taskMetadataModel ->save ($ original_task ['id ' ], [$ realkey => $ keyval ]);
175
- unset($ values [$ key ]);
182
+ $ this ->taskMetadataModel ->save ($ original_task ['id ' ], [$ realkey => $ keyval ]);
183
+ unset($ values [$ key ]);
176
184
} else {
177
185
$ key_imploded = array ();
178
186
foreach ($ keyval as $ k => $ v ) {
179
- if ($ v ) { array_push ($ key_imploded , implode (', ' , $ v )); }
187
+ if ($ v ) { array_push ($ key_imploded , implode (', ' , $ v )); }
180
188
}
181
189
$ keys_extracted = implode (', ' , $ key_imploded );
182
190
if (empty ($ keys_extracted )) { $ keys_extracted = '' ; }
183
191
$ this ->taskMetadataModel ->save ($ original_task ['id ' ], [$ realkey => $ keys_extracted ]);
184
192
unset($ values [$ key ]);
185
193
}
186
194
}
187
- }
195
+ }
188
196
}
189
197
}
0 commit comments