File: //home/armgroup/libs/app/Http/Livewire/Admin/Banner/Edit.php
<?php
namespace App\Http\Livewire\Admin\Banner;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Banner;
use App\Models\BannerItem;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class Edit extends Component
{
use ConvertEmptyStringsToNull, General;
public Banner $banner;
public $items;
public $deleteIds = [];
protected $listeners = ['imageChanged'];
public function mount()
{
$this->items = $this->banner
->items()
->get([
'title',
'description',
'url',
'image',
'sort_order',
'id', 'banner_id',
'limitation',
'started_at',
'ended_at',
])->toArray();
}
protected function rules()
{
return [
'banner.name' => 'required|max:250|string',
'banner.width' => 'nullable|integer|min:0|max:90000',
'banner.height' => 'nullable|integer|min:0|max:90000',
'banner.status' => 'required|in:0,1',
'items.*.title' => 'nullable|max:250|string|required_without_all:items.*.url,items.*.image,items.*.description',
'items.*.url' => 'nullable|max:250|string|required_without_all:items.*.title,items.*.image,items.*.description',
'items.*.image' => 'nullable|max:250|string|required_without_all:items.*.title,items.*.url,items.*.description',
'items.*.description' => 'nullable|string|max:60000|required_without_all:items.*.title,items.*.url,items.*.image',
'items.*.sort_order' => 'nullable|integer|min:0|max:10000',
'items.*.limitation' => 'required|boolean',
'items.*.started_at' => 'nullable|date|required_if:items.*.limitation,1',
'items.*.ended_at' => 'nullable|date|after:items.*.started_at|required_if:items.*.limitation,1',
];
}
protected $validationAttributes = [
'items.*.title' => 'عنوان',
'items.*.url' => 'لینک',
'items.*.image' => 'تصویر',
'items.*.description' => 'توضیحات',
'items.*.sort_order' => 'ترتیب',
'items.*.limitation' => 'محدودیت',
'items.*.started_at' => 'تاریخ شروع',
'items.*.ended_at' => 'تاریخ پایان',
];
public function render()
{
return view('livewire.admin.banner.edit');
}
public function update()
{
$this->validate();
foreach ($this->items as $index => $item) {
if (! $item['limitation']) {
$this->items[$index]['started_at'] = $this->items[$index]['ended_at'] = null;
}
}
DB::transaction(function () {
$this->banner->update([
'name' => $this->banner->name,
'height' => $this->banner->height,
'width' => $this->banner->width,
'status' => $this->banner->status,
]);
if (count($this->deleteIds)) {
BannerItem::whereIn('id', $this->deleteIds)->delete();
}
if (count($this->items)) {
BannerItem::upsert(
$this->items,
['id', 'banner_id'],
['title', 'description', 'url', 'image', 'sort_order', 'limitation', 'started_at', 'ended_at']
);
} else {
$this->banner->items()->delete();
}
saveActivity("بنر {$this->banner->name} ویرایش شد", $this->banner);
$this->doneMessage("بنر {$this->banner->name} با موفقیت ویرایش شد");
});
return redirect()->route('admin.banners.index');
}
public function addItem()
{
array_push($this->items, [
'id' => null,
'banner_id' => $this->banner->id,
'title' => null,
'description' => null,
'url' => null,
'image' => null,
'sort_order' => null,
'limitation' => 0,
'started_at' => null,
'ended_at' => null,
]);
$this->emit('addedItem', count($this->items) - 1);
}
public function removeItem($index)
{
if ($this->items[$index]['id']) {
$this->deleteIds[] = $this->items[$index]['id'];
}
unset($this->items[$index]);
}
public function imageChanged($index, $imagePath)
{
$this->items[$index]['image'] = ltrim(parse_url($imagePath, PHP_URL_PATH), '/');
}
}