You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Appearance.php 6.1KB

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