Bladeren bron

update timezone handling

3.x
Andrew Wallo 1 jaar geleden
bovenliggende
commit
3e2607182b

+ 41
- 1
app/Filament/Company/Clusters/Settings/Pages/CompanyProfile.php Bestand weergeven

8
 use App\Models\Locale\Country;
8
 use App\Models\Locale\Country;
9
 use App\Models\Locale\State;
9
 use App\Models\Locale\State;
10
 use App\Models\Setting\CompanyProfile as CompanyProfileModel;
10
 use App\Models\Setting\CompanyProfile as CompanyProfileModel;
11
+use App\Utilities\Localization\Timezone;
11
 use Filament\Actions\Action;
12
 use Filament\Actions\Action;
12
 use Filament\Actions\ActionGroup;
13
 use Filament\Actions\ActionGroup;
13
 use Filament\Forms\Components\Component;
14
 use Filament\Forms\Components\Component;
90
             $data = $this->form->getState();
91
             $data = $this->form->getState();
91
 
92
 
92
             $this->handleRecordUpdate($this->record, $data);
93
             $this->handleRecordUpdate($this->record, $data);
93
-
94
         } catch (Halt $exception) {
94
         } catch (Halt $exception) {
95
             return;
95
             return;
96
         }
96
         }
97
 
97
 
98
+        $countryChanged = $this->record->wasChanged('country');
99
+        $stateChanged = $this->record->wasChanged('state_id');
100
+
98
         $this->getSavedNotification()->send();
101
         $this->getSavedNotification()->send();
102
+
103
+        if ($countryChanged || $stateChanged) {
104
+            if ($countryChanged) {
105
+                $this->updateTimezone($this->record->country);
106
+            }
107
+
108
+            $this->getTimezoneChangeNotification()->send();
109
+        }
110
+    }
111
+
112
+    protected function updateTimezone(string $countryCode): void
113
+    {
114
+        $model = \App\Models\Setting\Localization::firstOrFail();
115
+
116
+        $timezones = Timezone::getTimezonesForCountry($countryCode);
117
+
118
+        if (! empty($timezones)) {
119
+            $model->update([
120
+                'timezone' => $timezones[0],
121
+            ]);
122
+        }
123
+    }
124
+
125
+    protected function getTimezoneChangeNotification(): Notification
126
+    {
127
+        return Notification::make()
128
+            ->warning()
129
+            ->title('Timezone Update Required')
130
+            ->body('You have changed your country or state. Please update your timezone to ensure accurate date and time information.')
131
+            ->actions([
132
+                \Filament\Notifications\Actions\Action::make('updateTimezone')
133
+                    ->label('Update Timezone')
134
+                    ->url(Localization::getUrl()),
135
+            ])
136
+            ->persistent()
137
+            ->send();
99
     }
138
     }
100
 
139
 
101
     protected function getSavedNotification(): Notification
140
     protected function getSavedNotification(): Notification
176
                     ->searchable()
215
                     ->searchable()
177
                     ->live()
216
                     ->live()
178
                     ->options(static fn (Get $get) => State::getStateOptions($get('country')))
217
                     ->options(static fn (Get $get) => State::getStateOptions($get('country')))
218
+                    ->afterStateUpdated(static fn (Set $set) => $set('city_id', null))
179
                     ->nullable(),
219
                     ->nullable(),
180
                 TextInput::make('address')
220
                 TextInput::make('address')
181
                     ->localizeLabel('Street Address')
221
                     ->localizeLabel('Street Address')

+ 6
- 1
app/Utilities/Localization/Timezone.php Bestand weergeven

16
             return [];
16
             return [];
17
         }
17
         }
18
 
18
 
19
-        $countryTimezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, strtoupper($countryCode));
19
+        $countryTimezones = self::getTimezonesForCountry($countryCode);
20
 
20
 
21
         if (empty($countryTimezones)) {
21
         if (empty($countryTimezones)) {
22
             return [];
22
             return [];
55
 
55
 
56
         return now($timezone)->translatedFormat($time_format);
56
         return now($timezone)->translatedFormat($time_format);
57
     }
57
     }
58
+
59
+    public static function getTimezonesForCountry(string $countryCode): array
60
+    {
61
+        return DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, strtoupper($countryCode));
62
+    }
58
 }
63
 }

Laden…
Annuleren
Opslaan