diff --git a/app/Filament/Dashboard/Resources/CitationResource.php b/app/Filament/Dashboard/Resources/CitationResource.php index 00776312..28ee9b40 100644 --- a/app/Filament/Dashboard/Resources/CitationResource.php +++ b/app/Filament/Dashboard/Resources/CitationResource.php @@ -6,18 +6,12 @@ use App\Filament\Dashboard\Resources\CitationResource\RelationManagers\CollectionRelationManager; use App\Filament\Dashboard\Resources\CitationResource\RelationManagers\MoleculeRelationManager; use App\Models\Citation; -use Closure; -use Filament\Forms\Components\Section; -use Filament\Forms\Components\Textarea; -use Filament\Forms\Components\TextInput; use Filament\Forms\Form; -use Filament\Forms\Get; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\HtmlString; use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager; class CitationResource extends Resource @@ -35,87 +29,7 @@ class CitationResource extends Resource public static function form(Form $form): Form { return $form - ->schema([ - Section::make() - ->schema([ - TextInput::make('failMessage') - ->default('') - ->hidden() - ->disabled(), - TextInput::make('doi') - ->label('DOI') - ->live(onBlur: true) - ->afterStateUpdated(function ($set, $state) { - if (doiRegxMatch($state)) { - $citationDetails = fetchDOICitation($state); - if ($citationDetails) { - $set('title', $citationDetails['title']); - $set('authors', $citationDetails['authors']); - $set('citation_text', $citationDetails['citation_text']); - $set('failMessage', 'Success'); - } else { - $set('failMessage', 'No citation found. Please fill in the details manually'); - } - } else { - $set('failMessage', 'Invalid DOI'); - } - }) - ->helperText(function ($get) { - - if ($get('failMessage') == 'Fetching') { - return new HtmlString(' - - - '); - } elseif ($get('failMessage') != 'Success') { - return new HtmlString(''.$get('failMessage').''); - } else { - return null; - } - }) - ->required() - ->unique() - ->rules([ - fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) { - if ($get('failMessage') != 'No citation found. Please fill in the details manually') { - $fail($get('failMessage')); - } - }, - ]) - ->validationMessages([ - 'unique' => 'The DOI already exists.', - ]), - ]), - - Section::make() - ->schema([ - TextInput::make('title') - ->disabled(function ($get, string $operation) { - if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') { - return false; - } else { - return true; - } - }), - TextInput::make('authors') - ->disabled(function ($get, string $operation) { - if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') { - return false; - } else { - return true; - } - }), - Textarea::make('citation_text') - ->label('Citation text / URL') - ->disabled(function ($get, string $operation) { - if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') { - return false; - } else { - return true; - } - }), - ])->columns(1), - ]); + ->schema(Citation::getForm()); } public static function table(Table $table): Table diff --git a/app/Filament/Dashboard/Resources/MoleculeResource/RelationManagers/CitationsRelationManager.php b/app/Filament/Dashboard/Resources/MoleculeResource/RelationManagers/CitationsRelationManager.php index 728e0e5c..08d7d923 100644 --- a/app/Filament/Dashboard/Resources/MoleculeResource/RelationManagers/CitationsRelationManager.php +++ b/app/Filament/Dashboard/Resources/MoleculeResource/RelationManagers/CitationsRelationManager.php @@ -2,6 +2,7 @@ namespace App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers; +use App\Models\Citation; use Filament\Forms\Form; use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; @@ -28,7 +29,10 @@ public function table(Table $table): Table // ]) ->headerActions([ - Tables\Actions\AttachAction::make(), + Tables\Actions\AttachAction::make() + ->recordSelectSearchColumns(['title', 'authors', 'doi']), + Tables\Actions\CreateAction::make() + ->form(Citation::getForm()), ]) ->actions([ Tables\Actions\EditAction::make(), diff --git a/app/Filament/Dashboard/Resources/ReportResource/RelationManagers/CitationsRelationManager.php b/app/Filament/Dashboard/Resources/ReportResource/RelationManagers/CitationsRelationManager.php index 7278d29b..4a901ca4 100644 --- a/app/Filament/Dashboard/Resources/ReportResource/RelationManagers/CitationsRelationManager.php +++ b/app/Filament/Dashboard/Resources/ReportResource/RelationManagers/CitationsRelationManager.php @@ -36,7 +36,7 @@ public function table(Table $table): Table ->headerActions([ Tables\Actions\AttachAction::make() ->multiple() - ->recordSelectSearchColumns(['title', 'authors']), + ->recordSelectSearchColumns(['title', 'authors', 'doi']), ]) ->actions([ Tables\Actions\DetachAction::make(), diff --git a/app/Models/Citation.php b/app/Models/Citation.php index 3cc86333..10979896 100644 --- a/app/Models/Citation.php +++ b/app/Models/Citation.php @@ -2,9 +2,15 @@ namespace App\Models; +use Closure; +use Filament\Forms\Components\Section; +use Filament\Forms\Components\Textarea; +use Filament\Forms\Components\TextInput; +use Filament\Forms\Get; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphToMany; +use Illuminate\Support\HtmlString; use OwenIt\Auditing\Contracts\Auditable; class Citation extends Model implements Auditable @@ -52,4 +58,90 @@ public function transformAudit(array $data): array { return changeAudit($data); } + + public static function getForm(): array + { + return [ + Section::make() + ->schema([ + TextInput::make('failMessage') + ->default('') + ->hidden() + ->disabled(), + TextInput::make('doi') + ->label('DOI') + ->live(onBlur: true) + ->afterStateUpdated(function ($set, $state) { + if (doiRegxMatch($state)) { + $set('failMessage', 'Fetching'); + $citationDetails = fetchDOICitation($state); + if ($citationDetails) { + $set('title', $citationDetails['title']); + $set('authors', $citationDetails['authors']); + $set('citation_text', $citationDetails['citation_text']); + $set('failMessage', 'Success'); + } else { + $set('failMessage', 'No citation found. Please fill in the details manually'); + } + } else { + $set('failMessage', 'Invalid DOI'); + } + }) + ->helperText(function ($get) { + + if ($get('failMessage') == 'Fetching') { + return new HtmlString(' + + + '); + } elseif ($get('failMessage') != 'Success') { + return new HtmlString(''.$get('failMessage').''); + } else { + return null; + } + }) + ->required() + ->unique() + ->rules([ + fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) { + if ($get('failMessage') != 'Success') { + $fail($get('failMessage')); + } + }, + ]) + ->validationMessages([ + 'unique' => 'The DOI already exists.', + ]), + ]), + + Section::make() + ->schema([ + TextInput::make('title') + ->disabled(function ($get, string $operation) { + if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') { + return false; + } else { + return true; + } + }), + TextInput::make('authors') + ->disabled(function ($get, string $operation) { + if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') { + return false; + } else { + return true; + } + }), + Textarea::make('citation_text') + ->label('Citation text / URL') + ->disabled(function ($get, string $operation) { + if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') { + return false; + } else { + return true; + } + }), + ])->columns(1), + ]; + } }