Refactor TemplatePreviewController to enhance template retrieval and default data handling: Updated the preview method to fetch template objects by ID, introduced default data management, and replaced placeholders with default values using TemplateService. Improved method signatures for clarity and consistency in data handling.

This commit is contained in:
Ümit Tunç
2025-11-01 15:55:39 -03:00
parent 4718535fda
commit 1d46c9243d
@@ -6,6 +6,7 @@ use App\Models\HeaderTemplate;
use App\Models\FooterTemplate;
use App\Models\SectionTemplate;
use App\Services\TemplatePreviewService;
use App\Services\TemplateService;
use Illuminate\Http\Request;
class TemplatePreviewController extends Controller
@@ -26,10 +27,16 @@ class TemplatePreviewController extends Controller
$type = 'section';
}
// ID varsa veritabanından çek, yoksa eskisi gibi çalış
// Get template and its default data
$content = '';
$defaultData = [];
if ($recordId) {
$content = $this->getTemplateContent($recordId, $type);
$template = $this->getTemplate($recordId, $type);
if ($template) {
$content = $template->html_content ?? '';
$defaultData = $template->default_data ?? [];
}
} else {
// Fallback: eski yöntem (content gönderimi)
$content = $request->input('content', '');
@@ -39,6 +46,11 @@ class TemplatePreviewController extends Controller
return response('No content to preview', 400);
}
// Replace placeholders with default data
if (!empty($defaultData)) {
$content = TemplateService::replacePlaceholders($content, $defaultData);
}
$html = TemplatePreviewService::getPreviewHtml($content, $type);
return response($html)
@@ -46,19 +58,19 @@ class TemplatePreviewController extends Controller
}
/**
* Get template content from database by ID
* Get template object from database by ID
*
* @param int $id
* @param string $type
* @return string
* @return object|null
*/
private function getTemplateContent(int $id, string $type): string
private function getTemplate(int $id, string $type): ?object
{
return match($type) {
'header' => HeaderTemplate::find($id)?->html_content ?? '',
'footer' => FooterTemplate::find($id)?->html_content ?? '',
'section' => SectionTemplate::find($id)?->html_content ?? '',
default => '',
'header' => HeaderTemplate::find($id),
'footer' => FooterTemplate::find($id),
'section' => SectionTemplate::find($id),
default => null,
};
}
}