Skip to content

Commit 1fe170e

Browse files
authored
Merge pull request #16040 from Godmartinz/template_validate_error
Adds a null check to label templates, adds return types for validation methods
2 parents 612a708 + 0eb34cb commit 1fe170e

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

app/Http/Controllers/SettingsController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ public function postLabels(StoreLabelSettings $request) : RedirectResponse
800800
}
801801

802802
if ($setting->save()) {
803+
803804
return redirect()->route('settings.labels.index')
804805
->with('success', trans('admin/settings/message.update.success'));
805806
}

app/Http/Requests/StoreLabelSettings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace App\Http\Requests;
44

5+
6+
use App\Models\Labels\Label;
57
use Illuminate\Foundation\Http\FormRequest;
68
use Illuminate\Support\Facades\Gate;
9+
use Illuminate\Validation\Rule;
710

811
class StoreLabelSettings extends FormRequest
912
{
@@ -22,6 +25,10 @@ public function authorize(): bool
2225
*/
2326
public function rules(): array
2427
{
28+
$names = Label::find()?->map(function ($label) {
29+
return $label->getName();
30+
})->values()->toArray();
31+
2532
return [
2633
'labels_per_page' => 'numeric',
2734
'labels_width' => 'numeric',
@@ -36,6 +43,10 @@ public function rules(): array
3643
'labels_pagewidth' => 'numeric|nullable',
3744
'labels_pageheight' => 'numeric|nullable',
3845
'qr_text' => 'max:31|nullable',
46+
'label2_template' => [
47+
'required',
48+
Rule::in($names),
49+
],
3950
];
4051
}
4152
}

app/Models/Labels/Label.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,14 @@ public final function write2DBarcode(TCPDF $pdf, $value, $type, $x, $y, $width,
411411
/**
412412
* Checks the template is internally valid
413413
*/
414-
public final function validate() {
414+
public final function validate() : void {
415415
$this->validateUnits();
416416
$this->validateSize();
417417
$this->validateMargins();
418418
$this->validateSupport();
419419
}
420420

421-
private function validateUnits() {
421+
private function validateUnits() : void {
422422
$validUnits = [ 'pt', 'mm', 'cm', 'in' ];
423423
$unit = $this->getUnit();
424424
if (!in_array(strtolower($unit), $validUnits)) {
@@ -430,7 +430,7 @@ private function validateUnits() {
430430
}
431431
}
432432

433-
private function validateSize() {
433+
private function validateSize() : void {
434434
$width = $this->getWidth();
435435
if (!is_numeric($width) || is_string($width)) {
436436
throw new \UnexpectedValueException(trans('admin/labels/message.invalid_return_type', [
@@ -450,7 +450,7 @@ private function validateSize() {
450450
}
451451
}
452452

453-
private function validateMargins() {
453+
private function validateMargins() : void {
454454
$marginTop = $this->getMarginTop();
455455
if (!is_numeric($marginTop) || is_string($marginTop)) {
456456
throw new \UnexpectedValueException(trans('admin/labels/message.invalid_return_type', [
@@ -488,7 +488,7 @@ private function validateMargins() {
488488
}
489489
}
490490

491-
private function validateSupport() {
491+
private function validateSupport() : void {
492492
$support1D = $this->getSupport1DBarcode();
493493
if (!is_bool($support1D)) {
494494
throw new \UnexpectedValueException(trans('admin/labels/message.invalid_return_type', [

app/View/Label.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Models\Labels\Sheet;
88
use Illuminate\Contracts\View\View;
99
use Illuminate\Support\Collection;
10+
use Illuminate\Support\Facades\Log;
1011
use Illuminate\Support\Facades\Storage;
1112
use Illuminate\Support\Traits\Macroable;
1213
use TCPDF;
@@ -38,7 +39,7 @@ public function render(callable $callback = null)
3839
$settings = $this->data->get('settings');
3940
$assets = $this->data->get('assets');
4041
$offset = $this->data->get('offset');
41-
$template = LabelModel::find($settings->label2_template);
42+
4243

4344
// If disabled, pass to legacy view
4445
if ((!$settings->label2_enable)) {
@@ -49,6 +50,12 @@ public function render(callable $callback = null)
4950
->with('count', $this->data->get('count'));
5051
}
5152

53+
$template = LabelModel::find($settings->label2_template);
54+
55+
if ($template === null) {
56+
return redirect()->route('settings.labels.index')->with('error', trans('admin/settings/message.labels.null_template'));
57+
}
58+
5259
$template->validate();
5360

5461
$pdf = new TCPDF(

resources/lang/en-US/admin/settings/message.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
'testing_authentication' => 'Testing LDAP Authentication...',
3737
'authentication_success' => 'User authenticated against LDAP successfully!'
3838
],
39+
'labels' => [
40+
'null_template' => 'Label template not found. Please select a template.',
41+
],
3942
'webhook' => [
4043
'sending' => 'Sending :app test message...',
4144
'success' => 'Your :webhook_name Integration works!',

resources/views/settings/labels.blade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
</div>
3737
<div class="box-body">
3838

39-
4039
<div class="col-md-12">
4140

4241
<!-- New Label Engine -->

0 commit comments

Comments
 (0)