Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Pages;
  3. use App\Enums\Setting\DateFormat;
  4. use App\Enums\Setting\NumberFormat;
  5. use App\Enums\Setting\TimeFormat;
  6. use App\Enums\Setting\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\Services\CompanySettingsService;
  11. use App\Utilities\Localization\Timezone;
  12. use Filament\Actions\Action;
  13. use Filament\Actions\ActionGroup;
  14. use Filament\Forms\Components\Component;
  15. use Filament\Forms\Components\Group;
  16. use Filament\Forms\Components\Section;
  17. use Filament\Forms\Components\Select;
  18. use Filament\Forms\Form;
  19. use Filament\Forms\Get;
  20. use Filament\Forms\Set;
  21. use Filament\Notifications\Notification;
  22. use Filament\Pages\Concerns\InteractsWithFormActions;
  23. use Filament\Pages\Page;
  24. use Filament\Support\Enums\MaxWidth;
  25. use Filament\Support\Exceptions\Halt;
  26. use Guava\FilamentClusters\Forms\Cluster;
  27. use Illuminate\Auth\Access\AuthorizationException;
  28. use Illuminate\Contracts\Support\Htmlable;
  29. use Illuminate\Database\Eloquent\Model;
  30. use Livewire\Attributes\Locked;
  31. use function Filament\authorize;
  32. /**
  33. * @property Form $form
  34. */
  35. class Localization extends Page
  36. {
  37. use InteractsWithFormActions;
  38. protected static ?string $title = 'Localization';
  39. protected static string $view = 'filament.company.pages.setting.localization';
  40. protected static ?string $cluster = Settings::class;
  41. public ?array $data = [];
  42. #[Locked]
  43. public ?LocalizationModel $record = null;
  44. public function getTitle(): string | Htmlable
  45. {
  46. return translate(static::$title);
  47. }
  48. public static function getNavigationLabel(): string
  49. {
  50. return translate(static::$title);
  51. }
  52. public function getMaxContentWidth(): MaxWidth | string | null
  53. {
  54. return MaxWidth::ScreenTwoExtraLarge;
  55. }
  56. public function mount(): void
  57. {
  58. $this->record = LocalizationModel::firstOrNew([
  59. 'company_id' => auth()->user()->current_company_id,
  60. ]);
  61. abort_unless(static::canView($this->record), 404);
  62. $this->fillForm();
  63. }
  64. public function fillForm(): void
  65. {
  66. $data = $this->record->attributesToArray();
  67. $this->form->fill($data);
  68. }
  69. public function save(): void
  70. {
  71. try {
  72. $data = $this->form->getState();
  73. $this->handleRecordUpdate($this->record, $data);
  74. } catch (Halt $exception) {
  75. return;
  76. }
  77. $this->getSavedNotification()->send();
  78. }
  79. protected function getSavedNotification(): Notification
  80. {
  81. return Notification::make()
  82. ->success()
  83. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  84. }
  85. public function form(Form $form): Form
  86. {
  87. return $form
  88. ->schema([
  89. $this->getGeneralSection(),
  90. $this->getDateAndTimeSection(),
  91. $this->getFinancialAndFiscalSection(),
  92. ])
  93. ->model($this->record)
  94. ->statePath('data')
  95. ->operation('edit');
  96. }
  97. protected function getGeneralSection(): Component
  98. {
  99. return Section::make('General')
  100. ->schema([
  101. Select::make('language')
  102. ->softRequired()
  103. ->localizeLabel()
  104. ->options(LocalizationModel::getAllLanguages())
  105. ->disabled(is_demo_environment())
  106. ->searchable(),
  107. Select::make('timezone')
  108. ->softRequired()
  109. ->localizeLabel()
  110. ->options(Timezone::getTimezoneOptions(CompanyProfileModel::first()->address->country_code))
  111. ->searchable(),
  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 = (int) $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. CompanySettingsService::invalidateSettings($record->company_id);
  188. $this->dispatch('localizationUpdated');
  189. }
  190. $record->save();
  191. return $record;
  192. }
  193. /**
  194. * @return array<Action | ActionGroup>
  195. */
  196. protected function getFormActions(): array
  197. {
  198. return [
  199. $this->getSaveFormAction(),
  200. ];
  201. }
  202. protected function getSaveFormAction(): Action
  203. {
  204. return Action::make('save')
  205. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  206. ->submit('save')
  207. ->keyBindings(['mod+s']);
  208. }
  209. public static function canView(Model $record): bool
  210. {
  211. try {
  212. return authorize('update', $record)->allowed();
  213. } catch (AuthorizationException $exception) {
  214. return $exception->toResponse()->allowed();
  215. }
  216. }
  217. }