Skip to content

A free open source IT asset/license management system

moves import to livewire modal, rearranges label index

Godfrey M933f9660ab61
diff --git a/app/Http/Requests/ImportCustomLabelRequest.php b/app/Http/Requests/ImportCustomLabelRequest.php
deleted file mode 100644
index 38174cb474..0000000000
--- a/app/Http/Requests/ImportCustomLabelRequest.php
+++ /dev/null
@@ -1,30 +0,0 @@
-<?php
-
-namespace App\Http\Requests;
-
-use Illuminate\Foundation\Http\FormRequest;
-
-class ImportCustomLabelRequest extends FormRequest
-{
- public function authorize(): bool
- {
- return true;
- }
-
- public function rules(): array
- {
- return [
- 'import_method' => ['required', 'in:json,text'],
- 'config_file' => ['required_if:import_method,json', 'file', 'mimes:json,txt'],
- 'config_snapshot' => ['required_if:import_method,text', 'nullable', 'string'],
- ];
- }
-
- public function rawConfigJson(): ?string
- {
- return $this->input('import_method') === 'json'
- ? file_get_contents($this->file('config_file')->getRealPath())
- : $this->input('config_snapshot');
- }
-
-}
\ No newline at end of file
diff --git a/app/Livewire/Labels/ImportLabel.php b/app/Livewire/Labels/ImportLabel.php
new file mode 100644
index 0000000000..48896a2eaa
--- /dev/null
+++ b/app/Livewire/Labels/ImportLabel.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace App\Livewire\Labels;
+
+use App\Services\CustomLabelImportValidator;
+use Illuminate\Validation\ValidationException;
+use Livewire\Component;
+use Livewire\WithFileUploads;
+
+class ImportLabel extends Component
+{
+ use WithFileUploads;
+
+ public string $importMethod = 'json';
+
+ public string $configSnapshot = '';
+
+ public $configFile;
+
+ public array $validationMessages = [];
+
+ protected function rules(): array
+ {
+ $rules = [
+ 'importMethod' => ['required', 'in:json,text'],
+ ];
+
+ if ($this->importMethod === 'json') {
+ $rules['configFile'] = [
+ 'required',
+ 'file',
+ 'mimes:json,txt',
+ ];
+ }
+
+ if ($this->importMethod === 'text') {
+ $rules['configSnapshot'] = [
+ 'required',
+ 'string',
+ ];
+ }
+
+ return $rules;
+ }
+ public function setImportMethod(string $method)
+ {
+ $this->importMethod = $method;
+
+ $this->resetValidation();
+ $this->validationMessages = [];
+ }
+ protected function rawConfigJson(): ?string
+ {
+ return $this->importMethod === 'json'
+ ? $this->configFile->get()
+ : $this->configSnapshot;
+ }
+ public function import(CustomLabelImportValidator $importValidator)
+ {
+ $this->validate();
+
+ $rawJson = $this->importMethod === 'json'
+ ? $this->configFile->get()
+ : $this->configSnapshot;
+
+ try {
+ $config = $importValidator->validate($rawJson);
+ } catch (ValidationException $e) {
+ $this->validationMessages = $e->validator
+ ->errors()
+ ->all();
+
+ return;
+ }
+
+ session()->flash('imported_label_config', $config);
+
+ $this->redirectRoute('settings.labels.create', [
+ 'label' => $config['template'] ?? null,
+ ]);
+ }
+
+ public function render()
+ {
+ return view('livewire.labels.import-label');
+ }
+}
\ No newline at end of file
diff --git a/resources/lang/en-US/admin/labels/general.php b/resources/lang/en-US/admin/labels/general.php
index e615c34885..34381d2353 100644
--- a/resources/lang/en-US/admin/labels/general.php
+++ b/resources/lang/en-US/admin/labels/general.php
@@ -83,6 +83,10 @@ return [
'dejavusans' => 'DejaVu Sans',
],
'create_label' => 'Create Label',
+ 'import_label_config' => 'Import Label Configuration',
+ 'import_method' => 'Import Method',
+ 'import_json_help' => 'Select Label Json file to import.',
+ 'import_paste_json' => 'Paste Label JSON Data',
'label_type' => 'Label Type',
'tape_labels' => 'Tape Labels',
'sheet_labels' => 'Sheet Labels',
diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php
index ecb4281d48..834c621771 100644
--- a/resources/lang/en-US/general.php
+++ b/resources/lang/en-US/general.php
@@ -346,6 +346,7 @@ return [
'sure_to_delete_var' => 'Are you sure you wish to delete :item?',
'delete_what' => 'Delete :item',
'submit' => 'Submit',
+ 'text' => 'Text',
'target' => 'Target',
'time_and_date_display' => 'Time and Date Display',
'total_assets' => 'total assets',
diff --git a/resources/views/livewire/labels/import-label.blade.php b/resources/views/livewire/labels/import-label.blade.php
new file mode 100644
index 0000000000..0d2280dee7
--- /dev/null
+++ b/resources/views/livewire/labels/import-label.blade.php
@@ -0,0 +1,138 @@
+<div
+ wire:ignore.self
+ class="modal fade"
+ id="import-label-modal"
+ tabindex="-1"
+ role="dialog"
+ aria-labelledby="importLabelModalLabel"
+>
+ <div class="modal-dialog" role="document">
+ <div class="modal-content">
+
+ <div class="modal-header">
+ <button type="button" class="close" data-dismiss="modal">
+ <span>&times;</span>
+ </button>
+
+ <h4 class="modal-title" id="importLabelModalLabel">
+ {{ trans('admin/labels/general.import_label_config') }}
+ </h4>
+ </div>
+
+ <div class="modal-body">
+
+ <div class="form-group">
+ <label>
+ {{ trans('admin/labels/general.import_method') }}
+ </label>
+
+ <div class="btn-group import-toggle" role="group">
+ <button
+ type="button"
+ class="btn {{ $importMethod === 'json' ? 'btn-primary active' : 'btn-default' }}"
+ wire:click="setImportMethod('json')"
+ >
+ JSON
+ </button>
+
+ <button
+ type="button"
+ class="btn {{ $importMethod === 'text' ? 'btn-primary active' : 'btn-default' }}"
+ wire:click="setImportMethod('text')"
+ >
+ {{ trans('general.text') }}
+ </button>
+ </div>
+ </div>
+
+ @if ($importMethod === 'text')
+ <div class="form-group">
+ <label for="config-snapshot">
+{{-- {{ trans('admin/labels/general.import_label_config') }}--}}
+ </label>
+
+ <textarea
+ id="config-snapshot"
+ wire:model.live="configSnapshot"
+ class="form-control"
+ rows="8"
+ placeholder=" {{ trans('admin/labels/general.import_paste_json') }}"
+ ></textarea>
+ </div>
+ @else
+ <div class="form-group">
+
+ <input
+ id="config-file"
+ type="file"
+ wire:model="configFile"
+ class="form-control"
+ accept=".json,application/json"
+ >
+
+ <p class="help-block">
+ {{ trans('admin/labels/general.import_json_help') }}
+ </p>
+ </div>
+ @endif
+
+ @error('configSnapshot')
+ <p class="help-block text-danger">
+ <i class="fa-solid fa-xmark"></i>
+ {{ $message }}
+ </p>
+ @enderror
+
+ @error('configFile')
+ <p class="help-block text-danger">
+ <i class="fa-solid fa-xmark"></i>
+ {{ $message }}
+ </p>
+ @enderror
+
+ @if ($validationMessages)
+ <ul class="alert-msg list-unstyled">
+ @foreach ($validationMessages as $message)
+ <li>
+ <i class="fa-solid fa-xmark text-danger"></i>
+ {{ $message }}
+ </li>
+ @endforeach
+ </ul>
+ @endif
+
+ <div wire:loading wire:target="configFile,import">
+ <p class="text-muted">
+ <i class="fas fa-spinner fa-spin"></i>
+ </p>
+ </div>
+
+ </div>
+
+ <div class="modal-footer">
+ <button
+ type="button"
+ class="btn btn-default"
+ data-dismiss="modal"
+ >
+ {{ trans('general.cancel') }}
+ </button>
+
+ <button
+ type="button"
+ class="btn btn-primary"
+ wire:click="import"
+ wire:loading.attr="disabled"
+ @if ($importMethod === 'text')
+ @disabled(trim($configSnapshot) === '')
+ @else
+ @disabled(!$configFile)
+ @endif
+ >
+ {{ trans('general.import') }}
+ </button>
+ </div>
+
+ </div>
+ </div>
+</div>
\ No newline at end of file
diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php
index 9be1558381..368fdff103 100644
--- a/resources/views/partials/bootstrap-table.blade.php
+++ b/resources/views/partials/bootstrap-table.blade.php
@@ -1772,6 +1772,18 @@
class: 'btn-info',
title: 'Create New Label'
}
+ },
+ btnImport: {
+ icon: 'fa-solid fa-file-import',
+ text: 'Import Label',
+ title: 'Import Label',
+ event() {
+ $('#import-label-modal').modal('show');
+ },
+ attributes: {
+ class: 'btn-info',
+ title: 'Import Label'
+ }
}
});
diff --git a/resources/views/partials/labels-new-engine.blade.php b/resources/views/partials/labels-new-engine.blade.php
index 0cb531f6a2..7ed4a731c0 100644
--- a/resources/views/partials/labels-new-engine.blade.php
+++ b/resources/views/partials/labels-new-engine.blade.php
@@ -1,103 +1,3 @@
-<!-- Import Label -->
-<div class="form-group">
- <div class="col-md-3 text-right">
- <label for="config_snapshot" class="control-label">
- Import Label Config
- </label>
- </div>
-
- <div class="col-md-7">
- <input
- type="hidden"
- name="import_method"
- id="import_method"
- value="json"
- form="import-label-form"
- >
-
- <div class="btn-group import-toggle" role="group">
- <button
- type="button"
- class="btn btn-primary import-toggle-btn active"
- data-method="json"
- >
- JSON
- </button>
-
- <button
- type="button"
- class="btn btn-default import-toggle-btn"
- data-method="text"
- >
- Text
- </button>
- </div>
-
- <div id="text-input-group" style="display:none; margin-top:10px;">
- <div class="import-inline-row">
- <textarea
- name="config_snapshot"
- form="import-label-form"
- id="config_snapshot"
- class="form-control"
- rows="4"
- placeholder="Paste label config JSON here"
- >{{ old('config_snapshot') }}</textarea>
-
- <button
- type="submit"
- form="import-label-form"
- id="import-text-button"
- class="btn btn-primary"
- disabled
- >
- Import
- </button>
- </div>
-
- <p id="json-validation-message" class="help-block" style="display:none;"></p>
- </div>
-
- <div id="json-input-group" style="margin-top:10px; margin-bottom:15px;">
- <div class="input-group">
- <input
- type="file"
- name="config_file"
- form="import-label-form"
- id="config_file"
- class="form-control"
- accept=".json,application/json"
- >
-
- <span class="input-group-btn">
- <button
- type="submit"
- form="import-label-form"
- id="import-file-button"
- class="btn btn-primary"
- disabled
- >
- Import
- </button>
- </span>
- </div>
- </div>
-
- @if ($errors->has('config_snapshot'))
- <ul class="alert-msg list-unstyled" style="margin-top: 10px;">
- @foreach ((array) $errors->get('config_snapshot') as $message)
- @if (is_array($message))
- @foreach ($message as $line)
- <li>{!! $line !!}</li>
- @endforeach
- @else
- <li>{!! $message !!}</li>
- @endif
- @endforeach
- </ul>
- @endif
- </div>
-</div>
<!-- New Settings -->
<fieldset name="select-template">
<x-form.legend>
@@ -116,53 +16,7 @@
</div>
</div>
</fieldset>
-{{--<fieldset name="select-template">--}}
-{{-- <x-form.legend>--}}
-{{-- {{ trans('admin/settings/general.select_template') }}--}}
-{{-- </x-form.legend>--}}
-{{-- <div class="form-group{{ $errors->has('label2_template') ? ' has-error' : '' }}">--}}
-{{-- <div class="col-md-12">--}}
-{{-- <table--}}
-{{-- data-columns="{{ \App\Presenters\LabelPresenter::dataTableLayout() }}"--}}
-{{-- data-cookie="true"--}}
-{{-- data-cookie-id-table="label2TemplateTable"--}}
-{{-- data-id-table="label2TemplateTable"--}}
-{{-- data-select-item-name="label2_template"--}}
-{{-- data-id-field="name"--}}
-{{-- data-side-pagination="server"--}}
-{{-- data-sort-name="name"--}}
-{{-- data-fixed-columns="false"--}}
-{{-- data-sort-order="asc"--}}
-{{-- data-url="{{ route('api.labels.index') }}"--}}
-{{-- id="label2TemplateTable"--}}
-{{-- class="table table-striped snipe-table"--}}
-{{-- ></table>--}}
-{{-- <script>--}}
-{{-- document.addEventListener('DOMContentLoaded', () => {--}}
-{{-- const chosenLabel = "{{ old('label2_template', $chosenLabel ?? '') }}";--}}
-{{-- $('#label2TemplateTable').on('load-success.bs.table', () => {--}}
-{{-- if (chosenLabel) {--}}
-{{-- $('input[name="label2_template"][value="' + chosenLabel + '"]').prop('checked', true);--}}
-{{-- }--}}
-
-{{-- const form = document.getElementById('settingsForm');--}}
-{{-- form?.dispatchEvent(new Event('change'));--}}
-
-{{-- // Attach event listeners for template selection changes--}}
-{{-- document.querySelectorAll('input[name="label2_template"]').forEach(radio => {--}}
-{{-- radio.addEventListener('change', function() {--}}
-{{-- if (this.checked) {--}}
-{{-- document.getElementById('label2_preview_template').textContent = this.value;--}}
-{{-- }--}}
-{{-- });--}}
-{{-- });--}}
-{{-- });--}}
-{{-- });--}}
-{{-- </script>--}}
-{{-- </div>--}}
-{{-- </div>--}}
-{{--</fieldset>--}}
<fieldset name="label-preview">
<x-form.legend>
{{ trans('admin/settings/general.label2_label_preview') }}: <code
@@ -173,6 +27,23 @@
</div>
</fieldset>
+<fieldset name="field-definitions">
+ <x-form.legend help_text="{!! trans('admin/settings/general.label2_fields_help') !!}">
+ {{ trans('admin/settings/general.label_fields') }}
+ </x-form.legend>
+ <div class="form-group {{ $errors->has('label2_fields') ? ' has-error' : '' }}">
+ <div class="col-md-12">
+ @include('partials.label2-field-definitions', [
+ 'name' => 'label2_fields',
+ 'value' => old('label2_fields', $setting->label2_fields),
+ 'customFields' => $customFields,
+ 'template' => $setting->label2_template,
+ ])
+ <x-form.error name="label2_fields"/>
+ </div>
+ </div>
+</fieldset>
+
<fieldset name="label-settings">
<x-form.legend help_text="{{ trans('admin/settings/general.labels_title_help') }}">
{{ trans('admin/settings/general.labels_title') }}
@@ -336,22 +207,6 @@
</div>
</fieldset>
-<fieldset name="field-definitions">
- <x-form.legend help_text="{!! trans('admin/settings/general.label2_fields_help') !!}">
- {{ trans('admin/settings/general.label_fields') }}
- </x-form.legend>
- <div class="form-group {{ $errors->has('label2_fields') ? ' has-error' : '' }}">
- <div class="col-md-12">
- @include('partials.label2-field-definitions', [
- 'name' => 'label2_fields',
- 'value' => old('label2_fields', $setting->label2_fields),
- 'customFields' => $customFields,
- 'template' => $setting->label2_template,
- ])
- <x-form.error name="label2_fields" />
- </div>
- </div>
-</fieldset>
diff --git a/resources/views/settings/labels.blade.php b/resources/views/settings/labels.blade.php
index c27fd8df25..8f05ba3d84 100644
--- a/resources/views/settings/labels.blade.php
+++ b/resources/views/settings/labels.blade.php
@@ -14,30 +14,6 @@
.checkbox label {
padding-right: 40px;
}
-
- .import-toggle .btn {
- background-color: #f5f5f5; /* light neutral */
- color: #333;
- }
-
- .import-toggle .btn:hover {
- background-color: #e6e6e6;
- }
-
- .import-toggle .btn.active {
- background-color: #337ab7; /* Bootstrap primary */
- color: #fff;
- }
-
- .import-toggle .btn:focus,
- .import-toggle .btn:active:focus,
- .import-toggle .btn.active:focus,
- .import-toggle .btn.focus,
- .import-toggle .btn.active.focus {
- outline: none !important;
- box-shadow: none !important;
- -webkit-box-shadow: none !important;
- }
</style>
<form method="POST" action="{{ route('settings.labels.save') }}" accept-charset="UTF-8" id="settingsForm" autocomplete="off" class="form-horizontal" role="form">
@@ -102,15 +78,11 @@
</div> <!-- /.row-->
</form>
<livewire:labels.new-label-setup/>
+ <livewire:labels.import-label/>
<form id="delete-custom-label-form" method="POST" style="display:none;">
@csrf
@method('DELETE')
</form>
- <form id="import-label-form" method="POST" enctype="multipart/form-data"
- action="{{ route('settings.labels.import') }}">
- @csrf
- </form>
-
@stop
@push('js')
@@ -284,53 +256,6 @@
window.URL.revokeObjectURL(url);
});
- $('.import-toggle-btn').on('click', function () {
- const method = $(this).data('method');
-
- $('.import-toggle-btn')
- .removeClass('btn-primary active')
- .addClass('btn-default');
-
- $(this)
- .addClass('btn-primary active')
- .removeClass('btn-default');
-
- $('#import_method').val(method);
-
- $('#text-input-group').toggle(method === 'text');
- $('#json-input-group').toggle(method === 'json');
- });
-
- $('#config_file').on('change', function () {
- const hasFile = this.files && this.files.length > 0;
-
- $('#import-file-button').prop('disabled', !hasFile);
- });
-
- $('#config_snapshot').on('input', function () {
-
- const value = $(this).val().trim();
-
- try {
- JSON.parse(value);
-
- $('#import-text-button').prop('disabled', false);
-
- $('#json-validation-message')
- .hide()
- .text('');
-
- } catch (e) {
-
- $('#import-text-button').prop('disabled', true);
-
- $('#json-validation-message')
- .show()
- .addClass('text-danger')
- .text('Invalid JSON');
- }
- });
-
</script>
{{-- Can't use @script here because we're not in a livewire component so let's manually load --}}
@livewireScripts