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.

CompanyProfile.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Pages;
  3. use App\Enums\Setting\EntityType;
  4. use App\Filament\Company\Clusters\Settings;
  5. use App\Filament\Forms\Components\CountrySelect;
  6. use App\Models\Locale\State;
  7. use App\Models\Setting\CompanyProfile as CompanyProfileModel;
  8. use App\Utilities\Localization\Timezone;
  9. use Filament\Actions\Action;
  10. use Filament\Actions\ActionGroup;
  11. use Filament\Forms\Components\Component;
  12. use Filament\Forms\Components\FileUpload;
  13. use Filament\Forms\Components\Group;
  14. use Filament\Forms\Components\Hidden;
  15. use Filament\Forms\Components\Section;
  16. use Filament\Forms\Components\Select;
  17. use Filament\Forms\Components\TextInput;
  18. use Filament\Forms\Form;
  19. use Filament\Forms\Get;
  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 Illuminate\Auth\Access\AuthorizationException;
  26. use Illuminate\Contracts\Support\Htmlable;
  27. use Illuminate\Database\Eloquent\Model;
  28. use Illuminate\Support\Facades\Auth;
  29. use Livewire\Attributes\Locked;
  30. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  31. use function Filament\authorize;
  32. /**
  33. * @property Form $form
  34. */
  35. class CompanyProfile extends Page
  36. {
  37. use InteractsWithFormActions;
  38. protected static ?string $title = 'Company Profile';
  39. protected static string $view = 'filament.company.pages.setting.company-profile';
  40. protected static ?string $cluster = Settings::class;
  41. public ?array $data = [];
  42. #[Locked]
  43. public ?CompanyProfileModel $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 = CompanyProfileModel::firstOrNew([
  59. 'company_id' => auth()->user()->currentCompany->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 updateTimezone(string $countryCode): void
  80. {
  81. $model = \App\Models\Setting\Localization::firstOrFail();
  82. $timezones = Timezone::getTimezonesForCountry($countryCode);
  83. if (! empty($timezones)) {
  84. $model->update([
  85. 'timezone' => $timezones[0],
  86. ]);
  87. }
  88. }
  89. protected function getTimezoneChangeNotification(): Notification
  90. {
  91. return Notification::make()
  92. ->info()
  93. ->title('Timezone Update Required')
  94. ->body('You have changed your country or state. Please update your timezone to ensure accurate date and time information.')
  95. ->actions([
  96. \Filament\Notifications\Actions\Action::make('updateTimezone')
  97. ->label('Update Timezone')
  98. ->url(Localization::getUrl()),
  99. ])
  100. ->persistent()
  101. ->send();
  102. }
  103. protected function getSavedNotification(): Notification
  104. {
  105. return Notification::make()
  106. ->success()
  107. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  108. }
  109. public function form(Form $form): Form
  110. {
  111. return $form
  112. ->schema([
  113. $this->getIdentificationSection(),
  114. $this->getLocationDetailsSection(),
  115. $this->getLegalAndComplianceSection(),
  116. ])
  117. ->model($this->record)
  118. ->statePath('data')
  119. ->operation('edit');
  120. }
  121. protected function getIdentificationSection(): Component
  122. {
  123. return Section::make('Identification')
  124. ->schema([
  125. Group::make()
  126. ->schema([
  127. TextInput::make('email')
  128. ->email()
  129. ->localizeLabel()
  130. ->maxLength(255)
  131. ->required(),
  132. TextInput::make('phone_number')
  133. ->tel()
  134. ->nullable()
  135. ->localizeLabel(),
  136. ])->columns(1),
  137. FileUpload::make('logo')
  138. ->openable()
  139. ->maxSize(2048)
  140. ->localizeLabel()
  141. ->visibility('public')
  142. ->disk('public')
  143. ->directory('logos/company')
  144. ->imageResizeMode('contain')
  145. ->imageCropAspectRatio('1:1')
  146. ->panelAspectRatio('1:1')
  147. ->panelLayout('integrated')
  148. ->removeUploadedFileButtonPosition('center bottom')
  149. ->uploadButtonPosition('center bottom')
  150. ->uploadProgressIndicatorPosition('center bottom')
  151. ->getUploadedFileNameForStorageUsing(
  152. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  153. ->prepend(Auth::user()->currentCompany->id . '_'),
  154. )
  155. ->extraAttributes(['class' => 'w-32 h-32'])
  156. ->acceptedFileTypes(['image/png', 'image/jpeg']),
  157. ])->columns();
  158. }
  159. protected function getLocationDetailsSection(): Component
  160. {
  161. return Section::make('Address Information')
  162. ->relationship('address')
  163. ->schema([
  164. Hidden::make('type')
  165. ->default('general'),
  166. TextInput::make('address_line_1')
  167. ->label('Address Line 1')
  168. ->required()
  169. ->maxLength(255),
  170. TextInput::make('address_line_2')
  171. ->label('Address Line 2')
  172. ->maxLength(255),
  173. CountrySelect::make('country')
  174. ->clearStateField()
  175. ->required(),
  176. Select::make('state_id')
  177. ->localizeLabel('State / Province')
  178. ->searchable()
  179. ->options(static fn (Get $get) => State::getStateOptions($get('country')))
  180. ->nullable(),
  181. TextInput::make('city')
  182. ->localizeLabel('City / Town')
  183. ->required()
  184. ->maxLength(255),
  185. TextInput::make('postal_code')
  186. ->label('Postal Code / Zip Code')
  187. ->required()
  188. ->maxLength(255),
  189. ])
  190. ->columns(2);
  191. }
  192. protected function getLegalAndComplianceSection(): Component
  193. {
  194. return Section::make('Legal & Compliance')
  195. ->schema([
  196. Select::make('entity_type')
  197. ->localizeLabel()
  198. ->options(EntityType::class)
  199. ->required(),
  200. TextInput::make('tax_id')
  201. ->localizeLabel('Tax ID')
  202. ->maxLength(50)
  203. ->nullable(),
  204. ])->columns();
  205. }
  206. protected function handleRecordUpdate(CompanyProfileModel $record, array $data): CompanyProfileModel
  207. {
  208. $record->fill($data);
  209. $keysToWatch = [
  210. 'logo',
  211. ];
  212. if ($record->isDirty($keysToWatch)) {
  213. $this->dispatch('companyProfileUpdated');
  214. }
  215. $record->save();
  216. return $record;
  217. }
  218. /**
  219. * @return array<Action | ActionGroup>
  220. */
  221. protected function getFormActions(): array
  222. {
  223. return [
  224. $this->getSaveFormAction(),
  225. ];
  226. }
  227. protected function getSaveFormAction(): Action
  228. {
  229. return Action::make('save')
  230. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  231. ->submit('save')
  232. ->keyBindings(['mod+s']);
  233. }
  234. public static function canView(Model $record): bool
  235. {
  236. try {
  237. return authorize('update', $record)->allowed();
  238. } catch (AuthorizationException $exception) {
  239. return $exception->toResponse()->allowed();
  240. }
  241. }
  242. }