File: /home/armgroup/libs/app/Http/Livewire/Frontend/Product/Comment/Form.php
<?php
namespace App\Http\Livewire\Frontend\Product\Comment;
use App\Enums\ScoreItems;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Comment;
use App\Models\Product;
use App\Models\Score;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class Form extends Component
{
use ConvertEmptyStringsToNull, General;
public Product $product;
public $comment;
public $name;
public $email;
public $positive_points = [''];
public $negative_points = [''];
public $items = [
ScoreItems::ITEM1 => null,
ScoreItems::ITEM2 => null,
ScoreItems::ITEM3 => null,
ScoreItems::ITEM4 => null,
ScoreItems::ITEM5 => null,
];
public function mount()
{
if (auth()->check()) {
$this->name = auth()->user()->name;
$this->email = auth()->user()->email;
}
}
protected function rules()
{
return [
'name' => 'required|string|max:200',
'email' => 'nullable|email|max:200',
'comment' => 'nullable|string|max:60000|min:5',
'positive_points' => 'nullable|array',
'negative_points' => 'nullable|array',
'positive_points.*' => 'nullable|string|max:100',
'negative_points.*' => 'nullable|string|max:100',
'items' => 'nullable|array',
'items.*' => 'nullable|integer|between:1,5',
];
}
public function render()
{
return view('livewire.frontend.product.comment.form');
}
public function save()
{
$this->validate();
$this->positive_points = array_filter($this->positive_points) ?: null;
$this->negative_points = array_filter($this->negative_points) ?: null;
if (! $this->positive_points && ! $this->negative_points && ! $this->comment) {
$this->addError('general', 'حداقل یکی از موارد دیدگاه، نقاط قوت یا نقاط ضعف باید تکمیل شود.');
$this->positive_points = [''];
$this->negative_points = [''];
return;
}
DB::transaction(function () {
$comment = Comment::create([
'user_id' => auth()->check() ? auth()->id() : null,
'parent_id' => null,
'commentable_id' => $this->product->id,
'commentable_type' => Product::class,
'name' => $this->name,
'email' => $this->email,
'comment' => $this->comment,
'positive_points' => $this->positive_points,
'negative_points' => $this->negative_points,
]);
$items = array_filter($this->items);
if (count($items)) {
Score::create([
'user_id' => auth()->check() ? auth()->id() : null,
'scorable_id' => $comment->id,
'scorable_type' => Comment::class,
'score' => round(collect($items)->avg()),
'items' => $items,
]);
}
});
$this->comment = null;
$this->positive_points = [''];
$this->negative_points = [''];
$this->items = [
ScoreItems::ITEM1 => null,
ScoreItems::ITEM2 => null,
ScoreItems::ITEM3 => null,
ScoreItems::ITEM4 => null,
ScoreItems::ITEM5 => null,
];
$this->doneMessage('نظر شما با موفقیت ثبت شد');
return redirect()->route('products.show', $this->product);
}
public function addPositive()
{
array_push($this->positive_points, '');
}
public function deletePositive($index)
{
if (count($this->positive_points) > 1) {
unset($this->positive_points[$index]);
}
}
public function addNegative()
{
array_push($this->negative_points, '');
}
public function deleteNegative($index)
{
if (count($this->negative_points) > 1) {
unset($this->negative_points[$index]);
}
}
}