Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Appearance.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Pages;
  3. use App\Enums\Setting\Font;
  4. use App\Enums\Setting\PrimaryColor;
  5. use App\Filament\Company\Clusters\Settings;
  6. use App\Models\Setting\Appearance as AppearanceModel;
  7. use App\Services\CompanySettingsService;
  8. use Filament\Actions\Action;
  9. use Filament\Actions\ActionGroup;
  10. use Filament\Forms\Components\Component;
  11. use Filament\Forms\Components\Section;
  12. use Filament\Forms\Components\Select;
  13. use Filament\Forms\Form;
  14. use Filament\Notifications\Notification;
  15. use Filament\Pages\Concerns\InteractsWithFormActions;
  16. use Filament\Pages\Page;
  17. use Filament\Support\Enums\MaxWidth;
  18. use Filament\Support\Exceptions\Halt;
  19. use Illuminate\Auth\Access\AuthorizationException;
  20. use Illuminate\Contracts\Support\Htmlable;
  21. use Illuminate\Database\Eloquent\Model;
  22. use Livewire\Attributes\Locked;
  23. use function Filament\authorize;
  24. /**
  25. * @property Form $form
  26. */
  27. class Appearance extends Page
  28. {
  29. use InteractsWithFormActions;
  30. protected static ?string $title = 'Appearance';
  31. protected static string $view = 'filament.company.pages.setting.appearance';
  32. protected static ?string $cluster = Settings::class;
  33. public ?array $data = [];
  34. #[Locked]
  35. public ?AppearanceModel $record = null;
  36. public function getTitle(): string | Htmlable
  37. {
  38. return translate(static::$title);
  39. }
  40. public function getMaxContentWidth(): MaxWidth | string | null
  41. {
  42. return MaxWidth::ScreenTwoExtraLarge;
  43. }
  44. public static function getNavigationLabel(): string
  45. {
  46. return translate(static::$title);
  47. }
  48. public function mount(): void
  49. {
  50. $this->record = AppearanceModel::firstOrNew([
  51. 'company_id' => auth()->user()->currentCompany->id,
  52. ]);
  53. abort_unless(static::canView($this->record), 404);
  54. $this->fillForm();
  55. }
  56. public function fillForm(): void
  57. {
  58. $data = $this->record->attributesToArray();
  59. $this->form->fill($data);
  60. }
  61. public function save(): void
  62. {
  63. try {
  64. $data = $this->form->getState();
  65. $this->handleRecordUpdate($this->record, $data);
  66. } catch (Halt $exception) {
  67. return;
  68. }
  69. $this->getSavedNotification()->send();
  70. }
  71. protected function getSavedNotification(): Notification
  72. {
  73. return Notification::make()
  74. ->success()
  75. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  76. }
  77. public function form(Form $form): Form
  78. {
  79. return $form
  80. ->schema([
  81. $this->getGeneralSection(),
  82. ])
  83. ->model($this->record)
  84. ->statePath('data')
  85. ->operation('edit');
  86. }
  87. protected function getGeneralSection(): Component
  88. {
  89. return Section::make('General')
  90. ->schema([
  91. Select::make('primary_color')
  92. ->allowHtml()
  93. ->softRequired()
  94. ->localizeLabel()
  95. ->options(
  96. collect(PrimaryColor::cases())
  97. ->sort(static fn ($a, $b) => $a->value <=> $b->value)
  98. ->mapWithKeys(static fn ($case) => [
  99. $case->value => "<span class='flex items-center gap-x-4'>
  100. <span class='rounded-full w-4 h-4' style='background:rgb(" . $case->getColor()[600] . ")'></span>
  101. <span>" . $case->getLabel() . '</span>
  102. </span>',
  103. ]),
  104. ),
  105. Select::make('font')
  106. ->allowHtml()
  107. ->softRequired()
  108. ->localizeLabel()
  109. ->options(
  110. collect(Font::cases())
  111. ->mapWithKeys(static fn ($case) => [
  112. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
  113. ]),
  114. ),
  115. ])->columns();
  116. }
  117. protected function handleRecordUpdate(AppearanceModel $record, array $data): AppearanceModel
  118. {
  119. $record->fill($data);
  120. $keysToWatch = [
  121. 'primary_color',
  122. 'font',
  123. ];
  124. if ($record->isDirty($keysToWatch)) {
  125. CompanySettingsService::invalidateSettings($record->company_id);
  126. $this->dispatch('appearanceUpdated');
  127. }
  128. $record->save();
  129. return $record;
  130. }
  131. /**
  132. * @return array<Action | ActionGroup>
  133. */
  134. protected function getFormActions(): array
  135. {
  136. return [
  137. $this->getSaveFormAction(),
  138. ];
  139. }
  140. protected function getSaveFormAction(): Action
  141. {
  142. return Action::make('save')
  143. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  144. ->submit('save')
  145. ->keyBindings(['mod+s']);
  146. }
  147. public static function canView(Model $record): bool
  148. {
  149. try {
  150. return authorize('update', $record)->allowed();
  151. } catch (AuthorizationException $exception) {
  152. return $exception->toResponse()->allowed();
  153. }
  154. }
  155. }