158 lines
4.8 KiB
PHP
158 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Forms;
|
|
|
|
use App\Enums\OutletStatus;
|
|
use App\Models\Outlet;
|
|
use App\Rules\PhoneNumber;
|
|
use App\Traits\WithMediaHandler;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
use Livewire\Form;
|
|
|
|
class OutletForm extends Form
|
|
{
|
|
use WithMediaHandler;
|
|
|
|
public ?Outlet $outlet;
|
|
|
|
public string $name = '';
|
|
|
|
public string $phone_number = '';
|
|
|
|
public string $address = '';
|
|
|
|
public string $landmark = '';
|
|
|
|
public ?string $maps_url = null;
|
|
|
|
public array $opening_hours = [
|
|
'senin' => ['open' => null, 'close' => null],
|
|
'selasa' => ['open' => null, 'close' => null],
|
|
'rabu' => ['open' => null, 'close' => null],
|
|
'kamis' => ['open' => null, 'close' => null],
|
|
'jumat' => ['open' => null, 'close' => null],
|
|
'sabtu' => ['open' => null, 'close' => null],
|
|
'minggu' => ['open' => null, 'close' => null],
|
|
];
|
|
|
|
public array $facilities = [''];
|
|
|
|
public string $opened_date = '';
|
|
|
|
public ?string $status = null;
|
|
|
|
public array $featured_image = [];
|
|
|
|
public array $images = [];
|
|
|
|
public function rules(): array
|
|
{
|
|
$days = ['senin', 'selasa', 'rabu', 'kamis', 'jumat', 'sabtu', 'minggu'];
|
|
|
|
$rules = [
|
|
'name' => ['required', 'string', 'max:100'],
|
|
'phone_number' => ['required', 'string', new PhoneNumber],
|
|
'address' => ['required', 'string'],
|
|
'landmark' => ['required', 'string', 'max:40'],
|
|
'maps_url' => [
|
|
'nullable',
|
|
'string',
|
|
'regex:/^(https?:\/\/)?(www\.)?(google\.[a-z.]+\/maps\/.+|goo\.gl\/maps\/.+|maps\.app\.goo\.gl\/.+)$/i',
|
|
],
|
|
'opening_hours' => ['required', 'array'],
|
|
'facilities' => ['required', 'array', 'min:1'],
|
|
'facilities.*' => ['required', 'string', 'max:20'],
|
|
'opened_date' => ['required', 'date'],
|
|
'status' => ['required', Rule::in(OutletStatus::cases())],
|
|
'featured_image' => ['required', 'array', 'max:1'],
|
|
'images' => ['required', 'array', 'max:5'],
|
|
];
|
|
|
|
foreach ($days as $day) {
|
|
$rules["opening_hours.$day"] = ['required', 'array'];
|
|
$rules["opening_hours.$day.open"] = ['nullable', 'date_format:H:i'];
|
|
$rules["opening_hours.$day.close"] = ['nullable', 'date_format:H:i'];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
public function validationAttributes(): array
|
|
{
|
|
return [
|
|
'name' => 'nama',
|
|
'phone_number' => 'nomor telepon',
|
|
'address' => 'alamat',
|
|
'landmark' => 'patokan',
|
|
'maps_url' => 'tautan google maps',
|
|
'opening_hours.*' => 'jam beroperasi',
|
|
'facilities.*' => 'fasilitas',
|
|
'opened_date' => 'tanggal buka',
|
|
'featured_image' => 'gambar utama',
|
|
'images' => 'gambar',
|
|
];
|
|
}
|
|
|
|
public function setOutlet(Outlet $outlet)
|
|
{
|
|
$this->outlet = $outlet;
|
|
|
|
$this->name = $outlet->name;
|
|
$this->phone_number = $outlet->phone_number;
|
|
$this->address = $outlet->address;
|
|
$this->landmark = $outlet->landmark;
|
|
$this->maps_url = $outlet->maps_url;
|
|
$this->opening_hours = $outlet->opening_hours;
|
|
$this->facilities = $outlet->facilities;
|
|
$this->opened_date = $outlet->opened_date;
|
|
$this->status = $outlet->status->value;
|
|
|
|
$this->featured_image = $this->mapMediaCollection($outlet->getMedia('featured_image'));
|
|
$this->images = $this->mapMediaCollection($outlet->getMedia('images'));
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$this->validate();
|
|
|
|
$data = $this->all();
|
|
|
|
if ($data['status'] == OutletStatus::TERMINATED) {
|
|
$data['closed_date'] = now();
|
|
} else {
|
|
$data['closed_date'] = null;
|
|
}
|
|
|
|
DB::transaction(function () use ($data) {
|
|
$outlet = Outlet::create($data);
|
|
|
|
$this->uploadMedia($this->featured_image, $outlet, 'featured_image');
|
|
$this->uploadMedia($this->images, $outlet, 'images');
|
|
});
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$this->validate();
|
|
|
|
$data = $this->all();
|
|
|
|
$this->syncMedia($data['featured_image'], $this->outlet, 'featured_image');
|
|
$this->syncMedia($data['images'], $this->outlet, 'images');
|
|
|
|
if ($data['status'] == OutletStatus::TERMINATED->value) {
|
|
$data['closed_date'] = now()->toDateString();
|
|
} else {
|
|
$data['closed_date'] = null;
|
|
}
|
|
|
|
DB::transaction(function () use ($data) {
|
|
$this->outlet->update($data);
|
|
|
|
$this->uploadMedia($this->featured_image, $this->outlet, 'featured_image');
|
|
$this->uploadMedia($this->images, $this->outlet, 'images');
|
|
});
|
|
}
|
|
}
|