Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Appearance.php 6.9KB

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