@@ -15,30 +15,30 @@ class NotificationsController extends Controller
1515{
1616 protected $ category ;
1717 protected $ subject ;
18-
18+
1919 public function __construct (Category $ category )
2020 {
2121 $ this ->category = $ category ;
2222 $ this ->subject = '[ ' .trans ('panichd::email/globals.notify-ticket-category ' , ['name ' => $ category ->name ]).'] # ' ;
2323 }
24-
24+
2525 public function newTicket (Ticket $ ticket )
2626 {
2727 $ notification_owner = \PanicHDMember::find (auth ()->user ()->id );
2828 $ template = 'panichd::emails.new_ticket ' ;
29-
29+
3030 // Affects only agent notification.
3131 $ subject = $ this ->subject .$ ticket ->id .' ' .trans ('panichd::email/globals.notify-created-by ' , ['name ' => $ ticket ->user ->name ] ).trans ('panichd::lang.colon ' ).$ ticket ->subject ;
32-
32+
3333 $ data = [
3434 'ticket ' => serialize ($ ticket ),
3535 'notification_owner ' => serialize ($ notification_owner ),
3636 'current_level ' => $ notification_owner ->currentLevel ()
3737 ];
38-
38+
3939 // Notificate assigned agent
4040 $ a_to = $ this ->defaultRecipients ($ ticket , $ notification_owner , $ subject , $ template );
41-
41+
4242 // Not hidden notices only: Notificate Department email with specific template
4343 if (!$ ticket ->hidden and Setting::grab ('departments_notices_feature ' ) and ($ ticket ->owner ->ticketit_department == '0 ' || $ ticket ->owner ->ticketit_department != "" ) and !in_array ($ ticket ->owner ->email , [$ notification_owner ->email , $ ticket ->agent ->email ])){
4444 $ a_to [] = [
@@ -47,11 +47,11 @@ public function newTicket(Ticket $ticket)
4747 'template ' => Setting::grab ('email.owner.newticket.template ' )
4848 ];
4949 }
50-
50+
5151 // Send notifications
5252 $ this ->sendNotification ($ a_to , $ data );
5353 }
54-
54+
5555 public function ticketClosed (Ticket $ original_ticket , Ticket $ ticket )
5656 {
5757 $ notification_owner = auth ()->user ();
@@ -62,10 +62,10 @@ public function ticketClosed(Ticket $original_ticket, Ticket $ticket)
6262 'notification_owner ' => serialize ($ notification_owner ),
6363 'original_ticket ' => serialize ($ original_ticket )
6464 ];
65-
65+
6666 // Notificate assigned agent
6767 $ a_to = $ this ->defaultRecipients ($ ticket , $ notification_owner , $ subject , $ template );
68-
68+
6969 // Notificate ticket owner
7070 if (Setting::grab ('list_owner_notification ' ) and !$ ticket ->hidden and !in_array ($ ticket ->owner ->email , [$ notification_owner ->email , $ ticket ->agent ->email ])){
7171 $ a_to [] = [
@@ -74,11 +74,11 @@ public function ticketClosed(Ticket $original_ticket, Ticket $ticket)
7474 'template ' => $ template
7575 ];
7676 }
77-
77+
7878 // Send notifications
7979 $ this ->sendNotification ($ a_to , $ data );
8080 }
81-
81+
8282 public function ticketStatusUpdated (Ticket $ original_ticket , Ticket $ ticket )
8383 {
8484 $ notification_owner = auth ()->user ();
@@ -91,10 +91,10 @@ public function ticketStatusUpdated(Ticket $original_ticket, Ticket $ticket)
9191 'original_ticket ' => serialize ($ original_ticket ),
9292 'notification_type ' => 'status '
9393 ];
94-
94+
9595 // Notificate assigned agent
9696 $ a_to = $ this ->defaultRecipients ($ ticket , $ notification_owner , $ subject , $ template );
97-
97+
9898 // Notificate ticket owner
9999 if (Setting::grab ('status_owner_notification ' ) and !$ ticket ->hidden and !in_array ($ ticket ->owner ->email , [$ notification_owner ->email , $ ticket ->agent ->email ])){
100100 $ a_to [] = [
@@ -103,7 +103,7 @@ public function ticketStatusUpdated(Ticket $original_ticket, Ticket $ticket)
103103 'template ' => $ template
104104 ];
105105 }
106-
106+
107107 // Send notifications
108108 $ this ->sendNotification ($ a_to , $ data );
109109 }
@@ -119,10 +119,10 @@ public function ticketAgentUpdated(Ticket $original_ticket, Ticket $ticket)
119119 'original_ticket ' => serialize ($ original_ticket ),
120120 'notification_type ' => 'agent '
121121 ];
122-
122+
123123 // Notificate assigned agent
124124 $ a_to = $ this ->defaultRecipients ($ ticket , $ notification_owner , $ subject , $ template );
125-
125+
126126 // Send notifications
127127 $ this ->sendNotification ($ a_to , $ data );
128128 }
@@ -142,28 +142,30 @@ public function newComment(Comment $comment, $request)
142142 'notification_owner ' => serialize ($ notification_owner ),
143143 'notification_type ' => $ comment ->type
144144 ];
145-
145+
146146 // Notificate assigned agent
147147 $ a_to = $ this ->defaultRecipients ($ ticket , $ notification_owner , $ subject , $ template );
148-
148+
149149 // Notificate ticket owner
150150 if ($ comment ->type == 'reply ' and !$ ticket ->hidden and !in_array ($ ticket ->owner ->email , [$ notification_owner ->email , $ ticket ->agent ->email ])){
151- if ($ request ->has ('add_in_user_notification_text ' )){
152- $ data ['add_in_user_notification_text ' ] = true ;
151+ if ($ request ->has ('add_in_user_notification_text ' ) or (isset ($ comment ->add_in_user_notification_text ))){
152+ // Element in request comes from Comment modal
153+ // $comment property comes from an embedded comment when editing or creating a ticket
154+ $ data ['add_in_user_notification_text ' ] = true ;
153155 }
154-
156+
155157 $ a_to [] = [
156158 'recipient ' => $ ticket ->owner ,
157159 'subject ' => $ subject ,
158160 'template ' => $ template
159161 ];
160162 }
161-
163+
162164 // Send notifications
163165 $ this ->sendNotification ($ a_to , $ data );
164- }
166+ }
165167 }
166-
168+
167169 public function commentUpdate (Comment $ original_comment , Comment $ comment )
168170 {
169171 if ($ comment ->type == 'note ' ){
@@ -179,7 +181,7 @@ public function commentUpdate(Comment $original_comment, Comment $comment)
179181 'ticket ' => serialize ($ ticket ),
180182 'notification_owner ' => serialize ($ notification_owner )
181183 ];
182-
184+
183185 $ a_to =[];
184186
185187 // Email to ticket->agent
@@ -190,7 +192,7 @@ public function commentUpdate(Comment $original_comment, Comment $comment)
190192 'template ' => $ template
191193 ];
192194 }
193-
195+
194196 // Email to comment->owner
195197 if (!$ ticket ->hidden and !in_array ($ comment ->owner ->email , [$ notification_owner ->email , $ ticket ->agent ->email ])){
196198 $ a_to [] = [
@@ -199,9 +201,9 @@ public function commentUpdate(Comment $original_comment, Comment $comment)
199201 'template ' => $ template
200202 ];
201203 }
202-
204+
203205 $ this ->sendNotification ($ a_to , $ data );
204- }
206+ }
205207 }
206208
207209 /**
@@ -216,14 +218,14 @@ public function notificationResend(Request $request)
216218 $ template = 'panichd::emails.new_comment ' ;
217219 $ subject = trans ('panichd::lang.email-resend-abbr ' ) . trans ('panichd::lang.colon ' ) . $ this ->subject .$ ticket ->id . ' ' . trans ('panichd::email/globals.notify-new-note-by ' , ['name ' => $ comment ->user ->name ]) . trans ('panichd::lang.colon ' ) . $ ticket ->subject ;
218220 $ data = [
219- 'comment ' => serialize ($ comment ),
221+ 'comment ' => serialize ($ comment ),
220222 'ticket ' => serialize ($ ticket ),
221223 'notification_owner ' => serialize ($ notification_owner ),
222224 'notification_type ' => $ comment ->type
223225 ];
224-
226+
225227 $ a_to = [];
226-
228+
227229 if ($ request ->has ('to_agent ' )){
228230 $ a_to [] = [
229231 'recipient ' => $ ticket ->agent ,
@@ -238,24 +240,24 @@ public function notificationResend(Request $request)
238240 'template ' => $ template
239241 ];
240242 }
241-
243+
242244 // Load $this->category for sendNotification
243245 $ this ->category = $ ticket ->category ;
244-
246+
245247 // Send notifications
246248 $ this ->sendNotification ($ a_to , $ data );
247-
249+
248250 return back ()->with ('status ' ,'Notificacions reenviades correctament ' );
249251 }
250-
251-
252+
253+
252254 /**
253255 * Create array with default notification recipients
254256 */
255257 public function defaultRecipients ($ ticket , $ notification_owner , $ subject , $ template )
256258 {
257259 $ a_to = [];
258-
260+
259261 // Email to ticket->agent
260262 if ($ ticket ->agent ->email != $ notification_owner ->email ){
261263 $ a_to [] = [
@@ -264,10 +266,10 @@ public function defaultRecipients($ticket, $notification_owner, $subject, $templ
264266 'template ' => $ template
265267 ];
266268 }
267-
269+
268270 return $ a_to ;
269271 }
270-
272+
271273 /**
272274 * Send email notifications from specified mailbox to to other involved users.
273275 *
@@ -278,7 +280,7 @@ public function defaultRecipients($ticket, $notification_owner, $subject, $templ
278280 public function sendNotification ($ a_to , $ data )
279281 {
280282 $ email_replyto = new \stdClass ();
281-
283+
282284 if (Setting::grab ('email.account.name ' ) != "default " and Setting::grab ('email.account.mailbox ' ) != "default " ){
283285 // ReplyTo: Use tickets email account
284286 $ email_replyto ->email_name = Setting::grab ('email.account.name ' );
@@ -288,34 +290,34 @@ public function sendNotification($a_to, $data)
288290 $ email_replyto ->email_name = config ('mail.from.name ' );
289291 $ email_replyto ->email = config ('mail.from.address ' );
290292 }
291-
293+
292294 // From: Use same as ReplyTo
293295 $ email_from = clone $ email_replyto ;
294-
296+
295297 if ($ this ->category ->email_name != "" and $ this ->category ->email != "" ){
296298 // From: Use category email account
297299 $ email_from ->email_name = $ this ->category ->email_name ;
298300 $ email_from ->email = $ this ->category ->email ;
299-
301+
300302 if ($ this ->category ->email_replies == 1 ){
301303 // ReplyTo: Use category email account
302304 $ email_replyto = $ email_from ;
303305 }
304306 }
305-
307+
306308 // Add Email From to template
307309 $ data = array_merge ($ data , [ 'email_from ' => serialize ($ email_from ) ]);
308-
310+
309311 // Send emails
310312 if (LaravelVersion::lt ('5.4 ' )) {
311313 foreach ($ a_to as $ to ){
312314 if ($ to ['recipient ' ]->email != "" ){
313315 // Pass recipient user object to every generated notification email
314316 $ data = array_merge ($ data , ['recipient ' => $ to ['recipient ' ]]);
315-
317+
316318 $ mail_callback = function ($ m ) use ($ to , $ email_from , $ email_replyto ) {
317319 $ m ->to ($ to ['recipient ' ]->email , $ to ['recipient ' ]->name );
318-
320+
319321 $ m ->from ($ email_from ->email , $ email_from ->email_name );
320322 $ m ->replyTo ($ email_replyto ->email , $ email_replyto ->email_name );
321323
@@ -329,13 +331,13 @@ public function sendNotification($a_to, $data)
329331 }
330332 }
331333 }
332-
334+
333335 } elseif (LaravelVersion::min ('5.4 ' )) {
334336 foreach ($ a_to as $ to ){
335337 if ($ to ['recipient ' ]->email != "" ){
336338 // Pass recipient user object to every generated notification email
337339 $ data = array_merge ($ data , ['recipient ' => $ to ['recipient ' ]]);
338-
340+
339341 $ mail = new \PanicHD \PanicHD \Mail \PanicHDNotification ($ to ['template ' ], $ data , $ email_from , $ email_replyto , $ to ['subject ' ]);
340342
341343 if (Setting::grab ('queue_emails ' )) {
@@ -347,4 +349,4 @@ public function sendNotification($a_to, $data)
347349 }
348350 }
349351 }
350- }
352+ }
0 commit comments