From c17c011488861014169a53c7d2cd7ed086a7d1e0 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 8 Jan 2025 09:47:20 -0800 Subject: [PATCH 1/8] checks if template is null, adds return types for validation methods --- app/Models/Labels/Label.php | 10 +++++----- app/View/Label.php | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/Models/Labels/Label.php b/app/Models/Labels/Label.php index bf03236d00ab..87e5f3bee122 100644 --- a/app/Models/Labels/Label.php +++ b/app/Models/Labels/Label.php @@ -411,14 +411,14 @@ public final function write2DBarcode(TCPDF $pdf, $value, $type, $x, $y, $width, /** * Checks the template is internally valid */ - public final function validate() { + public final function validate() : void { $this->validateUnits(); $this->validateSize(); $this->validateMargins(); $this->validateSupport(); } - private function validateUnits() { + private function validateUnits() : void { $validUnits = [ 'pt', 'mm', 'cm', 'in' ]; $unit = $this->getUnit(); if (!in_array(strtolower($unit), $validUnits)) { @@ -430,7 +430,7 @@ private function validateUnits() { } } - private function validateSize() { + private function validateSize() : void { $width = $this->getWidth(); if (!is_numeric($width) || is_string($width)) { throw new \UnexpectedValueException(trans('admin/labels/message.invalid_return_type', [ @@ -450,7 +450,7 @@ private function validateSize() { } } - private function validateMargins() { + private function validateMargins() : void { $marginTop = $this->getMarginTop(); if (!is_numeric($marginTop) || is_string($marginTop)) { throw new \UnexpectedValueException(trans('admin/labels/message.invalid_return_type', [ @@ -488,7 +488,7 @@ private function validateMargins() { } } - private function validateSupport() { + private function validateSupport() : void{ $support1D = $this->getSupport1DBarcode(); if (!is_bool($support1D)) { throw new \UnexpectedValueException(trans('admin/labels/message.invalid_return_type', [ diff --git a/app/View/Label.php b/app/View/Label.php index c28b8a95949b..a7c6641a519c 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -49,6 +49,10 @@ public function render(callable $callback = null) ->with('count', $this->data->get('count')); } + if ($template === null) { + throw new \UnexpectedValueException('Template is null.'); + } + $template->validate(); $pdf = new TCPDF( From ce7a8ad808d6d2e2a902756df2f98545644c5fc1 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 8 Jan 2025 09:48:45 -0800 Subject: [PATCH 2/8] add a space --- app/Models/Labels/Label.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Labels/Label.php b/app/Models/Labels/Label.php index 87e5f3bee122..2405da5401d1 100644 --- a/app/Models/Labels/Label.php +++ b/app/Models/Labels/Label.php @@ -488,7 +488,7 @@ private function validateMargins() : void { } } - private function validateSupport() : void{ + private function validateSupport() : void { $support1D = $this->getSupport1DBarcode(); if (!is_bool($support1D)) { throw new \UnexpectedValueException(trans('admin/labels/message.invalid_return_type', [ From f4e69679ca0a7e7cd87dd785bc47aeef1e7b51d1 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 8 Jan 2025 12:13:31 -0800 Subject: [PATCH 3/8] add a try catch around template validation --- app/View/Label.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/View/Label.php b/app/View/Label.php index a7c6641a519c..e3f0ba72480e 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -38,7 +38,6 @@ public function render(callable $callback = null) $settings = $this->data->get('settings'); $assets = $this->data->get('assets'); $offset = $this->data->get('offset'); - $template = LabelModel::find($settings->label2_template); // If disabled, pass to legacy view if ((!$settings->label2_enable)) { @@ -49,11 +48,24 @@ public function render(callable $callback = null) ->with('count', $this->data->get('count')); } - if ($template === null) { - throw new \UnexpectedValueException('Template is null.'); + try { + $template = LabelModel::find($settings->label2_template); + + if ($template === null) { + throw new \UnexpectedValueException('Template is null.'); + } + + $template->validate(); + } catch (\UnexpectedValueException $e) { + + \Log::error('Validation failed: ' . $e->getMessage()); + + } catch (\Throwable $e) { + + \Log::error('An unexpected error occurred: ' . $e->getMessage()); + } - $template->validate(); $pdf = new TCPDF( $template->getOrientation(), From 0118504cd3c15fedf49f2d04c93897e5adf078d5 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 8 Jan 2025 15:32:23 -0800 Subject: [PATCH 4/8] adds redirect to render and save if template is null --- app/Http/Controllers/SettingsController.php | 5 +++++ app/View/Label.php | 21 +++++++++++-------- .../lang/en-US/admin/settings/message.php | 3 +++ resources/views/settings/labels.blade.php | 3 --- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index b89a221faa07..603cc7455c93 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -798,7 +798,12 @@ public function postLabels(StoreLabelSettings $request) : RedirectResponse $setting->labels_display_model = 0; } + if ($setting->save()) { + if ($setting->label2_template === null) { + return redirect()->route('settings.labels.index')->with('error', trans('admin/settings/message.labels.null_template')); + } + return redirect()->route('settings.labels.index') ->with('success', trans('admin/settings/message.update.success')); } diff --git a/app/View/Label.php b/app/View/Label.php index e3f0ba72480e..4faa6ed97efb 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -7,6 +7,7 @@ use App\Models\Labels\Sheet; use Illuminate\Contracts\View\View; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Traits\Macroable; use TCPDF; @@ -39,6 +40,7 @@ public function render(callable $callback = null) $assets = $this->data->get('assets'); $offset = $this->data->get('offset'); + // If disabled, pass to legacy view if ((!$settings->label2_enable)) { return view('hardware/labels') @@ -51,21 +53,22 @@ public function render(callable $callback = null) try { $template = LabelModel::find($settings->label2_template); - if ($template === null) { - throw new \UnexpectedValueException('Template is null.'); - } + if ($template === null) { + return redirect()->route('settings.labels.index')->with('error', trans('admin/settings/message.labels.null_template')); + } - $template->validate(); - } catch (\UnexpectedValueException $e) { + $template->validate(); + } catch (\UnexpectedValueException $e) { - \Log::error('Validation failed: ' . $e->getMessage()); + \Log::error('Validation failed: ' . $e->getMessage()); - } catch (\Throwable $e) { + } catch (\Throwable $e) { - \Log::error('An unexpected error occurred: ' . $e->getMessage()); + \Log::error('An unexpected error occurred: ' . $e->getMessage()); - } + } + $template->validate(); $pdf = new TCPDF( $template->getOrientation(), diff --git a/resources/lang/en-US/admin/settings/message.php b/resources/lang/en-US/admin/settings/message.php index c91575144e5d..727b27b021e1 100644 --- a/resources/lang/en-US/admin/settings/message.php +++ b/resources/lang/en-US/admin/settings/message.php @@ -36,6 +36,9 @@ 'testing_authentication' => 'Testing LDAP Authentication...', 'authentication_success' => 'User authenticated against LDAP successfully!' ], + 'labels' => [ + 'null_template' => 'Label template not found, Please select a template.', + ], 'webhook' => [ 'sending' => 'Sending :app test message...', 'success' => 'Your :webhook_name Integration works!', diff --git a/resources/views/settings/labels.blade.php b/resources/views/settings/labels.blade.php index 4c01c2177d27..4e17a77b3739 100644 --- a/resources/views/settings/labels.blade.php +++ b/resources/views/settings/labels.blade.php @@ -26,7 +26,6 @@
-

@@ -35,8 +34,6 @@

- -
From 3ac08774183f3385d9dad6adceb53382bb04f887 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 14 Jan 2025 09:50:43 -0800 Subject: [PATCH 5/8] remove try/catch, add rules for template --- app/Http/Controllers/SettingsController.php | 4 ---- app/Http/Requests/StoreLabelSettings.php | 1 + app/View/Label.php | 12 ------------ 3 files changed, 1 insertion(+), 16 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 603cc7455c93..e963baaf5cb7 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -798,11 +798,7 @@ public function postLabels(StoreLabelSettings $request) : RedirectResponse $setting->labels_display_model = 0; } - if ($setting->save()) { - if ($setting->label2_template === null) { - return redirect()->route('settings.labels.index')->with('error', trans('admin/settings/message.labels.null_template')); - } return redirect()->route('settings.labels.index') ->with('success', trans('admin/settings/message.update.success')); diff --git a/app/Http/Requests/StoreLabelSettings.php b/app/Http/Requests/StoreLabelSettings.php index a203d2702df7..e11ead8bafaa 100644 --- a/app/Http/Requests/StoreLabelSettings.php +++ b/app/Http/Requests/StoreLabelSettings.php @@ -36,6 +36,7 @@ public function rules(): array 'labels_pagewidth' => 'numeric|nullable', 'labels_pageheight' => 'numeric|nullable', 'qr_text' => 'max:31|nullable', + 'label2_template' => 'required|string', ]; } } diff --git a/app/View/Label.php b/app/View/Label.php index 4faa6ed97efb..27406937dca6 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -50,7 +50,6 @@ public function render(callable $callback = null) ->with('count', $this->data->get('count')); } - try { $template = LabelModel::find($settings->label2_template); if ($template === null) { @@ -58,17 +57,6 @@ public function render(callable $callback = null) } $template->validate(); - } catch (\UnexpectedValueException $e) { - - \Log::error('Validation failed: ' . $e->getMessage()); - - } catch (\Throwable $e) { - - \Log::error('An unexpected error occurred: ' . $e->getMessage()); - - } - - $template->validate(); $pdf = new TCPDF( $template->getOrientation(), From 69d255f584bee46f147c4d74f99fd7ab51e70a41 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 14 Jan 2025 12:07:59 -0800 Subject: [PATCH 6/8] adds validation rules for label names --- app/Http/Requests/StoreLabelSettings.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/Http/Requests/StoreLabelSettings.php b/app/Http/Requests/StoreLabelSettings.php index e11ead8bafaa..6a39418a6719 100644 --- a/app/Http/Requests/StoreLabelSettings.php +++ b/app/Http/Requests/StoreLabelSettings.php @@ -2,8 +2,11 @@ namespace App\Http\Requests; + +use App\Models\Labels\Label; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Facades\Gate; +use Illuminate\Validation\Rule; class StoreLabelSettings extends FormRequest { @@ -22,6 +25,10 @@ public function authorize(): bool */ public function rules(): array { + $names = Label::find()?->map(function ($label) { + return $label->getName(); + })->values()->toArray(); + return [ 'labels_per_page' => 'numeric', 'labels_width' => 'numeric', @@ -36,7 +43,10 @@ public function rules(): array 'labels_pagewidth' => 'numeric|nullable', 'labels_pageheight' => 'numeric|nullable', 'qr_text' => 'max:31|nullable', - 'label2_template' => 'required|string', + 'label2_template' => [ + 'required', + Rule::in($names), + ], ]; } } From 79a4c8387955e4c20b20aad6de60a1bfe665d127 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 16 Jan 2025 13:23:16 -0800 Subject: [PATCH 7/8] add spaces --- resources/views/settings/labels.blade.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/resources/views/settings/labels.blade.php b/resources/views/settings/labels.blade.php index 4e17a77b3739..488fce1ee75a 100644 --- a/resources/views/settings/labels.blade.php +++ b/resources/views/settings/labels.blade.php @@ -26,6 +26,7 @@
+

@@ -34,6 +35,7 @@

+
From 0eb34cb979edfe5f0eca927a4dc6f0f41994ea32 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 16 Jan 2025 13:24:32 -0800 Subject: [PATCH 8/8] adds period to translation --- resources/lang/en-US/admin/settings/message.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lang/en-US/admin/settings/message.php b/resources/lang/en-US/admin/settings/message.php index 727b27b021e1..c0cd20979361 100644 --- a/resources/lang/en-US/admin/settings/message.php +++ b/resources/lang/en-US/admin/settings/message.php @@ -37,7 +37,7 @@ 'authentication_success' => 'User authenticated against LDAP successfully!' ], 'labels' => [ - 'null_template' => 'Label template not found, Please select a template.', + 'null_template' => 'Label template not found. Please select a template.', ], 'webhook' => [ 'sending' => 'Sending :app test message...',