Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CompanyProfile.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Pages;
  3. use App\Enums\EntityType;
  4. use App\Filament\Company\Clusters\Settings;
  5. use App\Models\Locale\City;
  6. use App\Models\Locale\Country;
  7. use App\Models\Locale\State;
  8. use App\Models\Setting\CompanyProfile as CompanyProfileModel;
  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\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\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
  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 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->getIdentificationSection(),
  90. $this->getLocationDetailsSection(),
  91. $this->getLegalAndComplianceSection(),
  92. ])
  93. ->model($this->record)
  94. ->statePath('data')
  95. ->operation('edit');
  96. }
  97. protected function getIdentificationSection(): Component
  98. {
  99. return Section::make('Identification')
  100. ->schema([
  101. Group::make()
  102. ->schema([
  103. TextInput::make('email')
  104. ->email()
  105. ->localizeLabel()
  106. ->maxLength(255)
  107. ->required(),
  108. TextInput::make('phone_number')
  109. ->tel()
  110. ->nullable()
  111. ->localizeLabel(),
  112. ])->columns(1),
  113. FileUpload::make('logo')
  114. ->openable()
  115. ->maxSize(2048)
  116. ->localizeLabel()
  117. ->visibility('public')
  118. ->disk('public')
  119. ->directory('logos/company')
  120. ->imageResizeMode('contain')
  121. ->imageCropAspectRatio('1:1')
  122. ->panelAspectRatio('1:1')
  123. ->panelLayout('compact')
  124. ->removeUploadedFileButtonPosition('center bottom')
  125. ->uploadButtonPosition('center bottom')
  126. ->uploadProgressIndicatorPosition('center bottom')
  127. ->getUploadedFileNameForStorageUsing(
  128. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  129. ->prepend(Auth::user()->currentCompany->id . '_'),
  130. )
  131. ->extraAttributes(['class' => 'w-32 h-32'])
  132. ->acceptedFileTypes(['image/png', 'image/jpeg']),
  133. ])->columns();
  134. }
  135. protected function getLocationDetailsSection(): Component
  136. {
  137. return Section::make('Location Details')
  138. ->schema([
  139. Select::make('country')
  140. ->searchable()
  141. ->localizeLabel()
  142. ->live()
  143. ->options(Country::getAvailableCountryOptions())
  144. ->afterStateUpdated(static function (Set $set) {
  145. $set('state_id', null);
  146. $set('city_id', null);
  147. })
  148. ->required(),
  149. Select::make('state_id')
  150. ->localizeLabel('State / Province')
  151. ->searchable()
  152. ->live()
  153. ->options(static fn (Get $get) => State::getStateOptions($get('country')))
  154. ->nullable(),
  155. TextInput::make('address')
  156. ->localizeLabel('Street Address')
  157. ->maxLength(255)
  158. ->nullable(),
  159. Select::make('city_id')
  160. ->localizeLabel('City / Town')
  161. ->searchable()
  162. ->options(static fn (Get $get) => City::getCityOptions($get('country'), $get('state_id')))
  163. ->nullable(),
  164. TextInput::make('zip_code')
  165. ->localizeLabel('Zip / Postal Code')
  166. ->maxLength(20)
  167. ->nullable(),
  168. ])->columns();
  169. }
  170. protected function getLegalAndComplianceSection(): Component
  171. {
  172. return Section::make('Legal & Compliance')
  173. ->schema([
  174. Select::make('entity_type')
  175. ->localizeLabel()
  176. ->options(EntityType::class)
  177. ->required(),
  178. TextInput::make('tax_id')
  179. ->localizeLabel('Tax ID')
  180. ->maxLength(50)
  181. ->nullable(),
  182. ])->columns();
  183. }
  184. protected function handleRecordUpdate(CompanyProfileModel $record, array $data): CompanyProfileModel
  185. {
  186. $record->fill($data);
  187. $keysToWatch = [
  188. 'logo',
  189. ];
  190. if ($record->isDirty($keysToWatch)) {
  191. $this->dispatch('companyProfileUpdated');
  192. }
  193. $record->save();
  194. return $record;
  195. }
  196. /**
  197. * @return array<Action | ActionGroup>
  198. */
  199. protected function getFormActions(): array
  200. {
  201. return [
  202. $this->getSaveFormAction(),
  203. ];
  204. }
  205. protected function getSaveFormAction(): Action
  206. {
  207. return Action::make('save')
  208. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  209. ->submit('save')
  210. ->keyBindings(['mod+s']);
  211. }
  212. public static function canView(Model $record): bool
  213. {
  214. try {
  215. return authorize('update', $record)->allowed();
  216. } catch (AuthorizationException $exception) {
  217. return $exception->toResponse()->allowed();
  218. }
  219. }
  220. }