您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UpdateProfileInformation.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. ->avatar()
  110. ->extraAttributes([
  111. 'style' => 'width: 6rem; height: 6rem;',
  112. ])
  113. ->placeholder(static function () {
  114. return new HtmlString('
  115. <div style="display: inline-block; cursor: pointer;">
  116. <div class="flex items-center justify-center bg-gray-50 dark:bg-gray-800" style="
  117. border-radius: 50%;
  118. width: 50px;
  119. height: 50px;">
  120. ' . Blade::render('<x-heroicon-o-camera class="w-8 h-8 text-gray-800 dark:text-gray-300" />') . '
  121. </div>
  122. </div>
  123. ');
  124. })
  125. ->disk(FilamentCompanies::profilePhotoDisk())
  126. ->directory(FilamentCompanies::profilePhotoStoragePath())
  127. ->saveUploadedFileUsing(function (User $record, UploadedFile $file) {
  128. $record->updateProfilePhoto($file);
  129. })
  130. ->deleteUploadedFileUsing(function (User $record) {
  131. $record->deleteProfilePhoto();
  132. })
  133. ->image()
  134. ->nullable(),
  135. Forms\Components\TextInput::make('name')
  136. ->label(__('Name'))
  137. ->required()
  138. ->maxLength(255)
  139. ->autofocus(),
  140. Forms\Components\TextInput::make('email')
  141. ->label(__('Email'))
  142. ->email()
  143. ->required()
  144. ->autocomplete('username')
  145. ->maxLength(255)
  146. ->unique(ignoreRecord: true),
  147. ])
  148. ->operation('edit')
  149. ->model($this->getUser())
  150. ->statePath('data');
  151. }
  152. public function render(): View
  153. {
  154. return view('livewire.update-profile-information');
  155. }
  156. }