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

UpdateProfileInformation.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Livewire;
  3. use App\Models\User;
  4. use Filament\Facades\Filament;
  5. use Filament\Forms;
  6. use Filament\Forms\Concerns\InteractsWithForms;
  7. use Filament\Forms\Contracts\HasForms;
  8. use Filament\Forms\Form;
  9. use Filament\Notifications\Notification;
  10. use Filament\Support\Exceptions\Halt;
  11. use Illuminate\Contracts\Auth\Authenticatable;
  12. use Illuminate\Contracts\View\View;
  13. use Illuminate\Database\Eloquent\Model;
  14. use Illuminate\Http\UploadedFile;
  15. use Illuminate\Support\Facades\Blade;
  16. use Illuminate\Support\HtmlString;
  17. use Livewire\Component;
  18. use RuntimeException;
  19. use Wallo\FilamentCompanies\FilamentCompanies;
  20. /**
  21. * @property Form $form
  22. */
  23. class UpdateProfileInformation extends Component implements HasForms
  24. {
  25. use InteractsWithForms;
  26. /**
  27. * @var array<string, mixed> | null
  28. */
  29. public ?array $data = [];
  30. public function mount(): void
  31. {
  32. $this->fillForm();
  33. }
  34. public function getUser(): Authenticatable | Model
  35. {
  36. $user = Filament::auth()->user();
  37. if (! $user instanceof Model) {
  38. throw new RuntimeException('The authenticated user object must be an Eloquent model to allow profile information to be updated.');
  39. }
  40. return $user;
  41. }
  42. public function fillForm(): void
  43. {
  44. $data = $this->getUser()->withoutRelations()->toArray();
  45. $data = $this->mutateFormDataBeforeFill($data);
  46. $this->form->fill($data);
  47. }
  48. /**
  49. * @param array<string, mixed> $data
  50. * @return array<string, mixed>
  51. */
  52. protected function mutateFormDataBeforeFill(array $data): array
  53. {
  54. return $data;
  55. }
  56. /**
  57. * @param array<string, mixed> $data
  58. * @return array<string, mixed>
  59. */
  60. protected function mutateFormDataBeforeSave(array $data): array
  61. {
  62. return $data;
  63. }
  64. public function save(): void
  65. {
  66. try {
  67. $data = $this->form->getState();
  68. $data = $this->mutateFormDataBeforeSave($data);
  69. $this->handleRecordUpdate($this->getUser(), $data);
  70. } catch (Halt $exception) {
  71. return;
  72. }
  73. $this->getSavedNotification()?->send();
  74. $this->fillForm();
  75. }
  76. /**
  77. * @param array<string, mixed> $data
  78. */
  79. protected function handleRecordUpdate(Model $record, array $data): Model
  80. {
  81. $record->update($data);
  82. return $record;
  83. }
  84. protected function getSavedNotification(): ?Notification
  85. {
  86. $title = $this->getSavedNotificationTitle();
  87. if (blank($title)) {
  88. return null;
  89. }
  90. return Notification::make()
  91. ->success()
  92. ->title($this->getSavedNotificationTitle())
  93. ->body($this->getSavedNotificationBody());
  94. }
  95. protected function getSavedNotificationTitle(): ?string
  96. {
  97. return __('filament-companies::default.notifications.profile_information_updated.title');
  98. }
  99. protected function getSavedNotificationBody(): ?string
  100. {
  101. return __('filament-companies::default.notifications.profile_information_updated.body');
  102. }
  103. public function form(Form $form): Form
  104. {
  105. return $form
  106. ->schema([
  107. Forms\Components\FileUpload::make('profile_photo_path')
  108. ->label('Photo')
  109. ->extraAttributes([
  110. 'style' => 'width: 6rem; height: 6rem;',
  111. ])
  112. ->panelLayout('compact circle')
  113. ->removeUploadedFileButtonPosition('center bottom')
  114. ->uploadButtonPosition('center bottom')
  115. ->imageResizeMode('cover')
  116. ->imageResizeUpscale(false)
  117. ->imageResizeTargetHeight('500')
  118. ->imageResizeTargetWidth('500')
  119. ->imageCropAspectRatio('1:1')
  120. ->uploadProgressIndicatorPosition('center bottom')
  121. ->loadingIndicatorPosition('center bottom')
  122. ->placeholder(static function () {
  123. return new HtmlString('
  124. <div style="display: inline-block; cursor: pointer;">
  125. <div class="flex items-center justify-center bg-gray-50 dark:bg-gray-800" style="
  126. border-radius: 50%;
  127. width: 50px;
  128. height: 50px;">
  129. ' . Blade::render('<x-heroicon-o-camera class="w-8 h-8 text-gray-800 dark:text-gray-300" />') . '
  130. </div>
  131. </div>
  132. ');
  133. })
  134. ->disk(FilamentCompanies::profilePhotoDisk())
  135. ->directory(FilamentCompanies::profilePhotoStoragePath())
  136. ->saveUploadedFileUsing(function (User $record, UploadedFile $file) {
  137. $record->updateProfilePhoto($file);
  138. })
  139. ->deleteUploadedFileUsing(function (User $record) {
  140. $record->deleteProfilePhoto();
  141. })
  142. ->image()
  143. ->nullable(),
  144. Forms\Components\TextInput::make('name')
  145. ->label(__('Name'))
  146. ->required()
  147. ->maxLength(255)
  148. ->autofocus(),
  149. Forms\Components\TextInput::make('email')
  150. ->label(__('Email'))
  151. ->email()
  152. ->required()
  153. ->autocomplete('username')
  154. ->maxLength(255)
  155. ->unique(ignoreRecord: true),
  156. ])
  157. ->operation('edit')
  158. ->model($this->getUser())
  159. ->statePath('data');
  160. }
  161. public function render(): View
  162. {
  163. return view('livewire.update-profile-information');
  164. }
  165. }