class CreateMultipleDocument extends Page { use CreateRecord\Concerns\Translatable, InteractsWithFormActions; protected static string $resource = DocumentResource::class; protected static string $view = 'filament.app.resources.document-resource.pages.create-multiple-document'; protected static ?string $navigationIcon = 'heroicon-o-document-text'; protected static ?string $navigationGroup = 'Suplements'; public ?array $data = []; public ?Document $record = null; public function mount(): void { $this->authorizeAccess(); $this->fillForm(); //$this->previousUrl = url()->previous(); } protected function authorizeAccess(): void { static::authorizeResourceAccess(); abort_unless(static::getResource()::canCreate(), 403); } // protected function fillForm(): void // { // /** @internal Read the DocBlock above the following method. */ // $this->fillFormWithDefaultsAndCallHooks(); // } /** * @internal Never override or call this method. If you completely override `fillForm()`, copy the contents of this method into your override. */ protected function fillFormWithDefaultsAndCallHooks(): void { $this->callHook('beforeFill'); $this->form->fill(); $this->callHook('afterFill'); } protected function mutateFormDataBeforeFill(array $data): array { return $data; } protected function mutateFormDataBeforeSave(array $data): array { return $data; } public function form(Form $form): Form { return $form ->schema([ Forms\Components\Section::make('Select holders') ->schema([ Forms\Components\Select::make('holders') ->relationship('holder', 'full_name') ->searchable() ->multiple() ->preload() ->required(), ]), Forms\Components\Section::make('Select base document') ->schema([ Forms\Components\Select::make('document_base_id') ->options(DocumentBase::all()->pluck('title', 'id')->toArray()) ->searchable() ->preload() ->required() ->optionsLimit(15) ->live(), ]), Forms\Components\Section::make('Document preview') ->schema([ Forms\Components\View::make('') ->view('filament.app.resources.components.document-preview'), ]) ->visible(fn(Get $get) => $get('document_base_id') !== null), ])->model(Document::class) ->statePath('data') ->operation('create'); } /** * @return array */ protected function getForms(): array { return [ 'form' => $this->form( static::getResource()::form( $this->makeForm() ->operation('create') ->model($this->getModel()) ->statePath($this->getFormStatePath()) ->columns($this->hasInlineLabels() ? 1 : 2) ->inlineLabel($this->hasInlineLabels()), ) ), ]; } /** * @return array */ protected function getFormActions(): array { return [ $this->getCreateFormAction(), $this->getCreateAnotherFormAction(), $this->getCancelFormAction(), ]; } protected function getCreateFormAction(): Action { return Action::make('create') ->label(__('filament-panels::resources/pages/create-record.form.actions.create.label')) ->submit('create') ->keyBindings(['mod+s']); } protected function getSubmitFormAction(): Action { return $this->getCreateFormAction(); } protected function getCreateAnotherFormAction(): Action { return Action::make('createAnother') ->label(__('filament-panels::resources/pages/create-record.form.actions.create_another.label')) ->action('createAnother') ->keyBindings(['mod+shift+s']) ->color('gray'); } protected function getCancelFormAction(): Action { return Action::make('cancel') ->label(__('filament-panels::resources/pages/create-record.form.actions.cancel.label')) ->url($this->previousUrl ?? static::getResource()::getUrl()) ->color('gray'); } public function save(): void { try { $data = $this->form->getState(); $data = $this->mutateFormDataBeforeSave($data); $this->handleRecordCreation($data); } catch (Halt $exception) { return; } $this->getCreatedNotification()?->send(); if ($redirectUrl = $this->getRedirectUrl()) { $this->redirect($redirectUrl); } } protected function getRedirectUrl(): ?string { $resource = static::getResource(); return null; return $resource::getUrl('index'); } protected function handleRecordCreation(array $data): Model { debug($data); $record = new ($this->getModel())($data); //$record->save(); return $record; } protected function getCreatedNotification(): ?Notification { $title = $this->getCreatedNotificationTitle(); if (blank($title)) { return null; } return Notification::make() ->success() ->title($title); } protected function getCreatedNotificationTitle(): ?string { return __('filament-panels::resources/pages/create-record.notifications.created.title'); } protected function getHeaderActions(): array { return [ LocaleSwitcher::make(), ]; } }