From 7a5faa96196c3ebc12c8b6461e8575d0ba6a5f4d Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 30 Oct 2023 12:03:50 -0700 Subject: [PATCH 1/7] Adjust margin on custom report page --- resources/views/reports/custom.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/reports/custom.blade.php b/resources/views/reports/custom.blade.php index 78c07c519..f8427f646 100644 --- a/resources/views/reports/custom.blade.php +++ b/resources/views/reports/custom.blade.php @@ -14,7 +14,7 @@ @section('content')
-
+
{{ Form::open(['method' => 'post', 'class' => 'form-horizontal', 'id' => 'custom-report-form']) }} {{csrf_field()}} From 06186c9b12ff7cd1103e2b8ab41217147802a607 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 30 Oct 2023 16:30:53 -0700 Subject: [PATCH 2/7] WIP: Simply post the form to a different controller --- app/Http/Controllers/ReportsController.php | 1 + .../Controllers/SavedReportsController.php | 7 +-- resources/views/reports/custom.blade.php | 50 +++++++++++-------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index a04cd8356..4b4db866a 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -407,6 +407,7 @@ class ReportsController extends Controller */ public function postCustom(Request $request) { + dd('postCustom', $request->all()); ini_set('max_execution_time', env('REPORT_TIME_LIMIT', 12000)); //12000 seconds = 200 minutes $this->authorize('reports.view'); diff --git a/app/Http/Controllers/SavedReportsController.php b/app/Http/Controllers/SavedReportsController.php index 519e07b79..31b91f034 100644 --- a/app/Http/Controllers/SavedReportsController.php +++ b/app/Http/Controllers/SavedReportsController.php @@ -6,13 +6,8 @@ use Illuminate\Http\Request; class SavedReportsController extends Controller { - //a method to the madness public function store(Request $request) { - - - dd($request->all()); - + dd('saved reports', $request->all()); } - } diff --git a/resources/views/reports/custom.blade.php b/resources/views/reports/custom.blade.php index f8427f646..94bdc3787 100644 --- a/resources/views/reports/custom.blade.php +++ b/resources/views/reports/custom.blade.php @@ -16,7 +16,12 @@
- {{ Form::open(['method' => 'post', 'class' => 'form-horizontal', 'id' => 'custom-report-form']) }} + {{ Form::open([ + 'method' => 'post', + 'class' => 'form-horizontal', + 'id' => 'custom-report-form', + 'url' => '/reports/custom', + ]) }} {{csrf_field()}} @@ -449,27 +454,28 @@ $("#savetemplateform").submit(function(e) { e.preventDefault(e); - let elements = Array.from(document.getElementById("custom-report-form").elements).map(item=>item.name); - console.log(elements); - - $("#savetemplateoptions").val(elements) - - let formElement = document.getElementById('custom-report-form') - - let inputsAsArray = Array.from(formElement.elements) - - inputsAsArray.map(function(item){ - // not a real method - if (item.isACheckbox()){ - return {name: item.name, type: checkbox, checked: item.checked}; - } - - if (item.isASelect){ - return {name:item.name, type: select, selected: [item.elements]} - } - }) - // set hidden input to variable - e.currentTarget.submit(); + $('#custom-report-form').attr('action', '/reports/savedtemplate').submit() + // let elements = Array.from(document.getElementById("custom-report-form").elements).map(item=>item.name); + // console.log(elements); + // + // $("#savetemplateoptions").val(elements) + // + // let formElement = document.getElementById('custom-report-form') + // + // let inputsAsArray = Array.from(formElement.elements) + // + // inputsAsArray.map(function(item){ + // // not a real method + // if (item.isACheckbox()){ + // return {name: item.name, type: checkbox, checked: item.checked}; + // } + // + // if (item.isASelect){ + // return {name:item.name, type: select, selected: [item.elements]} + // } + // }) + // // set hidden input to variable + // e.currentTarget.submit(); }); From b7011d853a1a5fd0236a26b9e437bca786584d68 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 2 Nov 2023 17:10:50 -0700 Subject: [PATCH 3/7] WIP: add methods to restore settings from saved report --- app/Http/Controllers/ReportsController.php | 8 +- .../Controllers/SavedReportsController.php | 10 ++- app/Models/SavedReport.php | 26 ++++++ database/factories/SavedReportFactory.php | 21 +++++ resources/views/reports/custom.blade.php | 82 +++++++++---------- tests/Unit/SavedReportTest.php | 37 +++++++++ 6 files changed, 141 insertions(+), 43 deletions(-) create mode 100644 database/factories/SavedReportFactory.php create mode 100644 tests/Unit/SavedReportTest.php diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 4b4db866a..d60e201e7 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 31b91f034..fcb9b5460 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 cd1cdd7e5..81c5b9a66 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 000000000..72279bd3e --- /dev/null +++ b/database/factories/SavedReportFactory.php @@ -0,0 +1,21 @@ + $this->faker->word(), + 'options' => json_encode([]), + ]; + } +} diff --git a/resources/views/reports/custom.blade.php b/resources/views/reports/custom.blade.php index 94bdc3787..b9ce08a8c 100644 --- a/resources/views/reports/custom.blade.php +++ b/resources/views/reports/custom.blade.php @@ -40,147 +40,147 @@ @@ -190,32 +190,32 @@

{{ trans('general.checked_out_to') }} {{ trans('general.fields') }}:

@@ -228,7 +228,7 @@ @foreach ($customfields as $customfield) @@ -258,7 +258,7 @@
- +
@@ -266,9 +266,9 @@
- + to - +
@@ -324,13 +324,13 @@
diff --git a/tests/Unit/SavedReportTest.php b/tests/Unit/SavedReportTest.php new file mode 100644 index 000000000..a99408a84 --- /dev/null +++ b/tests/Unit/SavedReportTest.php @@ -0,0 +1,37 @@ +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')); + } +} From 4f031149e83828bb9390ec5bd3ccdb2b9b2afc5c Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 30 Nov 2023 12:12:57 -0800 Subject: [PATCH 4/7] Scaffold a couple test cases --- tests/Unit/SavedReportTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Unit/SavedReportTest.php b/tests/Unit/SavedReportTest.php index a99408a84..fad10abbb 100644 --- a/tests/Unit/SavedReportTest.php +++ b/tests/Unit/SavedReportTest.php @@ -34,4 +34,18 @@ class SavedReportTest extends TestCase $this->assertEquals('', (new SavedReport)->textValue('is_a_text_value')); } + + public function testParsingSelectValues() + { + $this->markTestIncomplete(); + } + + public function testSelectValuesDoNotIncludeDeletedModels() + { + $this->markTestIncomplete(); + + // report saved with select option for a company (or whatever) + // company is deleted + // ensure company's id is not returned + } } From bca7f208a6c44640a19113809775a36f65c96697 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 30 Nov 2023 16:57:21 -0800 Subject: [PATCH 5/7] Implement restoring select values --- app/Models/SavedReport.php | 22 ++++++++- .../forms/edit/category-select.blade.php | 12 +++++ .../forms/edit/company-select.blade.php | 7 +++ .../forms/edit/department-select.blade.php | 12 +++++ .../forms/edit/location-select.blade.php | 7 +++ .../forms/edit/manufacturer-select.blade.php | 12 +++++ .../forms/edit/model-select.blade.php | 12 +++++ .../forms/edit/status-select.blade.php | 7 +++ .../forms/edit/supplier-select.blade.php | 7 +++ resources/views/reports/custom.blade.php | 18 ++++---- tests/Unit/SavedReportTest.php | 45 ++++++++++++++++--- 11 files changed, 145 insertions(+), 16 deletions(-) diff --git a/app/Models/SavedReport.php b/app/Models/SavedReport.php index 81c5b9a66..afefc37ba 100644 --- a/app/Models/SavedReport.php +++ b/app/Models/SavedReport.php @@ -24,7 +24,7 @@ class SavedReport extends Model 'options', ]; - public function checkmarkValue($property) + public function checkmarkValue($property): string { // Assuming we're using the null object pattern, // return the default value if the object is not saved yet. @@ -37,7 +37,25 @@ class SavedReport extends Model return $this->options[$property] ?? '0'; } - public function textValue($property) + public function selectValue($property) + { + return $this->options[$property] ?? null; + } + + public function selectValues($property) + { + if (!isset($this->options[$property])) { + return null; + } + + if ($this->options[$property] === [null]) { + return null; + } + + return $this->options[$property]; + } + + public function textValue($property): string { // Assuming we're using the null object pattern, // return the default value if the object is not saved yet. diff --git a/resources/views/partials/forms/edit/category-select.blade.php b/resources/views/partials/forms/edit/category-select.blade.php index 684b8d76d..ea473d9c5 100644 --- a/resources/views/partials/forms/edit/category-select.blade.php +++ b/resources/views/partials/forms/edit/category-select.blade.php @@ -5,6 +5,18 @@
+ @isset ($selected) + @foreach ($selected as $company_id) + + @endforeach + @endisset @if ($company_id = Request::old($fieldname, (isset($item)) ? $item->{$fieldname} : ''))