|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tapp\FilamentForum\Filament\Resources\Admin\ForumPostResource\Widgets; |
| 4 | + |
| 5 | +use Filament\Widgets\Concerns\InteractsWithPageTable; |
| 6 | +use Filament\Widgets\StatsOverviewWidget as BaseWidget; |
| 7 | +use Filament\Widgets\StatsOverviewWidget\Stat; |
| 8 | +use Illuminate\Support\Facades\DB; |
| 9 | +use Tapp\FilamentForum\Filament\Resources\Admin\ForumPostResource\Pages\ListForumPosts; |
| 10 | +use Tapp\FilamentForum\Models\ForumComment; |
| 11 | + |
| 12 | +class ForumPostStats extends BaseWidget |
| 13 | +{ |
| 14 | + use InteractsWithPageTable; |
| 15 | + |
| 16 | + protected static bool $isLazy = false; |
| 17 | + |
| 18 | + protected ?string $pollingInterval = null; |
| 19 | + |
| 20 | + protected function getTablePage(): string |
| 21 | + { |
| 22 | + return ListForumPosts::class; |
| 23 | + } |
| 24 | + |
| 25 | + protected function getStats(): array |
| 26 | + { |
| 27 | + $query = $this->getPageTableQuery(); |
| 28 | + |
| 29 | + $postIds = (clone $query)->pluck('forum_posts.id'); |
| 30 | + |
| 31 | + $totalPosts = (clone $query)->count(); |
| 32 | + |
| 33 | + $totalFavorited = DB::table('favorite_forum_post') |
| 34 | + ->whereIn('forum_post_id', $postIds) |
| 35 | + ->distinct('forum_post_id') |
| 36 | + ->count('forum_post_id'); |
| 37 | + |
| 38 | + $totalComments = ForumComment::query() |
| 39 | + ->whereIn('forum_post_id', $postIds) |
| 40 | + ->count(); |
| 41 | + |
| 42 | + return [ |
| 43 | + Stat::make(__('Posts'), number_format($totalPosts)) |
| 44 | + ->description(__('Total forum posts')) |
| 45 | + ->descriptionIcon('heroicon-m-chat-bubble-left-right') |
| 46 | + ->color('primary'), |
| 47 | + Stat::make(__('Favorited'), number_format($totalFavorited)) |
| 48 | + ->description(__('Posts with favorites')) |
| 49 | + ->descriptionIcon('heroicon-m-heart') |
| 50 | + ->color('danger'), |
| 51 | + Stat::make(__('Comments'), number_format($totalComments)) |
| 52 | + ->description(__('Total comments')) |
| 53 | + ->descriptionIcon('heroicon-m-chat-bubble-bottom-center-text') |
| 54 | + ->color('success'), |
| 55 | + ]; |
| 56 | + } |
| 57 | +} |
0 commit comments