Allow saving custom reports

This commit is contained in:
Marcus Moore 2023-12-11 14:19:03 -08:00
parent e636d7b9d5
commit c3b53b28e3
No known key found for this signature in database
5 changed files with 45 additions and 19 deletions

View file

@ -397,9 +397,7 @@ class ReportsController extends Controller
return view('reports/custom', [ return view('reports/custom', [
'customfields' => $customfields, 'customfields' => $customfields,
'saved_reports' => $saved_reports, 'saved_reports' => $saved_reports,
// @todo: temporary 'savedReport' => $saved_reports->find($request->input('report')) ?? new SavedReport,
'savedReport' => $saved_reports->find($request->input('report')),
// 'savedReport' => new SavedReport,
]); ]);
} }

View file

@ -2,21 +2,17 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\SavedReport;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class SavedReportsController extends Controller class SavedReportsController extends Controller
{ {
public function store(Request $request) public function store(Request $request)
{ {
// @todo: make this dynamic $report = $request->user()->savedReports()->create([
$savedReport = SavedReport::first(); 'name' => $request->get('report_name'),
'options' => $request->except(['_token', 'report_name']),
]);
$savedReport->options = $request->except('_token'); return redirect()->route('reports/custom', ['report' => $report->id]);
$savedReport->save();
// @todo: redirect back with the saved report pre-populated?
return redirect()->back();
} }
} }

View file

@ -24,6 +24,8 @@ class SavedReport extends Model
'options', 'options',
]; ];
// @todo: add $rules
//we will need a bit to catch and store the name of the report. //we will need a bit to catch and store the name of the report.
//for now the blip above is creating the name, but can be confusing if multiple are made at once //for now the blip above is creating the name, but can be confusing if multiple are made at once

View file

@ -14,6 +14,7 @@ use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Contracts\Translation\HasLocalePreference; use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
@ -329,6 +330,11 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
return $this->belongsToMany(\App\Models\License::class, 'license_seats', 'assigned_to', 'license_id')->withPivot('id'); return $this->belongsToMany(\App\Models\License::class, 'license_seats', 'assigned_to', 'license_id')->withPivot('id');
} }
public function savedReports(): HasMany
{
return $this->hasMany(SavedReport::class);
}
/** /**
* Establishes a count of all items assigned * Establishes a count of all items assigned
* *

View file

@ -371,14 +371,11 @@
{{ trans('general.generate') }} {{ trans('general.generate') }}
</button> </button>
</div> </div>
<div style=padding-bottom:5px>
<a href="#" class="btn btn-primary" style="width: 100%">
{{ trans('admin/reports/general.apply_and_generate') }}</a>
</div>
<div style=padding-bottom:5px> <div style=padding-bottom:5px>
<form method="post" id="savetemplateform" action="{{ route("savedreports/store") }}"> <form method="post" id="savetemplateform" action="{{ route("savedreports/store") }}">
@csrf @csrf
<input type="hidden" id="savetemplateform" name="options"> <input type="hidden" id="savetemplateform" name="options">
<input type="text" id="report_name" name="report_name">
{{-- this will be a box to name the report? --}} {{-- this will be a box to name the report? --}}
<button class = "btn btn-primary" style="width: 100%"> <button class = "btn btn-primary" style="width: 100%">
{{ trans('admin/reports/general.save_template') }} {{ trans('admin/reports/general.save_template') }}
@ -391,13 +388,30 @@
{{-- <strong class="caret"></strong>--}} {{-- <strong class="caret"></strong>--}}
{{-- </a>--}} {{-- </a>--}}
{{-- {!! Form::select('brand', array('1'=>'Text','2'=>'Logo','3'=>'Logo + Text'), old('brand', $setting->brand), array('class' => 'form-control select2', 'style'=>'width: 150px ;')) !!}--}} {{-- {!! Form::select('brand', array('1'=>'Text','2'=>'Logo','3'=>'Logo + Text'), old('brand', $setting->brand), array('class' => 'form-control select2', 'style'=>'width: 150px ;')) !!}--}}
<select class="form-control select2"> <select
id="saved_report_select"
class="form-control select2"
data-placeholder="Load Saved Report"
data-allow-clear="true"
>
<option></option>
@foreach($saved_reports as $report) @foreach($saved_reports as $report)
<option> <option value="{{ $report->id }}" @if (request()->input('report') == $report->id) selected @endif>
{{ $report->name }} {{ $report->name }}
</option> </option>
@endforeach @endforeach
</select> </select>
@push('js')
<script>
$('#saved_report_select')
.on('select2:select', function (event) {
window.location.href = '{{ route('reports/custom') }}?report=' + event.params.data.id;
})
.on('select2:clearing', function (event) {
window.location.href = '{{ route('reports/custom') }}';
});
</script>
@endpush
</div> </div>
</div> </div>
</div> </div>
@ -445,7 +459,17 @@
$("#savetemplateform").submit(function(e) { $("#savetemplateform").submit(function(e) {
e.preventDefault(e); e.preventDefault(e);
$('#custom-report-form').attr('action', '/reports/savedtemplate').submit()
let form = $('#custom-report-form');
$('<input>').attr({
type: 'hidden',
name: 'report_name',
value: $('#report_name').val(),
}).appendTo(form);
form.attr('action', '/reports/savedtemplate').submit();
// let elements = Array.from(document.getElementById("custom-report-form").elements).map(item=>item.name); // let elements = Array.from(document.getElementById("custom-report-form").elements).map(item=>item.name);
// console.log(elements); // console.log(elements);
// //