From f609eb3ec1b89aed511b68b6dea0b5544a4966f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Sat, 27 Dec 2025 09:38:38 +0300 Subject: [PATCH] Enhance Template Import and Preview Functionality: Updated the ImportHtmlTemplates command to improve asset path handling by moving files to storage and updating HTML asset paths. Added a preview action in SectionTemplatesTable for better template management, along with a new Blade view for rendering previews. Adjusted route for template preview to accept any request method for flexibility. --- app/Console/Commands/ImportHtmlTemplates.php | 40 +++++++++++++++++++ .../Tables/SectionTemplatesTable.php | 8 ++++ app/Services/TemplateService.php | 8 +++- .../section-templates/preview.blade.php | 11 +++++ routes/web.php | 2 +- 5 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 resources/views/filament/admin/resources/section-templates/preview.blade.php diff --git a/app/Console/Commands/ImportHtmlTemplates.php b/app/Console/Commands/ImportHtmlTemplates.php index 886010c..2009896 100644 --- a/app/Console/Commands/ImportHtmlTemplates.php +++ b/app/Console/Commands/ImportHtmlTemplates.php @@ -7,6 +7,7 @@ use App\Models\HeaderTemplate; use App\Models\SectionTemplate; use Illuminate\Console\Command; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Storage; use DOMDocument; use DOMXPath; use DOMElement; @@ -313,14 +314,53 @@ class ImportHtmlTemplates extends Command protected function fixAssetPath($path) { + // Eğer URL ise elleme + if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) { + return $path; + } + + // Assets klasöründeki dosyaları storage'a taşı if (str_starts_with($path, 'assets/')) { + $sourcePath = public_path('html/' . $path); + + if (File::exists($sourcePath)) { + // Dosya adını ve klasörünü belirle + $fileName = basename($path); + // Çakışmayı önlemek için orijinal klasör yapısını korumaya çalışabiliriz + // veya basitçe 'templates/imported' altına atabiliriz. + + // Storage hedef yolu: templates/imported/filename.ext + $targetDir = 'templates/imported'; + $targetPath = $targetDir . '/' . $fileName; + + // Eğer storage'da bu dosya yoksa kopyala + if (!Storage::disk('public')->exists($targetPath)) { + Storage::disk('public')->put($targetPath, File::get($sourcePath)); + } + + // Veritabanına kaydedilecek değer: templates/imported/filename.ext + // Bu değer Filament FileUpload tarafından 'public' diskinde otomatik olarak bulunur. + return $targetPath; + } + + // Dosya bulunamadıysa eski usul devam et return '/html/' . $path; } + return $path; } protected function fixAssetPathInString($html) { + // HTML string içindeki asset pathlerini güncelle + // Burada da aynı mantıkla storage pathlerine çevirebilirdik ama + // string içindeki pathleri tek tek bulup kopyalamak zor ve performanssız olabilir. + // Şimdilik sadece URL'leri düzeltiyoruz, import edilen resimler zaten processNode kısmında hallediliyor. + // Background-image gibi CSS içindeki pathler için burası önemli. + + // CSS içindeki url('assets/...') kısımlarını yakalamak için daha genel bir yaklaşım + // Ancak processNode zaten img taglerini hallettiği için burası sadece kalanlar için. + $html = str_replace('src="assets/', 'src="/html/assets/', $html); $html = str_replace('href="assets/', 'href="/html/assets/', $html); $html = str_replace("src='assets/", "src='/html/assets/", $html); diff --git a/app/Filament/Admin/Resources/SectionTemplates/Tables/SectionTemplatesTable.php b/app/Filament/Admin/Resources/SectionTemplates/Tables/SectionTemplatesTable.php index 9e3f887..401001c 100644 --- a/app/Filament/Admin/Resources/SectionTemplates/Tables/SectionTemplatesTable.php +++ b/app/Filament/Admin/Resources/SectionTemplates/Tables/SectionTemplatesTable.php @@ -8,6 +8,7 @@ use Filament\Actions\EditAction; use Filament\Actions\ForceDeleteBulkAction; use Filament\Actions\RestoreBulkAction; use Filament\Actions\ViewAction; +use Filament\Actions\Action; use Filament\Tables\Columns\IconColumn; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Filters\TrashedFilter; @@ -51,6 +52,13 @@ class SectionTemplatesTable TrashedFilter::make(), ]) ->recordActions([ + Action::make('preview') + ->label(__('section-templates.action_preview')) + ->icon('heroicon-o-eye') + ->modalContent(fn ($record) => view('filament.admin.resources.section-templates.preview', ['record' => $record])) + ->modalSubmitAction(false) + ->modalCancelAction(false) + ->modalWidth('7xl'), ViewAction::make(), EditAction::make(), ]) diff --git a/app/Services/TemplateService.php b/app/Services/TemplateService.php index bd5b036..650e38a 100644 --- a/app/Services/TemplateService.php +++ b/app/Services/TemplateService.php @@ -314,8 +314,9 @@ class TemplateService * @param string $html HTML content with placeholders * @param array $data Data array for placeholders * @param Model|null $model Optional model instance (Page, Blog, etc.) for dynamic data + * @param array $defaultData Optional default data for fallback */ - public static function replacePlaceholders(string $html, array $data, ?Model $model = null): string + public static function replacePlaceholders(string $html, array $data, ?Model $model = null, array $defaultData = []): string { // Handle special {page.content} placeholder - Sayfa/model içeriğini gösterir if (str_contains($html, '{page.content}')) { @@ -564,6 +565,11 @@ class TemplateService } } + // 5. Fallback to default data + if (($value === null || $value === '') && isset($defaultData[$placeholder])) { + $value = $defaultData[$placeholder]; + } + // Handle different value types if (is_array($value)) { // Handle arrays (e.g., multiple images) diff --git a/resources/views/filament/admin/resources/section-templates/preview.blade.php b/resources/views/filament/admin/resources/section-templates/preview.blade.php new file mode 100644 index 0000000..83141f2 --- /dev/null +++ b/resources/views/filament/admin/resources/section-templates/preview.blade.php @@ -0,0 +1,11 @@ +
+
+ +
+
+ diff --git a/routes/web.php b/routes/web.php index b605d17..3de21cf 100644 --- a/routes/web.php +++ b/routes/web.php @@ -77,7 +77,7 @@ Route::get('/blog/{slug}', [BlogController::class, 'show'])->name('blog.show'); Route::post('/blog/{slug}/comment', [BlogController::class, 'storeComment'])->name('blog.comment.store'); // Template Preview (admin panel için) -Route::post('/admin/template-preview', [TemplatePreviewController::class, 'preview']) +Route::any('/admin/template-preview', [TemplatePreviewController::class, 'preview']) ->middleware(['auth']) ->name('template.preview');