|
| 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 | +} |
0 commit comments