Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace App\Filament\Company\Pages\Setting;
  3. use App\Enums\{Font, MaxContentWidth, ModalWidth, PrimaryColor, RecordsPerPage, TableSortDirection};
  4. use App\Models\Setting\Appearance as AppearanceModel;
  5. use Filament\Actions\{Action, ActionGroup};
  6. use Filament\Forms\Components\{Component, Section, Select};
  7. use Filament\Forms\Form;
  8. use Filament\Notifications\Notification;
  9. use Filament\Pages\Concerns\InteractsWithFormActions;
  10. use Filament\Pages\Page;
  11. use Filament\Support\Exceptions\Halt;
  12. use Illuminate\Auth\Access\AuthorizationException;
  13. use Illuminate\Database\Eloquent\Model;
  14. use Livewire\Attributes\Locked;
  15. use Wallo\FilamentSelectify\Components\{ButtonGroup, ToggleButton};
  16. use function Filament\authorize;
  17. /**
  18. * @property Form $form
  19. */
  20. class Appearance extends Page
  21. {
  22. use InteractsWithFormActions;
  23. protected static ?string $navigationIcon = 'heroicon-o-paint-brush';
  24. protected static ?string $navigationLabel = 'Appearance';
  25. protected static ?string $navigationGroup = 'Settings';
  26. protected static ?string $slug = 'settings/appearance';
  27. protected ?string $heading = 'Appearance';
  28. protected static string $view = 'filament.company.pages.setting.appearance';
  29. public ?array $data = [];
  30. #[Locked]
  31. public ?AppearanceModel $record = null;
  32. public function mount(): void
  33. {
  34. $this->record = AppearanceModel::firstOrNew([
  35. 'company_id' => auth()->user()->currentCompany->id,
  36. ]);
  37. abort_unless(static::canView($this->record), 404);
  38. $this->fillForm();
  39. }
  40. public function fillForm(): void
  41. {
  42. $data = $this->record->attributesToArray();
  43. $data = $this->mutateFormDataBeforeFill($data);
  44. $this->form->fill($data);
  45. }
  46. protected function mutateFormDataBeforeFill(array $data): array
  47. {
  48. return $data;
  49. }
  50. protected function mutateFormDataBeforeSave(array $data): array
  51. {
  52. return $data;
  53. }
  54. public function save(): void
  55. {
  56. try {
  57. $data = $this->form->getState();
  58. $data = $this->mutateFormDataBeforeSave($data);
  59. $this->handleRecordUpdate($this->record, $data);
  60. } catch (Halt $exception) {
  61. return;
  62. }
  63. $this->getSavedNotification()?->send();
  64. if ($redirectUrl = $this->getRedirectUrl()) {
  65. $this->redirect($redirectUrl);
  66. }
  67. }
  68. protected function getSavedNotification(): ?Notification
  69. {
  70. $title = $this->getSavedNotificationTitle();
  71. if (blank($title)) {
  72. return null;
  73. }
  74. return Notification::make()
  75. ->success()
  76. ->title($this->getSavedNotificationTitle());
  77. }
  78. protected function getSavedNotificationTitle(): ?string
  79. {
  80. return __('filament-panels::pages/tenancy/edit-tenant-profile.notifications.saved.title');
  81. }
  82. protected function getRedirectUrl(): ?string
  83. {
  84. return null;
  85. }
  86. public function form(Form $form): Form
  87. {
  88. return $form
  89. ->schema([
  90. $this->getGeneralSection(),
  91. $this->getLayoutSection(),
  92. $this->getDataPresentationSection(),
  93. ])
  94. ->model($this->record)
  95. ->statePath('data')
  96. ->operation('edit');
  97. }
  98. protected function getGeneralSection(): Component
  99. {
  100. return Section::make('General')
  101. ->schema([
  102. Select::make('primary_color')
  103. ->label('Primary Color')
  104. ->native(false)
  105. ->allowHtml()
  106. ->selectablePlaceholder(false)
  107. ->rule('required')
  108. ->options(
  109. collect(PrimaryColor::cases())
  110. ->mapWithKeys(static fn ($case) => [
  111. $case->value => "<span class='flex items-center gap-x-4'>
  112. <span class='rounded-full w-4 h-4' style='background:rgb(" . $case->getColor()[600] . ")'></span>
  113. <span>" . str($case->value)->title() . '</span>
  114. </span>',
  115. ]),
  116. ),
  117. Select::make('font')
  118. ->label('Font')
  119. ->native(false)
  120. ->selectablePlaceholder(false)
  121. ->rule('required')
  122. ->allowHtml()
  123. ->options(
  124. collect(Font::cases())
  125. ->mapWithKeys(static fn ($case) => [
  126. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
  127. ]),
  128. ),
  129. ])->columns();
  130. }
  131. protected function getLayoutSection(): Component
  132. {
  133. return Section::make('Layout')
  134. ->schema([
  135. Select::make('max_content_width')
  136. ->label('Max Content Width')
  137. ->native(false)
  138. ->selectablePlaceholder(false)
  139. ->rule('required')
  140. ->options(MaxContentWidth::class),
  141. Select::make('modal_width')
  142. ->label('Modal Width')
  143. ->native(false)
  144. ->selectablePlaceholder(false)
  145. ->rule('required')
  146. ->options(ModalWidth::class),
  147. ButtonGroup::make('has_top_navigation')
  148. ->label('Navigation Layout')
  149. ->boolean('Top Navigation', 'Side Navigation')
  150. ->rule('required'),
  151. ToggleButton::make('is_table_striped')
  152. ->label('Striped Tables')
  153. ->onLabel('Enabled')
  154. ->offLabel('Disabled')
  155. ->rule('required'),
  156. ])->columns();
  157. }
  158. protected function getDataPresentationSection(): Component
  159. {
  160. return Section::make('Data Presentation')
  161. ->schema([
  162. Select::make('table_sort_direction')
  163. ->label('Table Sort Direction')
  164. ->native(false)
  165. ->selectablePlaceholder(false)
  166. ->rule('required')
  167. ->options(TableSortDirection::class),
  168. Select::make('records_per_page')
  169. ->label('Records Per Page')
  170. ->native(false)
  171. ->selectablePlaceholder(false)
  172. ->rule('required')
  173. ->options(RecordsPerPage::class),
  174. ])->columns();
  175. }
  176. protected function handleRecordUpdate(AppearanceModel $record, array $data): AppearanceModel
  177. {
  178. $record_array = array_map('strval', $record->toArray());
  179. $data_array = array_map('strval', $data);
  180. $diff = array_diff_assoc($data_array, $record_array);
  181. $keysToWatch = [
  182. 'primary_color',
  183. 'max_content_width',
  184. 'has_top_navigation',
  185. 'font',
  186. ];
  187. foreach ($diff as $key => $value) {
  188. if (in_array($key, $keysToWatch, true)) {
  189. $this->dispatch('appearanceUpdated');
  190. }
  191. }
  192. // If the primary color or font has changed, we need to update the associated models accent_color column.
  193. if (array_key_exists('primary_color', $diff) || array_key_exists('font', $diff)) {
  194. $primaryColorToHex = PrimaryColor::from($data['primary_color'])->getHexCode();
  195. $font = Font::from($data['font'])->value;
  196. $this->record->company->defaultBill()->update([
  197. 'accent_color' => $primaryColorToHex,
  198. 'font' => $font,
  199. ]);
  200. $this->record->company->defaultInvoice()->update([
  201. 'accent_color' => $primaryColorToHex,
  202. 'font' => $font,
  203. ]);
  204. }
  205. $record->update($data);
  206. return $record;
  207. }
  208. /**
  209. * @return array<Action | ActionGroup>
  210. */
  211. protected function getFormActions(): array
  212. {
  213. return [
  214. $this->getSaveFormAction(),
  215. ];
  216. }
  217. protected function getSaveFormAction(): Action
  218. {
  219. return Action::make('save')
  220. ->label(__('filament-panels::pages/tenancy/edit-tenant-profile.form.actions.save.label'))
  221. ->submit('save')
  222. ->keyBindings(['mod+s']);
  223. }
  224. public static function canView(Model $record): bool
  225. {
  226. try {
  227. return authorize('update', $record)->allowed();
  228. } catch (AuthorizationException $exception) {
  229. return $exception->toResponse()->allowed();
  230. }
  231. }
  232. }