소스 검색

update timezone handling

3.x
Andrew Wallo 1 년 전
부모
커밋
3e2607182b
2개의 변경된 파일47개의 추가작업 그리고 2개의 파일을 삭제
  1. 41
    1
      app/Filament/Company/Clusters/Settings/Pages/CompanyProfile.php
  2. 6
    1
      app/Utilities/Localization/Timezone.php

+ 41
- 1
app/Filament/Company/Clusters/Settings/Pages/CompanyProfile.php 파일 보기

@@ -8,6 +8,7 @@ use App\Models\Locale\City;
8 8
 use App\Models\Locale\Country;
9 9
 use App\Models\Locale\State;
10 10
 use App\Models\Setting\CompanyProfile as CompanyProfileModel;
11
+use App\Utilities\Localization\Timezone;
11 12
 use Filament\Actions\Action;
12 13
 use Filament\Actions\ActionGroup;
13 14
 use Filament\Forms\Components\Component;
@@ -90,12 +91,50 @@ class CompanyProfile extends Page
90 91
             $data = $this->form->getState();
91 92
 
92 93
             $this->handleRecordUpdate($this->record, $data);
93
-
94 94
         } catch (Halt $exception) {
95 95
             return;
96 96
         }
97 97
 
98
+        $countryChanged = $this->record->wasChanged('country');
99
+        $stateChanged = $this->record->wasChanged('state_id');
100
+
98 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 140
     protected function getSavedNotification(): Notification
@@ -176,6 +215,7 @@ class CompanyProfile extends Page
176 215
                     ->searchable()
177 216
                     ->live()
178 217
                     ->options(static fn (Get $get) => State::getStateOptions($get('country')))
218
+                    ->afterStateUpdated(static fn (Set $set) => $set('city_id', null))
179 219
                     ->nullable(),
180 220
                 TextInput::make('address')
181 221
                     ->localizeLabel('Street Address')

+ 6
- 1
app/Utilities/Localization/Timezone.php 파일 보기

@@ -16,7 +16,7 @@ class Timezone
16 16
             return [];
17 17
         }
18 18
 
19
-        $countryTimezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, strtoupper($countryCode));
19
+        $countryTimezones = self::getTimezonesForCountry($countryCode);
20 20
 
21 21
         if (empty($countryTimezones)) {
22 22
             return [];
@@ -55,4 +55,9 @@ class Timezone
55 55
 
56 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
 }

Loading…
취소
저장