diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 0b15f35626..7d2d2660ab 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -800,6 +800,7 @@ class SettingsController extends Controller } 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 a203d2702d..6a39418a67 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 @@ class StoreLabelSettings extends FormRequest */ 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 @@ class StoreLabelSettings extends FormRequest '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 bf03236d00..2405da5401 100644 --- a/app/Models/Labels/Label.php +++ b/app/Models/Labels/Label.php @@ -411,14 +411,14 @@ abstract class Label /** * 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 @@ abstract class Label } } - 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 @@ abstract class Label } } - 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 @@ abstract class Label } } - 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 22dbe6d26e..e213646a1c 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -7,6 +7,7 @@ use App\Models\Labels\Label as LabelModel; 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 @@ class Label implements View $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 @@ class Label implements View ->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 a256402c68..9be2901747 100644 --- a/resources/lang/en-US/admin/settings/message.php +++ b/resources/lang/en-US/admin/settings/message.php @@ -36,6 +36,9 @@ return [ '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 68f87bf6fe..0a32fcbadd 100644 --- a/resources/views/settings/labels.blade.php +++ b/resources/views/settings/labels.blade.php @@ -36,7 +36,6 @@