LAR_Maintenance/app/Http/Controllers/MaintenanceController.php
2024-08-16 13:28:14 +02:00

98 lines
3.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\MaintenanceHistory;
use Carbon\Carbon;
use Illuminate\Http\Request;
class MaintenanceController extends BaseController
{
public function index()
{
return view('maintenance.index');
}
public function planned()
{
return view('maintenance.planned');
}
public function plannedDetail(MaintenanceHistory $maintenance_history)
{
if (!empty($maintenance_history->finished_at)) {
abort(404);
}
return view('maintenance.planned-detail', [
'maintenance_history' => $maintenance_history ?? [],
]);
}
public function plannedDetailPut(Request $request, MaintenanceHistory $maintenance_history)
{
if (!empty($maintenance_history->finished_at)) {
abort(404);
}
return view('maintenance.planned-detail-done', [
'maintenance_history' => $maintenance_history,
'maintenance_task_status' => $request->input('maintenance_task_status') ?? [],
'maintenance_host_skipped' => $request->input('maintenance_host_skipped') ?? [],
]);
}
public function plannedDetailFinishPost(Request $request, MaintenanceHistory $maintenance_history)
{
if (!empty($maintenance_history->finished_at)) {
abort(404);
}
$request->validate([
"skippedHostsSummary.*.*" => 'required|string'
]);
// if ($request->has('skippedHostsSummary') || $request->has('skippedHostTasksSummary'))
// {
// return redirect()->back()->with('error', __('boilerplate::ui.incorect.old.password'));
// }
foreach ($request->input('skippedHostsSummary') as $history_host_id => $summary) {
$maintenance_history->historyHosts->find($history_host_id)->update([
'summary' => $summary,
]);
}
if ($request->has('skippedHostTasksSummary')) {
foreach ($maintenance_history->historyHosts as $history_host) {
if (!isset($request->input('skippedHostTasksSummary')[$history_host->id]))
continue;
foreach ($request->input('skippedHostTasksSummary')[$history_host->id] as $history_task_id => $task_summary) {
$history_host->historyTasks->find($history_task_id)->update([
'summary' => $task_summary,
]);
}
}
}
$maintenance_history->finished_at = Carbon::now();
$maintenance_history->save();
return redirect()->route('maintenance.history.detail', $maintenance_history->id);
}
public function history(Request $request)
{
return view('maintenance.history');
}
public function historyDetail(Request $request, MaintenanceHistory $maintenance_history)
{
return view('maintenance.history-detail-done', [
'maintenance_history' => $maintenance_history ?? []
]);
}
}