選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Localization.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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\Group;
  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\Forms\Set;
  20. use Filament\Notifications\Notification;
  21. use Filament\Pages\Concerns\InteractsWithFormActions;
  22. use Filament\Pages\Page;
  23. use Filament\Support\Exceptions\Halt;
  24. use Guava\FilamentClusters\Forms\Cluster;
  25. use Illuminate\Auth\Access\AuthorizationException;
  26. use Illuminate\Contracts\Support\Htmlable;
  27. use Illuminate\Database\Eloquent\Model;
  28. use Livewire\Attributes\Locked;
  29. use function Filament\authorize;
  30. /**
  31. * @property Form $form
  32. */
  33. class Localization extends Page
  34. {
  35. use InteractsWithFormActions;
  36. protected static ?string $title = 'Localization';
  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(CompanyProfileModel::first()->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. Select::make('number_format')
  141. ->softRequired()
  142. ->localizeLabel()
  143. ->options(NumberFormat::class),
  144. Select::make('percent_first')
  145. ->softRequired()
  146. ->localizeLabel('Percent Position')
  147. ->boolean($beforeNumber, $afterNumber, $selectPosition),
  148. Group::make()
  149. ->schema([
  150. Cluster::make([
  151. Select::make('fiscal_year_end_month')
  152. ->softRequired()
  153. ->options(array_combine(range(1, 12), array_map(static fn ($month) => now()->month($month)->monthName, range(1, 12))))
  154. ->afterStateUpdated(static fn (Set $set) => $set('fiscal_year_end_day', null))
  155. ->columnSpan(2)
  156. ->live(),
  157. Select::make('fiscal_year_end_day')
  158. ->placeholder('Day')
  159. ->softRequired()
  160. ->columnSpan(1)
  161. ->options(function (Get $get) {
  162. $month = $get('fiscal_year_end_month');
  163. $daysInMonth = now()->month($month)->daysInMonth;
  164. return array_combine(range(1, $daysInMonth), range(1, $daysInMonth));
  165. })
  166. ->live(),
  167. ])
  168. ->columns(3)
  169. ->columnSpan(2)
  170. ->required()
  171. ->markAsRequired(false)
  172. ->label('Fiscal Year End'),
  173. ])->columns(3),
  174. ])->columns();
  175. }
  176. protected function handleRecordUpdate(LocalizationModel $record, array $data): LocalizationModel
  177. {
  178. $record->fill($data);
  179. $keysToWatch = [
  180. 'language',
  181. 'timezone',
  182. 'date_format',
  183. 'week_start',
  184. 'time_format',
  185. ];
  186. if ($record->isDirty($keysToWatch)) {
  187. $this->dispatch('localizationUpdated');
  188. }
  189. $record->save();
  190. return $record;
  191. }
  192. /**
  193. * @return array<Action | ActionGroup>
  194. */
  195. protected function getFormActions(): array
  196. {
  197. return [
  198. $this->getSaveFormAction(),
  199. ];
  200. }
  201. protected function getSaveFormAction(): Action
  202. {
  203. return Action::make('save')
  204. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  205. ->submit('save')
  206. ->keyBindings(['mod+s']);
  207. }
  208. public static function canView(Model $record): bool
  209. {
  210. try {
  211. return authorize('update', $record)->allowed();
  212. } catch (AuthorizationException $exception) {
  213. return $exception->toResponse()->allowed();
  214. }
  215. }
  216. }