Files
autocrm/app/Filament/Tenant/Pages/Settings.php
T
Vasyka f1d196f018 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)
2026-05-07 12:51:19 +00:00

152 lines
6.4 KiB
PHP

<?php
namespace App\Filament\Tenant\Pages;
use App\Tenancy\TenantManager;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas;
use Filament\Schemas\Schema;
class Settings extends Page
{
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-cog-6-tooth';
protected static ?string $navigationLabel = 'Setări';
protected static string|\UnitEnum|null $navigationGroup = 'Admin';
protected static ?int $navigationSort = 90;
protected static ?string $title = 'Setări companie';
protected string $view = 'filament.tenant.pages.settings';
public ?array $data = [];
public function mount(): void
{
$company = app(TenantManager::class)->current();
if (! $company) {
return;
}
$settings = (array) ($company->settings ?? []);
// Filament v5: fill via $this->form->fill() (initializes the schema state).
$this->form->fill([
'display_name' => $company->display_name ?? $company->name,
'city' => $company->city,
'phone' => $company->phone,
'email' => $company->email,
'currency' => $settings['currency'] ?? 'MDL',
'language' => $settings['language'] ?? 'ro',
'theme_color' => $settings['theme_color'] ?? '#3B82F6',
'labor_rate' => $settings['labor_rate'] ?? 400,
'services' => isset($settings['services']) ? implode(', ', (array) $settings['services']) : '',
'cars' => isset($settings['cars']) ? implode(', ', (array) $settings['cars']) : '',
]);
}
public function form(Schema $schema): Schema
{
return $schema
->components([
Schemas\Components\Section::make('Brand & contact')
->columns(2)
->schema([
Forms\Components\TextInput::make('display_name')->label('Denumire afișată')->maxLength(120),
Forms\Components\TextInput::make('city')->label('Oraș')->maxLength(60),
Forms\Components\TextInput::make('phone')->label('Telefon')->tel()->maxLength(40),
Forms\Components\TextInput::make('email')->email()->maxLength(120),
]),
Schemas\Components\Section::make('Localizare & monedă')
->columns(3)
->schema([
Forms\Components\Select::make('language')
->label('Limbă default')
->options(['ro' => 'Română', 'ru' => 'Русский', 'en' => 'English'])
->required(),
Forms\Components\TextInput::make('currency')->label('Monedă')->maxLength(8)->required(),
Forms\Components\ColorPicker::make('theme_color')->label('Culoare brand'),
]),
Schemas\Components\Section::make('Servicii & tarif')
->columns(2)
->schema([
Forms\Components\TextInput::make('labor_rate')->label('Tarif normo-oră')->numeric()->required(),
]),
Schemas\Components\Section::make('Liste configurabile')
->columns(1)
->schema([
Forms\Components\Textarea::make('services')
->label('Servicii oferite (separate prin virgulă)')
->rows(2),
Forms\Components\Textarea::make('cars')
->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');
}
public function save(): void
{
$data = $this->form->getState();
$company = app(TenantManager::class)->current();
if (! $company) {
Notification::make()->title('Tenant not resolved')->danger()->send();
return;
}
$company->update([
'display_name' => $data['display_name'] ?? null,
'city' => $data['city'] ?? null,
'phone' => $data['phone'] ?? null,
'email' => $data['email'] ?? null,
'settings' => array_merge((array) $company->settings, [
'language' => $data['language'] ?? 'ro',
'currency' => $data['currency'] ?? 'MDL',
'theme_color' => $data['theme_color'] ?? '#3B82F6',
'labor_rate' => (float) ($data['labor_rate'] ?? 400),
'services' => array_values(array_filter(array_map('trim', explode(',', (string) ($data['services'] ?? ''))))),
'cars' => array_values(array_filter(array_map('trim', explode(',', (string) ($data['cars'] ?? ''))))),
]),
]);
// 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();
}
}