cms-laravel/app/Livewire/PhotoSlider/PhotoSliderCreate.php

181 lines
6.1 KiB
PHP

<?php
namespace App\Livewire\PhotoSlider;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Http;
use Carbon\Carbon;
class PhotoSliderCreate extends Component
{
use WithFileUploads;
public $loading = false;
public $promotionsOptions = [];
public $photoSliderLimit = false;
public $dateStartEnd = null;
public $form = [
'promotion_uuid' => '',
'title' => '',
'description' => '',
'image' => null,
'date_start' => '',
'date_end' => '',
'start_time' => '',
'end_time' => '',
];
public $errors = [];
public function mount()
{
$this->checkLimit();
$this->fetchPromotions();
}
public function checkLimit()
{
try {
$response = Http::get('https://api.example.com/photoSliderCount');
$this->photoSliderLimit = $response->json()['data'] >= 10;
} catch (\Exception $e) {
$this->photoSliderLimit = true;
session()->flash('error', 'Failed to check photo slider limit: ' . $e->getMessage());
}
}
public function fetchPromotions()
{
try {
$response = Http::get('https://api.example.com/getPromotions');
$promotions = $response->json()['data'] ?? [];
$this->promotionsOptions = array_map(function ($item) {
return [
'label' => $item['title'],
'value' => $item['promotion_uuid'],
'date' => [
'dateStart' => $item['date_start'],
'dateEnd' => $item['date_end'],
],
];
}, $promotions);
} catch (\Exception $e) {
session()->flash('error', 'Failed to load promotions: ' . $e->getMessage());
}
}
public function fetchDate($id)
{
foreach ($this->promotionsOptions as $option) {
if ($option['value'] == $id) {
$this->dateStartEnd = [
'date_start' => $option['date']['dateStart'],
'date_end' => $option['date']['dateEnd'],
];
break;
}
}
}
public function autoFillDetails($id)
{
try {
$response = Http::get("https://api.example.com/promotion/{$id}");
$data = $response->json()['data'] ?? [];
$this->form = array_merge($this->form, [
'title' => $data['title'] ?? '',
'description' => $data['description'] ?? '',
'image' => $data['image'] ?? '',
'date_start' => Carbon::parse($data['date_start'])->format('Y-m-d'),
'date_end' => Carbon::parse($data['date_end'])->format('Y-m-d'),
'start_time' => Carbon::parse($data['date_start'])->format('H:i'),
'end_time' => Carbon::parse($data['date_end'])->format('H:i'),
]);
} catch (\Exception $e) {
session()->flash('error', 'Failed to autofill details: ' . $e->getMessage());
}
}
public function submit()
{
$this->validateForm();
$this->loading = true;
try {
$formData = new \GuzzleHttp\Psr7\MultipartStream([
[
'name' => 'promotion_uuid',
'contents' => $this->form['promotion_uuid'] ?? '',
],
[
'name' => 'title',
'contents' => $this->form['title'] ?? '',
],
[
'name' => 'description',
'contents' => $this->form['description'] ?? '',
],
[
'name' => 'date_start',
'contents' => Carbon::parse($this->form['date_start'] . ' ' . $this->form['start_time'])->format('Y-m-d\TH:i:s'),
],
[
'name' => 'date_end',
'contents' => Carbon::parse($this->form['date_end'] . ' ' . $this->form['end_time'])->format('Y-m-d\TH:i:s'),
],
[
'name' => 'image',
'contents' => $this->form['image'] ? fopen($this->form['image']->getPathname(), 'r') : '',
'filename' => $this->form['image'] ? $this->form['image']->getClientOriginalName() : '',
],
]);
$response = Http::withHeaders(['Content-Type' => 'multipart/form-data; boundary=' . $formData->getBoundary()])
->withBody($formData)
->post('https://api.example.com/photoSlider');
if ($response->successful()) {
session()->flash('success', 'New record added.');
$this->redirect(route('photo-slider.index'));
}
} catch (\Exception $e) {
session()->flash('error', 'Failed to create record: ' . $e->getMessage());
}
$this->loading = false;
}
public function validateForm()
{
$rules = [
'title' => 'required|max:128',
'image' => 'required',
'date_start' => 'required|date',
'date_end' => 'required|date',
'start_time' => 'required',
'end_time' => 'required',
];
$messages = [
'title.required' => 'Title is required!',
'title.max' => 'Maximum character is 128.',
'image.required' => 'Upload Image is required!',
'date_start.required' => 'Start Appearance Date is required!',
'date_end.required' => 'End Appearance Date is required!',
'start_time.required' => 'Start Time is required!',
'end_time.required' => 'End Time is required!',
];
$this->errors = [];
try {
$this->validate($rules, $messages);
} catch (\Exception $e) {
$this->errors = collect($e->errors())->flatten()->toArray();
throw $e;
}
}
public function render()
{
return view('livewire.photo-slider.photo-slider-create');
}
}