Skip to content

A free open source IT asset/license management system

add custom labels to label table, add doc blocks to CustomUserLabel model

Godfrey M9810fa14cc38
diff --git a/app/Http/Controllers/Api/LabelsController.php b/app/Http/Controllers/Api/LabelsController.php
index 65940dfb62..360df040dc 100644
--- a/app/Http/Controllers/Api/LabelsController.php
+++ b/app/Http/Controllers/Api/LabelsController.php
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Transformers\LabelsTransformer;
+use App\Models\Labels\CustomUserLabel;
use App\Models\Labels\Label;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -21,12 +22,33 @@ class LabelsController extends Controller
{
$this->authorize('view', Label::class);
- $labels = Label::find();
+ $baseLabels = Label::find()
+ ->map(function (Label $label) {
+ return [
+ 'source' => 'base',
+ 'label' => $label,
+ ];
+ });
+
+ $customLabels = CustomUserLabel::query()
+ ->get()
+ ->map(function ($label) {
+ return [
+ 'source' => 'custom',
+ 'label' => $label,
+ ];
+ });
+
+ $labels = $baseLabels->merge($customLabels);
if ($request->filled('search')) {
$search = $request->input('search');
- $labels = $labels->filter(function ($label, $index) use ($search) {
- return stripos($label->getName(), $search) !== false;
+
+ $labels = $labels->filter(function ($row) use ($search) {
+ $label = $row['label'];
+ $name = $row['source'] === 'custom' ? $label->name : $label->getName();
+
+ return stripos($name, $search) !== false;
});
}
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php
index 6c15c9f569..c7cebc3c23 100755
--- a/app/Http/Controllers/LabelsController.php
+++ b/app/Http/Controllers/LabelsController.php
@@ -7,7 +7,9 @@ use App\Models\AssetModel;
use App\Models\Category;
use App\Models\Company;
use App\Models\CustomField;
+use App\Models\Labels\CustomLabels\PreviewLabel;
use App\Models\Labels\CustomUserLabel;
+use App\Models\Labels\CustomLabels\CustomSheetLabel;
use App\Models\Labels\DefaultLabel;
use App\Models\Labels\Label;
use App\Models\Location;
@@ -115,7 +117,15 @@ class LabelsController extends Controller
]);
$supports = collect($validated['supports'])
- ->map(fn($value) => (bool)$value)
+ ->map(function ($value, $key) {
+ if ($key === 'fields') {
+ return (int)$value;
+ }
+
+ return in_array($key, ['asset_tag', 'barcode_1d', 'barcode_2d', 'logo', 'title'], true)
+ ? (bool)$value
+ : $value;
+ })
->toArray();
$castNumeric = function ($array) {
@@ -129,7 +139,7 @@ class LabelsController extends Controller
$page = $castNumeric($validated['page']);
$grid = $castNumeric($validated['grid']);
- $label = $castNumeric($validated['label']);
+ $labelConfig = $castNumeric($validated['label']);
$content = $castNumeric($validated['content']);
$baseLabel = CustomUserLabel::makeBaseLabel($validated['template'] ?? null);
@@ -137,22 +147,35 @@ class LabelsController extends Controller
if (!$baseLabel) {
return redirect()->back()->with('error', trans('admin/labels/labels.base_label_missing'));
}
- $baseConfig = $baseLabel->getEditorConfigSections();
- $finalConfig = [
+ // Loading both base and working label in the preview to ensure millimeter-based comparisons for overrides.
+ $baseWorkingLabel = new PreviewLabel();
+ $baseWorkingLabel->seedFromTemplate($baseLabel);
+ $baseConfig = $baseWorkingLabel->getEditorConfigSections();
+
+ $submittedConfig = [
'page' => $page,
'grid' => $grid,
- 'label' => $label,
+ 'label' => $labelConfig,
'content' => $content,
'supports' => $supports,
];
+ $mergedConfig = array_replace_recursive($baseConfig, $submittedConfig);
+
+ $workingLabel = new PreviewLabel();
+ $workingLabel->seedFromTemplate($baseLabel);
+ $workingLabel->applyEditorConfig($mergedConfig);
+
+ $finalConfig = $workingLabel->getEditorConfigSections();
+
$configSnapshot = [
'template' => $validated['template'],
'type' => $validated['type'] ?? 'sheet',
'name' => $validated['name'],
...$finalConfig,
];
+
$overrides = CustomUserLabel::diffEditorConfig($finalConfig, $baseConfig);
$customLabel = CustomUserLabel::create([
@@ -165,7 +188,7 @@ class LabelsController extends Controller
]);
return app(SettingsController::class)->getLabels()
- ->with('success', $customLabel['name'] . ' created successfully.');
+ ->with('success', $customLabel->name . ' created successfully.');
}
public function edit(Request $request)
diff --git a/app/Http/Transformers/LabelsTransformer.php b/app/Http/Transformers/LabelsTransformer.php
index b37b655751..f4ee49aae6 100644
--- a/app/Http/Transformers/LabelsTransformer.php
+++ b/app/Http/Transformers/LabelsTransformer.php
@@ -6,20 +6,29 @@ use App\Models\Labels\Label;
use App\Models\Labels\RectangleSheet;
use App\Models\Labels\Sheet;
use Illuminate\Support\Collection;
+use App\Models\Labels\CustomUserLabel;
class LabelsTransformer
{
public function transformLabels(Collection $labels, $total)
{
$array = [];
- foreach ($labels as $label) {
- $array[] = self::transformLabel($label);
+ foreach ($labels as $row) {
+ $array[] = self::transformLabelRow($row);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
- public function transformLabel(Label $label)
+ protected function transformLabelRow(array $row): array
+ {
+ return match ($row['source']) {
+ 'custom' => $this->transformCustomLabel($row['label']),
+ default => $this->transformBaseLabel($row['label']),
+ };
+ }
+
+ public function transformBaseLabel(Label $label): array
{
$array = [
'name' => $label->getName(),
@@ -67,4 +76,53 @@ class LabelsTransformer
return $array;
}
+
+ protected function transformCustomLabel(CustomUserLabel $label): array
+ {
+ $snapshot = $label->config_snapshot ?? [];
+
+ return [
+ 'name' => $label->name,
+ 'source' => 'custom',
+ 'source_label' => 'Custom',
+ 'base_label' => $label->base_label,
+ 'type' => $label->type,
+ 'is_default' => $label->is_default,
+
+ 'unit' => 'mm',
+
+ 'width' => number_format((float)data_get($snapshot, 'label.width', 0), 2),
+ 'height' => number_format((float)data_get($snapshot, 'label.height', 0), 2),
+
+ 'margin_top' => data_get($snapshot, 'label.margin_top'),
+ 'margin_bottom' => data_get($snapshot, 'label.margin_bottom'),
+ 'margin_left' => data_get($snapshot, 'label.margin_left'),
+ 'margin_right' => data_get($snapshot, 'label.margin_right'),
+
+ 'support_asset_tag' => data_get($snapshot, 'supports.asset_tag'),
+ 'support_1d_barcode' => data_get($snapshot, 'supports.barcode_1d'),
+ 'support_2d_barcode' => data_get($snapshot, 'supports.barcode_2d'),
+ 'support_fields' => data_get($snapshot, 'supports.fields'),
+ 'support_logo' => data_get($snapshot, 'supports.logo'),
+ 'support_title' => data_get($snapshot, 'supports.title'),
+
+ 'sheet_info' => [
+ 'label_width' => data_get($snapshot, 'label.width'),
+ 'label_height' => data_get($snapshot, 'label.height'),
+ 'label_margin_top' => data_get($snapshot, 'label.margin_top'),
+ 'label_margin_bottom' => data_get($snapshot, 'label.margin_bottom'),
+ 'label_margin_left' => data_get($snapshot, 'label.margin_left'),
+ 'label_margin_right' => data_get($snapshot, 'label.margin_right'),
+ 'labels_per_page' => data_get($snapshot, 'grid.rows', 1) * data_get($snapshot, 'grid.columns', 1),
+ 'label_border' => data_get($snapshot, 'label.border'),
+ ],
+
+ 'rectanglesheet_info' => [
+ 'columns' => data_get($snapshot, 'grid.columns'),
+ 'rows' => data_get($snapshot, 'grid.rows'),
+ 'column_spacing' => data_get($snapshot, 'grid.column_spacing'),
+ 'row_spacing' => data_get($snapshot, 'grid.row_spacing'),
+ ],
+ ];
+ }
}
diff --git a/app/Models/Labels/CustomUserLabel.php b/app/Models/Labels/CustomUserLabel.php
index 0a15258f4d..9bd3b77f50 100644
--- a/app/Models/Labels/CustomUserLabel.php
+++ b/app/Models/Labels/CustomUserLabel.php
@@ -26,11 +26,14 @@ class CustomUserLabel extends Model
'is_default' => 'boolean',
];
-
- public function applyOverrides(array $config): array
- {
- return array_merge($config, $this->overrides ?? []);
- }
+ /**
+ * Builds a lookup of base label templates for instantiation.
+ * Example:
+ * [
+ * 'L4736_A' => App\Models\Labels\Sheets\Avery\L4736_A::class,
+ * 'DefaultLabel' => App\Models\Labels\DefaultLabel::class,
+ * ]
+ */
public static function availableBaseLabels(): array
{
@@ -81,6 +84,12 @@ class CustomUserLabel extends Model
return $labels;
}
+ /**
+ * Creates a base label instance from a template.
+ *
+ * @param string|null $template
+ * @return object|null
+ */
public static function makeBaseLabel(?string $template): ?object
{
if (!$template) {
@@ -88,8 +97,9 @@ class CustomUserLabel extends Model
}
$available = static::availableBaseLabels();
+ $templateKey = class_basename(str_replace('\\', '/', $template));
- $class = $available[$template] ?? null;
+ $class = $available[$templateKey] ?? null;
if (!$class || !class_exists($class)) {
return null;
@@ -98,12 +108,22 @@ class CustomUserLabel extends Model
return new $class();
}
+ /**
+ * Returns the differences between the final config and base config.
+ *
+ * Only includes values that have been added or overridden.
+ *
+ * @param array<string, mixed> $finalConfig
+ * @param array<string, mixed> $baseConfig
+ * @return array<string, mixed>
+ */
public static function diffEditorConfig(array $finalConfig, array $baseConfig): array
{
$diff = [];
foreach ($finalConfig as $key => $value) {
- $baseValue = $baseConfig[$key] ?? null;
+ $hasBaseKey = array_key_exists($key, $baseConfig);
+ $baseValue = $hasBaseKey ? $baseConfig[$key] : null;
if (is_array($value) && is_array($baseValue)) {
$nestedDiff = static::diffEditorConfig($value, $baseValue);
@@ -115,11 +135,46 @@ class CustomUserLabel extends Model
continue;
}
- if ($value !== $baseValue) {
- $diff[$key] = $value;
+ if (!$hasBaseKey) {
+ $diff[$key] = static::normalizeDiffValue($value);
+ continue;
+ }
+
+ if (static::valuesDiffer($value, $baseValue)) {
+ $diff[$key] = static::normalizeDiffValue($value);
}
}
return $diff;
}
+
+ /**
+ * Determines if values differ for override detection, applying a tolerance
+ * to prevent insignificant numeric differences from being treated as changes.
+ */
+ protected static function valuesDiffer($value, $baseValue, float $epsilon = 0.001): bool
+ {
+ if (is_bool($value) || is_bool($baseValue)) {
+ return (bool)$value !== (bool)$baseValue;
+ }
+
+ if (is_numeric($value) && is_numeric($baseValue)) {
+ return abs((float)$value - (float)$baseValue) > $epsilon;
+ }
+
+ return $value !== $baseValue;
+ }
+
+ /**
+ * Normalizes values for diff output, rounding numerics to reduce noise
+ * from minor precision differences.
+ */
+ protected static function normalizeDiffValue($value)
+ {
+ if (is_numeric($value)) {
+ return round((float)$value, 3);
+ }
+
+ return $value;
+ }
}
\ No newline at end of file