Skip to content

A free open source IT asset/license management system

WIP: add methods to restore settings from saved report

Marcus Mooreb7011d853a1a
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php
index 4b4db866ac..d60e201e77 100644
--- a/app/Http/Controllers/ReportsController.php
+++ b/app/Http/Controllers/ReportsController.php
@@ -394,7 +394,13 @@ class ReportsController extends Controller
$customfields = CustomField::get();
$saved_reports = SavedReport::get();
- return view('reports/custom')->with('customfields', $customfields)->with('saved_reports', $saved_reports);
+ return view('reports/custom', [
+ 'customfields' => $customfields,
+ 'saved_reports' => $saved_reports,
+ // @todo: temporary
+ 'savedReport' => $saved_reports->first(),
+ // 'savedReport' => new SavedReport,
+ ]);
}
/**
diff --git a/app/Http/Controllers/SavedReportsController.php b/app/Http/Controllers/SavedReportsController.php
index 31b91f0347..fcb9b54604 100644
--- a/app/Http/Controllers/SavedReportsController.php
+++ b/app/Http/Controllers/SavedReportsController.php
@@ -2,12 +2,20 @@
namespace App\Http\Controllers;
+use App\Models\SavedReport;
use Illuminate\Http\Request;
class SavedReportsController extends Controller
{
public function store(Request $request)
{
- dd('saved reports', $request->all());
+ $savedReport = SavedReport::first();
+
+ $savedReport->options = $request->except('_token');
+
+ $savedReport->save();
+
+ // @todo: should this redirect elsewhere?
+ return redirect()->back();
}
}
diff --git a/app/Models/SavedReport.php b/app/Models/SavedReport.php
index cd1cdd7e58..81c5b9a66c 100644
--- a/app/Models/SavedReport.php
+++ b/app/Models/SavedReport.php
@@ -23,4 +23,30 @@ class SavedReport extends Model
'name',
'options',
];
+
+ public function checkmarkValue($property)
+ {
+ // Assuming we're using the null object pattern,
+ // return the default value if the object is not saved yet.
+ if (is_null($this->id)) {
+ return '1';
+ }
+
+ // Return the property's value if it exists
+ // and return the default value if not.
+ return $this->options[$property] ?? '0';
+ }
+
+ public function textValue($property)
+ {
+ // Assuming we're using the null object pattern,
+ // return the default value if the object is not saved yet.
+ if (is_null($this->id)) {
+ return '';
+ }
+
+ // Return the property's value if it exists
+ // and return the default value if not.
+ return $this->options[$property] ?? '';
+ }
}
diff --git a/database/factories/SavedReportFactory.php b/database/factories/SavedReportFactory.php
new file mode 100644
index 0000000000..72279bd3e5
--- /dev/null
+++ b/database/factories/SavedReportFactory.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Database\Factories;
+
+use Illuminate\Database\Eloquent\Factories\Factory;
+
+class SavedReportFactory extends Factory
+{
+ /**
+ * Define the model's default state.
+ *
+ * @return array
+ */
+ public function definition()
+ {
+ return [
+ 'name' => $this->faker->word(),
+ 'options' => json_encode([]),
+ ];
+ }
+}
diff --git a/resources/views/reports/custom.blade.php b/resources/views/reports/custom.blade.php
index 94bdc37879..b9ce08a8c3 100644
--- a/resources/views/reports/custom.blade.php
+++ b/resources/views/reports/custom.blade.php
@@ -40,147 +40,147 @@
</label>
<label class="form-control">
- {{ Form::checkbox('id', '1', '1') }}
+ {{ Form::checkbox('id', '1', $savedReport->checkmarkValue('id')) }}
{{ trans('general.id') }}
</label>
<label class="form-control">
- {{ Form::checkbox('company', '1', '1') }}
+ {{ Form::checkbox('company', '1', $savedReport->checkmarkValue('company')) }}
{{ trans('general.company') }}
</label>
<label class="form-control">
- {{ Form::checkbox('asset_tag', '1', '1') }}
+ {{ Form::checkbox('asset_tag', '1', $savedReport->checkmarkValue('asset_tag')) }}
{{ trans('general.asset_tag') }}
</label>
<label class="form-control">
- {{ Form::checkbox('asset_name', '1', '1') }}
+ {{ Form::checkbox('asset_name', '1', $savedReport->checkmarkValue('asset_name')) }}
{{ trans('admin/hardware/form.name') }}
</label>
<label class="form-control">
- {{ Form::checkbox('manufacturer', '1', '1') }}
+ {{ Form::checkbox('manufacturer', '1', $savedReport->checkmarkValue('manufacturer')) }}
{{ trans('general.manufacturer') }}
</label>
<label class="form-control">
- {{ Form::checkbox('model', '1', '1') }}
+ {{ Form::checkbox('model', '1', $savedReport->checkmarkValue('model')) }}
{{ trans('general.asset_models') }}
</label>
<label class="form-control">
- {{ Form::checkbox('category', '1', '1') }}
+ {{ Form::checkbox('category', '1', $savedReport->checkmarkValue('category')) }}
{{ trans('general.category') }}
</label>
<label class="form-control">
- {{ Form::checkbox('serial', '1', '1') }}
+ {{ Form::checkbox('serial', '1', $savedReport->checkmarkValue('serial')) }}
{{ trans('admin/hardware/table.serial') }}
</label>
<label class="form-control">
- {{ Form::checkbox('purchase_date', '1', '1') }}
+ {{ Form::checkbox('purchase_date', '1', $savedReport->checkmarkValue('purchase_date')) }}
{{ trans('admin/licenses/table.purchase_date') }}
</label>
<label class="form-control">
- {{ Form::checkbox('purchase_cost', '1', '1') }}
+ {{ Form::checkbox('purchase_cost', '1', $savedReport->checkmarkValue('purchase_cost')) }}
{{ trans('admin/hardware/form.cost') }}
</label>
<label class="form-control">
- {{ Form::checkbox('eol', '1', '1') }}
+ {{ Form::checkbox('eol', '1', $savedReport->checkmarkValue('eol')) }}
{{ trans('admin/hardware/table.eol') }}
</label>
<label class="form-control">
- {{ Form::checkbox('order', '1', '1') }}
+ {{ Form::checkbox('order', '1', $savedReport->checkmarkValue('order')) }}
{{ trans('admin/hardware/form.order') }}
</label>
<label class="form-control">
- {{ Form::checkbox('supplier', '1', '1') }}
+ {{ Form::checkbox('supplier', '1', $savedReport->checkmarkValue('supplier')) }}
{{ trans('general.suppliers') }}
</label>
<label class="form-control">
- {{ Form::checkbox('location', '1', '1') }}
+ {{ Form::checkbox('location', '1', $savedReport->checkmarkValue('location')) }}
{{ trans('general.location') }}
</label>
<label class="form-control">
- {{ Form::checkbox('location_address', '1', '1') }}
+ {{ Form::checkbox('location_address', '1', $savedReport->checkmarkValue('location_address')) }}
- {{ trans('general.address') }}
</label>
<label class="form-control">
- {{ Form::checkbox('rtd_location', '1', '1') }}
+ {{ Form::checkbox('rtd_location', '1', $savedReport->checkmarkValue('rtd_location')) }}
{{ trans('admin/hardware/form.default_location') }}
</label>
<label class="form-control">
- {{ Form::checkbox('rtd_location_address', '1', '1') }}
+ {{ Form::checkbox('rtd_location_address', '1', $savedReport->checkmarkValue('rtd_location_address')) }}
- {{ trans('general.address') }}
</label>
<label class="form-control">
- {{ Form::checkbox('status', '1', '1') }}
+ {{ Form::checkbox('status', '1', $savedReport->checkmarkValue('status')) }}
{{ trans('general.status') }}
</label>
<label class="form-control">
- {{ Form::checkbox('warranty', '1', '1') }}
+ {{ Form::checkbox('warranty', '1', $savedReport->checkmarkValue('warranty')) }}
{{ trans('admin/hardware/form.warranty') }}
</label>
<label class="form-control">
- {{ Form::checkbox('depreciation', '1', '1') }}
+ {{ Form::checkbox('depreciation', '1', $savedReport->checkmarkValue('depreciation')) }}
{{ trans('general.depreciation') }}
</label>
<label class="form-control">
- {{ Form::checkbox('checkout_date', '1', '1') }}
+ {{ Form::checkbox('checkout_date', '1', $savedReport->checkmarkValue('checkout_date')) }}
{{ trans('admin/hardware/table.checkout_date') }}
</label>
<label class="form-control">
- {{ Form::checkbox('expected_checkin', '1', '1') }}
+ {{ Form::checkbox('expected_checkin', '1', $savedReport->checkmarkValue('expected_checkin')) }}
{{ trans('admin/hardware/form.expected_checkin') }}
</label>
<label class="form-control">
- {{ Form::checkbox('created_at', '1', '1') }}
+ {{ Form::checkbox('created_at', '1', $savedReport->checkmarkValue('created_at')) }}
{{ trans('general.created_at') }}
</label>
<label class="form-control">
- {{ Form::checkbox('updated_at', '1', '1') }}
+ {{ Form::checkbox('updated_at', '1', $savedReport->checkmarkValue('updated_at')) }}
{{ trans('general.updated_at') }}
</label>
<label class="form-control">
- {{ Form::checkbox('deleted_at', '1', '1') }}
+ {{ Form::checkbox('deleted_at', '1', $savedReport->checkmarkValue('deleted_at')) }}
{{ trans('general.deleted') }}
</label>
<label class="form-control">
- {{ Form::checkbox('last_audit_date', '1', '1') }}
+ {{ Form::checkbox('last_audit_date', '1', $savedReport->checkmarkValue('last_audit_date')) }}
{{ trans('general.last_audit') }}
</label>
<label class="form-control">
- {{ Form::checkbox('next_audit_date', '1', '1') }}
+ {{ Form::checkbox('next_audit_date', '1', $savedReport->checkmarkValue('next_audit_date')) }}
{{ trans('general.next_audit_date') }}
</label>
<label class="form-control">
- {{ Form::checkbox('notes', '1', '1') }}
+ {{ Form::checkbox('notes', '1', $savedReport->checkmarkValue('notes')) }}
{{ trans('general.notes') }}
</label>
<label class="form-control">
- {{ Form::checkbox('url', '1', '1') }}
+ {{ Form::checkbox('url', '1', $savedReport->checkmarkValue('url')) }}
- {{ trans('admin/manufacturers/table.url') }}
</label>
@@ -190,32 +190,32 @@
<h2>{{ trans('general.checked_out_to') }} {{ trans('general.fields') }}:</h2>
<label class="form-control">
- {{ Form::checkbox('assigned_to', '1', '1') }}
+ {{ Form::checkbox('assigned_to', '1', $savedReport->checkmarkValue('assigned_to')) }}
{{ trans('admin/licenses/table.assigned_to') }}
</label>
<label class="form-control">
- {{ Form::checkbox('username', '1', '1') }}
+ {{ Form::checkbox('username', '1', $savedReport->checkmarkValue('username')) }}
{{ trans('admin/users/table.username') }}
</label>
<label class="form-control">
- {{ Form::checkbox('employee_num', '1', '1') }}
+ {{ Form::checkbox('employee_num', '1', $savedReport->checkmarkValue('employee_num')) }}
{{ trans('general.employee_number') }}
</label>
<label class="form-control">
- {{ Form::checkbox('manager', '1', '1') }}
+ {{ Form::checkbox('manager', '1', $savedReport->checkmarkValue('manager')) }}
{{ trans('admin/users/table.manager') }}
</label>
<label class="form-control">
- {{ Form::checkbox('department', '1', '1') }}
+ {{ Form::checkbox('department', '1', $savedReport->checkmarkValue('department')) }}
{{ trans('general.department') }}
</label>
<label class="form-control">
- {{ Form::checkbox('title', '1', '1') }}
+ {{ Form::checkbox('title', '1', $savedReport->checkmarkValue('title')) }}
{{ trans('admin/users/table.title') }}
</label>
@@ -228,7 +228,7 @@
@foreach ($customfields as $customfield)
<label class="form-control">
- {{ Form::checkbox($customfield->db_column_name(), '1', '1') }}
+ {{ Form::checkbox($customfield->db_column_name(), '1', $savedReport->checkmarkValue($customfield->db_column_name())) }}
{{ $customfield->name }}
</label>
@@ -258,7 +258,7 @@
<div class="form-group">
<label for="by_order_number" class="col-md-3 control-label">{{ trans('general.order_number') }}</label>
<div class="col-md-5 col-sm-8">
- <input class="form-control" type="text" name="by_order_number" value="" aria-label="by_order_number">
+ <input class="form-control" type="text" name="by_order_number" value="{{ $savedReport->textValue('by_order_number') }}" aria-label="by_order_number">
</div>
</div>
@@ -266,9 +266,9 @@
<div class="form-group purchase-range">
<label for="purchase_start" class="col-md-3 control-label">{{ trans('general.purchase_date') }} {{ trans('general.range') }}</label>
<div class="input-daterange input-group col-md-6" id="datepicker">
- <input type="text" class="form-control" name="purchase_start" aria-label="purchase_start">
+ <input type="text" class="form-control" name="purchase_start" aria-label="purchase_start" value="{{ $savedReport->textValue('purchase_start') }}">
<span class="input-group-addon">to</span>
- <input type="text" class="form-control" name="purchase_end" aria-label="purchase_end">
+ <input type="text" class="form-control" name="purchase_end" aria-label="purchase_end" value="{{ $savedReport->textValue('purchase_end') }}">
</div>
</div>
@@ -324,13 +324,13 @@
<div class="col-md-9 col-md-offset-3">
<label class="form-control">
- {{ Form::checkbox('exclude_archived', '1', old('exclude_archived')) }}
+ {{ Form::checkbox('exclude_archived', '1', $savedReport->checkmarkValue('exclude_archived')) }}
{{ trans('general.exclude_archived') }}
</label>
</div>
<div class="col-md-9 col-md-offset-3">
<label class="form-control">
- {{ Form::checkbox('use_bom', '1', old('use_bom')) }}
+ {{ Form::checkbox('use_bom', '1', $savedReport->checkmarkValue('use_bom')) }}
{{ trans('general.bom_remark') }}
</label>
</div>
diff --git a/tests/Unit/SavedReportTest.php b/tests/Unit/SavedReportTest.php
new file mode 100644
index 0000000000..a99408a846
--- /dev/null
+++ b/tests/Unit/SavedReportTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Unit;
+
+use App\Models\SavedReport;
+use Tests\TestCase;
+
+class SavedReportTest extends TestCase
+{
+ public function testParsingCheckmarkValues()
+ {
+ $savedReport = SavedReport::factory()->create([
+ 'options' => [
+ 'is_a_checkbox_value' => '1',
+ ],
+ ]);
+
+ $this->assertEquals('1', $savedReport->checkmarkValue('is_a_checkbox_value'));
+ $this->assertEquals('0', $savedReport->checkmarkValue('non_existent_key'));
+
+ $this->assertEquals('1', (new SavedReport)->checkmarkValue('is_a_checkbox_value'));
+ }
+
+ public function testParsingTextValues()
+ {
+ $savedReport = SavedReport::factory()->create([
+ 'options' => [
+ 'is_a_text_value' => 'some text',
+ ],
+ ]);
+
+ $this->assertEquals('some text', $savedReport->textValue('is_a_text_value'));
+ $this->assertEquals('', $savedReport->textValue('non_existent_key'));
+
+ $this->assertEquals('', (new SavedReport)->textValue('is_a_text_value'));
+ }
+}