|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Usamamuneerchaudhary\Commentify\Http\Livewire; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
7
|
|
|
use Livewire\Attributes\On; |
|
8
|
|
|
use Livewire\Component; |
|
9
|
|
|
use Livewire\WithPagination; |
|
10
|
|
|
use Usamamuneerchaudhary\Commentify\Events\CommentPosted; |
|
11
|
|
|
|
|
12
|
|
|
class Comments extends Component |
|
13
|
|
|
{ |
|
14
|
|
|
use AuthorizesRequests, WithPagination; |
|
15
|
|
|
|
|
16
|
|
|
public Model $model; |
|
17
|
|
|
|
|
18
|
|
|
public $users = []; |
|
19
|
|
|
|
|
20
|
|
|
public $showDropdown = false; |
|
21
|
|
|
|
|
22
|
|
|
public $sort = 'newest'; |
|
23
|
|
|
|
|
24
|
|
|
protected $numberOfPaginatorsRendered = []; |
|
25
|
|
|
|
|
26
|
|
|
public $newCommentState = [ |
|
27
|
|
|
'body' => '', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
protected $listeners = [ |
|
31
|
|
|
'refresh' => '$refresh', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
protected $validationAttributes = [ |
|
35
|
|
|
'newCommentState.body' => 'comment', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
public function mount(Model $model) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->model = $model; |
|
41
|
|
|
$this->sort = config('commentify.default_sort', 'newest'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function updatedSort(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->resetPage(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|null |
|
50
|
|
|
{ |
|
51
|
|
|
$requireApproval = config('commentify.require_approval', false); |
|
52
|
|
|
|
|
53
|
|
|
$query = $this->model |
|
54
|
|
|
->comments() |
|
55
|
|
|
->with([ |
|
56
|
|
|
'user', |
|
57
|
|
|
'likes', |
|
58
|
|
|
'children' => function ($query) use ($requireApproval) { |
|
59
|
|
|
$query->with([ |
|
60
|
|
|
'user', |
|
61
|
|
|
'likes', |
|
62
|
|
|
'children' => function ($nestedQuery) use ($requireApproval) { |
|
63
|
|
|
$nestedQuery->with('user', 'likes'); |
|
64
|
|
|
if ($requireApproval) { |
|
65
|
|
|
$nestedQuery->approved(); |
|
66
|
|
|
} |
|
67
|
|
|
}, |
|
68
|
|
|
]); |
|
69
|
|
|
if ($requireApproval) { |
|
70
|
|
|
$query->approved(); |
|
71
|
|
|
} |
|
72
|
|
|
}, |
|
73
|
|
|
]) |
|
74
|
|
|
->parent() |
|
75
|
|
|
->withCount('children'); |
|
76
|
|
|
|
|
77
|
|
|
// Filter by approval status if moderation is enabled |
|
78
|
|
|
if ($requireApproval) { |
|
79
|
|
|
$query->approved(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (config('commentify.enable_sorting', true)) { |
|
83
|
|
|
$query = match ($this->sort) { |
|
84
|
|
|
'oldest' => $query->oldest(), |
|
85
|
|
|
'most_liked' => $query->mostLiked(), |
|
86
|
|
|
'most_replied' => $query->mostReplied(), |
|
87
|
|
|
default => $query->newest(), |
|
88
|
|
|
}; |
|
89
|
|
|
} else { |
|
90
|
|
|
$query = $query->newest(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$comments = $query->paginate(config('commentify.pagination_count', 10)); |
|
94
|
|
|
|
|
95
|
|
|
return view('commentify::livewire.comments', [ |
|
96
|
|
|
'comments' => $comments, |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
#[On('refresh')] |
|
101
|
|
|
public function postComment(): void |
|
102
|
|
|
{ |
|
103
|
|
|
if (config('commentify.read_only')) { |
|
104
|
|
|
session()->flash('message', __('commentify::commentify.comments.read_only_message')); |
|
105
|
|
|
session()->flash('alertType', 'warning'); |
|
106
|
|
|
|
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Authorize using the CommentPolicy@create method |
|
111
|
|
|
$this->authorize('create', \Usamamuneerchaudhary\Commentify\Models\Comment::class); |
|
112
|
|
|
|
|
113
|
|
|
$this->validate([ |
|
114
|
|
|
'newCommentState.body' => 'required', |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
$comment = $this->model->comments()->make($this->newCommentState); |
|
118
|
|
|
$comment->user()->associate(auth()->user()); |
|
119
|
|
|
|
|
120
|
|
|
// Set approval status based on config |
|
121
|
|
|
$comment->is_approved = ! config('commentify.require_approval', false); |
|
122
|
|
|
|
|
123
|
|
|
$comment->save(); |
|
124
|
|
|
|
|
125
|
|
|
if (config('commentify.enable_notifications', false)) { |
|
126
|
|
|
event(new CommentPosted($comment)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$this->newCommentState = [ |
|
130
|
|
|
'body' => '', |
|
131
|
|
|
]; |
|
132
|
|
|
$this->users = []; |
|
133
|
|
|
$this->showDropdown = false; |
|
134
|
|
|
|
|
135
|
|
|
$this->resetPage(); |
|
136
|
|
|
session()->flash('message', 'Comment Posted Successfully!'); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|