Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a null check to label templates, adds return types for validation methods #16040

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
11 changes: 11 additions & 0 deletions app/Http/Requests/StoreLabelSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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',
Expand All @@ -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),
],
];
}
}
10 changes: 5 additions & 5 deletions app/Models/Labels/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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', [
Expand All @@ -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', [
Expand Down Expand Up @@ -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', [
Expand Down
9 changes: 8 additions & 1 deletion app/View/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions resources/lang/en-US/admin/settings/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!',
Expand Down
1 change: 0 additions & 1 deletion resources/views/settings/labels.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
</div>
<div class="box-body">

Godmartinz marked this conversation as resolved.
Show resolved Hide resolved

<div class="col-md-12">

<!-- New Label Engine -->
Expand Down
Loading