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ů.

CompanyProfile.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Filament\Company\Pages\Setting;
  3. use App\Enums\EntityType;
  4. use App\Models\Locale\City;
  5. use App\Models\Locale\Country;
  6. use App\Models\Locale\State;
  7. use App\Models\Setting\CompanyProfile as CompanyProfileModel;
  8. use Filament\Actions\Action;
  9. use Filament\Actions\ActionGroup;
  10. use Filament\Facades\Filament;
  11. use Filament\Forms\Components\Component;
  12. use Filament\Forms\Components\FileUpload;
  13. use Filament\Forms\Components\Group;
  14. use Filament\Forms\Components\Section;
  15. use Filament\Forms\Components\Select;
  16. use Filament\Forms\Components\TextInput;
  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 Illuminate\Auth\Access\AuthorizationException;
  25. use Illuminate\Contracts\Support\Htmlable;
  26. use Illuminate\Database\Eloquent\Model;
  27. use Illuminate\Support\Facades\Auth;
  28. use Livewire\Attributes\Locked;
  29. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  30. use function Filament\authorize;
  31. /**
  32. * @property Form $form
  33. */
  34. class CompanyProfile extends Page
  35. {
  36. use InteractsWithFormActions;
  37. protected static ?string $title = 'Company Profile';
  38. protected static ?string $slug = 'settings/company-profile';
  39. protected static string $view = 'filament.company.pages.setting.company-profile';
  40. public ?array $data = [];
  41. #[Locked]
  42. public ?CompanyProfileModel $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 static function getNavigationParentItem(): ?string
  52. {
  53. if (Filament::hasTopNavigation()) {
  54. return translate('Company');
  55. }
  56. return null;
  57. }
  58. public function mount(): void
  59. {
  60. $this->record = CompanyProfileModel::firstOrNew([
  61. 'company_id' => auth()->user()->currentCompany->id,
  62. ]);
  63. abort_unless(static::canView($this->record), 404);
  64. $this->fillForm();
  65. }
  66. public function fillForm(): void
  67. {
  68. $data = $this->record->attributesToArray();
  69. $this->form->fill($data);
  70. }
  71. public function save(): void
  72. {
  73. try {
  74. $data = $this->form->getState();
  75. $this->handleRecordUpdate($this->record, $data);
  76. } catch (Halt $exception) {
  77. return;
  78. }
  79. $this->getSavedNotification()->send();
  80. }
  81. protected function getSavedNotification(): Notification
  82. {
  83. return Notification::make()
  84. ->success()
  85. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  86. }
  87. public function form(Form $form): Form
  88. {
  89. return $form
  90. ->schema([
  91. $this->getIdentificationSection(),
  92. $this->getLocationDetailsSection(),
  93. $this->getLegalAndComplianceSection(),
  94. ])
  95. ->model($this->record)
  96. ->statePath('data')
  97. ->operation('edit');
  98. }
  99. protected function getIdentificationSection(): Component
  100. {
  101. return Section::make('Identification')
  102. ->schema([
  103. Group::make()
  104. ->schema([
  105. TextInput::make('email')
  106. ->email()
  107. ->localizeLabel()
  108. ->maxLength(255)
  109. ->required(),
  110. TextInput::make('phone_number')
  111. ->tel()
  112. ->nullable()
  113. ->localizeLabel(),
  114. ])->columns(1),
  115. FileUpload::make('logo')
  116. ->openable()
  117. ->maxSize(2048)
  118. ->localizeLabel()
  119. ->visibility('public')
  120. ->disk('public')
  121. ->directory('logos/company')
  122. ->imageResizeMode('contain')
  123. ->imageCropAspectRatio('1:1')
  124. ->panelAspectRatio('1:1')
  125. ->panelLayout('compact')
  126. ->removeUploadedFileButtonPosition('center bottom')
  127. ->uploadButtonPosition('center bottom')
  128. ->uploadProgressIndicatorPosition('center bottom')
  129. ->getUploadedFileNameForStorageUsing(
  130. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  131. ->prepend(Auth::user()->currentCompany->id . '_'),
  132. )
  133. ->extraAttributes(['class' => 'w-32 h-32'])
  134. ->acceptedFileTypes(['image/png', 'image/jpeg']),
  135. ])->columns();
  136. }
  137. protected function getLocationDetailsSection(): Component
  138. {
  139. return Section::make('Location Details')
  140. ->schema([
  141. Select::make('country')
  142. ->searchable()
  143. ->localizeLabel()
  144. ->live()
  145. ->options(Country::getAvailableCountryOptions())
  146. ->afterStateUpdated(static function (Set $set) {
  147. $set('state_id', null);
  148. $set('city_id', null);
  149. })
  150. ->required(),
  151. Select::make('state_id')
  152. ->localizeLabel('State / Province')
  153. ->searchable()
  154. ->live()
  155. ->options(static fn (Get $get) => State::getStateOptions($get('country')))
  156. ->nullable(),
  157. TextInput::make('address')
  158. ->localizeLabel('Street Address')
  159. ->maxLength(255)
  160. ->nullable(),
  161. Select::make('city_id')
  162. ->localizeLabel('City / Town')
  163. ->searchable()
  164. ->options(static fn (Get $get) => City::getCityOptions($get('country'), $get('state_id')))
  165. ->nullable(),
  166. TextInput::make('zip_code')
  167. ->localizeLabel('Zip / Postal Code')
  168. ->maxLength(20)
  169. ->nullable(),
  170. ])->columns();
  171. }
  172. protected function getLegalAndComplianceSection(): Component
  173. {
  174. return Section::make('Legal & Compliance')
  175. ->schema([
  176. Select::make('entity_type')
  177. ->localizeLabel()
  178. ->options(EntityType::class)
  179. ->required(),
  180. TextInput::make('tax_id')
  181. ->localizeLabel('Tax ID')
  182. ->maxLength(50)
  183. ->nullable(),
  184. ])->columns();
  185. }
  186. protected function handleRecordUpdate(CompanyProfileModel $record, array $data): CompanyProfileModel
  187. {
  188. $record->fill($data);
  189. $keysToWatch = [
  190. 'logo',
  191. ];
  192. if ($record->isDirty($keysToWatch)) {
  193. $this->dispatch('companyProfileUpdated');
  194. }
  195. $record->save();
  196. return $record;
  197. }
  198. /**
  199. * @return array<Action | ActionGroup>
  200. */
  201. protected function getFormActions(): array
  202. {
  203. return [
  204. $this->getSaveFormAction(),
  205. ];
  206. }
  207. protected function getSaveFormAction(): Action
  208. {
  209. return Action::make('save')
  210. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  211. ->submit('save')
  212. ->keyBindings(['mod+s']);
  213. }
  214. public static function canView(Model $record): bool
  215. {
  216. try {
  217. return authorize('update', $record)->allowed();
  218. } catch (AuthorizationException $exception) {
  219. return $exception->toResponse()->allowed();
  220. }
  221. }
  222. }