| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Comment; |
| 6 |
use FluentBoards\App\Models\Task; |
| 7 |
use FluentBoards\App\Models\TaskActivity; |
| 8 |
|
| 9 |
class CommentService |
| 10 |
{ |
| 11 |
public function getComments($id, $per_page, $filter) |
| 12 |
{ |
| 13 |
$task = Task::findOrFail($id); |
| 14 |
|
| 15 |
$commentsQuery = $task->comments()->whereNull('parent_id') |
| 16 |
->with(['user']); |
| 17 |
|
| 18 |
if ($filter == 'oldest') { |
| 19 |
$commentsQuery = $commentsQuery->oldest(); |
| 20 |
} else { // latest or newest |
| 21 |
$commentsQuery = $commentsQuery->latest(); |
| 22 |
} |
| 23 |
$comments = $commentsQuery->paginate($per_page); |
| 24 |
|
| 25 |
foreach ($comments as $comment) { |
| 26 |
$comment->replies = $this->getReplies($comment); |
| 27 |
$comment->replies_count = count($comment->replies); |
| 28 |
} |
| 29 |
|
| 30 |
return $comments; |
| 31 |
} |
| 32 |
|
| 33 |
public function getTotal($id) |
| 34 |
{ |
| 35 |
$task = Task::findOrFail($id); |
| 36 |
$totalComment = Comment::where('task_id', $task->id) |
| 37 |
->type('comment') |
| 38 |
->count(); |
| 39 |
$totalReply = Comment::where('task_id', $task->id) |
| 40 |
->type('reply') |
| 41 |
->count(); |
| 42 |
|
| 43 |
return $totalComment + $totalReply; |
| 44 |
} |
| 45 |
|
| 46 |
public function getReplies($comment) |
| 47 |
{ |
| 48 |
$replies = Comment::where('parent_id', $comment->id)->with(['user'])->get(); |
| 49 |
return $replies; |
| 50 |
} |
| 51 |
|
| 52 |
public function create($commentData, $id) |
| 53 |
{ |
| 54 |
$comment = Comment::create($commentData); |
| 55 |
do_action('fluent_boards/task_comment_added', $comment); |
| 56 |
return $comment; |
| 57 |
} |
| 58 |
|
| 59 |
public function update($commentData, $comment_id) |
| 60 |
{ |
| 61 |
$comment = Comment::findOrFail($comment_id); |
| 62 |
|
| 63 |
if ($comment->created_by != get_current_user_id()) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
$oldComment = $comment->description; |
| 68 |
$comment->description = $commentData['description']; |
| 69 |
$comment->save(); |
| 70 |
do_action('fluent_boards/task_comment_updated', $comment->task_id, $oldComment, $comment->description); |
| 71 |
|
| 72 |
return $comment; |
| 73 |
} |
| 74 |
|
| 75 |
public function delete($comment_id) |
| 76 |
{ |
| 77 |
$comment = Comment::findOrFail($comment_id); |
| 78 |
$taskId = $comment->task_id; |
| 79 |
|
| 80 |
if ($comment->created_by != get_current_user_id()) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
$commentDescription = strip_tags($comment->description); |
| 85 |
|
| 86 |
$deleted = $comment->delete(); |
| 87 |
|
| 88 |
if ($deleted) { |
| 89 |
$this->relatedReplyDelete($comment_id); |
| 90 |
|
| 91 |
$task = Task::findOrFail($taskId); |
| 92 |
$task->comments_count = $task->comments_count - 1; |
| 93 |
$task->save(); |
| 94 |
} |
| 95 |
|
| 96 |
do_action('fluent_boards/task_comment_deleted', $taskId, $commentDescription); |
| 97 |
} |
| 98 |
|
| 99 |
public function relatedReplyDelete($comment_id) |
| 100 |
{ |
| 101 |
$replies = Comment::where('parent_id', $comment_id) |
| 102 |
->type('reply') |
| 103 |
->get(); |
| 104 |
foreach ($replies as $reply) { |
| 105 |
$reply->delete(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function updateReply($replyData, $id) |
| 110 |
{ |
| 111 |
$reply = Comment::findOrFail($id); |
| 112 |
|
| 113 |
if ($reply->created_by != get_current_user_id()) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
$oldReply = $reply->description; |
| 118 |
$reply->description = $replyData['description']; |
| 119 |
$reply->save(); |
| 120 |
// do_action('fluent_boards/task_comment_updated', $comment->task_id, $oldComment, $comment->description); |
| 121 |
|
| 122 |
return $reply; |
| 123 |
} |
| 124 |
|
| 125 |
public function deleteReply($id) |
| 126 |
{ |
| 127 |
$reply = Comment::findOrFail($id); |
| 128 |
// $taskId = $reply->task_id; |
| 129 |
|
| 130 |
if ($reply->created_by != get_current_user_id()) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
$reply->delete(); |
| 135 |
|
| 136 |
// do_action('fluent_boards/task_comment_deleted', $taskId); |
| 137 |
} |
| 138 |
} |
| 139 |
|