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.9KB

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