lk.zachem.info/app/Modules/Post/Http/Livewire/PostCreator.php

106 lines
2.4 KiB
PHP

<?php
namespace Modules\Post\Http\Livewire;
use Livewire\Component;
use Livewire\Attributes\Validate;
use Livewire\Attributes\On;
class PostCreator extends Component
{
public $categories = [];
protected function rules()
{
return [
];
}
protected function messages()
{
return [
];
}
public function mount()
{
}
public function updated($propertyName)
{
}
public function render()
{
return view(
'post::form.index'
);
}
public function rendered()
{
$this->dispatch('phoneInputAdded');
}
public function resetData()
{
$this->mount();
}
public function back()
{
$this->status = FormStatus::IN_PROCESS;
}
public function save()
{
$hasErrors = false;
foreach ($this->selectedObjects as $complexId=>$room) {
if ($this->createDeal($complexId, $room)) {
unset($this->selectedObjects[$complexId]);
} else {
$hasErrors = true;
}
}
if ($hasErrors) {
return $this->status = FormStatus::ERROR;
}
$this->status = FormStatus::SUCCESS;
}
private function createDeal($complexId, $room)
{
if (
!$deal = Deal::create([
'agent_id' => $this->agentId,
'complex_id' => $complexId,
'plan7_data' => (is_array($room) && array_key_exists('id', $room)) ? json_encode($room) : null
])
) {
return false;
}
foreach ($this->contacts as $contact) {
if (
!$newUser = Client::updateOrCreate(
['phone' => $contact['phones'][0]],
[
'name' => trim($contact['firstName'] . ' ' . $contact['secondName']),
'phone' => $contact['phones'][0]
]
)
) {
return false;
}
if (
!$dealClient = DealClients::firstOrCreate([
'deal_id' => $deal->id,
'client_id' => $newUser->id
])
) {
return false;
}
}
$this->dispatch('clientCreated');
return true;
}
}