LAR_Maintenance/app/Livewire/Maintenance/Form.php

131 lines
3.8 KiB
PHP
Raw Normal View History

2024-07-30 16:13:21 +00:00
<?php
2024-07-31 16:45:14 +00:00
2024-07-30 16:13:21 +00:00
namespace App\Livewire\Maintenance;
2024-07-31 16:45:14 +00:00
use App\Models\Host;
2024-07-30 16:13:21 +00:00
use Livewire\Component;
use App\Models\Maintenance;
2024-08-06 06:31:51 +00:00
use App\Models\MaintenanceTask;
2024-08-06 09:02:45 +00:00
use App\Models\Task;
2024-07-30 16:13:21 +00:00
class Form extends Component
{
public $model;
public string $name = "";
2024-07-31 16:45:14 +00:00
public string $description = "";
public string $schedule = "";
2024-07-30 16:13:21 +00:00
public $action = 'store';
2024-07-31 16:45:14 +00:00
public $hosts = [];
public $hosts_available = [];
2024-08-06 06:31:51 +00:00
public $hosts_tasks = [];
public $hosts_tasks_available = [];
2024-07-31 16:45:14 +00:00
2024-07-30 16:13:21 +00:00
protected function rules()
{
return [
'name' => 'required',
2024-07-31 16:45:14 +00:00
'description' => 'required',
'schedule' => 'required',
2024-07-30 16:13:21 +00:00
];
}
2024-07-31 16:45:14 +00:00
public function mount($model = null)
{
$this->hosts_available = Host::all()->pluck('hostname', 'id')->toArray();
2024-08-06 09:02:45 +00:00
$this->hosts_tasks_available = Task::all()->pluck('name', 'id')->toArray();
2024-08-06 06:31:51 +00:00
2024-07-30 16:13:21 +00:00
if (!empty($model)) {
$maintenance = Maintenance::find($model);
$this->model = $model;
$this->name = $maintenance->name;
2024-07-31 16:45:14 +00:00
$this->description = $maintenance->description;
$this->schedule = $maintenance->schedule;
2024-08-06 06:31:51 +00:00
2024-07-31 16:45:14 +00:00
$this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray();
2024-08-06 06:31:51 +00:00
foreach ($maintenance->tasks as $task) {
2024-08-06 09:02:45 +00:00
$this->hosts_tasks[$task->host_id][] = $task->task_id;
2024-08-06 06:31:51 +00:00
}
2024-07-30 16:13:21 +00:00
$this->action = 'update';
}
}
2024-08-07 05:52:07 +00:00
public function updatedHosts($value)
{
foreach ($this->hosts_tasks as $host_id => $tasks) {
if (in_array($host_id, $this->hosts)) {
continue;
}
unset($this->hosts_tasks[$host_id]);
}
}
2024-07-30 16:13:21 +00:00
public function store()
{
$validatedData = $this->validate();
2024-07-31 16:45:14 +00:00
$maintenance = Maintenance::create($validatedData);
$hosts = Host::whereIn('id', $this->hosts)->get();
foreach ($hosts as $key => $host) {
$maintenance->hosts()->attach($host);
2024-08-07 05:52:07 +00:00
$tasks = Task::whereIn('id', $this->hosts_tasks[$host->id])->get();
2024-08-06 15:02:14 +00:00
foreach ($tasks as $task) {
$maintenance->tasks()->create([
'task_id' => $task->id,
'host_id' => $host->id,
]);
}
2024-07-31 16:45:14 +00:00
}
2024-07-30 16:13:21 +00:00
$this->dispatch('closeModal');
}
public function update()
{
$validatedData = $this->validate();
$maintenance = Maintenance::find($this->model);
2024-07-31 16:45:14 +00:00
$maintenance->hosts()->whereNotIn('host_id', $this->hosts)->delete();
2024-08-06 09:02:45 +00:00
2024-07-30 16:13:21 +00:00
if (!empty($maintenance)) {
$maintenance->update($validatedData);
2024-07-31 16:45:14 +00:00
$hosts = Host::whereIn('id', $this->hosts)->whereNotIn('id', $maintenance->hosts()->pluck('hosts.id')->toArray())->get();
foreach ($hosts as $key => $host) {
$maintenance->hosts()->attach($host);
}
2024-08-06 06:31:51 +00:00
$maintenance->refresh();
2024-08-06 09:02:45 +00:00
2024-08-06 06:31:51 +00:00
foreach ($maintenance->hosts as $host) {
2024-08-06 09:02:45 +00:00
$alreadyUsedHosts = $maintenance->tasks()->where('host_id', $host->id)->distinct('maintenance_tasks.task_id')->pluck('maintenance_tasks.task_id')->toArray();
$tasks = Task::whereIn('id', $this->hosts_tasks[$host->id])->whereNotIn('id', $alreadyUsedHosts)->get();
foreach ($tasks as $key => $task) {
$maintenance->tasks()->create([
'task_id' => $task->id,
'host_id' => $host->id,
2024-08-06 06:31:51 +00:00
]);
}
}
2024-07-30 16:13:21 +00:00
}
2024-07-31 16:45:14 +00:00
2024-07-30 16:13:21 +00:00
$this->dispatch('closeModal');
}
public function render()
{
return view('livewire.maintenance.form');
}
2024-07-31 16:45:14 +00:00
public function addHost()
{
$this->hosts[] = [];
}
public function removeHost($id)
{
unset($this->hosts[$id]);
}
2024-07-30 16:13:21 +00:00
}