A free open source IT asset/license management system
Label preview is loading
Godfrey M997d1434c0c4
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.phpindex 537e664c28..0dcbe713d6 100755--- a/app/Http/Controllers/LabelsController.php+++ b/app/Http/Controllers/LabelsController.php@@ -88,7 +88,7 @@ class LabelsController extends Controller}}- return (new LabelView($template->getName()))+ return (new LabelView())->with('assets', collect([$exampleAsset]))->with('settings', $settings)->with('template', $template)@@ -129,6 +129,19 @@ class LabelsController extends Controller? new DefaultLabel(): Label::find($labelName);+ $editorConfig = [+ 'page' => $request->input('page', []),+ 'grid' => $request->input('grid', []),+ 'label' => $request->input('label', []),+ 'content' => $request->input('content', []),+ 'supports' => $request->input('supports', []),+ ];+ $template = new \App\Models\Labels\CustomLabels\PreviewLabel();++ if (method_exists($template, 'applyEditorConfig')) {+ $template->applyEditorConfig($editorConfig);+ }+$exampleAsset = new Asset;$exampleAsset->id = 999999;$exampleAsset->name = 'JEN-867-5309';@@ -194,18 +207,11 @@ class LabelsController extends Controller});$settings = Setting::getSettings();-- $overrides = array_replace_recursive(- $request->input('page', []),- $request->input('grid', []),- $request->input('label', []),- $request->input('content', []),- $request->input('supports', [])- );--- foreach ($overrides as $key => $value) {- $settings->{$key} = $value;+ if (request()->has('settings')) {+ $overrides = request()->input('settings');+ foreach ($overrides as $key => $value) {+ $settings->$key = $value;+ }}return (new LabelView)diff --git a/app/Models/Labels/CustomLabels/CustomSheetLabel.php b/app/Models/Labels/CustomLabels/CustomSheetLabel.phpindex b2e0575e33..137352ac67 100644--- a/app/Models/Labels/CustomLabels/CustomSheetLabel.php+++ b/app/Models/Labels/CustomLabels/CustomSheetLabel.php@@ -6,5 +6,269 @@ use App\Models\Labels\RectangleSheet;abstract class CustomSheetLabel extends CustomLabel{+ protected string $unit = 'mm';+ protected ?float $pageWidth = 210.0;+ protected ?float $pageHeight = 297.0;++ protected ?float $pageMarginTop = 0.0;+ protected ?float $pageMarginRight = 0.0;+ protected ?float $pageMarginBottom = 0.0;+ protected ?float $pageMarginLeft = 0.0;++ protected int $rows = 1;+ protected int $columns = 1;++ protected ?float $labelWidth = 50.0;+ protected ?float $labelHeight = 25.0;++ protected float $labelRowSpacing = 0.0;+ protected float $labelColumnSpacing = 0.0;++ protected float $labelMarginTop = 0.0;+ protected float $labelMarginRight = 0.0;+ protected float $labelMarginBottom = 0.0;+ protected float $labelMarginLeft = 0.0;++ protected bool $supportTitle = true;+ protected int $supportFields = 4;+ protected bool $support1DBarcode = true;+ protected bool $support2DBarcode = false;+ protected bool $supportLogo = false;+ protected bool $supportAssetTag = true;++ public function getUnit()+ {+ return $this->unit;+ }++ public function getPageWidth()+ {+ return $this->pageWidth;+ }++ public function getPageHeight()+ {+ return $this->pageHeight;+ }++ public function getPageMarginTop()+ {+ return $this->pageMarginTop;+ }++ public function getPageMarginRight()+ {+ return $this->pageMarginRight;+ }++ public function getPageMarginBottom()+ {+ return $this->pageMarginBottom;+ }++ public function getPageMarginLeft()+ {+ return $this->pageMarginLeft;+ }++ public function getRows()+ {+ return $this->rows;+ }++ public function getColumns()+ {+ return $this->columns;+ }++ public function getLabelWidth()+ {+ return $this->labelWidth;+ }++ public function getLabelHeight()+ {+ return $this->labelHeight;+ }++ public function getLabelRowSpacing()+ {+ return $this->labelRowSpacing;+ }++ public function getLabelColumnSpacing()+ {+ return $this->labelColumnSpacing;+ }++ public function getLabelMarginTop()+ {+ return $this->labelMarginTop;+ }++ public function getLabelMarginRight()+ {+ return $this->labelMarginRight;+ }++ public function getLabelMarginBottom()+ {+ return $this->labelMarginBottom;+ }++ public function getLabelMarginLeft()+ {+ return $this->labelMarginLeft;+ }++ public function getSupportTitle(): bool+ {+ return $this->supportTitle;+ }++ public function getSupportFields(): int+ {+ return $this->supportFields;+ }++ public function getSupport1DBarcode(): bool+ {+ return $this->support1DBarcode;+ }++ public function getSupport2DBarcode(): bool+ {+ return $this->support2DBarcode;+ }++ public function getSupportLogo(): bool+ {+ return $this->supportLogo;+ }++ public function getSupportAssetTag(): bool+ {+ return $this->supportAssetTag;+ }++ public function getLabelBorder()+ {+ return 0;+ }++ public function preparePDF($pdf)+ {+ $pdf->SetMargins(0, 0, 0);+ $pdf->SetAutoPageBreak(false, 0);+ }++ public function applyEditorConfig(array $config): static+ {+ $this->hydrateFromEditorConfig($config);++ return $this;+ }++ protected function hydrateFromEditorConfig(array $config): void+ {+ $page = $config['page'] ?? [];+ $grid = $config['grid'] ?? [];+ $label = $config['label'] ?? [];+ $supports = $config['supports'] ?? [];++ $this->pageWidth = isset($page['width']) ? (float)$page['width'] : $this->pageWidth;+ $this->pageHeight = isset($page['height']) ? (float)$page['height'] : $this->pageHeight;++ $this->pageMarginTop = isset($page['margin_top']) ? (float)$page['margin_top'] : $this->pageMarginTop;+ $this->pageMarginRight = isset($page['margin_right']) ? (float)$page['margin_right'] : $this->pageMarginRight;+ $this->pageMarginBottom = isset($page['margin_bottom']) ? (float)$page['margin_bottom'] : $this->pageMarginBottom;+ $this->pageMarginLeft = isset($page['margin_left']) ? (float)$page['margin_left'] : $this->pageMarginLeft;++ $this->rows = isset($grid['rows']) ? (int)$grid['rows'] : $this->rows;+ $this->columns = isset($grid['columns']) ? (int)$grid['columns'] : $this->columns;+ $this->labelRowSpacing = isset($grid['row_spacing']) ? (float)$grid['row_spacing'] : $this->labelRowSpacing;+ $this->labelColumnSpacing = isset($grid['column_spacing']) ? (float)$grid['column_spacing'] : $this->labelColumnSpacing;++ $this->labelWidth = isset($label['width']) ? (float)$label['width'] : $this->labelWidth;+ $this->labelHeight = isset($label['height']) ? (float)$label['height'] : $this->labelHeight;++ $this->labelMarginTop = isset($label['margin_top']) ? (float)$label['margin_top'] : $this->labelMarginTop;+ $this->labelMarginRight = isset($label['margin_right']) ? (float)$label['margin_right'] : $this->labelMarginRight;+ $this->labelMarginBottom = isset($label['margin_bottom']) ? (float)$label['margin_bottom'] : $this->labelMarginBottom;+ $this->labelMarginLeft = isset($label['margin_left']) ? (float)$label['margin_left'] : $this->labelMarginLeft;++ $this->supportTitle = isset($supports['title']) ? (bool)$supports['title'] : $this->supportTitle;+ $this->supportFields = isset($supports['fields']) ? (bool)$supports['fields'] : $this->supportFields;+ $this->support1DBarcode = isset($supports['barcode1d']) ? (bool)$supports['barcode1d'] : $this->support1DBarcode;+ $this->support2DBarcode = isset($supports['barcode2d']) ? (bool)$supports['barcode2d'] : $this->support2DBarcode;+ $this->supportLogo = isset($supports['logo']) ? (bool)$supports['logo'] : $this->supportLogo;+ $this->supportAssetTag = isset($supports['asset_tag']) ? (bool)$supports['asset_tag'] : $this->supportAssetTag;+ }++ public function write($pdf, $record)+ {+ $pa = $this->getLabelPrintableArea();++ $currentX = $pa->x1;+ $currentY = $pa->y1;+ $usableWidth = $pa->w;++ if ($record->has('title') && $this->getSupportTitle()) {+ static::writeText(+ $pdf,+ $record->get('title'),+ $currentX,+ $currentY,+ 'freesans',+ '',+ 8,+ 'C',+ $usableWidth,+ 8,+ true,+ 0+ );++ $currentY += 10;+ }++ if ($record->has('fields') && $this->getSupportFields()) {+ foreach ($record->get('fields') as $field) {+ static::writeText(+ $pdf,+ $field['label'] ?? '',+ $currentX,+ $currentY,+ 'freesans',+ '',+ 6,+ 'L',+ $usableWidth,+ 6,+ true,+ 0+ );++ $currentY += 7;++ static::writeText(+ $pdf,+ $field['value'] ?? '',+ $currentX,+ $currentY,+ 'freemono',+ 'B',+ 7,+ 'L',+ $usableWidth,+ 7,+ true,+ 0+ );++ $currentY += 9;+ }+ }+ }}\ No newline at end of filediff --git a/app/View/Label.php b/app/View/Label.phpindex 684618e0f0..350ef8ce9c 100644--- a/app/View/Label.php+++ b/app/View/Label.php@@ -49,6 +49,7 @@ class Label implements View$settings = $this->data->get('settings');$assets = $this->data->get('assets');$offset = $this->data->get('offset');+ $template = $this->data->get('template');// If disabled, pass to legacy view@@ -60,7 +61,9 @@ class Label implements View->with('count', $this->data->get('count'));}+ if ($template === null) {$template = LabelModel::find($settings->label2_template);+ }if ($template === null) {return redirect()->route('settings.labels.index')->with('error', trans('admin/settings/message.labels.null_template'));@@ -88,7 +91,6 @@ class Label implements View$pdf->setCreator('Snipe-IT');$pdf->SetSubject('Asset Labels');$template->preparePDF($pdf);-// Get fields from settings$fieldDefinitions = collect(explode(';', $settings->label2_fields))->filter(fn($fieldString) => !empty($fieldString))diff --git a/resources/views/partials/custom-label-preview.blade.php b/resources/views/partials/custom-label-preview.blade.phpindex 8b138774c1..f103a95c8e 100644--- a/resources/views/partials/custom-label-preview.blade.php+++ b/resources/views/partials/custom-label-preview.blade.php@@ -44,76 +44,76 @@@push('js')<script>document.addEventListener('alpine:init', () => {- Alpine.data('custom_label_preview', () => ({- _form: null,- _popped: null,- _previewURL: '',-- get previewURL() {- return this._previewURL;- },- set previewURL(url) {- this._previewURL = url;+ Alpine.data('custom_label_preview', () => ({- if (this._popped && !this._popped.closed) {- this._popped.location = url;- }- },+ _form: null,- _init() {- this._form = document.getElementById('label-customizer-form');+ _init: function () {+ const shell = document.querySelector('.label-customizer-shell');+ this._form = document.querySelector('.label-customizer-shell form');if (!this._form) {+ console.log('custom_label_preview: form not found');return;}this.updateURL();- this._form.addEventListener('input', this.updateURL.bind(this));this._form.addEventListener('change', this.updateURL.bind(this));+ this._form.addEventListener('input', this.updateURL.bind(this));},- updateURL() {+ updateURL: function () {const fields = $(this._form).serializeArray();- const templateField = fields.find(field => field.name === 'template');- if (!templateField || !templateField.value) {- return;- }-- const payload = {};-- fields.forEach(field => {- if (['_token'].includes(field.name)) {- return;- }+ const payload = Object.assign({}, ...fields+ .filter((value) => value.name !== '_token')+ .map((value) => ({[value.name]: value.value}))+ );- payload[field.name] = field.value;- });+ const template = payload.template;- const template = templateField.value;+ if (!template) {+ console.log('custom_label_preview: missing template');+ return;+ }this.previewURL = '{{ route("labels.customizer-preview", ["labelName" => ":label"]) }}'.replace(':label', template.replaceAll('\\', '/')).concat('?' + $.param(payload) + '#toolbar=0');++ console.log('custom_label_preview url:', this.previewURL);},- popout() {- if (!this.previewURL) {- return;+ _previewURL: '',+ get previewURL() {+ return this._previewURL;+ },+ set previewURL(url) {+ this._previewURL = url;++ if (this._popped && !this._popped.closed) {+ this._popped.location = this.previewURL;}+ },++ _popped: null,+ popout: function () {+ if (!this.previewURL) return;this._popped = window.open(this.previewURL);}+}));+});</script>@endpush-<div x-data="custom_label_preview" x-init="_init" class="clp-root">+<div x-data="custom_label_preview()" x-init="_init()" class="clp-root"><div class="clp-top">- <button class="clp-pop-button btn btn-default" x-on:click.prevent="popout" title="Pop Out">+ <button class="clp-pop-button btn btn-default" x-on:click.prevent="popout()" title="Pop Out"><i class="fa-solid fa-maximize"></i></button></div>diff --git a/resources/views/settings/label-edit.blade.php b/resources/views/settings/label-edit.blade.phpindex 26696669e8..97b88f457c 100644--- a/resources/views/settings/label-edit.blade.php+++ b/resources/views/settings/label-edit.blade.php@@ -287,4 +287,7 @@</div></div></div>-@stop\ No newline at end of file+@stop+@push('js')+ @livewireScripts+@endpush\ No newline at end of filediff --git a/routes/web.php b/routes/web.phpindex ba68cb7f6b..c751976d0c 100644--- a/routes/web.php+++ b/routes/web.php@@ -59,12 +59,6 @@ Route::group(['middleware' => 'auth'], function () {[LabelsController::class, 'show'])->where('labelName', '.*')->name('labels.show');- Route::get('/test-email', function () {- $mailable = new \App\Mail\CheckoutComponentMail(-- );- return $mailable->render(); // dumps HTML- });/** Manufacturers*/@@ -216,9 +210,8 @@ Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'authorize:superuser->breadcrumbs(fn (Trail $trail) =>$trail->parent('settings.index')->push(trans('admin/settings/general.labels_title'), route('settings.labels.index')));- Route::get('/labels/customizer-preview/{labelName}', [LabelsController::class, 'customLabelPreview'])- ->where('labelName', '.*')- ->name('labels.customizer-preview');+ Route::get('labels/customizer-preview/{labelName}', [LabelsController::class, 'customLabelPreview']+ )->where('labelName', '.*')->name('labels.customizer-preview');Route::post('labels', [SettingsController::class, 'postLabels'])->name('settings.labels.save');