You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Localization.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Pages;
  3. use App\Enums\DateFormat;
  4. use App\Enums\NumberFormat;
  5. use App\Enums\TimeFormat;
  6. use App\Enums\WeekStart;
  7. use App\Filament\Company\Clusters\Settings;
  8. use App\Models\Setting\CompanyProfile as CompanyProfileModel;
  9. use App\Models\Setting\Localization as LocalizationModel;
  10. use App\Utilities\Localization\Timezone;
  11. use Filament\Actions\Action;
  12. use Filament\Actions\ActionGroup;
  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\Enums\MaxWidth;
  24. use Filament\Support\Exceptions\Halt;
  25. use Guava\FilamentClusters\Forms\Cluster;
  26. use Illuminate\Auth\Access\AuthorizationException;
  27. use Illuminate\Contracts\Support\Htmlable;
  28. use Illuminate\Database\Eloquent\Model;
  29. use Livewire\Attributes\Locked;
  30. use function Filament\authorize;
  31. /**
  32. * @property Form $form
  33. */
  34. class Localization extends Page
  35. {
  36. use InteractsWithFormActions;
  37. protected static ?string $title = 'Localization';
  38. protected static string $view = 'filament.company.pages.setting.localization';
  39. protected static ?string $cluster = Settings::class;
  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 function getMaxContentWidth(): MaxWidth
  52. {
  53. return MaxWidth::ScreenTwoExtraLarge;
  54. }
  55. public function mount(): void
  56. {
  57. $this->record = LocalizationModel::firstOrNew([
  58. 'company_id' => auth()->user()->currentCompany->id,
  59. ]);
  60. abort_unless(static::canView($this->record), 404);
  61. $this->fillForm();
  62. }
  63. public function fillForm(): void
  64. {
  65. $data = $this->record->attributesToArray();
  66. $this->form->fill($data);
  67. }
  68. public function save(): void
  69. {
  70. try {
  71. $data = $this->form->getState();
  72. $this->handleRecordUpdate($this->record, $data);
  73. } catch (Halt $exception) {
  74. return;
  75. }
  76. $this->getSavedNotification()->send();
  77. }
  78. protected function getSavedNotification(): Notification
  79. {
  80. return Notification::make()
  81. ->success()
  82. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  83. }
  84. public function form(Form $form): Form
  85. {
  86. return $form
  87. ->schema([
  88. $this->getGeneralSection(),
  89. $this->getDateAndTimeSection(),
  90. $this->getFinancialAndFiscalSection(),
  91. ])
  92. ->model($this->record)
  93. ->statePath('data')
  94. ->operation('edit');
  95. }
  96. protected function getGeneralSection(): Component
  97. {
  98. return Section::make('General')
  99. ->schema([
  100. Select::make('language')
  101. ->softRequired()
  102. ->localizeLabel()
  103. ->options(LocalizationModel::getAllLanguages())
  104. ->searchable(),
  105. Select::make('timezone')
  106. ->localizeLabel()
  107. ->options(Timezone::getTimezoneOptions(CompanyProfileModel::first()->country))
  108. ->searchable()
  109. ->nullable(),
  110. ])->columns();
  111. }
  112. protected function getDateAndTimeSection(): Component
  113. {
  114. return Section::make('Date & Time')
  115. ->schema([
  116. Select::make('date_format')
  117. ->softRequired()
  118. ->localizeLabel()
  119. ->options(DateFormat::class)
  120. ->live(),
  121. Select::make('time_format')
  122. ->softRequired()
  123. ->localizeLabel()
  124. ->options(TimeFormat::class),
  125. Select::make('week_start')
  126. ->softRequired()
  127. ->localizeLabel()
  128. ->options(WeekStart::class),
  129. ])->columns();
  130. }
  131. protected function getFinancialAndFiscalSection(): Component
  132. {
  133. $beforeNumber = translate('Before Number');
  134. $afterNumber = translate('After Number');
  135. $selectPosition = translate('Select Position');
  136. return Section::make('Financial & Fiscal')
  137. ->schema([
  138. Select::make('number_format')
  139. ->softRequired()
  140. ->localizeLabel()
  141. ->options(NumberFormat::class),
  142. Select::make('percent_first')
  143. ->softRequired()
  144. ->localizeLabel('Percent Position')
  145. ->boolean($beforeNumber, $afterNumber, $selectPosition),
  146. Group::make()
  147. ->schema([
  148. Cluster::make([
  149. Select::make('fiscal_year_end_month')
  150. ->softRequired()
  151. ->options(array_combine(range(1, 12), array_map(static fn ($month) => now()->month($month)->monthName, range(1, 12))))
  152. ->afterStateUpdated(static fn (Set $set) => $set('fiscal_year_end_day', null))
  153. ->columnSpan(2)
  154. ->live(),
  155. Select::make('fiscal_year_end_day')
  156. ->placeholder('Day')
  157. ->softRequired()
  158. ->columnSpan(1)
  159. ->options(function (Get $get) {
  160. $month = $get('fiscal_year_end_month');
  161. $daysInMonth = now()->month($month)->daysInMonth;
  162. return array_combine(range(1, $daysInMonth), range(1, $daysInMonth));
  163. })
  164. ->live(),
  165. ])
  166. ->columns(3)
  167. ->columnSpan(2)
  168. ->required()
  169. ->markAsRequired(false)
  170. ->label('Fiscal Year End'),
  171. ])->columns(3),
  172. ])->columns();
  173. }
  174. protected function handleRecordUpdate(LocalizationModel $record, array $data): LocalizationModel
  175. {
  176. $record->fill($data);
  177. $keysToWatch = [
  178. 'language',
  179. 'timezone',
  180. 'date_format',
  181. 'week_start',
  182. 'time_format',
  183. ];
  184. if ($record->isDirty($keysToWatch)) {
  185. $this->dispatch('localizationUpdated');
  186. }
  187. $record->save();
  188. return $record;
  189. }
  190. /**
  191. * @return array<Action | ActionGroup>
  192. */
  193. protected function getFormActions(): array
  194. {
  195. return [
  196. $this->getSaveFormAction(),
  197. ];
  198. }
  199. protected function getSaveFormAction(): Action
  200. {
  201. return Action::make('save')
  202. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  203. ->submit('save')
  204. ->keyBindings(['mod+s']);
  205. }
  206. public static function canView(Model $record): bool
  207. {
  208. try {
  209. return authorize('update', $record)->allowed();
  210. } catch (AuthorizationException $exception) {
  211. return $exception->toResponse()->allowed();
  212. }
  213. }
  214. }