From c4d7282f9b4b16a2a2eef0a67276290977b9e0fe Mon Sep 17 00:00:00 2001 From: Sagar Date: Thu, 2 May 2024 15:39:31 +0200 Subject: [PATCH] feat: created a view for collection and added widgets --- .../Resources/CollectionResource.php | 11 ++++ .../Pages/ViewCollection.php | 20 +++++++ .../Widgets/CollectionStats.php | 52 +++++++++++++++++++ app/Models/Molecule.php | 4 +- 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 app/Filament/Dashboard/Resources/CollectionResource/Pages/ViewCollection.php create mode 100644 app/Filament/Dashboard/Resources/CollectionResource/Widgets/CollectionStats.php diff --git a/app/Filament/Dashboard/Resources/CollectionResource.php b/app/Filament/Dashboard/Resources/CollectionResource.php index 5d40793b..c3fd65ae 100644 --- a/app/Filament/Dashboard/Resources/CollectionResource.php +++ b/app/Filament/Dashboard/Resources/CollectionResource.php @@ -20,6 +20,8 @@ use Filament\Tables\Table; use Illuminate\Support\Str; use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager; +use App\Filament\Dashboard\Resources\CollectionResource\Widgets\CollectionStats; + class CollectionResource extends Resource { @@ -78,6 +80,7 @@ public static function table(Table $table): Table // ]) ->actions([ + Tables\Actions\ViewAction::make(), Tables\Actions\EditAction::make(), ]) ->bulkActions([ @@ -102,6 +105,14 @@ public static function getPages(): array 'index' => Pages\ListCollections::route('/'), 'create' => Pages\CreateCollection::route('/create'), 'edit' => Pages\EditCollection::route('/{record}/edit'), + 'view' => Pages\ViewCollection::route('/{record}'), + ]; + } + + public static function getWidgets(): array + { + return [ + CollectionStats::class, ]; } } diff --git a/app/Filament/Dashboard/Resources/CollectionResource/Pages/ViewCollection.php b/app/Filament/Dashboard/Resources/CollectionResource/Pages/ViewCollection.php new file mode 100644 index 00000000..f974a8bf --- /dev/null +++ b/app/Filament/Dashboard/Resources/CollectionResource/Pages/ViewCollection.php @@ -0,0 +1,20 @@ +record->entries->count()) + // ->description('Total count') + // ->color('primary'), + // Stat::make('Total Entries', Cache::rememberForever('stats.collections', function () { + // return Collection::count(); + // })), + Stat::make('Total Entries', Cache::rememberForever('stats.collections'.$this->record->id.'entries.count', function () { + return $this->record->entries->count(); + })), + Stat::make('Total Molecules', Cache::rememberForever('stats.collections'.$this->record->id.'molecules.count', function () { + return $this->record->molecules->count(); + })), + Stat::make('Total Citations', Cache::rememberForever('stats.collections'.$this->record->id.'citations.count', function () { + return $this->record->citations->count(); + })), + Stat::make('Total Organisms', Cache::rememberForever('stats.collections'.$this->record->id.'organisms.count', function () { + // refactor the below with eloquent relations if possible + $molecules = $this->record->molecules; + $count = 0; + foreach($molecules as $molecule) { + $count += $molecule->organisms()->count(); + } + return $count; + })), + Stat::make('Total Geo Locations', Cache::rememberForever('stats.collections'.$this->record->id.'geo_locations.count', function () { + // refactor the below with eloquent relations if possible + $molecules = $this->record->molecules; + $count = 0; + foreach($molecules as $molecule) { + $count += $molecule->geoLocations()->count(); + } + return $count; + })), + ]; + } +} diff --git a/app/Models/Molecule.php b/app/Models/Molecule.php index 15108924..36f8f476 100644 --- a/app/Models/Molecule.php +++ b/app/Models/Molecule.php @@ -100,12 +100,12 @@ public function collections(): BelongsToMany return $this->belongsToMany(Collection::class)->withPivot('url', 'reference', 'mol_filename', 'structural_comments')->withTimestamps(); } - public function organisms() + public function organisms(): BelongsToMany { return $this->belongsToMany(Organism::class)->withPivot('id', 'organism_parts')->withTimestamps(); } - public function geoLocations() + public function geoLocations(): BelongsToMany { return $this->belongsToMany(GeoLocation::class)->withPivot('locations')->withTimestamps(); }