Skip to content

A free open source IT asset/license management system

adds import input, route, controller method, fixes config_snapshot

Godfrey Mc852697fd82d
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php
index 97c7286063..956127e785 100755
--- a/app/Http/Controllers/LabelsController.php
+++ b/app/Http/Controllers/LabelsController.php
@@ -19,6 +19,7 @@ use Illuminate\Http\Request;
use App\Models\Supplier;
use App\Models\User;
use App\View\Label as LabelView;
+use Illuminate\Validation\ValidationException;
class LabelsController extends Controller
{
@@ -170,6 +171,7 @@ class LabelsController extends Controller
$finalConfig = $workingLabel->getEditorConfigSections();
$configSnapshot = [
+ 'unit' => 'mm',
'template' => $validated['template'],
'type' => $validated['type'] ?? 'sheet',
'name' => $validated['name'],
@@ -198,21 +200,26 @@ class LabelsController extends Controller
if ($selectedLabel) {
$selectedLabel = str_replace('/', '\\', $selectedLabel);
}
-
+ $importedConfig = session('imported_label_config');
+ if (!$importedConfig) {
+ $selectedLabel = data_get($importedConfig, 'template', $selectedLabel);
+ }
try {
$template = $selectedLabel
? Label::find(str_replace('/', '\\', $selectedLabel))
- : new \App\Models\Labels\DefaultLabel();
+ : new DefaultLabel();
} catch (\Throwable $e) {
- $template = new \App\Models\Labels\DefaultLabel();
+ $template = new DefaultLabel();
}
- $label = (new \App\Models\Labels\CustomLabels\PreviewLabel())
+ $label = (new PreviewLabel())
->seedFromTemplate($template);
+ $config = $importedConfig ?: $label->toEditorConfig();
return view('settings.label-edit', [
- 'config' => $label->toEditorConfig(),
+ 'config' => $config,
'selectedLabel' => $selectedLabel,
+ 'importedConfig' => $importedConfig,
]);
}
@@ -350,4 +357,32 @@ class LabelsController extends Controller
->with('bulkedit', false)
->with('count', 0);
}
+
+ public function import(Request $request)
+ {
+ $validated = $request->validate([
+ 'config_snapshot' => ['required', 'json'],
+ ]);
+
+ $config = json_decode($validated['config_snapshot'], true);
+
+ if (!is_array($config)) {
+ throw ValidationException::withMessages([
+ 'config_snapshot' => 'The imported label config must be a JSON object.',
+ ]);
+ }
+
+ foreach (['template', 'type', 'name'] as $requiredKey) {
+ if (!array_key_exists($requiredKey, $config)) {
+ throw ValidationException::withMessages([
+ 'config_snapshot' => "The imported label config is missing [{$requiredKey}].",
+ ]);
+ }
+ }
+
+ return redirect()
+ ->route('settings.labels.edit')
+ ->with('imported_label_config', $config);
+ }
+
}
diff --git a/app/Models/Labels/Label.php b/app/Models/Labels/Label.php
index 3d1fd6a201..b99d943467 100644
--- a/app/Models/Labels/Label.php
+++ b/app/Models/Labels/Label.php
@@ -807,8 +807,10 @@ abstract class Label
public function getEditorConfigSections(): array
{
return [
+ 'unit' => 'mm',
'page' => $this->getPageEditorConfig(),
'grid' => $this->getGridEditorConfig(),
+ 'printable_area' => $this->getPrintableAreaEditorConfig(),
'label' => $this->getLabelEditorConfig(),
'content' => $this->getContentEditorConfig(),
'supports' => $this->getSupportsEditorConfig(),
diff --git a/resources/views/settings/label-edit.blade.php b/resources/views/settings/label-edit.blade.php
index 0272e88db6..8516d53a9e 100644
--- a/resources/views/settings/label-edit.blade.php
+++ b/resources/views/settings/label-edit.blade.php
@@ -85,6 +85,10 @@
<form id="label-customizer-form" method="POST" action="{{route('settings.labels.store')}}"
class="form-horizontal">
@csrf
+ @if (! empty($importedConfig))
+ <input type="hidden" name="imported_config_snapshot"
+ value="{{ e(json_encode($importedConfig)) }}">
+ @endif
@php
$printable = $config['label_printable_area'] ?? $config['printable_area'] ?? null;
@@ -106,7 +110,7 @@
type="text"
name="name"
class="form-control"
- value="{{ old('name', $defaultName) }}"
+ value="{{ old('name', data_get($importedConfig ?? [], 'name', $defaultName)) }}"
placeholder="Enter label name"
style="max-width: 320px;"
>
diff --git a/resources/views/settings/labels.blade.php b/resources/views/settings/labels.blade.php
index 5661b2e05e..1356e5b77a 100644
--- a/resources/views/settings/labels.blade.php
+++ b/resources/views/settings/labels.blade.php
@@ -62,7 +62,17 @@
@if ($setting->label2_enable)
<!-- New Settings -->
-
+ <textarea
+ name="config_snapshot"
+ form="import-label-form"
+ class="form-control"
+ rows="6"
+ placeholder="Paste label config JSON here"
+ >{{ old('config_snapshot') }}</textarea>
+
+ <button type="submit" form="import-label-form" class="btn btn-primary">
+ Import Label Config
+ </button>
<fieldset name="select-template">
<x-form.legend>
{{ trans('admin/settings/general.select_template') }}
@@ -588,6 +598,9 @@
@csrf
@method('DELETE')
</form>
+ <form id="import-label-form" method="POST" action="{{ route('settings.labels.import') }}">
+ @csrf
+ </form>
@stop
diff --git a/routes/web.php b/routes/web.php
index b4758d0d45..317bf465db 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -214,6 +214,8 @@ Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'authorize:superuser
->name('settings.labels.store');
Route::delete('settings/labels/{label}', [LabelsController::class, 'destroy'])
->name('settings.labels.destroy');
+ Route::post('/settings/labels/import', [LabelsController::class, 'import'])
+ ->name('settings.labels.import');
Route::get('ldap', [SettingsController::class, 'getLdapSettings'])
->name('settings.ldap.index')