|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backstage\Users\Pages\Auth; |
| 4 | + |
| 5 | +use Mockery\Matcher\Not; |
| 6 | +use Filament\Pages\Auth\EditProfile; |
| 7 | +use Filament\Forms\Components\Select; |
| 8 | +use Filament\Forms\Components\TextInput; |
| 9 | +use Backstage\Users\Enums\NotificationType; |
| 10 | +use Backstage\Users\Models\User; |
| 11 | +use Backstage\Users\Models\UserNotificationPreference; |
| 12 | +use Filament\Facades\Filament; |
| 13 | + |
| 14 | +class Profile extends EditProfile |
| 15 | +{ |
| 16 | + protected function getForms(): array |
| 17 | + { |
| 18 | + return [ |
| 19 | + 'form' => $this->form( |
| 20 | + $this->makeForm() |
| 21 | + ->schema([ |
| 22 | + $this->getNameFormComponent(), |
| 23 | + $this->getEmailFormComponent(), |
| 24 | + static::getNotificationFormComponent(), |
| 25 | + $this->getPasswordFormComponent(), |
| 26 | + $this->getPasswordConfirmationFormComponent(), |
| 27 | + ]) |
| 28 | + ->operation('edit') |
| 29 | + ->model($this->getUser()) |
| 30 | + ->statePath('data') |
| 31 | + ->inlineLabel(! static::isSimple()), |
| 32 | + ), |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + public static function getNotificationFormComponent(): Select |
| 37 | + { |
| 38 | + $types = NotificationType::cases(); |
| 39 | + |
| 40 | + $options = []; |
| 41 | + |
| 42 | + foreach ($types as $type) { |
| 43 | + $options[$type->value] = $type->label(); |
| 44 | + } |
| 45 | + |
| 46 | + return Select::make('notification_preferences') |
| 47 | + ->label(__('Notification preferences')) |
| 48 | + ->options(fn() => $options) |
| 49 | + ->live() |
| 50 | + ->placeholder(fn() => ('Select notification preferences')) |
| 51 | + ->searchingMessage(__('Searching notification types...')) |
| 52 | + ->searchPrompt(__('Search notification types...')) |
| 53 | + ->saveRelationshipsUsing(function (User $record, array $state) { |
| 54 | + $state = collect($state)->map(fn($value) => NotificationType::from($value)); |
| 55 | + |
| 56 | + $state->each(function (NotificationType $type) use ($record) { |
| 57 | + if (!$record->notificationPreferences->contains('navigation_type', $type->value)) { |
| 58 | + |
| 59 | + $record->notificationPreferences()->create([ |
| 60 | + 'navigation_type' => $type->value, |
| 61 | + ]); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + $record->notificationPreferences()->whereNotIn('navigation_type', $state)->delete(); |
| 66 | + }) |
| 67 | + ->multiple(); |
| 68 | + } |
| 69 | + |
| 70 | + protected function mutateFormDataBeforeFill(array $data): array |
| 71 | + { |
| 72 | + $data = parent::mutateFormDataBeforeFill($data); |
| 73 | + |
| 74 | + $user = $this->getUser(); |
| 75 | + |
| 76 | + if ($user->notificationPreferences->isNotEmpty()) { |
| 77 | + $data['notification_preferences'] = $user->notificationPreferences->pluck('navigation_type')->map(fn(NotificationType $record) => $record->value)->toArray(); |
| 78 | + } |
| 79 | + |
| 80 | + return $data; |
| 81 | + } |
| 82 | +} |
0 commit comments