b2in/database/seeders/CmsContentSeeder.php
2026-04-10 17:18:17 +02:00

118 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Database\Seeders;
use FluxCms\Core\Models\CmsContent;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Lang;
class CmsContentSeeder extends Seeder
{
/**
* Sections to skip (not migrated, handled elsewhere or irrelevant).
*
* netzwerk_cabinet_partner / netzwerk_immobilien_hint: siehe NetzwerkPageCmsSeeder
* (nur einfügen wenn leer, damit manuelle CMS-Anpassungen nicht überschrieben werden).
*
* @var array<string>
*/
protected array $skipSections = [
'netzwerk_cabinet_partner',
'netzwerk_immobilien_hint',
];
public function run(): void
{
/** @var array<string, array{0: string, 1: string}> $sectionMap */
$sectionMap = config('cms_section_map.sections', []);
/** @var array<string, string> $langKeys */
$langKeys = config('cms_section_map.lang_keys', []);
/** @var array<string, mixed> $configTheme */
$configTheme = config('content.themes.b2in', []);
/** @var array<string, mixed> $langDe */
$langDe = Lang::get('b2in.themes.b2in', [], 'de');
/** @var array<string, mixed> $langEn */
$langEn = Lang::get('b2in.themes.b2in', [], 'en');
if (! is_array($langDe) || empty($langDe)) {
$this->command?->warn('No b2in theme found in resources/lang/de/b2in.php (key: b2in.themes.b2in) skipping.');
return;
}
if (! is_array($langEn)) {
$langEn = [];
}
/** @var array<string, mixed> $themeDe */
$themeDe = array_merge($configTheme, $langDe);
/** @var array<string, mixed> $themeEn */
$themeEn = array_merge($configTheme, $langEn);
$created = 0;
$updated = 0;
foreach ($sectionMap as $sectionKey => $groupKeyPair) {
if (in_array($sectionKey, $this->skipSections, true)) {
continue;
}
$langSectionKey = $langKeys[$sectionKey] ?? $sectionKey;
if (! array_key_exists($langSectionKey, $themeDe)) {
$this->command?->warn("Unmapped or missing theme data for section: {$sectionKey} (lang key: {$langSectionKey}) skipping.");
continue;
}
$sectionDataDe = $themeDe[$langSectionKey];
$sectionDataEn = $themeEn[$langSectionKey] ?? $sectionDataDe;
[$group, $key] = $groupKeyPair;
$type = $this->detectType($sectionDataDe);
$content = CmsContent::query()
->where('group', $group)
->where('key', $key)
->first();
$value = [
'de' => $sectionDataDe,
'en' => $sectionDataEn,
];
if ($content) {
$content->update([
'type' => $type,
'value' => $value,
]);
$updated++;
} else {
CmsContent::query()->create([
'group' => $group,
'key' => $key,
'type' => $type,
'value' => $value,
'order' => 0,
]);
$created++;
}
}
$this->command?->info("CmsContent: {$created} created, {$updated} updated.");
}
private function detectType(mixed $data): string
{
if (is_array($data)) {
return 'json';
}
if (is_string($data) && preg_match('/<[^>]+>/', $data)) {
return 'html';
}
return 'text';
}
}