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

Localization.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace App\Filament\Company\Pages\Setting;
  3. use App\Enums\DateFormat;
  4. use App\Enums\NumberFormat;
  5. use App\Enums\TimeFormat;
  6. use App\Enums\WeekStart;
  7. use App\Models\Setting\CompanyProfile as CompanyProfileModel;
  8. use App\Models\Setting\Localization as LocalizationModel;
  9. use App\Utilities\Localization\Timezone;
  10. use Filament\Actions\Action;
  11. use Filament\Actions\ActionGroup;
  12. use Filament\Facades\Filament;
  13. use Filament\Forms\Components\Component;
  14. use Filament\Forms\Components\DatePicker;
  15. use Filament\Forms\Components\Section;
  16. use Filament\Forms\Components\Select;
  17. use Filament\Forms\Form;
  18. use Filament\Forms\Get;
  19. use Filament\Notifications\Notification;
  20. use Filament\Pages\Concerns\InteractsWithFormActions;
  21. use Filament\Pages\Page;
  22. use Filament\Support\Exceptions\Halt;
  23. use Illuminate\Auth\Access\AuthorizationException;
  24. use Illuminate\Contracts\Support\Htmlable;
  25. use Illuminate\Database\Eloquent\Model;
  26. use Illuminate\Support\Str;
  27. use Livewire\Attributes\Locked;
  28. use function Filament\authorize;
  29. /**
  30. * @property Form $form
  31. */
  32. class Localization extends Page
  33. {
  34. use InteractsWithFormActions;
  35. protected static ?string $navigationIcon = 'heroicon-o-language';
  36. protected static ?string $title = 'Localization';
  37. protected static ?string $navigationGroup = 'Settings';
  38. protected static ?string $slug = 'settings/localization';
  39. protected static string $view = 'filament.company.pages.setting.localization';
  40. public ?array $data = [];
  41. #[Locked]
  42. public ?LocalizationModel $record = null;
  43. public function getTitle(): string | Htmlable
  44. {
  45. return translate(static::$title);
  46. }
  47. public static function getNavigationLabel(): string
  48. {
  49. return translate(static::$title);
  50. }
  51. public static function getNavigationParentItem(): ?string
  52. {
  53. if (Filament::hasTopNavigation()) {
  54. return translate('Company');
  55. }
  56. return null;
  57. }
  58. public function mount(): void
  59. {
  60. $this->record = LocalizationModel::firstOrNew([
  61. 'company_id' => auth()->user()->currentCompany->id,
  62. ]);
  63. abort_unless(static::canView($this->record), 404);
  64. $this->fillForm();
  65. }
  66. public function fillForm(): void
  67. {
  68. $data = $this->record->attributesToArray();
  69. $this->form->fill($data);
  70. }
  71. public function save(): void
  72. {
  73. try {
  74. $data = $this->form->getState();
  75. $this->handleRecordUpdate($this->record, $data);
  76. } catch (Halt $exception) {
  77. return;
  78. }
  79. $this->getSavedNotification()->send();
  80. }
  81. protected function getSavedNotification(): Notification
  82. {
  83. return Notification::make()
  84. ->success()
  85. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  86. }
  87. public function form(Form $form): Form
  88. {
  89. return $form
  90. ->schema([
  91. $this->getGeneralSection(),
  92. $this->getDateAndTimeSection(),
  93. $this->getFinancialAndFiscalSection(),
  94. ])
  95. ->model($this->record)
  96. ->statePath('data')
  97. ->operation('edit');
  98. }
  99. protected function getGeneralSection(): Component
  100. {
  101. return Section::make('General')
  102. ->schema([
  103. Select::make('language')
  104. ->softRequired()
  105. ->localizeLabel()
  106. ->options(LocalizationModel::getAllLanguages())
  107. ->searchable(),
  108. Select::make('timezone')
  109. ->localizeLabel()
  110. ->options(Timezone::getTimezoneOptions(CompanyProfileModel::first()->country))
  111. ->searchable()
  112. ->nullable(),
  113. ])->columns();
  114. }
  115. protected function getDateAndTimeSection(): Component
  116. {
  117. return Section::make('Date & Time')
  118. ->schema([
  119. Select::make('date_format')
  120. ->softRequired()
  121. ->localizeLabel()
  122. ->options(DateFormat::class)
  123. ->live(),
  124. Select::make('time_format')
  125. ->softRequired()
  126. ->localizeLabel()
  127. ->options(TimeFormat::class),
  128. Select::make('week_start')
  129. ->softRequired()
  130. ->localizeLabel()
  131. ->options(WeekStart::class),
  132. ])->columns();
  133. }
  134. protected function getFinancialAndFiscalSection(): Component
  135. {
  136. $beforeNumber = translate('Before Number');
  137. $afterNumber = translate('After Number');
  138. $selectPosition = translate('Select Position');
  139. return Section::make('Financial & Fiscal')
  140. ->schema([
  141. DatePicker::make('fiscal_year_start')
  142. ->localizeLabel()
  143. ->live()
  144. ->extraAttributes(['wire:key' => Str::random()]) // Required to reinitialize the datepicker when the date_format state changes
  145. ->maxDate(static fn (Get $get) => $get('fiscal_year_end'))
  146. ->displayFormat(static function (LocalizationModel $record, Get $get) {
  147. return $get('date_format') ?? DateFormat::DEFAULT;
  148. })
  149. ->seconds(false)
  150. ->softRequired(),
  151. DatePicker::make('fiscal_year_end')
  152. ->softRequired()
  153. ->localizeLabel()
  154. ->live()
  155. ->extraAttributes(['wire:key' => Str::random()]) // Required to reinitialize the datepicker when the date_format state changes
  156. ->minDate(static fn (Get $get) => $get('fiscal_year_start'))
  157. ->disabled(static fn (Get $get): bool => ! filled($get('fiscal_year_start')))
  158. ->displayFormat(static function (LocalizationModel $record, Get $get) {
  159. return $get('date_format') ?? DateFormat::DEFAULT;
  160. })
  161. ->seconds(false),
  162. Select::make('number_format')
  163. ->softRequired()
  164. ->localizeLabel()
  165. ->options(NumberFormat::class),
  166. Select::make('percent_first')
  167. ->softRequired()
  168. ->localizeLabel('Percent Position')
  169. ->boolean($beforeNumber, $afterNumber, $selectPosition),
  170. ])->columns();
  171. }
  172. protected function handleRecordUpdate(LocalizationModel $record, array $data): LocalizationModel
  173. {
  174. $record->fill($data);
  175. $keysToWatch = [
  176. 'language',
  177. 'timezone',
  178. 'date_format',
  179. 'week_start',
  180. 'time_format',
  181. ];
  182. if ($record->isDirty($keysToWatch)) {
  183. $this->dispatch('localizationUpdated');
  184. }
  185. $record->save();
  186. return $record;
  187. }
  188. /**
  189. * @return array<Action | ActionGroup>
  190. */
  191. protected function getFormActions(): array
  192. {
  193. return [
  194. $this->getSaveFormAction(),
  195. ];
  196. }
  197. protected function getSaveFormAction(): Action
  198. {
  199. return Action::make('save')
  200. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  201. ->submit('save')
  202. ->keyBindings(['mod+s']);
  203. }
  204. public static function canView(Model $record): bool
  205. {
  206. try {
  207. return authorize('update', $record)->allowed();
  208. } catch (AuthorizationException $exception) {
  209. return $exception->toResponse()->allowed();
  210. }
  211. }
  212. }