Skip to content

A free open source IT asset/license management system

adds label directory search for template match, override diff logic

Godfrey M5d7c9e630430
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php
index a065da0423..6c15c9f569 100755
--- a/app/Http/Controllers/LabelsController.php
+++ b/app/Http/Controllers/LabelsController.php
@@ -131,7 +131,14 @@ class LabelsController extends Controller
$grid = $castNumeric($validated['grid']);
$label = $castNumeric($validated['label']);
$content = $castNumeric($validated['content']);
- $baseLabel = Models\Labels\
+
+ $baseLabel = CustomUserLabel::makeBaseLabel($validated['template'] ?? null);
+
+ if (!$baseLabel) {
+ return redirect()->back()->with('error', trans('admin/labels/labels.base_label_missing'));
+ }
+ $baseConfig = $baseLabel->getEditorConfigSections();
+
$finalConfig = [
'page' => $page,
'grid' => $grid,
@@ -139,22 +146,16 @@ class LabelsController extends Controller
'content' => $content,
'supports' => $supports,
];
- $baseConfig = [
- 'page' => $baseLabel->getPageEditorConfig(),
- 'grid' => $baseLabel->getGridEditorConfig(),
- 'label' => $baseLabel->getLabelEditorConfig(),
- 'content' => $baseLabel->getContentEditorConfig(),
- 'supports' => $baseLabel->getSupportsEditorConfig(),
- ];
$configSnapshot = [
'template' => $validated['template'],
'type' => $validated['type'] ?? 'sheet',
'name' => $validated['name'],
- ...$overrides,
+ ...$finalConfig,
];
+ $overrides = CustomUserLabel::diffEditorConfig($finalConfig, $baseConfig);
- $label = CustomUserLabel::create([
+ $customLabel = CustomUserLabel::create([
'name' => $validated['name'],
'base_label' => $validated['template'] ?? null,
'type' => $validated['type'] ?? 'sheet',
@@ -164,7 +165,7 @@ class LabelsController extends Controller
]);
return app(SettingsController::class)->getLabels()
- ->with('success', $label['name'] . ' created successfully.');
+ ->with('success', $customLabel['name'] . ' created successfully.');
}
public function edit(Request $request)
diff --git a/app/Models/Labels/CustomUserLabel.php b/app/Models/Labels/CustomUserLabel.php
index dee582b1bb..0a15258f4d 100644
--- a/app/Models/Labels/CustomUserLabel.php
+++ b/app/Models/Labels/CustomUserLabel.php
@@ -3,6 +3,9 @@
namespace App\Models\Labels;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Str;
+use RecursiveDirectoryIterator;
+use RecursiveIteratorIterator;
class CustomUserLabel extends Model
{
@@ -29,24 +32,94 @@ class CustomUserLabel extends Model
return array_merge($config, $this->overrides ?? []);
}
- protected function resolveLabelClass(string $template): string
+ public static function availableBaseLabels(): array
{
- // Custom labels first
- $custom = "App\\Models\\Labels\\CustomLabels\\{$template}";
- if (class_exists($custom)) {
- return $custom;
+ $namespaceRoot = 'App\\Models\\Labels\\';
+ $basePath = app_path('Models/Labels');
+
+ $allowedRoots = [
+ $basePath . '/Sheets',
+ $basePath . '/Tapes',
+ $basePath . '/DefaultLabel.php',
+ ];
+
+ $labels = [];
+
+ foreach ($allowedRoots as $root) {
+ if (is_file($root)) {
+ $files = [new \SplFileInfo($root)];
+ } else {
+ $files = new \RecursiveIteratorIterator(
+ new \RecursiveDirectoryIterator($root)
+ );
+ }
+
+ foreach ($files as $file) {
+ if (!$file->isFile() || $file->getExtension() !== 'php') {
+ continue;
+ }
+
+ $fullPath = $file->getPathname();
+ $relativePath = Str::after($fullPath, $basePath . DIRECTORY_SEPARATOR);
+
+ $class = $namespaceRoot . str_replace(
+ [DIRECTORY_SEPARATOR, '.php'],
+ ['\\', ''],
+ $relativePath
+ );
+
+ if (!class_exists($class)) {
+ continue;
+ }
+
+ $labels[class_basename($class)] = $class;
+ }
+ }
+
+ ksort($labels);
+
+ return $labels;
+ }
+
+ public static function makeBaseLabel(?string $template): ?object
+ {
+ if (!$template) {
+ return null;
}
- // Avery sheets
- if (!str_starts_with($template, '_') && !str_starts_with($template, 'L')) {
- $template = '_' . $template;
+ $available = static::availableBaseLabels();
+
+ $class = $available[$template] ?? null;
+
+ if (!$class || !class_exists($class)) {
+ return null;
}
- $avery = "App\\Models\\Labels\\Sheets\\Avery\\{$template}";
- if (class_exists($avery)) {
- return $avery;
+ return new $class();
+ }
+
+ public static function diffEditorConfig(array $finalConfig, array $baseConfig): array
+ {
+ $diff = [];
+
+ foreach ($finalConfig as $key => $value) {
+ $baseValue = $baseConfig[$key] ?? null;
+
+ if (is_array($value) && is_array($baseValue)) {
+ $nestedDiff = static::diffEditorConfig($value, $baseValue);
+
+ if (!empty($nestedDiff)) {
+ $diff[$key] = $nestedDiff;
+ }
+
+ continue;
+ }
+
+ if ($value !== $baseValue) {
+ $diff[$key] = $value;
+ }
}
- throw new \InvalidArgumentException("Unknown label template: {$template}");
+ return $diff;
}
}
\ No newline at end of file
diff --git a/app/Models/Labels/Label.php b/app/Models/Labels/Label.php
index 210a0f965c..3d1fd6a201 100644
--- a/app/Models/Labels/Label.php
+++ b/app/Models/Labels/Label.php
@@ -803,4 +803,15 @@ abstract class Label
'rotation' => $this->getRotation(),
];
}
+
+ public function getEditorConfigSections(): array
+ {
+ return [
+ 'page' => $this->getPageEditorConfig(),
+ 'grid' => $this->getGridEditorConfig(),
+ 'label' => $this->getLabelEditorConfig(),
+ 'content' => $this->getContentEditorConfig(),
+ 'supports' => $this->getSupportsEditorConfig(),
+ ];
+ }
}