91 lines
2.2 KiB
PHP
91 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Maintenance;
|
|
|
|
use App\Models\Host;
|
|
use Livewire\Component;
|
|
use App\Models\Maintenance;
|
|
|
|
class Form extends Component
|
|
{
|
|
public $model;
|
|
public string $name = "";
|
|
public string $description = "";
|
|
public string $schedule = "";
|
|
|
|
public $action = 'store';
|
|
|
|
public $hosts = [];
|
|
public $hosts_available = [];
|
|
|
|
|
|
protected function rules()
|
|
{
|
|
return [
|
|
'name' => 'required',
|
|
'description' => 'required',
|
|
'schedule' => 'required',
|
|
];
|
|
}
|
|
|
|
public function mount($model = null)
|
|
{
|
|
$this->hosts_available = Host::all()->pluck('hostname', 'id')->toArray();
|
|
if (!empty($model)) {
|
|
$maintenance = Maintenance::find($model);
|
|
|
|
$this->model = $model;
|
|
|
|
$this->name = $maintenance->name;
|
|
$this->description = $maintenance->description;
|
|
$this->schedule = $maintenance->schedule;
|
|
$this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray();
|
|
|
|
$this->action = 'update';
|
|
}
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$validatedData = $this->validate();
|
|
$maintenance = Maintenance::create($validatedData);
|
|
$hosts = Host::whereIn('id', $this->hosts)->get();
|
|
foreach ($hosts as $key => $host) {
|
|
$maintenance->hosts()->attach($host);
|
|
}
|
|
$this->dispatch('closeModal');
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$validatedData = $this->validate();
|
|
$maintenance = Maintenance::find($this->model);
|
|
$maintenance->hosts()->whereNotIn('host_id', $this->hosts)->delete();
|
|
|
|
if (!empty($maintenance)) {
|
|
$maintenance->update($validatedData);
|
|
$hosts = Host::whereIn('id', $this->hosts)->whereNotIn('id', $maintenance->hosts()->pluck('hosts.id')->toArray())->get();
|
|
foreach ($hosts as $key => $host) {
|
|
$maintenance->hosts()->attach($host);
|
|
}
|
|
}
|
|
|
|
$this->dispatch('closeModal');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.maintenance.form');
|
|
}
|
|
|
|
public function addHost()
|
|
{
|
|
$this->hosts[] = [];
|
|
}
|
|
|
|
public function removeHost($id)
|
|
{
|
|
unset($this->hosts[$id]);
|
|
}
|
|
}
|