Skip to content

Commit 8d2216f

Browse files
committed
Merge branch 'comment-recipients' into dev
2 parents 1702104 + 36b3b69 commit 8d2216f

29 files changed

Lines changed: 772 additions & 330 deletions

src/Controllers/CommentsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function store(Request $request)
194194
}
195195

196196
DB::commit();
197-
event(new CommentCreated(Models\Comment::find($comment->id), $request));
197+
event(new CommentCreated(clone $comment, $request));
198198

199199
session()->flash('status', trans('panichd::lang.comment-has-been-added-ok'));
200200

src/Controllers/ConfigurationsController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function index()
2727
'email.template', 'email.owner.newticket.template', 'email.header', 'email.signoff', 'email.signature', 'email.dashboard',
2828
'email.google_plus_link', 'email.facebook_link', 'email.twitter_link', 'email.footer', 'email.footer_link',
2929
'email.color_body_bg', 'email.color_header_bg', 'email.color_content_bg', 'email.color_footer_bg',
30-
'email.color_button_bg', 'email.account.name', 'email.account.mailbox' ];
30+
'email.color_button_bg', 'email.account.name', 'email.account.mailbox', 'custom_recipients' ];
3131
$tickets_section = ['default_priority_id', 'default_status_id', 'default_close_status_id', 'default_reopen_status_id',
3232
'subject_content_column', 'paginate_items', 'attachments_ticket_max_size', 'attachments_ticket_max_files_num', 'attachments_mimes',
3333
'attachments_path', 'thumbnails_path', 'oldest_year', 'user_route', 'html_replacements', 'list_text_max_length', 'use_default_status_id', 'newest_list_reload_seconds'];
@@ -135,7 +135,7 @@ public function update(Request $request, $id)
135135
// refresh cached settings
136136
\Cache::forget('panichd::settings');
137137
\Cache::forget('panichd::settings.'.$configuration->slug);
138-
138+
139139
return redirect()->action('\PanicHD\PanicHD\Controllers\ConfigurationsController@index');
140-
}
141-
}
140+
}
141+
}

src/Controllers/NotificationsController.php

Lines changed: 153 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PanicHD\PanicHD\Helpers\LaravelVersion;
99
use PanicHD\PanicHD\Models\Category;
1010
use PanicHD\PanicHD\Models\Comment;
11+
use PanicHD\PanicHD\Models\CommentNotification;
12+
use PanicHD\PanicHD\Models\Member;
1113
use PanicHD\PanicHD\Models\Setting;
1214
use PanicHD\PanicHD\Models\Ticket;
1315

@@ -143,26 +145,87 @@ public function newComment(Comment $comment, $request)
143145
'notification_type' => $comment->type
144146
];
145147

146-
// Notificate assigned agent
147-
$a_to = $this->defaultRecipients($ticket, $notification_owner, $subject, $template);
148-
149-
// Notificate ticket owner
150-
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') 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;
155-
}
156-
157-
$a_to[] = [
158-
'recipient' => $ticket->owner,
159-
'subject' => $subject,
160-
'template' => $template
161-
];
162-
}
148+
$member = Member::find(auth()->user()->id);
149+
$category_level = $member->levelInCategory($ticket->category_id);
150+
$permission_level = ($member->currentLevel() > 1 and $category_level > 1) ? $category_level : 1;
151+
152+
$a_recipients = [];
153+
154+
if($permission_level < 2 or !Setting::grab('custom_recipients')){
155+
// Add recipients for each default one (Ticket agent)
156+
foreach ($this->defaultRecipients($ticket, $notification_owner, $subject, $template) as $default){
157+
$a_recipients[] = $default['recipient']->id;
158+
}
159+
160+
// Add ticket owner
161+
if(!$ticket->hidden and !is_null($ticket->owner) and !in_array($ticket->owner->email, [$notification_owner->email, $ticket->agent->email])
162+
and ($comment->type == 'reply' or ($comment->type != 'reply' and $ticket->owner->levelInCategory($ticket->category->id) > 1))){
163+
164+
$a_recipients[] = $ticket->owner->id;
165+
}
166+
167+
foreach($ticket->comments()->whereIn('type', ['reply', 'note'])->with('owner', 'notifications')->get() as $comm){
168+
// Previous comment authors
169+
if ($comm->owner->id != $notification_owner->id and !in_array($comm->owner->id, $a_recipients)
170+
and ($comment->type == 'reply' or ($comment->type != 'reply' and $comm->owner->levelInCategory($ticket->category->id) > 1))){
171+
172+
$a_recipients[] = $comm->owner->id;
173+
}
174+
175+
// All previous comments recipients
176+
foreach ($comm->notifications as $notification){
177+
$recipient = Member::find($notification->member_id);
178+
if (count($recipient) == 1 and ($comment->type == 'reply' or ($comment->type != 'reply' and $recipient->levelInCategory($ticket->category->id) > 1))
179+
and $notification_owner->id!= $recipient->id and !in_array($recipient->id, $a_recipients)){
180+
181+
$a_recipients[] = $recipient->id;
182+
}
183+
}
184+
}
185+
}
186+
187+
if($permission_level > 1){
188+
if (Setting::grab('custom_recipients')){
189+
/* About $a_recipients
190+
* $comment->a_recipients come from embedded comments i TicketsController
191+
* $request recipients come from a comment modal in Ticket card
192+
*/
193+
$a_recipients = isset($comment->a_recipients) ? $comment->a_recipients : ($comment->type == 'note' ? $request->note_recipients : $request->reply_recipients);
194+
}
195+
196+
if (count($a_recipients) > 0){
197+
if ($request->has('add_in_user_notification_text') or (isset($comment->add_in_user_notification_text))){
198+
// Element in request comes from Comment modal
199+
// $comment property comes from an embedded comment when editing or creating a ticket
200+
$data['add_in_user_notification_text'] = true;
201+
}
202+
}
203+
}
204+
205+
if (count($a_recipients) > 0){
206+
foreach($a_recipients as $member_id){
207+
$recipient = Member::find($member_id);
208+
if (count($recipient) == 1){
209+
// Register the notified email
210+
$notification = CommentNotification::create([
211+
'comment_id' => $comment->id,
212+
'name' => $recipient->name,
213+
'email' => $recipient->email,
214+
'member_id' => $member_id
215+
]);
216+
217+
// Add email to actual mail recipients
218+
$a_to[] = [
219+
'recipient' => $recipient,
220+
'subject' => $subject,
221+
'template' => $template
222+
];
223+
}
224+
}
225+
}
163226

164227
// Send notifications
165-
$this->sendNotification($a_to, $data);
228+
if(isset($a_to)) $this->sendNotification($a_to, $data);
166229
}
167230
}
168231

@@ -182,27 +245,21 @@ public function commentUpdate(Comment $original_comment, Comment $comment)
182245
'notification_owner' => serialize($notification_owner)
183246
];
184247

185-
$a_to=[];
186-
187-
// Email to ticket->agent
188-
if ($ticket->agent->email != $notification_owner->email){
189-
$a_to[] = [
190-
'recipient' => $ticket->agent,
191-
'subject' => $subject,
192-
'template' => $template
193-
];
194-
}
195-
196-
// Email to comment->owner
197-
if (!$ticket->hidden and !in_array($comment->owner->email, [$notification_owner->email, $ticket->agent->email])){
198-
$a_to[] = [
199-
'recipient' => $comment->owner,
200-
'subject' => $subject,
201-
'template' => $template
202-
];
203-
}
204-
205-
$this->sendNotification($a_to, $data);
248+
// Send notification to all comment notified users
249+
foreach ($comment->notifications as $notification){
250+
$recipient = Member::find($notification->member_id);
251+
if (count($recipient) == 1){
252+
if ($recipient->email != $notification_owner->email){
253+
$a_to[] = [
254+
'recipient' => $recipient,
255+
'subject' => $subject,
256+
'template' => $template
257+
];
258+
}
259+
}
260+
}
261+
262+
if(isset($a_to)) $this->sendNotification($a_to, $data);
206263
}
207264
}
208265

@@ -212,8 +269,8 @@ public function commentUpdate(Comment $original_comment, Comment $comment)
212269
*/
213270
public function notificationResend(Request $request)
214271
{
215-
$comment=Comment::findOrFail($request->input('comment_id'));
216-
$ticket=$comment->ticket;
272+
$comment = Comment::with('notifications')->findOrFail($request->input('comment_id'));
273+
$ticket = $comment->ticket;
217274
$notification_owner = $comment->user;
218275
$template = 'panichd::emails.new_comment';
219276
$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;
@@ -224,30 +281,66 @@ public function notificationResend(Request $request)
224281
'notification_type' => $comment->type
225282
];
226283

227-
$a_to = [];
228-
229-
if ($request->has('to_agent')){
230-
$a_to[] = [
231-
'recipient' => $ticket->agent,
232-
'subject' => $subject,
233-
'template' => $template
234-
];
235-
}
236-
if (!$ticket->hidden and $request->has('to_owner') and (!$request->has('to_agent') or ($request->has('to_agent') and $ticket->owner->email!=$ticket->agent->email))){
237-
$a_to[] = [
238-
'recipient' => $ticket->owner,
239-
'subject' => $subject,
240-
'template' => $template
241-
];
242-
}
284+
if ($request->has('recipients')){
285+
286+
foreach($request->recipients as $recipient_key){
287+
// Search by member_id or by email address
288+
$recipient = Member::where('id', $recipient_key)->orWhere('email', $recipient_key)->first();
289+
290+
if (!is_null($recipient)){
291+
$a_to[] = [
292+
'recipient' => $recipient,
293+
'subject' => $subject,
294+
'template' => $template
295+
];
296+
297+
$notification = $comment->notifications()->where('member_id', $recipient->id)->first();
298+
if (is_null($notification)){
299+
// Register the notified email
300+
$notification = CommentNotification::create([
301+
'comment_id' => $comment->id,
302+
'name' => $recipient->name,
303+
'email' => $recipient->email,
304+
'member_id' => $recipient->id
305+
]);
306+
307+
if ($comment->notifications->count() == 0){
308+
// No previous registered notifications
309+
if ($ticket->agent->id == $comment->owner->id){
310+
// Message from agent to owner, so we register the non registered past owner notification
311+
$notification = CommentNotification::create([
312+
'comment_id' => $comment->id,
313+
'name' => $ticket->owner->name,
314+
'email' => $ticket->owner->email,
315+
'member_id' => $ticket->owner->id
316+
]);
317+
}elseif($ticket->owner->id == $comment->owner->id){
318+
// Message from owner to agent, so we register the non registered past agent notification
319+
$notification = CommentNotification::create([
320+
'comment_id' => $comment->id,
321+
'name' => $ticket->agent->name,
322+
'email' => $ticket->agent->email,
323+
'member_id' => $ticket->agent->id
324+
]);
325+
}
326+
}
327+
}
328+
}
329+
330+
331+
}
332+
}
243333

244334
// Load $this->category for sendNotification
245335
$this->category = $ticket->category;
246336

247337
// Send notifications
248-
$this->sendNotification($a_to, $data);
249-
250-
return back()->with('status','Notificacions reenviades correctament');
338+
if(isset($a_to)){
339+
$this->sendNotification($a_to, $data);
340+
return back()->with('status', trans('panichd::lang.notification-resend-confirmation'));
341+
}else{
342+
return back()->with('warning', trans('panichd::lang.notification-resend-no-recipients'));
343+
}
251344
}
252345

253346

0 commit comments

Comments
 (0)