This commit is contained in:
Jonatan Rek 2024-08-06 11:02:45 +02:00
parent d0fbb5d5c1
commit ecb255fc6a
17 changed files with 108 additions and 65 deletions

View File

@ -4,6 +4,7 @@
use App\Models\Maintenance; use App\Models\Maintenance;
use App\Models\MaintenanceHistory; use App\Models\MaintenanceHistory;
use App\Models\MaintenanceTaskHistory;
use Cron\CronExpression; use Cron\CronExpression;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
@ -42,16 +43,20 @@ public function handle(): void
]); ]);
$maintenancePlanned->refresh(); $maintenancePlanned->refresh();
$tasks = $maintenancePlanned->tasks; $hosts = $maintenance->hosts;
if(!empty($tasks)){ foreach ($hosts as $key => $host) {
dd($tasks); $maintenancePlannedHost = $maintenancePlanned->historyHosts()->create([
'host_id' => $host->id
]);
$tasks = $maintenance->tasks;
foreach ($tasks as $key => $task) { foreach ($tasks as $key => $task) {
$maintenancePlanned->tasks()->create([ $maintenancePlannedHost->historyTasks()->create([
'host_id' => $task->host_id, 'maintenance_task_id' => $task->id,
'maintenance_history_id' => $maintenancePlanned->id
]); ]);
} }
} }
} }
die(); die();
} }

View File

@ -6,7 +6,7 @@
use Livewire\Component; use Livewire\Component;
use App\Models\Maintenance; use App\Models\Maintenance;
use App\Models\MaintenanceTask; use App\Models\MaintenanceTask;
use Illuminate\Console\View\Components\Task; use App\Models\Task;
class Form extends Component class Form extends Component
{ {
@ -35,7 +35,7 @@ protected function rules()
public function mount($model = null) public function mount($model = null)
{ {
$this->hosts_available = Host::all()->pluck('hostname', 'id')->toArray(); $this->hosts_available = Host::all()->pluck('hostname', 'id')->toArray();
$this->hosts_tasks_available = MaintenanceTask::all()->pluck('name', 'id')->toArray(); $this->hosts_tasks_available = Task::all()->pluck('name', 'id')->toArray();
if (!empty($model)) { if (!empty($model)) {
$maintenance = Maintenance::find($model); $maintenance = Maintenance::find($model);
@ -48,7 +48,7 @@ public function mount($model = null)
$this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray(); $this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray();
foreach ($maintenance->tasks as $task) { foreach ($maintenance->tasks as $task) {
$this->hosts_tasks_available[$task->host_id][] = $task->id; $this->hosts_tasks[$task->host_id][] = $task->task_id;
} }
$this->action = 'update'; $this->action = 'update';
@ -79,10 +79,14 @@ public function update()
$maintenance->hosts()->attach($host); $maintenance->hosts()->attach($host);
} }
$maintenance->refresh(); $maintenance->refresh();
foreach ($maintenance->hosts as $host) { foreach ($maintenance->hosts as $host) {
foreach ($this->hosts_tasks[$host->id] as $task_id => $host_task_name) { $alreadyUsedHosts = $maintenance->tasks()->where('host_id', $host->id)->distinct('maintenance_tasks.task_id')->pluck('maintenance_tasks.task_id')->toArray();
$host->tasks()->create([ $tasks = Task::whereIn('id', $this->hosts_tasks[$host->id])->whereNotIn('id', $alreadyUsedHosts)->get();
'task_id' => $task_id foreach ($tasks as $key => $task) {
$maintenance->tasks()->create([
'task_id' => $task->id,
'host_id' => $host->id,
]); ]);
} }
} }

View File

@ -1,20 +1,20 @@
<?php <?php
namespace App\Livewire\MaintenanceTask; namespace App\Livewire\Task;
use App\Models\MaintenanceTask; use App\Models\Task;
use SteelAnts\DataTable\Livewire\DataTableComponent; use SteelAnts\DataTable\Livewire\DataTableComponent;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
class DataTable extends DataTableComponent class DataTable extends DataTableComponent
{ {
public $listeners = [ public $listeners = [
'maintenancetaskAdded' => '$refresh', 'taskAdded' => '$refresh',
'closeModal' => '$refresh', 'closeModal' => '$refresh',
]; ];
public function query(): Builder public function query(): Builder
{ {
return MaintenanceTask::query(); return Task::query();
} }
public function headers(): array public function headers(): array
@ -25,8 +25,8 @@ public function headers(): array
]; ];
} }
public function remove($maintenancetask_id){ public function remove($task_id){
MaintenanceTask::find($maintenancetask_id)->delete(); Task::find($task_id)->delete();
} }
public function actions($item) public function actions($item)
@ -47,8 +47,8 @@ public function actions($item)
]; ];
} }
public function edit($maintenancetask_id) public function edit($task_id)
{ {
$this->dispatch('openModal', 'maintenance-task.form', __('boilerplate::maintenance-task.edit'), ['model' => $maintenancetask_id]); $this->dispatch('openModal', 'task.form', __('boilerplate::task.edit'), ['model' => $task_id]);
} }
} }

View File

@ -1,13 +1,12 @@
<?php <?php
namespace App\Livewire\MaintenanceTask; namespace App\Livewire\Task;
use Livewire\Component; use Livewire\Component;
use App\Models\MaintenanceTask; use App\Models\Task;
class Form extends Component class Form extends Component
{ {
public $model; public $model;
public string $maintenance_id;
public string $name; public string $name;
public string $description = ""; public string $description = "";
@ -16,7 +15,6 @@ class Form extends Component
protected function rules() protected function rules()
{ {
return [ return [
'maintenance_id' => 'required',
'name' => 'required', 'name' => 'required',
'description' => 'required', 'description' => 'required',
]; ];
@ -24,13 +22,12 @@ protected function rules()
public function mount ($model = null){ public function mount ($model = null){
if (!empty($model)) { if (!empty($model)) {
$maintenanceTask = MaintenanceTask::find($model); $task = Task::find($model);
$this->model = $model; $this->model = $model;
$this->maintenance_id = $maintenanceTask->maintenance_id; $this->name = $task->name;
$this->name = $maintenanceTask->name; $this->description = $task->description;
$this->description = $maintenanceTask->description;
$this->action = 'update'; $this->action = 'update';
} }
@ -39,22 +36,22 @@ public function mount ($model = null){
public function store() public function store()
{ {
$validatedData = $this->validate(); $validatedData = $this->validate();
MaintenanceTask::create($validatedData); Task::create($validatedData);
$this->dispatch('closeModal'); $this->dispatch('closeModal');
} }
public function update() public function update()
{ {
$validatedData = $this->validate(); $validatedData = $this->validate();
$maintenanceTask = MaintenanceTask::find($this->model); $task = Task::find($this->model);
if (!empty($maintenanceTask)) { if (!empty($task)) {
$maintenanceTask->update($validatedData); $task->update($validatedData);
} }
$this->dispatch('closeModal'); $this->dispatch('closeModal');
} }
public function render() public function render()
{ {
return view('livewire.maintenance-task.form'); return view('livewire.task.form');
} }
} }

View File

@ -15,16 +15,16 @@ class Maintenance extends Model
'schedule', 'schedule',
]; ];
public function history()
{
return $this->hasMany(MaintenanceHistory::class);
}
public function hosts() public function hosts()
{ {
return $this->belongsToMany(Host::class, "maintenance_host", "maintenance_id", "host_id" ); return $this->belongsToMany(Host::class, "maintenance_host", "maintenance_id", "host_id" );
} }
public function history()
{
return $this->hasMany(MaintenanceHistory::class);
}
public function tasks() public function tasks()
{ {
return $this->hasMany(MaintenanceTask::class,); return $this->hasMany(MaintenanceTask::class,);

View File

@ -19,4 +19,9 @@ public function maintenance()
{ {
return $this->BelongsTo(Maintenance::class); return $this->BelongsTo(Maintenance::class);
} }
public function historyHosts()
{
return $this->hasMany(MaintenanceHostHistory::class);
}
} }

View File

@ -9,12 +9,17 @@ class MaintenanceHostHistory extends Model
{ {
use HasFactory; use HasFactory;
public function hosts() protected $fillable = [
'maintenance_history_id',
'host_id',
];
public function host()
{ {
return $this->belongsToMany(MaintenanceHostHistory::class); return $this->belongsTo(Host::class);
} }
public function tasks() public function historyTasks()
{ {
return $this->hasMany(MaintenanceTaskHistory::class); return $this->hasMany(MaintenanceTaskHistory::class);
} }

View File

@ -12,7 +12,12 @@ class MaintenanceTask extends Model
protected $fillable = [ protected $fillable = [
'maintenance_id', 'maintenance_id',
'host_id', 'host_id',
'name', 'task_id',
'description',
]; ];
public function task()
{
return $this->belongsTo(Task::class);
}
} }

View File

@ -9,8 +9,19 @@ class MaintenanceTaskHistory extends Model
{ {
use HasFactory; use HasFactory;
public function task() protected $fillable = [
'maintenance_task_id',
'maintenance_history_id',
'maintenance_host_history_id',
];
public function maintenance()
{ {
return $this->BelongsTo(MaintenanceTask::class); return $this->BelongsTo(MaintenanceHistory::class);
}
public function maintenanceTask()
{
return $this->belongsTo(MaintenanceTask::class);
} }
} }

View File

@ -8,4 +8,9 @@
class Task extends Model class Task extends Model
{ {
use HasFactory; use HasFactory;
protected $fillable = [
'name',
'description',
];
} }

View File

@ -18,6 +18,7 @@ public function up(): void
$table->id(); $table->id();
$table->foreignIdFor(Maintenance::class); $table->foreignIdFor(Maintenance::class);
$table->foreignIdFor(Task::class); $table->foreignIdFor(Task::class);
$table->foreignIdFor(Host::class);
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -2,6 +2,7 @@
use App\Models\Host; use App\Models\Host;
use App\Models\MaintenanceHistory; use App\Models\MaintenanceHistory;
use App\Models\MaintenanceHostHistory;
use App\Models\MaintenanceTask; use App\Models\MaintenanceTask;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
@ -18,7 +19,7 @@ public function up(): void
$table->id(); $table->id();
$table->foreignIdFor(MaintenanceTask::class); $table->foreignIdFor(MaintenanceTask::class);
$table->foreignIdFor(MaintenanceHistory::class); $table->foreignIdFor(MaintenanceHistory::class);
$table->foreignIdFor(Host::class); $table->foreignIdFor(MaintenanceHostHistory::class);
$table->string('status')->default(1); $table->string('status')->default(1);
$table->datetime('finished_at')->nullable(); $table->datetime('finished_at')->nullable();
$table->timestamps(); $table->timestamps();

View File

@ -2,6 +2,8 @@
use App\Models\Host; use App\Models\Host;
use App\Models\Maintenance; use App\Models\Maintenance;
use App\Models\MaintenanceHistory;
use App\Models\MaintenanceHostHistory;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
@ -15,7 +17,7 @@ public function up(): void
{ {
Schema::create('maintenance_host_histories', function (Blueprint $table) { Schema::create('maintenance_host_histories', function (Blueprint $table) {
$table->id(); $table->id();
$table->foreignIdFor(Maintenance::class); $table->foreignIdFor(MaintenanceHistory::class);
$table->foreignIdFor(Host::class); $table->foreignIdFor(Host::class);
$table->timestamps(); $table->timestamps();
}); });

View File

@ -5,7 +5,7 @@
<x-form::input group-class="mb-3" type="text" wire:model="schedule" id="schedule" label="schedule" /> <x-form::input group-class="mb-3" type="text" wire:model="schedule" id="schedule" label="schedule" />
<x-form::select group-class="mb-3" wire:model.live="hosts" label="Livewire Select" :options="$hosts_available" placeholder="Select value..." multiple /> <x-form::select group-class="mb-3" wire:model.live="hosts" label="Livewire Select" :options="$hosts_available" placeholder="Select value..." multiple />
@foreach($hosts as $host_id) @foreach($hosts as $key => $host_id)
<x-form::select group-class="mb-3" wire:model="hosts_tasks.{{ $host_id }}" label="Select Tasks for Host {{ $hosts_available[$host_id] }}" :options="$hosts_tasks_available" placeholder="Select value..." multiple /> <x-form::select group-class="mb-3" wire:model="hosts_tasks.{{ $host_id }}" label="Select Tasks for Host {{ $hosts_available[$host_id] }}" :options="$hosts_tasks_available" placeholder="Select value..." multiple />
@endforeach @endforeach

View File

@ -1,6 +1,5 @@
<div> <div>
<x-form::form wire:submit.prevent="{{$action}}"> <x-form::form wire:submit.prevent="{{$action}}">
<x-form::input group-class="mb-3" type="text" wire:model="maintenance_id" id="maintenance_id" label="maintenance_id"/>
<x-form::input group-class="mb-3" type="text" wire:model="name" id="name" label="name"/> <x-form::input group-class="mb-3" type="text" wire:model="name" id="name" label="name"/>
<x-form::quill group-class="mb-3" type="text" wire:model="description" id="description" label="description" /> <x-form::quill group-class="mb-3" type="text" wire:model="description" id="description" label="description" />
<x-form::button class="btn-primary" type="submit">Create</x-form::button> <x-form::button class="btn-primary" type="submit">Create</x-form::button>

View File

@ -3,18 +3,21 @@
<div class="page-header"> <div class="page-header">
<h1>{{ __('Planned Maintenance') }}</h1> <h1>{{ __('Planned Maintenance') }}</h1>
</div> </div>
<p>{!! $maintenance_history->maintenance->description !!}</p> <p>{!! $maintenance_history->maintenance->description !!}</p>
@foreach ($maintenance_history->maintenance->hosts as $host) @foreach ($maintenance_history->historyHosts as $historyHost)
<div class="mb-3"> <div class="mb-3">
<div class="btn-toggle btn py-3 w-100 bg-primary text-start" style="--bs-bg-opacity: .2;" data-bs-toggle="collapse" data-bs-target="#collapse{{ $host->id }}"> <div class="btn-toggle btn py-3 w-100 bg-primary text-start" style="--bs-bg-opacity: .2;" data-bs-toggle="collapse" data-bs-target="#collapse{{ $historyHost->id }}">
<b>{{ $host->hostname }}</b> <b>{{ $historyHost->host->hostname }}</b>
</div> </div>
<div class="collapse" id="collapse{{ $host->id }}"> <div class="collapse" id="collapse{{ $historyHost->id }}">
<div class="card card-body mt-2"> <div class="card card-body mt-2">
<h6>Description</h6> @foreach ($historyHost->historyTasks as $historyTasks)
</div> <x-form::checkbox wire:model="test" label="{{$historyTasks->maintenanceTask->task->name}}"/>
{!! $historyTasks->maintenanceTask->task->description !!}
@endforeach
</div> </div>
</div> </div>
</div>
@endforeach @endforeach
</div> </div>
</x-layout-app> </x-layout-app>

View File

@ -1,13 +1,13 @@
<x-layout-app> <x-layout-app>
<div class="container-xl"> <div class="container-xl">
<div class="page-header"> <div class="page-header">
<h1>{{ __('boilerplate::maintenance-task.title') }}</h1> <h1>{{ __('boilerplate::task.title') }}</h1>
<button class="btn btn-primary" onclick="Livewire.dispatch('openModal', {livewireComponents: 'maintenance-task.form', title: '{{ __('boilerplate::maintenance-task.create') }}'})"> <button class="btn btn-primary" onclick="Livewire.dispatch('openModal', {livewireComponents: 'task.form', title: '{{ __('boilerplate::task.create') }}'})">
<i class="me-2 fas fa-plus"></i><span>{{ __('boilerplate::ui.add') }}</span> <i class="me-2 fas fa-plus"></i><span>{{ __('boilerplate::ui.add') }}</span>
</button> </button>
</div> </div>
@livewire('maintenance-task.data-table', [], key('data-table')) @livewire('task.data-table', [], key('data-table'))
</div> </div>
</x-layout-app> </x-layout-app>