Skip to content

Commit

Permalink
Merge pull request #113 from Steinbeck-Lab/citations-doi-functionality
Browse files Browse the repository at this point in the history
Citations doi functionality
  • Loading branch information
CS76 authored Jun 27, 2024
2 parents 95e8b2c + af35ec6 commit f982a42
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 27 deletions.
89 changes: 84 additions & 5 deletions app/Filament/Dashboard/Resources/CitationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
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;

class CitationResource extends Resource
{
Expand All @@ -31,11 +35,86 @@ public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('doi'),
TextInput::make('title'),
TextInput::make('authors'),
TextArea::make('citation_text'),
])->columns(1);
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('<svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-dark inline" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg> ');
} elseif ($get('failMessage') != 'Success') {
return new HtmlString('<span style="color:red">'.$get('failMessage').'</span>');
} 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),
]);
}

public static function table(Table $table): Table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
namespace App\Filament\Dashboard\Resources\GeoLocationResource\Pages;

use App\Filament\Dashboard\Resources\GeoLocationResource;
use App\Models\Molecule;
use Filament\Resources\Pages\CreateRecord;

class CreateGeoLocation extends CreateRecord
{
protected static string $resource = GeoLocationResource::class;

protected function beforeCreate(): void
{
// $molecule = Molecule::where('identifier', $this->data['molecule_id'])->get();
// $this->data['molecule_id'] = $molecule[0]->id;
// dd($this->data);
// $this->data->molecules()->attach($molecule);
}
}
10 changes: 5 additions & 5 deletions app/Filament/Dashboard/Resources/ReportResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function form(Form $form): Form
{
return $form
->schema([
Select::make('choice')
Select::make('report_type')
->label('You want to report:')
->hintIcon('heroicon-m-question-mark-circle', tooltip: 'Select what you want to report. Ex: Molecule, Citation, Collection, Organism.')
->live()
Expand Down Expand Up @@ -71,7 +71,7 @@ public static function form(Form $form): Form
if ($get('collections') == []) {
return true;
}
} elseif (! request()->has('collection_uuid') && $get('choice') != 'collection') {
} elseif (! request()->has('collection_uuid') && $get('report_type') != 'collection') {
return true;
}
})
Expand All @@ -93,7 +93,7 @@ public static function form(Form $form): Form
if ($get('citations') == []) {
return true;
}
} elseif (! request()->has('citation_id') && $get('choice') != 'citation') {
} elseif (! request()->has('citation_id') && $get('report_type') != 'citation') {
return true;
}
})
Expand All @@ -113,7 +113,7 @@ public static function form(Form $form): Form
if ($get('organisms') == []) {
return true;
}
} elseif (! request()->has('organism_id') && $get('choice') != 'organism') {
} elseif (! request()->has('organism_id') && $get('report_type') != 'organism') {
return true;
}
})
Expand All @@ -131,7 +131,7 @@ public static function form(Form $form): Form
if (is_null($get('mol_id_csv'))) {
return true;
}
} elseif (! request()->has('compound_id') && $get('choice') != 'molecule') {
} elseif (! request()->has('compound_id') && $get('report_type') != 'molecule') {
return true;
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ protected function afterFill(): void

protected function beforeCreate(): void
{
if ($this->data['choice'] == 'collection') {
if ($this->data['report_type'] == 'collection') {
$this->data['citations'] = [];
$this->data['molecules'] = null;
$this->data['mol_id_csv'] = null;
$this->data['organisms'] = [];
} elseif ($this->data['choice'] == 'citation') {
} elseif ($this->data['report_type'] == 'citation') {
$this->data['collections'] = [];
$this->data['molecules'] = null;
$this->data['mol_id_csv'] = null;
$this->data['organisms'] = [];
} elseif ($this->data['choice'] == 'molecule') {
} elseif ($this->data['report_type'] == 'molecule') {
$this->data['collections'] = [];
$this->data['citations'] = [];
$this->data['organisms'] = [];
} elseif ($this->data['choice'] == 'organism') {
} elseif ($this->data['report_type'] == 'organism') {
$this->data['collections'] = [];
$this->data['citations'] = [];
$this->data['molecules'] = null;
$this->data['mol_id_csv'] = null;
}

if (! ($this->data['collections'] || $this->data['citations'] || $this->data['molecules'] || $this->data['organisms'])) {
if (! ($this->data['collections'] || $this->data['citations'] || $this->data['mol_id_csv'] || $this->data['organisms'])) {
Notification::make()
->danger()
->title('Select at least one Collection/Citation/Molecule/Organism')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class CitationsRelationManager extends RelationManager
{
Expand Down Expand Up @@ -46,4 +47,13 @@ public function table(Table $table): Table
]),
]);
}

public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
{
if ($ownerRecord->report_type === 'citation') {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class CollectionsRelationManager extends RelationManager
{
Expand Down Expand Up @@ -46,4 +47,13 @@ public function table(Table $table): Table
]),
]);
}

public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
{
if ($ownerRecord->report_type === 'collection') {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class MoleculesRelationManager extends RelationManager
{
Expand Down Expand Up @@ -46,4 +47,13 @@ public function table(Table $table): Table
]),
]);
}

public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
{
if ($ownerRecord->report_type === 'molecule') {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class OrganismsRelationManager extends RelationManager
{
Expand Down Expand Up @@ -45,4 +46,13 @@ public function table(Table $table): Table
]),
]);
}

public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
{
if ($ownerRecord->report_type === 'organism') {
return true;
}

return false;
}
}
Loading

0 comments on commit f982a42

Please sign in to comment.