LAR_Maintenance/app/Livewire/Maintenance/Form.php

159 lines
4.9 KiB
PHP

<?php
namespace App\Livewire\Maintenance;
use App\Models\Host;
use Livewire\Component;
use App\Models\Maintenance;
use App\Models\MaintenanceTask;
use App\Models\Task;
use App\Models\User;
use Illuminate\Http\Request;
class Form extends Component
{
public $model;
public string $name = "";
public string $description = "";
public string $schedule = "";
public $action = 'store';
public $hosts = [];
public $hosts_available = [];
public $hosts_tasks = [];
public $hosts_tasks_available = [];
public int $guestor_id;
public $guestor_available = [];
public $blocking_maintenance_id = null;
public $maintenances_available = [];
public $blocking_maintenance_offset = null;
public int $duration = 0;
protected function rules()
{
return [
'name' => 'required',
'guestor_id' => 'required|exists:users,id',
'blocking_maintenance_id' => 'exists:maintenances,id|nullable',
'blocking_maintenance_offset' => 'nullable',
'description' => 'required',
'schedule' => 'nullable',
'duration' => 'required|max:24',
];
}
public function mount(Request $request, $model = null)
{
$this->hosts_available = Host::all()->pluck('hostname', 'id')->toArray();
$this->hosts_tasks_available = Task::all()->pluck('name', 'id')->toArray();
$this->guestor_available = User::all()->pluck('name', 'id')->toArray();
$this->maintenances_available = Maintenance::where('id', '!=', $model)->pluck('name', 'id')->toArray();
$this->maintenances_available = [null => 'Select blocking maintenance...'] + $this->maintenances_available;
if (!empty($model)) {
$maintenance = Maintenance::find($model);
$this->model = $model;
$this->name = $maintenance->name;
$this->description = $maintenance->description;
$this->schedule = $maintenance->schedule;
$this->guestor_id = $maintenance->guestor_id;
$this->blocking_maintenance_id = $maintenance->blocking_maintenance_id ?? null;
$this->blocking_maintenance_offset = $maintenance->blocking_maintenance_offset ?? null;
$this->duration = $maintenance->duration ;
$this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray();
foreach ($maintenance->tasks as $task) {
$this->hosts_tasks[$task->host_id][] = $task->task_id;
}
$this->action = 'update';
} else {
$this->guestor_id = $request->user()->id;
}
}
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]);
}
}
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);
$tasks = Task::whereIn('id', $this->hosts_tasks[$host->id])->get();
foreach ($tasks as $task) {
$maintenance->tasks()->create([
'task_id' => $task->id,
'host_id' => $host->id,
]);
}
}
$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);
}
$maintenance->refresh();
foreach ($maintenance->hosts as $host) {
$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,
]);
}
}
}
$this->dispatch('closeModal');
}
public function render()
{
return view('livewire.maintenance.form');
}
public function addHost()
{
$this->hosts[] = [];
}
public function removeHost($id)
{
unset($this->hosts[$id]);
}
}