123 lines
3.3 KiB
PHP
123 lines
3.3 KiB
PHP
<?php
|
||
|
||
namespace App\Support;
|
||
|
||
use App\Models\Page;
|
||
|
||
class PageTemplateHero
|
||
{
|
||
public static function config(?string $template): ?array
|
||
{
|
||
if (! filled($template)) {
|
||
return null;
|
||
}
|
||
|
||
$heroes = config('page_templates.heroes', []);
|
||
|
||
return $heroes[$template] ?? null;
|
||
}
|
||
|
||
public static function hasHero(?string $template): bool
|
||
{
|
||
return filled(static::imagePath($template));
|
||
}
|
||
|
||
public static function imagePath(?string $template): ?string
|
||
{
|
||
return static::config($template)['image'] ?? null;
|
||
}
|
||
|
||
public static function webpPath(?string $template): ?string
|
||
{
|
||
return static::config($template)['webp'] ?? null;
|
||
}
|
||
|
||
public static function url(?string $template): ?string
|
||
{
|
||
$path = static::imagePath($template);
|
||
|
||
return $path ? asset($path) : null;
|
||
}
|
||
|
||
public static function webpUrl(?string $template): ?string
|
||
{
|
||
$path = static::webpPath($template);
|
||
|
||
return $path ? asset($path) : null;
|
||
}
|
||
|
||
public static function featuredStoragePath(Page $page): ?string
|
||
{
|
||
$value = $page->featured_image;
|
||
|
||
if (blank($value)) {
|
||
return null;
|
||
}
|
||
|
||
if (is_array($value)) {
|
||
$value = $value[0] ?? null;
|
||
}
|
||
|
||
return is_string($value) && $value !== '' ? $value : null;
|
||
}
|
||
|
||
public static function urlForPage(Page $page): ?string
|
||
{
|
||
if ($path = static::featuredStoragePath($page)) {
|
||
return asset('storage/' . $path);
|
||
}
|
||
|
||
return static::url($page->template);
|
||
}
|
||
|
||
/**
|
||
* Hero alanı için görsel verisi (yüklenen öne çıkan görsel veya şablon varsayılanı).
|
||
*
|
||
* @return array{image: string, webp?: string, width?: int, height?: int, from_upload: bool}|null
|
||
*/
|
||
public static function resolveForPage(?Page $page, ?string $templateFallback = null): ?array
|
||
{
|
||
if ($page && ($path = static::featuredStoragePath($page))) {
|
||
return [
|
||
'image' => asset('storage/' . $path),
|
||
'from_upload' => true,
|
||
];
|
||
}
|
||
|
||
$template = $page?->template ?? $templateFallback;
|
||
$config = static::config($template);
|
||
|
||
if (! $config) {
|
||
return null;
|
||
}
|
||
|
||
return array_merge($config, ['from_upload' => false]);
|
||
}
|
||
|
||
/**
|
||
* @return array<string, string>
|
||
*/
|
||
public static function templateFormOptions(): array
|
||
{
|
||
$base = [
|
||
'default' => __('pages.template_default'),
|
||
'landing' => __('pages.template_landing'),
|
||
'blog' => __('pages.template_blog'),
|
||
'contact' => __('pages.template_contact'),
|
||
'home' => 'Home',
|
||
'corporate.testimonials' => 'Müşteri Görüşleri (Kurumsal)',
|
||
'corporate.logos' => 'Logolarımız (Kurumsal)',
|
||
'corporate.partners' => 'Çözüm Ortaklarımız (Kurumsal)',
|
||
'corporate.bank-accounts' => 'Banka Bilgilerimiz (Kurumsal)',
|
||
'corporate.online-payment' => 'Online Ödeme (Kurumsal)',
|
||
'corporate.imprint' => 'Künye (Kurumsal)',
|
||
];
|
||
|
||
$fromConfig = collect(config('page_templates.labels', []))
|
||
->mapWithKeys(fn (string $label, string $key) => [$key => $label])
|
||
->all();
|
||
|
||
return array_merge($base, $fromConfig);
|
||
}
|
||
}
|