File: /home/armgroup/libs/app/Http/Controllers/Admin/CommentController.php
<?php
namespace App\Http\Controllers\Admin;
use App\DataTables\CommentsDataTable;
use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\Product;
use App\Models\Score;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class CommentController extends Controller
{
public function __construct()
{
$this->middleware('permission:manage comments');
}
public function index(CommentsDataTable $dataTable)
{
$types = Comment::TYPE;
return $dataTable->render('admin.comments.index', compact('types'));
}
public function show(Comment $comment)
{
abort_if($this->canNotAccessComment($comment), 403);
$comment->load('commentable', 'parent');
if (! $comment->checked) {
$comment->update(['checked' => true]);
}
return view('admin.comments.show', compact('comment'));
}
public function update(Comment $comment, Request $request)
{
abort_if($this->canNotAccessComment($comment), 403);
$request->validate(['status' => 'required|min:0|max:'.(count(Comment::STATUS_TRANSLATE) - 1)]);
$comment->update(['status' => $request->status]);
if ($comment->commentable_type == Product::class) {
$product = $comment->commentable->load('approvedComments');
$rate = $numberOfParticipants = 5;
if (count($product->approvedComments)) {
$q = Score::query()
->where('scorable_type', Comment::class)
->whereIn('scorable_id', $product->approvedComments->pluck('id')->toArray())
->get();
$rate = round($q->avg('score'), 1);
$numberOfParticipants = $q->count();
}
$product->update([
'score' => $rate,
'number_of_participants' => $numberOfParticipants,
]);
}
saveActivity("نظر {$comment->name} ویرایش شد", $comment);
$this->doneMessage("نظر {$comment->name} با موفقیت ویرایش شد.");
return view('admin.comments.show', compact('comment'));
}
public function destroy(Comment $comment)
{
abort_if($this->canNotAccessComment($comment), 403);
$comment->delete();
if ($comment->commentable_type == Product::class) {
$product = $comment->commentable->load('approvedComments');
$rate = $numberOfParticipants = 5;
if (count($product->approvedComments)) {
$q = Score::query()
->where('scorable_type', Comment::class)
->whereIn('scorable_id', $product->approvedComments->pluck('id')->toArray())
->get();
$rate = round($q->avg('score'), 1);
$numberOfParticipants = $q->count();
}
$product->update([
'score' => $rate,
'number_of_participants' => $numberOfParticipants,
]);
}
saveActivity("نظر {$comment->name} حذف شد", $comment);
$this->doneMessage("نظر {$comment->name} با موفقیت حذف شد.");
return redirect()->route('admin.comments.index');
}
public function reply(Comment $comment, Request $request)
{
abort_if($this->canNotAccessComment($comment), 403);
$validator = Validator::make($request->all(), [
'name' => 'sometimes|required|string|max:250',
'reply' => 'required|string|max:60000',
]);
if ($validator->fails()) {
$this->doneMessage($validator->errors()->first(), 'error');
return back()->withInput();
}
DB::transaction(function () use ($request, $comment) {
if ($request->has('name')) {
auth()->user()->update(['name' => $request->name]);
}
Comment::create([
'user_id' => auth()->id(),
'parent_id' => $comment->id,
'commentable_id' => $comment->commentable_id,
'commentable_type' => $comment->commentable_type,
'name' => auth()->user()->name,
'email' => auth()->user()->email,
'comment' => $request->reply,
'checked' => 1,
'status' => 1,
]);
saveActivity("به نظر {$comment->name} پاسخ داده شد", $comment);
});
$this->doneMessage('پاسخ شما با موفقیت ثبت شد');
return back();
}
private function canNotAccessComment(Comment $comment): bool
{
return auth()->user()->cannot('manage other users content') && $comment->commentable->user_id != auth()->id();
}
}