Refactor TemplatePreviewController to support template content retrieval by ID: Updated the preview method to accept a record ID for fetching template content from the database. Introduced a private method to handle content retrieval for header, footer, and section templates. Enhanced error handling for empty content scenarios, improving the overall robustness of the template preview functionality.

This commit is contained in:
Ümit Tunç
2025-11-01 15:31:53 -03:00
parent bea17d583f
commit 78d3bce584
2 changed files with 46 additions and 571 deletions
@@ -2,6 +2,9 @@
namespace App\Http\Controllers;
use App\Models\HeaderTemplate;
use App\Models\FooterTemplate;
use App\Models\SectionTemplate;
use App\Services\TemplatePreviewService;
use Illuminate\Http\Request;
@@ -11,11 +14,11 @@ class TemplatePreviewController extends Controller
* Render template preview
*
* @param Request $request
* @return \Illuminate\View\View
* @return \Illuminate\Http\Response
*/
public function preview(Request $request)
{
$content = $request->input('content', '');
$recordId = $request->input('record_id');
$type = $request->input('type', 'section'); // header, footer, section
// Validate type
@@ -23,10 +26,40 @@ class TemplatePreviewController extends Controller
$type = 'section';
}
// ID varsa veritabanından çek, yoksa eskisi gibi çalış
$content = '';
if ($recordId) {
$content = $this->getTemplateContent($recordId, $type);
} else {
// Fallback: eski yöntem (content gönderimi)
$content = $request->input('content', '');
}
if (empty($content)) {
return response('No content to preview', 400);
}
$html = TemplatePreviewService::getPreviewHtml($content, $type);
return response($html)
->header('Content-Type', 'text/html; charset=utf-8');
}
/**
* Get template content from database by ID
*
* @param int $id
* @param string $type
* @return string
*/
private function getTemplateContent(int $id, string $type): string
{
return match($type) {
'header' => HeaderTemplate::find($id)?->html_content ?? '',
'footer' => FooterTemplate::find($id)?->html_content ?? '',
'section' => SectionTemplate::find($id)?->html_content ?? '',
default => '',
};
}
}