123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
-
- namespace App\Filament\Company\Clusters\Settings\Pages;
-
- use App\Enums\Setting\Font;
- use App\Enums\Setting\PrimaryColor;
- use App\Enums\Setting\RecordsPerPage;
- use App\Enums\Setting\TableSortDirection;
- use App\Filament\Company\Clusters\Settings;
- use App\Models\Setting\Appearance as AppearanceModel;
- use App\Services\CompanySettingsService;
- use Filament\Actions\Action;
- use Filament\Actions\ActionGroup;
- use Filament\Forms\Components\Component;
- use Filament\Forms\Components\Section;
- use Filament\Forms\Components\Select;
- use Filament\Forms\Form;
- use Filament\Notifications\Notification;
- use Filament\Pages\Concerns\InteractsWithFormActions;
- use Filament\Pages\Page;
- use Filament\Support\Enums\MaxWidth;
- use Filament\Support\Exceptions\Halt;
- use Illuminate\Auth\Access\AuthorizationException;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Database\Eloquent\Model;
- use Livewire\Attributes\Locked;
-
- use function Filament\authorize;
-
- /**
- * @property Form $form
- */
- class Appearance extends Page
- {
- use InteractsWithFormActions;
-
- protected static ?string $title = 'Appearance';
-
- protected static string $view = 'filament.company.pages.setting.appearance';
-
- protected static ?string $cluster = Settings::class;
-
- public ?array $data = [];
-
- #[Locked]
- public ?AppearanceModel $record = null;
-
- public function getTitle(): string | Htmlable
- {
- return translate(static::$title);
- }
-
- public function getMaxContentWidth(): MaxWidth | string | null
- {
- return MaxWidth::ScreenTwoExtraLarge;
- }
-
- public static function getNavigationLabel(): string
- {
- return translate(static::$title);
- }
-
- public function mount(): void
- {
- $this->record = AppearanceModel::firstOrNew([
- 'company_id' => auth()->user()->currentCompany->id,
- ]);
-
- abort_unless(static::canView($this->record), 404);
-
- $this->fillForm();
- }
-
- public function fillForm(): void
- {
- $data = $this->record->attributesToArray();
-
- $this->form->fill($data);
- }
-
- public function save(): void
- {
- try {
- $data = $this->form->getState();
-
- $this->handleRecordUpdate($this->record, $data);
-
- } catch (Halt $exception) {
- return;
- }
-
- $this->getSavedNotification()->send();
- }
-
- protected function getSavedNotification(): Notification
- {
- return Notification::make()
- ->success()
- ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
- }
-
- public function form(Form $form): Form
- {
- return $form
- ->schema([
- $this->getGeneralSection(),
- $this->getDataPresentationSection(),
- ])
- ->model($this->record)
- ->statePath('data')
- ->operation('edit');
- }
-
- protected function getGeneralSection(): Component
- {
- return Section::make('General')
- ->schema([
- Select::make('primary_color')
- ->allowHtml()
- ->softRequired()
- ->localizeLabel()
- ->options(
- collect(PrimaryColor::cases())
- ->sort(static fn ($a, $b) => $a->value <=> $b->value)
- ->mapWithKeys(static fn ($case) => [
- $case->value => "<span class='flex items-center gap-x-4'>
- <span class='rounded-full w-4 h-4' style='background:rgb(" . $case->getColor()[600] . ")'></span>
- <span>" . $case->getLabel() . '</span>
- </span>',
- ]),
- ),
- Select::make('font')
- ->allowHtml()
- ->softRequired()
- ->localizeLabel()
- ->options(
- collect(Font::cases())
- ->mapWithKeys(static fn ($case) => [
- $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
- ]),
- ),
- ])->columns();
- }
-
- protected function getDataPresentationSection(): Component
- {
- return Section::make('Data Presentation')
- ->schema([
- Select::make('table_sort_direction')
- ->softRequired()
- ->localizeLabel()
- ->options(TableSortDirection::class),
- Select::make('records_per_page')
- ->softRequired()
- ->localizeLabel()
- ->options(RecordsPerPage::class),
- ])->columns();
- }
-
- protected function handleRecordUpdate(AppearanceModel $record, array $data): AppearanceModel
- {
- $record->fill($data);
-
- $keysToWatch = [
- 'primary_color',
- 'font',
- ];
-
- $cachedKeysToWatch = [
- 'primary_color',
- 'font',
- 'table_sort_direction',
- 'records_per_page',
- ];
-
- if ($record->isDirty($keysToWatch)) {
- $this->dispatch('appearanceUpdated');
- }
-
- if ($record->isDirty($cachedKeysToWatch)) {
- CompanySettingsService::invalidateSettings($record->company_id);
- }
-
- $record->save();
-
- return $record;
- }
-
- /**
- * @return array<Action | ActionGroup>
- */
- protected function getFormActions(): array
- {
- return [
- $this->getSaveFormAction(),
- ];
- }
-
- protected function getSaveFormAction(): Action
- {
- return Action::make('save')
- ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
- ->submit('save')
- ->keyBindings(['mod+s']);
- }
-
- public static function canView(Model $record): bool
- {
- try {
- return authorize('update', $record)->allowed();
- } catch (AuthorizationException $exception) {
- return $exception->toResponse()->allowed();
- }
- }
- }
|