diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index b89a221faa07..e963baaf5cb7 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -799,6 +799,7 @@ public function postLabels(StoreLabelSettings $request) : RedirectResponse } if ($setting->save()) { + 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..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,6 +43,10 @@ public function rules(): array 'labels_pagewidth' => 'numeric|nullable', 'labels_pageheight' => 'numeric|nullable', 'qr_text' => 'max:31|nullable', + 'label2_template' => [ + 'required', + Rule::in($names), + ], ]; } } diff --git a/app/Models/Labels/Label.php b/app/Models/Labels/Label.php index bf03236d00ab..2405da5401d1 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..27406937dca6 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; @@ -38,7 +39,7 @@ 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,6 +50,12 @@ public function render(callable $callback = null) ->with('count', $this->data->get('count')); } + $template = LabelModel::find($settings->label2_template); + + if ($template === null) { + return redirect()->route('settings.labels.index')->with('error', trans('admin/settings/message.labels.null_template')); + } + $template->validate(); $pdf = new TCPDF( diff --git a/resources/lang/en-US/admin/settings/message.php b/resources/lang/en-US/admin/settings/message.php index c91575144e5d..c0cd20979361 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..488fce1ee75a 100644 --- a/resources/views/settings/labels.blade.php +++ b/resources/views/settings/labels.blade.php @@ -36,7 +36,6 @@