Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Localization.php 7.4KB

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