From 63233c883e66b5a33a31d9f3786bb1e6931015a0 Mon Sep 17 00:00:00 2001 From: Kevin McKee Date: Fri, 27 Dec 2024 03:01:08 -0600 Subject: [PATCH 1/2] Use Model from Config for Resource (#7) * Use Model from Config for Resource * Update ListMails.php to use config Model * Update MailStatsWidget.php to use config Model --- src/Resources/MailResource.php | 7 +++++-- src/Resources/MailResource/Pages/ListMails.php | 16 +++++++++------- .../MailResource/Widgets/MailStatsWidget.php | 14 ++++++++------ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/Resources/MailResource.php b/src/Resources/MailResource.php index f8a3424..a22b615 100644 --- a/src/Resources/MailResource.php +++ b/src/Resources/MailResource.php @@ -29,8 +29,6 @@ class MailResource extends Resource { - protected static ?string $model = Mail::class; - protected static ?string $slug = 'mails'; protected static ?string $recordTitleAttribute = 'subject'; @@ -39,6 +37,11 @@ class MailResource extends Resource protected static bool $shouldRegisterNavigation = true; + public static function getModel(): string + { + return config('mails.models.mail'); + } + public static function getNavigationGroup(): ?string { return __('Mails'); diff --git a/src/Resources/MailResource/Pages/ListMails.php b/src/Resources/MailResource/Pages/ListMails.php index dbd0758..6143e2e 100644 --- a/src/Resources/MailResource/Pages/ListMails.php +++ b/src/Resources/MailResource/Pages/ListMails.php @@ -25,46 +25,48 @@ protected function getActions(): array public function getTabs(): array { + $class = config('mails.models.mail'); + return [ 'all' => Tab::make() ->label(__('All')) ->badgeColor('primary') ->icon('heroicon-o-inbox') - ->badge(Mail::count()), + ->badge($class::count()), 'sent' => Tab::make() ->label(__('Sent')) ->badgeColor('info') ->icon('heroicon-o-paper-airplane') - ->badge(Mail::sent()->count()) + ->badge($class::sent()->count()) ->modifyQueryUsing(fn (Builder $query) => $query->sent()), 'delivered' => Tab::make() ->label(__('Delivered')) ->badgeColor('success') ->icon('heroicon-o-check-circle') - ->badge(Mail::delivered()->count()) + ->badge($class::delivered()->count()) ->modifyQueryUsing(fn (Builder $query) => $query->delivered()), 'opened' => Tab::make() ->label(__('Opened')) ->badgeColor('info') ->icon('heroicon-o-eye') - ->badge(Mail::opened()->count()) + ->badge($class::opened()->count()) ->modifyQueryUsing(fn (Builder $query) => $query->opened()), 'clicked' => Tab::make() ->label(__('Clicked')) ->badgeColor('clicked') ->icon('heroicon-o-cursor-arrow-rays') - ->badge(Mail::clicked()->count()) + ->badge($class::clicked()->count()) ->modifyQueryUsing(fn (Builder $query) => $query->clicked()), 'bounced' => Tab::make() ->label(__('Bounced')) ->badgeColor('danger') ->icon('heroicon-o-x-circle') - ->badge(fn () => Mail::softBounced()->count() + Mail::hardBounced()->count()) + ->badge(fn () => $class::softBounced()->count() + $class::hardBounced()->count()) ->modifyQueryUsing(fn (Builder $query) => $query->where(function ($query) { $query->softBounced()->orWhere(function ($query) { $query->hardBounced(); @@ -75,7 +77,7 @@ public function getTabs(): array ->label(__('Unsent')) ->badgeColor('gray') ->icon('heroicon-o-x-circle') - ->badge(Mail::unsent()->count()) + ->badge($class::unsent()->count()) ->modifyQueryUsing(fn (Builder $query) => $query->unsent()), ]; } diff --git a/src/Resources/MailResource/Widgets/MailStatsWidget.php b/src/Resources/MailResource/Widgets/MailStatsWidget.php index 7064f95..b672e00 100644 --- a/src/Resources/MailResource/Widgets/MailStatsWidget.php +++ b/src/Resources/MailResource/Widgets/MailStatsWidget.php @@ -14,12 +14,14 @@ class MailStatsWidget extends BaseWidget protected function getStats(): array { - $bouncedMails = Mail::where(fn ($query) => $query->softBounced()->orWhere(fn ($query) => $query->hardBounced()))->count(); - $openedMails = Mail::opened()->count(); - $deliveredMails = Mail::delivered()->count(); - $clickedMails = Mail::clicked()->count(); - - $mailCount = Mail::count(); + $class = config('mails.models.mail'); + + $bouncedMails = $class::where(fn ($query) => $query->softBounced()->orWhere(fn ($query) => $query->hardBounced()))->count(); + $openedMails = $class::opened()->count(); + $deliveredMails = $class::delivered()->count(); + $clickedMails = $class::clicked()->count(); + + $mailCount = $class::count(); if ($mailCount === 0) { return []; From d78ad3de76a8f65f7e462219a3c8097b0189536e Mon Sep 17 00:00:00 2001 From: Baspa Date: Fri, 27 Dec 2024 09:01:25 +0000 Subject: [PATCH 2/2] Fix styling --- src/Resources/MailResource/Pages/ListMails.php | 3 +-- src/Resources/MailResource/Widgets/MailStatsWidget.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Resources/MailResource/Pages/ListMails.php b/src/Resources/MailResource/Pages/ListMails.php index 6143e2e..af3ff3e 100644 --- a/src/Resources/MailResource/Pages/ListMails.php +++ b/src/Resources/MailResource/Pages/ListMails.php @@ -7,7 +7,6 @@ use Illuminate\Database\Eloquent\Builder; use Vormkracht10\FilamentMails\Resources\MailResource; use Vormkracht10\FilamentMails\Resources\MailResource\Widgets\MailStatsWidget; -use Vormkracht10\Mails\Models\Mail; class ListMails extends ListRecords { @@ -26,7 +25,7 @@ protected function getActions(): array public function getTabs(): array { $class = config('mails.models.mail'); - + return [ 'all' => Tab::make() ->label(__('All')) diff --git a/src/Resources/MailResource/Widgets/MailStatsWidget.php b/src/Resources/MailResource/Widgets/MailStatsWidget.php index b672e00..f3259e6 100644 --- a/src/Resources/MailResource/Widgets/MailStatsWidget.php +++ b/src/Resources/MailResource/Widgets/MailStatsWidget.php @@ -4,7 +4,6 @@ use Filament\Widgets\StatsOverviewWidget as BaseWidget; use Filament\Widgets\StatsOverviewWidget\Stat; -use Vormkracht10\Mails\Models\Mail; class MailStatsWidget extends BaseWidget { @@ -15,7 +14,7 @@ class MailStatsWidget extends BaseWidget protected function getStats(): array { $class = config('mails.models.mail'); - + $bouncedMails = $class::where(fn ($query) => $query->softBounced()->orWhere(fn ($query) => $query->hardBounced()))->count(); $openedMails = $class::opened()->count(); $deliveredMails = $class::delivered()->count();