Skip to content

Commit 86ec8dd

Browse files
committed
combine label previews, remove custom preview partial
1 parent 0f88635 commit 86ec8dd

6 files changed

Lines changed: 159 additions & 254 deletions

File tree

app/Http/Controllers/LabelsController.php

Lines changed: 73 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -29,78 +29,46 @@ class LabelsController extends Controller
2929
*
3030
* @author Grant Le Roux <grant.leroux+snipe-it@gmail.com>
3131
*/
32-
public function show(string $labelName)
32+
public function show(Request $request, string $labelName)
3333
{
34-
$labelName = str_replace('/', '\\', $labelName);
35-
$template = Label::find($labelName);
34+
if ($request->filled('custom_label_id')) {
35+
$customLabel = CustomUserLabel::findOrFail($request->input('custom_label_id'));
3636

37-
$exampleAsset = new Asset;
37+
$baseTemplate = CustomUserLabel::makeBaseLabel($customLabel->base_label);
3838

39-
$exampleAsset->id = 999999;
40-
$exampleAsset->name = 'JEN-867-5309';
41-
$exampleAsset->asset_tag = '100001';
42-
$exampleAsset->serial = 'SN9876543210';
43-
$exampleAsset->asset_eol_date = '2025-01-01';
44-
$exampleAsset->order_number = '12345';
45-
$exampleAsset->purchase_date = '2023-01-01';
46-
$exampleAsset->status_id = 1;
47-
$exampleAsset->location_id = 1;
39+
$template = new PreviewLabel();
40+
$template->seedFromTemplate($baseTemplate);
41+
$template->applyEditorConfig($customLabel->config_snapshot);
4842

49-
$exampleAsset->company = new Company([
50-
'name' => trans('admin/labels/table.example_company'),
51-
'phone' => '1-555-555-5555',
52-
'email' => 'company@example.com',
53-
'logo' => 'label-preview-logo.png',
54-
]);
55-
$exampleAsset->is_label_preview = true;
43+
return $this->renderLabelPreview($request, $template);
44+
}
5645

57-
$exampleAsset->setRelation('assignedTo', new User(['first_name' => 'Luke', 'last_name' => 'Skywalker']));
58-
$exampleAsset->defaultLoc = new Location(['name' => trans('admin/labels/table.example_defaultloc'), 'phone' => '1-555-555-5555']);
59-
$exampleAsset->location = new Location(['name' => trans('admin/labels/table.example_location'), 'phone' => '1-555-555-5555']);
46+
$labelName = str_replace('/', '\\', $labelName);
6047

61-
$exampleAsset->model = new AssetModel;
62-
$exampleAsset->model->id = 999999;
63-
$exampleAsset->model->name = trans('admin/labels/table.example_model');
64-
$exampleAsset->model->model_number = 'MDL5678';
65-
$exampleAsset->model->manufacturer = new Manufacturer;
66-
$exampleAsset->model->manufacturer->id = 999999;
67-
$exampleAsset->model->manufacturer->name = trans('admin/labels/table.example_manufacturer');
68-
$exampleAsset->model->manufacturer->support_email = 'support@test.com';
69-
$exampleAsset->model->manufacturer->support_phone = '1-555-555-5555';
70-
$exampleAsset->model->manufacturer->support_url = 'https://example.com';
71-
$exampleAsset->supplier = new Supplier(['name' => trans('admin/labels/table.example_company')]);
72-
$exampleAsset->model->category = new Category;
73-
$exampleAsset->model->category->id = 999999;
74-
$exampleAsset->model->category->name = trans('admin/labels/table.example_category');
48+
$baseTemplate = $labelName === 'DefaultLabel'
49+
? new DefaultLabel()
50+
: Label::find($labelName);
7551

76-
$customFieldColumns = CustomField::where('field_encrypted', '=', 0)->pluck('db_column');
52+
$hasEditorConfig = collect(['page', 'grid', 'label', 'content', 'supports'])
53+
->some(fn($key) => $request->filled($key));
7754

78-
collect(explode(';', Setting::getSettings()->label2_fields))
79-
->filter()
80-
->each(function ($item) use ($customFieldColumns, $exampleAsset) {
81-
$pair = explode('=', $item);
55+
$template = $baseTemplate;
8256

83-
if (array_key_exists(1, $pair)) {
84-
if ($customFieldColumns->contains($pair[1])) {
85-
$exampleAsset->{$pair[1]} = "{{$pair[0]}}";
86-
}
87-
}
88-
});
57+
if ($hasEditorConfig) {
58+
$editorConfig = [
59+
'page' => $request->input('page', []),
60+
'grid' => $request->input('grid', []),
61+
'label' => $request->input('label', []),
62+
'content' => $request->input('content', []),
63+
'supports' => $request->input('supports', []),
64+
];
8965

90-
$settings = Setting::getSettings();
91-
if (request()->has('settings')) {
92-
$overrides = request()->input('settings');
93-
foreach ($overrides as $key => $value) {
94-
$settings->$key = $value;
95-
}
66+
$template = new PreviewLabel();
67+
$template->seedFromTemplate($baseTemplate);
68+
$template->applyEditorConfig($editorConfig);
9669
}
9770

98-
return (new LabelView())
99-
->with('assets', collect([$exampleAsset]))
100-
->with('settings', $settings)
101-
->with('template', $template)
102-
->with('bulkedit', false)
103-
->with('count', 0);
71+
return $this->renderLabelPreview($request, $template);
10472

10573
}
10674

@@ -369,33 +337,47 @@ public function destroy(CustomUserLabel $label)
369337
->with('success', $labelName . ' ' . trans('admin/labels/labels.label_deleted_successfully'));
370338
}
371339

372-
public function customLabelPreview(Request $request, string $labelName)
340+
public function import(Request $request)
373341
{
374-
$labelName = str_replace('/', '\\', $labelName);
375-
376-
$baseTemplate = $labelName === 'DefaultLabel'
377-
? new DefaultLabel()
378-
: Label::find($labelName);
379-
380-
$editorConfig = [
381-
'page' => $request->input('page', []),
382-
'grid' => $request->input('grid', []),
383-
'label' => $request->input('label', []),
384-
'content' => $request->input('content', []),
385-
'supports' => $request->input('supports', []),
386-
];
342+
$validated = $request->validate([
343+
'config_snapshot' => ['required', 'json'],
344+
]);
387345

388-
$template = new \App\Models\Labels\CustomLabels\PreviewLabel();
346+
$config = json_decode($validated['config_snapshot'], true);
389347

390-
if (method_exists($template, 'seedFromTemplate')) {
391-
$template->seedFromTemplate($baseTemplate);
348+
if (!is_array($config)) {
349+
throw ValidationException::withMessages([
350+
'config_snapshot' => 'The imported label config must be a JSON object.',
351+
]);
392352
}
393353

394-
if (method_exists($template, 'applyEditorConfig')) {
395-
$template->applyEditorConfig($editorConfig);
354+
foreach (['template', 'type', 'name'] as $requiredKey) {
355+
if (!array_key_exists($requiredKey, $config)) {
356+
throw ValidationException::withMessages([
357+
'config_snapshot' => "The imported label config is missing [{$requiredKey}].",
358+
]);
359+
}
396360
}
397361

362+
return redirect()
363+
->route('settings.labels.create')
364+
->with('imported_label_config', $config);
365+
}
366+
367+
protected function renderLabelPreview(Request $request, Label $template): LabelView
368+
{
369+
return (new LabelView)
370+
->with('assets', collect([$this->makeExampleAsset()]))
371+
->with('settings', $this->previewSettings($request))
372+
->with('template', $template)
373+
->with('bulkedit', false)
374+
->with('count', 0);
375+
}
376+
377+
protected function makeExampleAsset(): Asset
378+
{
398379
$exampleAsset = new Asset;
380+
399381
$exampleAsset->id = 999999;
400382
$exampleAsset->name = 'JEN-867-5309';
401383
$exampleAsset->asset_tag = '100001';
@@ -405,11 +387,13 @@ public function customLabelPreview(Request $request, string $labelName)
405387
$exampleAsset->purchase_date = '2023-01-01';
406388
$exampleAsset->status_id = 1;
407389
$exampleAsset->location_id = 1;
390+
$exampleAsset->is_label_preview = true;
408391

409392
$exampleAsset->company = new Company([
410393
'name' => trans('admin/labels/table.example_company'),
411394
'phone' => '1-555-555-5555',
412395
'email' => 'company@example.com',
396+
'logo' => 'label-preview-logo.png',
413397
]);
414398

415399
$exampleAsset->setRelation('assignedTo', new User([
@@ -447,8 +431,6 @@ public function customLabelPreview(Request $request, string $labelName)
447431
$exampleAsset->model->category->id = 999999;
448432
$exampleAsset->model->category->name = trans('admin/labels/table.example_category');
449433

450-
$exampleAsset->is_label_preview = true;
451-
452434
$customFieldColumns = CustomField::where('field_encrypted', 0)->pluck('db_column');
453435

454436
collect(explode(';', Setting::getSettings()->label2_fields))
@@ -461,7 +443,13 @@ public function customLabelPreview(Request $request, string $labelName)
461443
}
462444
});
463445

446+
return $exampleAsset;
447+
}
448+
449+
protected function previewSettings(Request $request): Setting
450+
{
464451
$settings = Setting::getSettings();
452+
465453
$settingOverrides = [
466454
'label2_title',
467455
'label2_asset_logo',
@@ -479,39 +467,12 @@ public function customLabelPreview(Request $request, string $labelName)
479467
}
480468
}
481469

482-
return (new LabelView)
483-
->with('assets', collect([$exampleAsset]))
484-
->with('settings', $settings)
485-
->with('template', $template)
486-
->with('bulkedit', false)
487-
->with('count', 0);
488-
}
489-
490-
public function import(Request $request)
491-
{
492-
$validated = $request->validate([
493-
'config_snapshot' => ['required', 'json'],
494-
]);
495-
496-
$config = json_decode($validated['config_snapshot'], true);
497-
498-
if (!is_array($config)) {
499-
throw ValidationException::withMessages([
500-
'config_snapshot' => 'The imported label config must be a JSON object.',
501-
]);
502-
}
503-
504-
foreach (['template', 'type', 'name'] as $requiredKey) {
505-
if (!array_key_exists($requiredKey, $config)) {
506-
throw ValidationException::withMessages([
507-
'config_snapshot' => "The imported label config is missing [{$requiredKey}].",
508-
]);
470+
if ($request->has('settings')) {
471+
foreach ($request->input('settings', []) as $key => $value) {
472+
$settings->{$key} = $value;
509473
}
510474
}
511475

512-
return redirect()
513-
->route('settings.labels.create')
514-
->with('imported_label_config', $config);
476+
return $settings;
515477
}
516-
517478
}

resources/views/partials/bootstrap-table.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,7 @@ function labelActionsFormatter(value, row) {
19131913
+ '<i class="fa-regular fa-clone"></i>'
19141914
+ '<span class="sr-only">{{ trans('general.clone') }}</span>'
19151915
+ '</a>&nbsp;';
1916-
// Only allow delete, share or edit for custom labels
1916+
// Only allow to delete, share or edit for custom labels
19171917
if (row.source === 'custom') {
19181918
// Update Button
19191919
var editUrl = editLabelUrlTemplate.replace('label_id', row.custom_label_id);

resources/views/partials/custom-label-preview.blade.php

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)