Skip to content

A free open source IT asset/license management system

make LabelPreviewAsset class, fix cloning custom labels, a bit more refactor

Godfrey Mec8ad7a87c94
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php
index c426bfa043..f1c981c9fe 100755
--- a/app/Http/Controllers/LabelsController.php
+++ b/app/Http/Controllers/LabelsController.php
@@ -4,30 +4,21 @@ namespace App\Http\Controllers;
use App\Http\Requests\ImportCustomLabelRequest;
use App\Models\Asset;
-use App\Models\AssetModel;
-use App\Models\Category;
-use App\Models\Company;
use App\Models\CustomField;
use App\Models\Labels\CustomLabels\PreviewSheetLabel;
use App\Models\Labels\CustomLabels\PreviewTapeLabel;
use App\Models\Labels\CustomUserLabel;
use App\Models\Labels\DefaultLabel;
use App\Models\Labels\Label;
-use App\Models\Location;
-use App\Models\Manufacturer;
use App\Models\Setting;
-use App\Models\Supplier;
-use App\Models\User;
use App\View\Label as LabelView;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use App\Services\CustomLabelImportValidator;
use Illuminate\Validation\Rule;
use App\Models\Labels\RectangleSheet;
+use App\Models\Labels\LabelPreviewAsset;
-use App\Models\Labels\CustomLabels\CustomSheetLabel;
-
-//use App\Models\Labels\CustomLabels\CustomTapeLabel;
class LabelsController extends Controller
{
/**
@@ -43,7 +34,7 @@ class LabelsController extends Controller
$id = (int)str_replace('custom:', '', $labelName);
$customLabel = CustomUserLabel::find($id);
-
+
if (!$customLabel) {
$template = new DefaultLabel;
} else {
@@ -198,13 +189,9 @@ class LabelsController extends Controller
);
}
- $makePreviewLabel = function () use ($isTape) {
- return $isTape
- ? new PreviewTapeLabel
- : new PreviewSheetLabel;
- };
+ $previewLabelClass = $isTape ? new PreviewTapeLabel : new PreviewSheetLabel;
- $baseWorkingLabel = $makePreviewLabel();
+ $baseWorkingLabel = new $previewLabelClass();
$baseWorkingLabel->seedFromTemplate($baseLabel);
$baseConfig = $baseWorkingLabel->getEditorConfigSections();
@@ -228,7 +215,7 @@ class LabelsController extends Controller
$mergedConfig = array_replace_recursive($baseConfig, $submittedConfig);
- $workingLabel = $makePreviewLabel();
+ $workingLabel = new $previewLabelClass();
$workingLabel->seedFromTemplate($baseLabel);
$workingLabel->applyEditorConfig($mergedConfig);
@@ -486,7 +473,7 @@ class LabelsController extends Controller
$template->applyEditorConfig($editorConfig);
- $exampleAsset = Asset::factory()->labelPreview()->make();
+ $exampleAsset = LabelPreviewAsset::make();;
$customFieldColumns = CustomField::where('field_encrypted', 0)->pluck('db_column');
@@ -552,14 +539,30 @@ class LabelsController extends Controller
public function createFromExisting(Request $request)
{
if ($request->filled('custom_label_id')) {
- $customLabel = CustomUserLabel::findOrFail($request->get('custom_label_id'));
+ $customLabel = CustomUserLabel::findOrFail(
+ $request->get('custom_label_id')
+ );
$config = $customLabel->config_snapshot;
$config['name'] = 'Copy of ' . $customLabel->name;
+ $previewLabel = $customLabel->type === 'tape'
+ ? new PreviewTapeLabel
+ : new PreviewSheetLabel;
+
+ $baseLabel = CustomUserLabel::makeBaseLabel(
+ $customLabel->base_label
+ );
+
+ if ($baseLabel) {
+ $previewLabel->seedFromTemplate($baseLabel);
+ }
+
+ $previewLabel->applyEditorConfig($config);
+
return view('settings.label-edit', [
'config' => $config,
- 'sections' => $customLabel->getEditorSections(),
+ 'sections' => $previewLabel->getEditorSections(),
'selectedLabel' => $customLabel->base_label,
'selectedType' => $customLabel->type,
'importedConfig' => null,
diff --git a/app/Models/Labels/LabelPreviewAsset.php b/app/Models/Labels/LabelPreviewAsset.php
new file mode 100644
index 0000000000..ccf7986a7c
--- /dev/null
+++ b/app/Models/Labels/LabelPreviewAsset.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace App\Models\Labels;
+
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\Category;
+use App\Models\Company;
+use App\Models\Location;
+use App\Models\Manufacturer;
+use App\Models\Supplier;
+use App\Models\User;
+
+class LabelPreviewAsset
+{
+ public static function make(): Asset
+ {
+ $asset = new Asset();
+
+ $asset->id = 999999;
+ $asset->name = 'JEN-867-5309';
+ $asset->asset_tag = '100001';
+ $asset->serial = 'SN9876543210';
+ $asset->asset_eol_date = '2025-01-01';
+ $asset->order_number = '12345';
+ $asset->purchase_date = '2023-01-01';
+ $asset->status_id = 1;
+ $asset->location_id = 1;
+
+ $asset->setRelation('company', new Company([
+ 'name' => trans('admin/labels/table.example_company'),
+ 'phone' => '1-555-555-5555',
+ 'email' => '[email protected]',
+ 'logo' => 'label-preview-logo.png',
+ ]));
+
+ $asset->setRelation('assignedTo', new User([
+ 'first_name' => 'Luke',
+ 'last_name' => 'Skywalker',
+ ]));
+
+ $asset->setRelation('defaultLoc', new Location([
+ 'name' => trans('admin/labels/table.example_defaultloc'),
+ 'phone' => '1-555-555-5555',
+ ]));
+
+ $asset->setRelation('location', new Location([
+ 'name' => trans('admin/labels/table.example_location'),
+ 'phone' => '1-555-555-5555',
+ ]));
+
+ $model = new AssetModel();
+ $model->id = 999999;
+ $model->name = trans('admin/labels/table.example_model');
+ $model->model_number = 'MDL5678';
+
+ $manufacturer = new Manufacturer();
+ $manufacturer->id = 999999;
+ $manufacturer->name = trans('admin/labels/table.example_manufacturer');
+
+ $model->setRelation('manufacturer', $manufacturer);
+
+ $category = new Category();
+ $category->id = 999999;
+ $category->name = trans('admin/labels/table.example_category');
+
+ $model->setRelation('category', $category);
+
+ $asset->setRelation('model', $model);
+
+ $asset->setRelation('supplier', new Supplier([
+ 'name' => trans('admin/labels/table.example_company'),
+ ]));
+
+ $asset->is_label_preview = true;
+
+ return $asset;
+ }
+}
\ No newline at end of file
diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php
index e8f20e6821..beba2fac86 100644
--- a/database/factories/AssetFactory.php
+++ b/database/factories/AssetFactory.php
@@ -427,69 +427,4 @@ class AssetFactory extends Factory
$asset->setValidating(true);
});
}
-
- public function labelPreview()
- {
- return $this->afterMaking(function (Asset $asset) {
-
- $asset->id = 999999;
- $asset->name = 'JEN-867-5309';
- $asset->asset_tag = '100001';
- $asset->serial = 'SN9876543210';
- $asset->asset_eol_date = '2025-01-01';
- $asset->order_number = '12345';
- $asset->purchase_date = '2023-01-01';
- $asset->status_id = 1;
- $asset->location_id = 1;
-
- $asset->company = new Company([
- 'name' => trans('admin/labels/table.example_company'),
- 'phone' => '1-555-555-5555',
- 'email' => '[email protected]',
- 'logo' => 'label-preview-logo.png',
- ]);
-
- $asset->setRelation('assignedTo', new User([
- 'first_name' => 'Luke',
- 'last_name' => 'Skywalker',
- ]));
-
- $asset->defaultLoc = new Location([
- 'name' => trans('admin/labels/table.example_defaultloc'),
- 'phone' => '1-555-555-5555',
- ]);
-
- $asset->location = new Location([
- 'name' => trans('admin/labels/table.example_location'),
- 'phone' => '1-555-555-5555',
- ]);
-
- $model = new AssetModel;
- $model->id = 999999;
- $model->name = trans('admin/labels/table.example_model');
- $model->model_number = 'MDL5678';
-
- $asset->setRelation('model', $model);
-
- $manufacturer = new Manufacturer;
- $manufacturer->id = 999999;
- $manufacturer->name = trans('admin/labels/table.example_manufacturer');
-
- $model->setRelation('manufacturer', $manufacturer);
-
- $asset->supplier = new Supplier([
- 'name' => trans('admin/labels/table.example_company'),
- ]);
-
- $category = new Category;
- $category->id = 999999;
- $category->name = trans('admin/labels/table.example_category');
-
- $model->setRelation('category', $category);
-
- $asset->is_label_preview = true;
- });
-
-
- }
}