snipe-it/app/Http/Controllers/ReportTemplatesController.php

80 lines
2.5 KiB
PHP
Raw Normal View History

2023-08-28 16:26:31 -07:00
<?php
namespace App\Http\Controllers;
2023-12-13 18:09:42 -08:00
use App\Models\CustomField;
2023-12-18 12:55:48 -08:00
use App\Models\ReportTemplate;
2023-08-28 16:26:31 -07:00
use Illuminate\Http\Request;
2023-12-18 12:55:48 -08:00
class ReportTemplatesController extends Controller
2023-08-28 16:26:31 -07:00
{
public function store(Request $request)
{
$this->authorize('reports.view');
2023-12-18 12:55:48 -08:00
$request->validate((new ReportTemplate)->getRules());
2023-12-11 16:20:36 -08:00
2023-12-18 12:55:48 -08:00
$report = $request->user()->reportTemplates()->create([
2023-12-11 16:20:36 -08:00
'name' => $request->get('name'),
'options' => $request->except(['_token', 'name']),
2023-12-11 14:19:03 -08:00
]);
2023-12-19 15:01:19 -08:00
// This is for error handling in creation. This probably is the wrong spot, and syntax is off, but i don't wanna forget
// if(is_null($report->name)) {
// return redirect()->route('reports/custom')->with('error', trans('reports/message.create.needs_title'));
// }
// elseif(exists($report->name)) {
// return redirect()->route('reports/custom')->with('error', trans('reports/message.create.duplicate'));
// }
2023-12-20 14:41:23 -08:00
return redirect()->route('report-templates.show', $report->id);
2023-08-28 16:26:31 -07:00
}
2023-12-13 14:31:58 -08:00
public function show(Request $request, $reportId)
{
$this->authorize('reports.view');
$reportTemplate = ReportTemplate::find($reportId);
if (!$reportTemplate) {
return redirect()->route('reports/custom')
->with('error', 'Template does not exist or you do not have permission to view it.');
}
$customfields = CustomField::get();
$report_templates = ReportTemplate::orderBy('name')->get();
return view('reports/custom', [
'customfields' => $customfields,
'report_templates' => $report_templates,
'reportTemplate' => $reportTemplate,
]);
}
2023-12-13 18:09:42 -08:00
public function edit(Request $request, $reportId)
{
2023-12-18 12:55:48 -08:00
$report = ReportTemplate::findOrFail($reportId);
2023-12-13 18:09:42 -08:00
return view('reports/custom', [
'customfields' => CustomField::get(),
2023-12-18 12:55:48 -08:00
'reportTemplate' => $report,
2023-12-13 18:09:42 -08:00
]);
}
2023-12-20 14:41:23 -08:00
public function update(Request $request, $reportId)
2023-12-13 14:31:58 -08:00
{
2023-12-20 14:41:23 -08:00
$this->authorize('reports.view');
$reportTemplate = ReportTemplate::find($reportId);
2023-12-13 14:31:58 -08:00
2023-12-20 14:41:23 -08:00
if (!$reportTemplate) {
// @todo: what is the behavior we want?
2023-12-13 14:31:58 -08:00
return redirect()->route('reports/custom');
}
2023-12-20 14:41:23 -08:00
$reportTemplate->options = $request->except(['_token', 'name']);
$reportTemplate->save();
2023-12-13 14:31:58 -08:00
2023-12-20 14:41:23 -08:00
return redirect()->route('report-templates.show', $reportTemplate->id);
2023-12-13 14:31:58 -08:00
}
2023-08-28 16:26:31 -07:00
}