55namespace Moox \Localization \Filament \Resources ;
66
77use Filament \Forms \Components \Select ;
8- use Filament \Forms \Components \Textarea ;
98use Filament \Forms \Components \TextInput ;
109use Filament \Forms \Components \Toggle ;
1110use Filament \Schemas \Components \Grid ;
1211use Filament \Schemas \Components \Section ;
12+ use Filament \Schemas \Components \Utilities \Get ;
1313use Filament \Schemas \Components \Utilities \Set ;
1414use Filament \Schemas \Schema ;
1515use Filament \Tables \Columns \IconColumn ;
@@ -142,7 +142,6 @@ public static function form(Schema $schema): Schema
142142 ->label (__ ('localization::fields.is_activ_admin ' ))
143143 ->default (true )
144144 ->disabled (function ($ get , $ livewire ) {
145- // Disabled when this localization is set as default
146145 $ record = $ livewire ->record ;
147146 $ isDefault = $ get ('is_default ' ) ?? ($ record !== null ? $ record ->is_default : false );
148147
@@ -151,47 +150,59 @@ public static function form(Schema $schema): Schema
151150 Toggle::make ('is_active_frontend ' )
152151 ->label (__ ('localization::fields.is_activ_frontend ' ))
153152 ->default (false ),
154- Toggle::make ('use_country_icon ' )
155- ->label (__ ('localization::fields.use_country_icon ' ))
156- ->default (false )
157- ->helperText (__ ('localization::fields.use_country_icon_help ' )),
158153 Toggle::make ('is_default ' )
159154 ->label (__ ('localization::fields.is_default ' ))
160155 ->default (false )
161156 ->disabled (function ($ get , $ livewire ) {
162- // Disabled when English is selected as default
163157 $ record = $ livewire ->record ;
164158 $ localeVariant = $ get ('locale_variant ' ) ?? ($ record !== null ? $ record ->locale_variant : '' );
165159 $ isDefault = $ get ('is_default ' ) ?? ($ record !== null ? $ record ->is_default : false );
166160
167- // If it is an English localization AND already set as default
168161 if (strpos ($ localeVariant , 'en_ ' ) === 0 && $ isDefault ) {
169162 return true ;
170163 }
171164
172165 return false ;
173166 })
174- ->afterStateUpdated (function ($ state , $ set , $ get , $ livewire ) {
167+ ->afterStateUpdated (function ($ state , $ set , $ livewire ) {
175168 if ($ state ) {
176- // When activated as default, automatically set is_active_admin to true
177169 $ set ('is_active_admin ' , true );
178170
179171 $ currentRecordId = $ livewire ->record ?->id;
180- $ languageId = $ get ('language_id ' );
181172
182- Localization::where ('language_id ' , $ languageId )
183- ->when ($ currentRecordId , function ($ query ) use ($ currentRecordId ) {
184- $ query ->where ('id ' , '!= ' , $ currentRecordId );
185- })
173+ Localization::query ()
174+ ->when ($ currentRecordId , fn ($ query ) => $ query ->where ('id ' , '!= ' , $ currentRecordId ))
186175 ->update (['is_default ' => false ]);
187176 } else {
188- // When default is deactivated, always set en_US as default
189177 $ enUsLocale = Localization::where ('locale_variant ' , 'en_US ' )->first ();
190- if ($ enUsLocale ) {
178+ if ($ enUsLocale && $ enUsLocale -> id !== $ livewire -> record ?->id ) {
191179 $ enUsLocale ->update (['is_default ' => true , 'is_active_admin ' => true ]);
192180 }
193181 }
194182 }),
183+ Toggle::make ('use_native_names ' )
184+ ->label (__ ('localization::fields.use_native_names ' ))
185+ ->default (true ),
186+ Toggle::make ('show_regional_variants ' )
187+ ->label (__ ('localization::fields.show_regional_variants ' ))
188+ ->default (true )
189+ ->live ()
190+ ->afterStateUpdated (function (bool $ state , Set $ set ): void {
191+ if (! $ state ) {
192+ $ set ('use_country_translations ' , false );
193+ }
194+ }),
195+ Toggle::make ('use_country_translations ' )
196+ ->label (__ ('localization::fields.use_country_translations ' ))
197+ ->default (true )
198+ ->disabled (fn (Get $ get ): bool => ! $ get ('show_regional_variants ' ))
199+ ->helperText (fn (Get $ get ): ?string => $ get ('show_regional_variants ' )
200+ ? null
201+ : __ ('localization::fields.country_names_requires_regional ' )),
202+ Toggle::make ('use_country_icon ' )
203+ ->label (__ ('localization::fields.use_country_icon ' ))
204+ ->default (false )
205+ ->helperText (__ ('localization::fields.use_country_icon_help ' )),
195206 TextInput::make ('routing_path ' )
196207 ->label (__ ('localization::fields.routing_path ' ))
197208 ->nullable (),
@@ -205,9 +216,6 @@ public static function form(Schema $schema): Schema
205216 ->label (__ ('localization::fields.translation_status ' ))
206217 ->numeric ()
207218 ->nullable (),
208- Textarea::make ('language_settings ' )
209- ->label (__ ('localization::fields.language_settings ' ))
210- ->json (),
211219 ])
212220 ->columnSpan (2 ),
213221 Grid::make ()
@@ -301,25 +309,24 @@ public static function table(Table $table): Table
301309 }
302310 }),
303311 // Display name & flag toggles (order: Native → Regional → Country names → Country flag)
304- ToggleColumn::make ('use_native_names_setting ' )
312+ ToggleColumn::make ('use_native_names ' )
305313 ->label (__ ('localization::fields.native ' ))
306- ->width (80 )
307- ->getStateUsing (fn (Localization $ record ): bool => $ record ->getLanguageSetting ('use_native_names ' ))
308- ->updateStateUsing (fn (Localization $ record , bool $ state ): bool => static ::persistLanguageSetting ($ record , 'use_native_names ' , $ state )),
309- ToggleColumn::make ('show_regional_variants_setting ' )
314+ ->width (80 ),
315+ ToggleColumn::make ('show_regional_variants ' )
310316 ->label (__ ('localization::fields.regional ' ))
311317 ->width (80 )
312- ->getStateUsing (fn (Localization $ record ): bool => $ record ->getLanguageSetting ('show_regional_variants ' ))
313- ->updateStateUsing (fn (Localization $ record , bool $ state ): bool => static ::persistLanguageSetting ($ record , 'show_regional_variants ' , $ state )),
314- ToggleColumn::make ('use_country_translations_setting ' )
318+ ->afterStateUpdated (function (bool $ state , Localization $ record ): void {
319+ if (! $ state ) {
320+ $ record ->update (['use_country_translations ' => false ]);
321+ }
322+ }),
323+ ToggleColumn::make ('use_country_translations ' )
315324 ->label (__ ('localization::fields.country_names ' ))
316325 ->width (95 )
317- ->tooltip (fn (Localization $ record ): ?string => $ record ->getLanguageSetting ( ' show_regional_variants ' )
326+ ->tooltip (fn (Localization $ record ): ?string => $ record ->show_regional_variants
318327 ? null
319328 : __ ('localization::fields.country_names_requires_regional ' ))
320- ->disabled (fn (Localization $ record ): bool => ! $ record ->getLanguageSetting ('show_regional_variants ' ))
321- ->getStateUsing (fn (Localization $ record ): bool => $ record ->getLanguageSetting ('use_country_translations ' ))
322- ->updateStateUsing (fn (Localization $ record , bool $ state ): bool => static ::persistLanguageSetting ($ record , 'use_country_translations ' , $ state )),
329+ ->disabled (fn (Localization $ record ): bool => ! $ record ->show_regional_variants ),
323330 ToggleColumn::make ('use_country_icon ' )
324331 ->label (__ ('localization::fields.country_flag ' ))
325332 ->width (95 ),
@@ -352,17 +359,4 @@ public static function getPages(): array
352359 ];
353360 }
354361
355- protected static function persistLanguageSetting (Localization $ record , string $ key , bool $ state ): bool
356- {
357- $ settings = $ record ->language_settings ?? [];
358- $ settings [$ key ] = $ state ;
359-
360- if ($ key === 'show_regional_variants ' && ! $ state ) {
361- $ settings ['use_country_translations ' ] = false ;
362- }
363-
364- $ record ->update (['language_settings ' => $ settings ]);
365-
366- return $ state ;
367- }
368362}
0 commit comments