Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Localization.php 7.6KB

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