Implement default data functionality for templates: Added 'default_data' field to HeaderTemplate, FooterTemplate, and SectionTemplate models. Enhanced TemplateService to generate dynamic fields based on this new data. Updated form schemas in template forms to include sections for default values, improving template management and usability. Added localization support for new fields in both English and Turkish.
This commit is contained in:
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace App\Filament\Admin\Resources\FooterTemplates\Schemas;
|
||||
|
||||
use App\Models\FooterTemplate;
|
||||
use App\Services\TemplateService;
|
||||
use Filament\Forms\Components\CodeEditor;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Group;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Components\View;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Forms\Components\CodeEditor\Enums\Language;
|
||||
@@ -51,6 +55,38 @@ class FooterTemplateForm
|
||||
->inline(false),
|
||||
])
|
||||
->columnSpanFull(),
|
||||
|
||||
// Default Data Section - Dynamic fields based on placeholders
|
||||
Section::make(__('footer-templates.section_default_data'))
|
||||
->description(__('footer-templates.section_default_data_desc'))
|
||||
->schema([
|
||||
Group::make()
|
||||
->schema(function (Get $get, $record): array {
|
||||
$htmlContent = $get('html_content');
|
||||
if (empty($htmlContent)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Create a temporary template object to parse placeholders
|
||||
$tempTemplate = new FooterTemplate();
|
||||
$tempTemplate->html_content = $htmlContent;
|
||||
|
||||
// Get existing default_data if editing
|
||||
$existingData = $record ? ($record->default_data ?? []) : [];
|
||||
|
||||
return TemplateService::generateDynamicFields(
|
||||
$tempTemplate,
|
||||
'default_data',
|
||||
$existingData
|
||||
);
|
||||
})
|
||||
->key('default_data_fields')
|
||||
->visible(fn (Get $get): bool => filled($get('html_content')))
|
||||
->columnSpanFull(),
|
||||
])
|
||||
->columnSpanFull()
|
||||
->collapsible(true)
|
||||
->collapsed(false),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace App\Filament\Admin\Resources\HeaderTemplates\Schemas;
|
||||
|
||||
use App\Models\HeaderTemplate;
|
||||
use App\Services\TemplateService;
|
||||
use Filament\Forms\Components\CodeEditor;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Group;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Components\View;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Forms\Components\CodeEditor\Enums\Language;
|
||||
@@ -50,6 +54,38 @@ class HeaderTemplateForm
|
||||
->inline(false),
|
||||
])
|
||||
->columnSpanFull(),
|
||||
|
||||
// Default Data Section - Dynamic fields based on placeholders
|
||||
Section::make(__('header-templates.section_default_data'))
|
||||
->description(__('header-templates.section_default_data_desc'))
|
||||
->schema([
|
||||
Group::make()
|
||||
->schema(function (Get $get, $record): array {
|
||||
$htmlContent = $get('html_content');
|
||||
if (empty($htmlContent)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Create a temporary template object to parse placeholders
|
||||
$tempTemplate = new HeaderTemplate();
|
||||
$tempTemplate->html_content = $htmlContent;
|
||||
|
||||
// Get existing default_data if editing
|
||||
$existingData = $record ? ($record->default_data ?? []) : [];
|
||||
|
||||
return TemplateService::generateDynamicFields(
|
||||
$tempTemplate,
|
||||
'default_data',
|
||||
$existingData
|
||||
);
|
||||
})
|
||||
->key('default_data_fields')
|
||||
->visible(fn (Get $get): bool => filled($get('html_content')))
|
||||
->columnSpanFull(),
|
||||
])
|
||||
->columnSpanFull()
|
||||
->collapsible(true)
|
||||
->collapsed(false),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,14 +542,43 @@ class PageForm
|
||||
->options(HeaderTemplate::where('is_active', true)->pluck('title', 'id'))
|
||||
->searchable()
|
||||
->live()
|
||||
->afterStateUpdated(fn (Set $set) => $set('header_data', []))
|
||||
->afterStateUpdated(function (Set $set, Get $get, $state, $record) {
|
||||
$template = HeaderTemplate::find($state);
|
||||
if ($template && $template->default_data) {
|
||||
// If page doesn't have header_data or it's empty, load defaults
|
||||
$currentData = $record ? ($record->header_data ?? []) : ($get('header_data') ?? []);
|
||||
if (empty($currentData)) {
|
||||
$set('header_data', $template->default_data);
|
||||
}
|
||||
} else {
|
||||
// If no template selected or no defaults, clear data
|
||||
if (!$record || empty($record->header_data)) {
|
||||
$set('header_data', []);
|
||||
}
|
||||
}
|
||||
})
|
||||
->columnSpanFull(),
|
||||
|
||||
Group::make()
|
||||
->schema(fn (Get $get): array => TemplateService::generateDynamicFields(
|
||||
HeaderTemplate::find($get('header_template_id')),
|
||||
'header_data'
|
||||
))
|
||||
->schema(function (Get $get, $record): array {
|
||||
$template = HeaderTemplate::find($get('header_template_id'));
|
||||
if (!$template) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Get page's header_data or template's default_data
|
||||
$pageData = $record ? ($record->header_data ?? []) : ($get('header_data') ?? []);
|
||||
$templateDefaults = $template->default_data ?? [];
|
||||
|
||||
// Merge: page data overrides template defaults
|
||||
$mergedData = array_merge($templateDefaults, $pageData);
|
||||
|
||||
return TemplateService::generateDynamicFields(
|
||||
$template,
|
||||
'header_data',
|
||||
$mergedData
|
||||
);
|
||||
})
|
||||
->visible(fn (Get $get): bool => filled($get('header_template_id')))
|
||||
->columnSpanFull(),
|
||||
])
|
||||
@@ -569,13 +598,38 @@ class PageForm
|
||||
->searchable()
|
||||
->live()
|
||||
->required()
|
||||
->afterStateUpdated(function (Set $set, Get $get, $state, $record) {
|
||||
$template = SectionTemplate::find($state);
|
||||
if ($template && $template->default_data) {
|
||||
// If section doesn't have section_data or it's empty, load defaults
|
||||
$currentData = $get('section_data') ?? [];
|
||||
if (empty($currentData)) {
|
||||
$set('section_data', $template->default_data);
|
||||
}
|
||||
}
|
||||
})
|
||||
->columnSpanFull(),
|
||||
|
||||
Group::make()
|
||||
->schema(fn (Get $get): array => TemplateService::generateDynamicFields(
|
||||
SectionTemplate::find($get('section_template_id')),
|
||||
'section_data'
|
||||
))
|
||||
->schema(function (Get $get): array {
|
||||
$template = SectionTemplate::find($get('section_template_id'));
|
||||
if (!$template) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Get section's data or template's default_data
|
||||
$sectionData = $get('section_data') ?? [];
|
||||
$templateDefaults = $template->default_data ?? [];
|
||||
|
||||
// Merge: section data overrides template defaults
|
||||
$mergedData = array_merge($templateDefaults, $sectionData);
|
||||
|
||||
return TemplateService::generateDynamicFields(
|
||||
$template,
|
||||
'section_data',
|
||||
$mergedData
|
||||
);
|
||||
})
|
||||
->visible(fn (Get $get): bool => filled($get('section_template_id')))
|
||||
->columnSpanFull(),
|
||||
])
|
||||
@@ -600,14 +654,43 @@ class PageForm
|
||||
->options(FooterTemplate::where('is_active', true)->pluck('title', 'id'))
|
||||
->searchable()
|
||||
->live()
|
||||
->afterStateUpdated(fn (Set $set) => $set('footer_data', []))
|
||||
->afterStateUpdated(function (Set $set, Get $get, $state, $record) {
|
||||
$template = FooterTemplate::find($state);
|
||||
if ($template && $template->default_data) {
|
||||
// If page doesn't have footer_data or it's empty, load defaults
|
||||
$currentData = $record ? ($record->footer_data ?? []) : ($get('footer_data') ?? []);
|
||||
if (empty($currentData)) {
|
||||
$set('footer_data', $template->default_data);
|
||||
}
|
||||
} else {
|
||||
// If no template selected or no defaults, clear data
|
||||
if (!$record || empty($record->footer_data)) {
|
||||
$set('footer_data', []);
|
||||
}
|
||||
}
|
||||
})
|
||||
->columnSpanFull(),
|
||||
|
||||
Group::make()
|
||||
->schema(fn (Get $get): array => TemplateService::generateDynamicFields(
|
||||
FooterTemplate::find($get('footer_template_id')),
|
||||
'footer_data'
|
||||
))
|
||||
->schema(function (Get $get, $record): array {
|
||||
$template = FooterTemplate::find($get('footer_template_id'));
|
||||
if (!$template) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Get page's footer_data or template's default_data
|
||||
$pageData = $record ? ($record->footer_data ?? []) : ($get('footer_data') ?? []);
|
||||
$templateDefaults = $template->default_data ?? [];
|
||||
|
||||
// Merge: page data overrides template defaults
|
||||
$mergedData = array_merge($templateDefaults, $pageData);
|
||||
|
||||
return TemplateService::generateDynamicFields(
|
||||
$template,
|
||||
'footer_data',
|
||||
$mergedData
|
||||
);
|
||||
})
|
||||
->visible(fn (Get $get): bool => filled($get('footer_template_id')))
|
||||
->columnSpanFull(),
|
||||
])
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace App\Filament\Admin\Resources\SectionTemplates\Schemas;
|
||||
|
||||
use App\Models\SectionTemplate;
|
||||
use App\Services\TemplateService;
|
||||
use Filament\Forms\Components\CodeEditor;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Group;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Components\View;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Forms\Components\CodeEditor\Enums\Language;
|
||||
@@ -50,6 +54,38 @@ class SectionTemplateForm
|
||||
->inline(false),
|
||||
])
|
||||
->columnSpanFull(),
|
||||
|
||||
// Default Data Section - Dynamic fields based on placeholders
|
||||
Section::make(__('section-templates.section_default_data'))
|
||||
->description(__('section-templates.section_default_data_desc'))
|
||||
->schema([
|
||||
Group::make()
|
||||
->schema(function (Get $get, $record): array {
|
||||
$htmlContent = $get('html_content');
|
||||
if (empty($htmlContent)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Create a temporary template object to parse placeholders
|
||||
$tempTemplate = new SectionTemplate();
|
||||
$tempTemplate->html_content = $htmlContent;
|
||||
|
||||
// Get existing default_data if editing
|
||||
$existingData = $record ? ($record->default_data ?? []) : [];
|
||||
|
||||
return TemplateService::generateDynamicFields(
|
||||
$tempTemplate,
|
||||
'default_data',
|
||||
$existingData
|
||||
);
|
||||
})
|
||||
->key('default_data_fields')
|
||||
->visible(fn (Get $get): bool => filled($get('html_content')))
|
||||
->columnSpanFull(),
|
||||
])
|
||||
->columnSpanFull()
|
||||
->collapsible(true)
|
||||
->collapsed(false),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user