Faza 7: White-label per tenant — logo + theme color dinamic

- spatie/laravel-medialibrary instalat (migration media table)
- filament/spatie-laravel-media-library-plugin
- Company implements HasMedia + InteractsWithMedia
  - collections: 'logo' + 'favicon' (singleFile)
  - getLogoUrl() / getFaviconUrl() helpers
- Settings page extins: secțiune Logo & favicon cu FileUpload
  - On save: clear collection + addMedia from temp upload + cleanup tmp file
- TenantPanelProvider render hooks:
  - HEAD_END: theme-color meta + favicon + CSS vars override
    (--primary-50 → --primary-950 generate din hex theme_color)
  - SIDEBAR_LOGO_BEFORE: afișare logo upload-uit, max-height 56px

Cum funcționează:
- Tenant uploadează logo în Settings
- La fiecare request, render hook injectează <style> cu CSS vars custom
- Filament respectă --primary-* → toate butoanele/badge-urile primesc culoarea brand
- Logo apare deasupra meniului (sidebar)
This commit is contained in:
2026-05-07 12:51:19 +00:00
parent a7bb9838f4
commit f1d196f018
6 changed files with 623 additions and 6 deletions
+33
View File
@@ -85,6 +85,27 @@ class Settings extends Page
->label('Mărci auto suportate (separate prin virgulă)')
->rows(2),
]),
Schemas\Components\Section::make('Logo & favicon')
->columns(2)
->schema([
Forms\Components\FileUpload::make('logo')
->label('Logo')
->image()
->imageEditor()
->disk('public')
->directory('tmp-uploads')
->visibility('public')
->maxSize(2048)
->helperText('PNG/SVG, max 2 MB. Apare în sidebar.'),
Forms\Components\FileUpload::make('favicon')
->label('Favicon')
->image()
->disk('public')
->directory('tmp-uploads')
->visibility('public')
->maxSize(512)
->helperText('PNG/ICO, max 512 KB.'),
]),
])
->statePath('data');
}
@@ -113,6 +134,18 @@ class Settings extends Page
]),
]);
// Logo + favicon → Spatie Media Library
foreach (['logo', 'favicon'] as $col) {
$path = $data[$col] ?? null;
if (! $path) continue;
$abs = \Illuminate\Support\Facades\Storage::disk('public')->path($path);
if (file_exists($abs)) {
$company->clearMediaCollection($col);
$company->addMedia($abs)->preservingOriginal()->toMediaCollection($col);
@unlink($abs);
}
}
Notification::make()->title('Setări salvate')->success()->send();
}
}
+23 -5
View File
@@ -4,10 +4,10 @@ namespace App\Models\Central;
use App\Models\Tenant\User;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains;
/**
* Tenant model extends Stancl Tenant for compatibility with the package
@@ -16,9 +16,9 @@ use Stancl\Tenancy\Database\Concerns\HasDomains;
* In single-DB mode we don't use HasDatabase. Domain identification is
* handled by our own ResolveTenant middleware (slug-based, not DNS records).
*/
class Company extends BaseTenant
class Company extends BaseTenant implements HasMedia
{
use SoftDeletes;
use InteractsWithMedia, SoftDeletes;
protected $table = 'companies';
@@ -78,4 +78,22 @@ class Company extends BaseTenant
$central = config('app.central_domain') ?: 'service.mir.md';
return "https://{$this->slug}.{$central}{$path}";
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('logo')->singleFile();
$this->addMediaCollection('favicon')->singleFile();
}
public function getLogoUrl(): ?string
{
$m = $this->getFirstMedia('logo');
return $m ? $m->getUrl() : null;
}
public function getFaviconUrl(): ?string
{
$m = $this->getFirstMedia('favicon');
return $m ? $m->getUrl() : null;
}
}
@@ -79,12 +79,53 @@ class TenantPanelProvider extends PanelProvider
$t = app(\App\Tenancy\TenantManager::class)->current();
$themeColor = $t?->settings['theme_color'] ?? '#3B82F6';
$name = $t?->display_name ?? $t?->name ?? 'AutoCRM';
$favicon = $t?->getFaviconUrl();
// Generate primary color shades from theme_color hex.
$hex = ltrim($themeColor, '#');
if (strlen($hex) === 6) {
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
} else { $r = 59; $g = 130; $b = 246; }
@endphp
<link rel="manifest" href="/manifest.json">
<meta name="theme-color" content="{{ $themeColor }}">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<meta name="apple-mobile-web-app-title" content="{{ $name }}">
@if ($favicon)
<link rel="icon" type="image/png" href="{{ $favicon }}">
<link rel="apple-touch-icon" href="{{ $favicon }}">
@endif
<style>
:root {
--primary-50: {{ "rgb({$r} {$g} {$b} / 0.05)" }};
--primary-100: {{ "rgb({$r} {$g} {$b} / 0.10)" }};
--primary-200: {{ "rgb({$r} {$g} {$b} / 0.20)" }};
--primary-300: {{ "rgb({$r} {$g} {$b} / 0.35)" }};
--primary-400: {{ "rgb({$r} {$g} {$b} / 0.55)" }};
--primary-500: {{ "rgb({$r} {$g} {$b})" }};
--primary-600: {{ "rgb({$r} {$g} {$b})" }};
--primary-700: {{ "rgb(" . max(0,$r-20) . " " . max(0,$g-20) . " " . max(0,$b-20) . ")" }};
--primary-800: {{ "rgb(" . max(0,$r-40) . " " . max(0,$g-40) . " " . max(0,$b-40) . ")" }};
--primary-900: {{ "rgb(" . max(0,$r-60) . " " . max(0,$g-60) . " " . max(0,$b-60) . ")" }};
--primary-950: {{ "rgb(" . max(0,$r-80) . " " . max(0,$g-80) . " " . max(0,$b-80) . ")" }};
}
</style>
BLADE)
)
->renderHook(
PanelsRenderHook::SIDEBAR_LOGO_BEFORE,
fn (): string => Blade::render(<<<'BLADE'
@php
$t = app(\App\Tenancy\TenantManager::class)->current();
$logo = $t?->getLogoUrl();
@endphp
@if ($logo)
<div style="padding: 12px 16px; display: flex; justify-content: center; border-bottom: 1px solid rgba(0,0,0,.06);">
<img src="{{ $logo }}" alt="logo" style="max-height: 56px; max-width: 100%;">
</div>
@endif
BLADE)
)
->renderHook(