Files
eatvio-infra/app/Http/Livewire/Consultant/RegistrationForm.php
2022-06-02 12:02:58 +00:00

159 lines
4.7 KiB
PHP

<?php
namespace App\Http\Livewire\Consultant;
use App\Models\User;
use App\Models\ConsultantEducation;
use App\Models\UserAttachment;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;
use Livewire\WithFileUploads;
class RegistrationForm extends Component
{
use WithFileUploads;
public User $user;
public $medicalEducations = [];
public $vkLink = "";
public $instagramLink = "";
public $facebookLink = "";
public $telegramLink = "";
public $hasMedicalEducation = false;
public $certPhotos = [];
public $tmpCertPhotos = [];
protected $rules = [
'medicalEducations.*.title' => 'required|string|min:6',
'medicalEducations.*.date' => 'required|date',
'vkLink' => 'required|url',
'instagramLink' => 'required|url',
'facebookLink' => 'required|url',
'telegramLink' => 'required|url',
'hasMedicalEducation' => 'required|boolean',
'tmpCertPhotos.*' => 'image|max:4024'
];
public function mount()
{
$this->user = Auth::user();
$consultant_request = $this->user->consultant_request;
if ($consultant_request) {
$this->medicalEducations = json_decode($consultant_request->completed_courses, true);
$this->vkLink = $consultant_request->vk_link;
$this->instagramLink = $consultant_request->insta_link;
$this->facebookLink= $consultant_request->fb_link;
$this->telegramLink = $consultant_request->tg_link;
$this->hasMedicalEducation = $consultant_request->has_medical_education;
}
$photos = $this->user->attachments()->certs()->get();
if ($photos) {
$this->certPhotos = collect($photos);
}
}
public function render()
{
return view('livewire.consultant.registration-form');
}
public function addEducation()
{
$this->validate();
$medicalEducations = $this->medicalEducations;
$this->resetExcept(
[
'user',
'vkLink',
'instagramLink',
'facebookLink',
'telegramLink',
'hasMedicalEducation',
'certPhotos',
'tmpCertPhotos'
]
);
$this->medicalEducations = $medicalEducations;
}
public function remove($index)
{
$filteredEducations = array_filter(
$this->medicalEducations,
function($val, $key) use ($index) {
return $key != $index;
},
ARRAY_FILTER_USE_BOTH
);
$this->medicalEducations = array_values($filteredEducations);
}
public function save()
{
$data = [
'user_id' => $this->user->id,
'completed_courses' => json_encode($this->medicalEducations),
'vk_link' => $this->vkLink,
'insta_link' => $this->instagramLink,
'fb_link' => $this->facebookLink,
'tg_link' => $this->telegramLink,
'has_medical_education' => $this->hasMedicalEducation,
'request_state' => 'created',
];
$this->validate();
ConsultantEducation::updateOrCreate(
[
'user_id' => $this->user->id
],
$data
);
/* $newConsultantRequest->save(); */
$attachmentGroup = "user_certs";
foreach($this->tmpCertPhotos as $photo) {
$curCert = $photo->storePublicly("public/" . $attachmentGroup . "/" . $this->user->id);
$fileData = [
'user_id' => $this->user->id,
'group' => $attachmentGroup,
'original_name' => $photo->getClientOriginalName(),
'size' => $photo->getSize(),
];
$newFile = UserAttachment::updateOrCreate(
$fileData,
[
'path' => preg_replace("/public\//xi", "/storage/", $curCert),
'specs' => json_encode(
[
'ext' => $photo->extension(),
'original_path' => $curCert,
]
)
]
);
$newFile->save();
$this->certPhotos = collect($this->user->attachments()->certs()->get());
$this->reset('tmpCertPhotos');
}
}
public function deleteImg($id)
{
$file = UserAttachment::find($id);
$fileSpecs = json_decode($file->specs, true);
Storage::delete($fileSpecs['original_path']);
$file->delete();
$this->certPhotos = $this->certPhotos->whereNotIn('id', [$id]);
}
}