Skip to content

Commit c273a65

Browse files
committed
chore(types): product types improvements
1 parent ea64105 commit c273a65

12 files changed

Lines changed: 171 additions & 42 deletions

database/migrations/2025_08_08_082024_create_product_types_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function up(): void
1717
});
1818

1919
Schema::create('pim_product_type_data', function (Blueprint $table) {
20+
$table->id();
2021
$table->foreignId('product_type_id')
2122
->constrained('pim_product_types')
2223
->cascadeOnUpdate()

database/migrations/2025_08_13_071152_add_product_type_id_to_catalogue_products_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function up(): void
1414
->after('category_id')
1515
->constrained('pim_product_types')
1616
->cascadeOnUpdate()
17-
->nullOnDelete();
17+
->restrictOnDelete();
1818
});
1919
}
2020

database/seeders/CatalogueSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CatalogueSeeder extends Seeder
1212
public function run(): void
1313
{
1414
$this->call(CategorySeeder::class);
15-
15+
$this->call(ProductTypeSeeder::class);
1616
$this->call(ProductSeeder::class);
1717
}
1818
}

database/seeders/ProductSeeder.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Eclipse\Catalogue\Seeders;
44

55
use Eclipse\Catalogue\Models\Product;
6+
use Eclipse\Catalogue\Models\ProductType;
67
use Exception;
78
use Illuminate\Database\Seeder;
89
use Illuminate\Support\Facades\Http;
@@ -13,10 +14,17 @@ class ProductSeeder extends Seeder
1314
public function run(): void
1415
{
1516
$this->ensureSampleImagesExist();
17+
$this->ensureProductTypesExist();
18+
19+
$productTypes = ProductType::all();
1620

1721
Product::factory()
1822
->count(100)
19-
->create();
23+
->create([
24+
'product_type_id' => function () use ($productTypes) {
25+
return $productTypes->random()->id;
26+
},
27+
]);
2028
}
2129

2230
private function ensureSampleImagesExist(): void
@@ -55,4 +63,13 @@ private function ensureSampleImagesExist(): void
5563

5664
$this->command->info('Sample images ready!');
5765
}
66+
67+
private function ensureProductTypesExist(): void
68+
{
69+
$productTypes = ProductType::all();
70+
71+
if ($productTypes->isEmpty()) {
72+
$this->call(ProductTypeSeeder::class);
73+
}
74+
}
5875
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Eclipse\Catalogue\Seeders;
4+
5+
use Eclipse\Catalogue\Models\ProductType;
6+
use Eclipse\Catalogue\Models\ProductTypeData;
7+
use Illuminate\Database\Seeder;
8+
9+
class ProductTypeSeeder extends Seeder
10+
{
11+
public function run(): void
12+
{
13+
$tenantModel = config('eclipse-catalogue.tenancy.model');
14+
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key');
15+
$tenants = $tenantModel::all();
16+
17+
$productTypes = [
18+
[
19+
'name' => $this->getTranslatedName('Common product'),
20+
'code' => 'COMMON',
21+
],
22+
[
23+
'name' => $this->getTranslatedName('T-shirt'),
24+
'code' => 'TSHIRT',
25+
],
26+
[
27+
'name' => $this->getTranslatedName('Shoes'),
28+
'code' => 'SHOES',
29+
],
30+
];
31+
32+
foreach ($productTypes as $productTypeData) {
33+
$productType = ProductType::firstOrCreate(
34+
['code' => $productTypeData['code']],
35+
$productTypeData
36+
);
37+
38+
foreach ($tenants as $tenant) {
39+
ProductTypeData::firstOrCreate(
40+
[
41+
'product_type_id' => $productType->id,
42+
$tenantFK => $tenant->id,
43+
],
44+
[
45+
'product_type_id' => $productType->id,
46+
$tenantFK => $tenant->id,
47+
'is_active' => true,
48+
'is_default' => false,
49+
]
50+
);
51+
}
52+
}
53+
}
54+
55+
/**
56+
* Get translated name for all available locales.
57+
*/
58+
protected function getTranslatedName(string $baseName): array
59+
{
60+
$locales = $this->getAvailableLocales();
61+
$translatedNames = [];
62+
63+
foreach ($locales as $locale) {
64+
if ($locale === 'en') {
65+
$translatedNames[$locale] = $baseName;
66+
} else {
67+
$translatedNames[$locale] = strtoupper($locale).': '.$baseName;
68+
}
69+
}
70+
71+
return $translatedNames;
72+
}
73+
74+
/**
75+
* Get available locales for the application.
76+
*/
77+
protected function getAvailableLocales(): array
78+
{
79+
if (class_exists(\Eclipse\Core\Models\Locale::class)) {
80+
return \Eclipse\Core\Models\Locale::getAvailableLocales()
81+
->pluck('id')
82+
->toArray();
83+
}
84+
85+
return ['en'];
86+
}
87+
}

resources/lang/en/product-type.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
'fields' => [
88
'name' => 'Name',
9-
'name_en' => 'Name (English)',
10-
'name_hr' => 'Name (Croatian)',
11-
'name_sl' => 'Name (Slovenian)',
12-
'name_sr' => 'Name (Serbian)',
139
'code' => 'Code',
1410
'is_active' => 'Active',
1511
'is_default' => 'Default Type',
@@ -28,10 +24,6 @@
2824

2925
'placeholders' => [
3026
'name' => 'Enter product type name',
31-
'name_en' => 'Enter product type name in English',
32-
'name_hr' => 'Enter product type name in Croatian',
33-
'name_sl' => 'Enter product type name in Slovenian',
34-
'name_sr' => 'Enter product type name in Serbian',
3527
'code' => 'Optional code for identification',
3628
'tenant' => 'Select tenant',
3729
],

src/Factories/ProductTypeFactory.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,39 @@ class ProductTypeFactory extends Factory
1111

1212
public function definition(): array
1313
{
14+
$baseName = $this->faker->words(2, true);
15+
16+
$locales = $this->getAvailableLocales();
17+
18+
$translatedNames = [];
19+
foreach ($locales as $locale) {
20+
if ($locale === 'en') {
21+
$translatedNames[$locale] = $baseName;
22+
} else {
23+
$translatedNames[$locale] = strtoupper($locale).': '.$baseName;
24+
}
25+
}
26+
1427
return [
15-
'name' => [
16-
'en' => $this->faker->words(2, true),
17-
'hr' => $this->faker->words(2, true),
18-
'sl' => $this->faker->words(2, true),
19-
'sr' => $this->faker->words(2, true),
20-
],
28+
'name' => $translatedNames,
2129
'code' => $this->faker->unique()->regexify('[A-Z]{2}[0-9]{3}'),
2230
];
2331
}
2432

33+
/**
34+
* Get available locales for the application.
35+
*/
36+
protected function getAvailableLocales(): array
37+
{
38+
if (class_exists(\Eclipse\Core\Models\Locale::class)) {
39+
return \Eclipse\Core\Models\Locale::getAvailableLocales()
40+
->pluck('id')
41+
->toArray();
42+
}
43+
44+
return ['en'];
45+
}
46+
2547
public function withName(string|array $name): static
2648
{
2749
return $this->state(fn (array $attributes) => [

src/Filament/Resources/ProductTypeResource/Pages/CreateProductType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
use Eclipse\Catalogue\Traits\HandlesTenantData;
88
use Eclipse\Catalogue\Traits\HasProductTypeForm;
99
use Eclipse\Catalogue\Traits\HasTenantFields;
10+
use Filament\Actions\LocaleSwitcher;
1011
use Filament\Forms\Form;
11-
use Filament\Resources\Concerns\Translatable;
1212
use Filament\Resources\Pages\CreateRecord;
13+
use Filament\Resources\Pages\CreateRecord\Concerns\Translatable;
1314
use Illuminate\Database\Eloquent\Model;
1415

1516
class CreateProductType extends CreateRecord
@@ -18,6 +19,13 @@ class CreateProductType extends CreateRecord
1819

1920
protected static string $resource = ProductTypeResource::class;
2021

22+
protected function getHeaderActions(): array
23+
{
24+
return [
25+
LocaleSwitcher::make(),
26+
];
27+
}
28+
2129
protected function getFormTenantFlags(): array
2230
{
2331
return ['is_active', 'is_default'];

src/Filament/Resources/ProductTypeResource/Pages/EditProductType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
use Eclipse\Catalogue\Traits\HasTenantFields;
99
use Filament\Actions\DeleteAction;
1010
use Filament\Actions\ForceDeleteAction;
11+
use Filament\Actions\LocaleSwitcher;
1112
use Filament\Actions\RestoreAction;
1213
use Filament\Forms\Form;
13-
use Filament\Resources\Concerns\Translatable;
1414
use Filament\Resources\Pages\EditRecord;
15+
use Filament\Resources\Pages\EditRecord\Concerns\Translatable;
1516
use Illuminate\Database\Eloquent\Model;
1617

1718
class EditProductType extends EditRecord
@@ -33,6 +34,7 @@ protected function getFormMutuallyExclusiveFlagSets(): array
3334
protected function getHeaderActions(): array
3435
{
3536
return [
37+
LocaleSwitcher::make(),
3638
DeleteAction::make(),
3739
ForceDeleteAction::make(),
3840
RestoreAction::make(),

src/Filament/Resources/ProductTypeResource/Pages/ListProductTypes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Eclipse\Catalogue\Filament\Resources\ProductTypeResource;
66
use Filament\Actions\CreateAction;
7+
use Filament\Actions\LocaleSwitcher;
78
use Filament\Resources\Concerns\Translatable;
89
use Filament\Resources\Pages\ListRecords;
910

@@ -16,6 +17,7 @@ class ListProductTypes extends ListRecords
1617
protected function getHeaderActions(): array
1718
{
1819
return [
20+
LocaleSwitcher::make(),
1921
CreateAction::make(),
2022
];
2123
}

0 commit comments

Comments
 (0)