Add dynamic template system: Implemented header, footer, and section templates with corresponding models, migrations, and Filament resources. Enhanced Page model to support dynamic templates and updated localization files for new terms. Added TemplateService for managing template data and rendering. Comprehensive documentation included for usage and best practices.

This commit is contained in:
Ümit Tunç
2025-10-31 14:53:34 -03:00
parent 0f78292c46
commit c042cb5114
46 changed files with 2709 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
class FooterTemplate extends Model
{
use SoftDeletes;
protected $fillable = [
'title',
'html_content',
'is_active',
];
protected $casts = [
'is_active' => 'boolean',
];
/**
* Get the pages that use this footer template.
*/
public function pages(): HasMany
{
return $this->hasMany(Page::class, 'footer_template_id');
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
class HeaderTemplate extends Model
{
use SoftDeletes;
protected $fillable = [
'title',
'html_content',
'is_active',
];
protected $casts = [
'is_active' => 'boolean',
];
/**
* Get the pages that use this header template.
*/
public function pages(): HasMany
{
return $this->hasMany(Page::class, 'header_template_id');
}
}
+47
View File
@@ -29,6 +29,12 @@ class Page extends Model
'show_in_menu',
'sections',
'data',
// Template System
'header_template_id',
'header_data',
'footer_template_id',
'footer_data',
'sections_data',
];
/**
@@ -49,6 +55,10 @@ class Page extends Model
'sort_order' => 'integer',
'sections' => 'array',
'data' => 'array',
// Template System
'header_data' => 'array',
'footer_data' => 'array',
'sections_data' => 'array',
];
public function author()
@@ -66,6 +76,22 @@ class Page extends Model
return $this->hasMany(Page::class, 'parent_id');
}
/**
* Get the header template for the page.
*/
public function headerTemplate()
{
return $this->belongsTo(HeaderTemplate::class, 'header_template_id');
}
/**
* Get the footer template for the page.
*/
public function footerTemplate()
{
return $this->belongsTo(FooterTemplate::class, 'footer_template_id');
}
public function getRouteKeyName()
{
return 'slug';
@@ -124,4 +150,25 @@ class Page extends Model
return $matchingSections[$index] ?? null;
}
/**
* Get templated sections with their templates loaded.
* Used for new dynamic template system.
*/
public function getTemplatedSectionsAttribute()
{
if (!$this->sections_data || !is_array($this->sections_data)) {
return collect([]);
}
return collect($this->sections_data)->map(function ($section) {
$templateId = $section['section_template_id'] ?? null;
$template = $templateId ? SectionTemplate::find($templateId) : null;
return [
'template' => $template,
'data' => $section['section_data'] ?? [],
];
})->filter(fn($section) => $section['template'] !== null);
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class SectionTemplate extends Model
{
use SoftDeletes;
protected $fillable = [
'title',
'html_content',
'is_active',
];
protected $casts = [
'is_active' => 'boolean',
];
/**
* Section templates are used via JSON in pages.sections_data
* No direct relation needed, but we can add a helper method
*/
public function getPagesUsingThisTemplate()
{
return Page::whereJsonContains('sections_data', [['section_template_id' => $this->id]])->get();
}
}