Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Appearance.php 7.0KB

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