148 lines
4.2 KiB
PHP
148 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Comment;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CommentModerationController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of comments for moderation.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$query = Comment::with(['user', 'product', 'moderator']);
|
|
|
|
// Filter by status
|
|
if ($request->filled('status')) {
|
|
$query->where('status', $request->status);
|
|
} else {
|
|
// Default to showing pending comments
|
|
$query->where('status', 'pending');
|
|
}
|
|
|
|
// Search in comment body or user name
|
|
if ($request->filled('search')) {
|
|
$search = $request->search;
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('body', 'like', "%{$search}%")
|
|
->orWhereHas('user', function ($userQuery) use ($search) {
|
|
$userQuery->where('name', 'like', "%{$search}%")
|
|
->orWhere('email', 'like', "%{$search}%");
|
|
})
|
|
->orWhereHas('product', function ($productQuery) use ($search) {
|
|
$productQuery->where('name', 'like', "%{$search}%");
|
|
});
|
|
});
|
|
}
|
|
|
|
// Get counts for tabs
|
|
$pendingCount = Comment::where('status', 'pending')->count();
|
|
$approvedCount = Comment::where('status', 'approved')->count();
|
|
$rejectedCount = Comment::where('status', 'rejected')->count();
|
|
|
|
$comments = $query->latest()->paginate(20);
|
|
|
|
return view('moderation.comments.index', compact(
|
|
'comments',
|
|
'pendingCount',
|
|
'approvedCount',
|
|
'rejectedCount'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing a comment.
|
|
*/
|
|
public function edit(Comment $comment)
|
|
{
|
|
$comment->load(['user', 'product', 'moderator']);
|
|
|
|
return view('moderation.comments.edit', compact('comment'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified comment.
|
|
*/
|
|
public function update(Request $request, Comment $comment)
|
|
{
|
|
$validated = $request->validate([
|
|
'body' => ['required', 'string', 'max:1000'],
|
|
]);
|
|
|
|
$comment->update($validated);
|
|
|
|
return redirect()->route('moderation.comments.index')
|
|
->with('success', 'Comment updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Approve a comment.
|
|
*/
|
|
public function approve(Request $request, Comment $comment)
|
|
{
|
|
$validated = $request->validate([
|
|
'moderation_note' => ['nullable', 'string', 'max:500'],
|
|
]);
|
|
|
|
$comment->update([
|
|
'status' => 'approved',
|
|
'moderated_by' => auth()->id(),
|
|
'moderated_at' => now(),
|
|
'moderation_note' => $validated['moderation_note'] ?? null,
|
|
]);
|
|
|
|
return back()->with('success', 'Comment approved successfully.');
|
|
}
|
|
|
|
/**
|
|
* Reject a comment.
|
|
*/
|
|
public function reject(Request $request, Comment $comment)
|
|
{
|
|
$validated = $request->validate([
|
|
'moderation_note' => ['nullable', 'string', 'max:500'],
|
|
]);
|
|
|
|
$comment->update([
|
|
'status' => 'rejected',
|
|
'moderated_by' => auth()->id(),
|
|
'moderated_at' => now(),
|
|
'moderation_note' => $validated['moderation_note'] ?? null,
|
|
]);
|
|
|
|
return back()->with('success', 'Comment rejected successfully.');
|
|
}
|
|
|
|
/**
|
|
* Set comment back to pending status.
|
|
*/
|
|
public function setPending(Request $request, Comment $comment)
|
|
{
|
|
$validated = $request->validate([
|
|
'moderation_note' => ['nullable', 'string', 'max:500'],
|
|
]);
|
|
|
|
$comment->update([
|
|
'status' => 'pending',
|
|
'moderated_by' => auth()->id(),
|
|
'moderated_at' => now(),
|
|
'moderation_note' => $validated['moderation_note'] ?? null,
|
|
]);
|
|
|
|
return back()->with('success', 'Comment status changed to pending review.');
|
|
}
|
|
|
|
/**
|
|
* Delete a comment.
|
|
*/
|
|
public function destroy(Comment $comment)
|
|
{
|
|
$comment->delete();
|
|
|
|
return redirect()->route('moderation.comments.index')
|
|
->with('success', 'Comment deleted successfully.');
|
|
}
|
|
}
|