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
5 changes: 5 additions & 0 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
return redirect()->route('settings.labels.index')
->with('success', trans('admin/settings/message.update.success'));
}
Expand Down
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
21 changes: 20 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,24 @@ public function render(callable $callback = null)
->with('count', $this->data->get('count'));
}

try {
$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();
} catch (\UnexpectedValueException $e) {

\Log::error('Validation failed: ' . $e->getMessage());

} catch (\Throwable $e) {

\Log::error('An unexpected error occurred: ' . $e->getMessage());

}
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved

$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.',
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
],
'webhook' => [
'sending' => 'Sending :app test message...',
'success' => 'Your :webhook_name Integration works!',
Expand Down
3 changes: 0 additions & 3 deletions resources/views/settings/labels.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

<div class="row">
<div class="col-sm-10 col-sm-offset-1 col-md-10">

<div class="panel box box-default">
<div class="box-header with-border">
<h2 class="box-title">
Expand All @@ -35,8 +34,6 @@
</h2>
</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