Skip to content

Commit 659e2c7

Browse files
committed
Member may comment any ticket where he has received notifications from
1 parent aad9304 commit 659e2c7

4 files changed

Lines changed: 53 additions & 35 deletions

File tree

src/Controllers/NotificationsController.php

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Middleware/UserAccessMiddleware.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public function handle($request, Closure $next)
4848
}
4949
}
5050

51-
if ($this->mod_route_prefix == 'ticket') {
52-
// Notified users may access the ticket
53-
if ($ticket->commentNotifications()->where('member_id', $member->id)->count() > 0){
54-
return $next($request);
55-
}
51+
// Notified users may access the ticket
52+
if ($ticket->commentNotifications()->where('member_id', $member->id)->count() > 0){
53+
return $next($request);
54+
}
5655

56+
if ($this->mod_route_prefix == 'ticket') {
5757
// Tickets from users in a visible ticketit_department value for current user
5858
if (in_array($ticket->user_id, $member->getMyNoticesUsers())){
5959
return $next($request);

src/Models/Member.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,22 @@ public static function canCloseTicket($id)
250250
/**
251251
* Check if user can make a comment on a ticket.
252252
*
253-
* @param int $id ticket id
253+
* @param $ticket instance of PanicHD\PanicHD\Models\Ticket
254254
*
255255
* @return bool
256256
*/
257-
public static function canCommentTicket($id)
257+
public static function canCommentTicket($ticket)
258258
{
259259
if (!auth()->check()) return false;
260260

261-
if (\PanicHDMember::canManageTicket($id)){
262-
return true;
263-
}else{
264-
return \PanicHDMember::isTicketOwner($id);
261+
if (\PanicHDMember::canManageTicket($ticket->id)
262+
or \PanicHDMember::isTicketOwner($ticket->id)
263+
or $ticket->commentNotifications()->where('member_id', auth()->user()->id)->count() > 0){
264+
265+
return true;
265266
}
267+
268+
return false;
266269
}
267270

268271
/**

src/Views/tickets/partials/comments/index.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@section('content')
22
@if(isset($ticket))
3-
@if($u->canCommentTicket($ticket->id) || ( !$comments->isEmpty() && $ticket->comments()->forLevel(1)->count() ) )
3+
@if($u->canCommentTicket($ticket) || ( !$comments->isEmpty() && $ticket->comments()->forLevel(1)->count() ) )
44
<div style="margin-top: 2em;">
55
<h3 style="margin-top: 0em;">{{ trans('panichd::lang.comments') }}
6-
@if ($menu == 'show' && $u->canCommentTicket($ticket->id))
6+
@if ($menu == 'show' && $u->canCommentTicket($ticket))
77
<button type="button" class="btn btn-light btn-default" data-toggle="modal" data-target="#modal-comment-new" data-add-comment="{{ $ticket->hidden ? 'no' : 'yes' }}">{{ $ticket->hidden ? trans('panichd::lang.show-ticket-add-note') : trans('panichd::lang.show-ticket-add-comment') }}</button>
88
@endif
99
</h3>

0 commit comments

Comments
 (0)