@@ -150,8 +150,21 @@ public function newComment(Comment $comment, $request)
150150 $ permission_level = ($ member ->currentLevel () > 1 and $ category_level > 1 ) ? $ category_level : 1 ;
151151
152152 if ($ permission_level < 2 ){
153- // Notificate assigned agent
154- $ a_to = $ this ->defaultRecipients ($ ticket , $ notification_owner , $ subject , $ template );
153+ // Get defaults
154+ $ a_defaults = $ this ->defaultRecipients ($ ticket , $ notification_owner , $ subject , $ template );
155+
156+ // Add recipients for each default one
157+ foreach ($ a_defaults as $ default ){
158+ $ a_recipients [] = $ default ['recipient ' ]->id ;
159+ }
160+
161+ // Add all members notified previously
162+ $ a_recipients = array_unique (array_merge ($ a_recipients , $ ticket ->commentNotifications ()->where ('member_id ' , '!= ' , $ member ->id )->groupBy ('member_id ' )->pluck ('member_id ' )->toArray ()));
163+
164+ if (!is_null ($ ticket ->owner ) and $ member ->id != $ ticket ->owner ->id and !in_array ($ ticket ->owner ->id , $ a_recipients )){
165+ // Add ticket owner if it's not the same as who comments
166+ $ a_recipients [] = $ ticket ->owner ->id ;
167+ }
155168
156169 }elseif ($ comment ->type == 'note ' or !$ ticket ->hidden ){
157170 // Selected recipients
@@ -160,26 +173,6 @@ public function newComment(Comment $comment, $request)
160173 $ a_recipients = isset ($ comment ->a_recipients ) ? $ comment ->a_recipients : ($ comment ->type == 'note ' ? $ request ->note_recipients : $ request ->reply_recipients );
161174
162175 if (count ($ a_recipients ) > 0 ){
163- foreach ($ a_recipients as $ member_id ){
164- $ recipient = Member::find ($ member_id );
165- if (count ($ recipient ) == 1 ){
166- // Register the notified email
167- $ notification = CommentNotification::create ([
168- 'comment_id ' => $ comment ->id ,
169- 'name ' => $ recipient ->name ,
170- 'email ' => $ recipient ->email ,
171- 'member_id ' => $ member_id
172- ]);
173-
174- // Add email to actual mail recipients
175- $ a_to [] = [
176- 'recipient ' => $ recipient ,
177- 'subject ' => $ subject ,
178- 'template ' => $ template
179- ];
180- }
181- }
182-
183176 if ($ request ->has ('add_in_user_notification_text ' ) or (isset ($ comment ->add_in_user_notification_text ))){
184177 // Element in request comes from Comment modal
185178 // $comment property comes from an embedded comment when editing or creating a ticket
@@ -188,6 +181,28 @@ public function newComment(Comment $comment, $request)
188181 }
189182 }
190183
184+ if (count ($ a_recipients ) > 0 ){
185+ foreach ($ a_recipients as $ member_id ){
186+ $ recipient = Member::find ($ member_id );
187+ if (count ($ recipient ) == 1 ){
188+ // Register the notified email
189+ $ notification = CommentNotification::create ([
190+ 'comment_id ' => $ comment ->id ,
191+ 'name ' => $ recipient ->name ,
192+ 'email ' => $ recipient ->email ,
193+ 'member_id ' => $ member_id
194+ ]);
195+
196+ // Add email to actual mail recipients
197+ $ a_to [] = [
198+ 'recipient ' => $ recipient ,
199+ 'subject ' => $ subject ,
200+ 'template ' => $ template
201+ ];
202+ }
203+ }
204+ }
205+
191206 // Send notifications
192207 if (isset ($ a_to )) $ this ->sendNotification ($ a_to , $ data );
193208 }
0 commit comments