88use App \Filament \Exports \ExcelExportWithNotificationInDB ;
99use App \Filament \Resources \UserResource ;
1010use App \Models \User ;
11+ use Filament \Notifications \Notification ;
1112use Illuminate \Database \Eloquent \Builder ;
1213use Illuminate \Support \Str ;
1314use pxlrbt \FilamentExcel \Actions \Tables \ExportAction as BaseAction ;
@@ -23,101 +24,110 @@ protected function setUp(): void
2324
2425 $ this ->color ('secondary ' );
2526
26- $ this ->exports ([
27- ExcelExportWithNotificationInDB::make ()
28- ->withFilename (fn () => sprintf (
29- '%s-%s ' ,
30- now ()->format ('Y_m_d-H_i_s ' ),
31- Str::slug (UserResource::getPluralModelLabel ()),
32- ))
33- ->fromTable ()
34- ->modifyQueryUsing (function (Builder $ query ) {
35- return $ query
36- ->addSelect ([
37- 'referrer ' ,
38- 'id ' ,
39- 'role ' ,
40- 'name ' ,
41- 'email ' ,
42- 'email_verified_at ' ,
43- 'created_at ' ,
44- 'newsletter ' ,
45- 'organization_id ' ,
46- 'created_by ' ,
47- ])
48- ->with ([
49- 'donations ' => fn ($ q ) => $ q ->select (['user_id ' , 'amount ' , 'status ' , 'created_at ' ]),
50- ])
51- ->withCount (['donations ' ]);
52- })
53- ->withColumns ([
54- Column::make ('id ' )
55- ->heading ('ID ' ),
56-
57- Column::make ('name ' )
58- ->heading (__ ('user.labels.name ' )),
59-
60- Column::make ('email ' )
61- ->heading (__ ('user.labels.email ' )),
62-
63- Column::make ('role ' )
64- ->heading (__ ('user.labels.role ' ))->formatStateUsing (
65- fn (User $ record ) => $ record ->role ->label ()
66- ),
67-
68- Column::make ('status ' )
69- ->heading (__ ('user.labels.status ' ))
70- ->formatStateUsing (
71- fn (User $ record ) => $ record ->email_verified_at ?
72- __ ('user.labels.status_display.verified ' ) :
73- __ ('user.labels.status_display.unverified ' )
74- ),
75-
76- Column::make ('created_at ' )
77- ->heading (__ ('user.labels.created_at ' ))
78- ->formatStateUsing (
79- fn (User $ record ) => $ record ->created_at ->toFormattedDateTime ()
80- ),
81-
82- Column::make ('newsletter_subscription ' )
83- ->heading (__ ('user.labels.newsletter_subscription ' ))
84- ->formatStateUsing (
85- fn (User $ record ) => $ record ->newsletter ?
27+ try {
28+ $ this ->exports ([
29+ ExcelExportWithNotificationInDB::make ()
30+ ->withFilename (fn () => sprintf (
31+ '%s-%s ' ,
32+ now ()->format ('Y_m_d-H_i_s ' ),
33+ Str::slug (UserResource::getPluralModelLabel ()),
34+ ))
35+ ->fromTable ()
36+ ->modifyQueryUsing (function (Builder $ query ) {
37+ return $ query
38+ ->addSelect ([
39+ 'referrer ' ,
40+ 'id ' ,
41+ 'role ' ,
42+ 'name ' ,
43+ 'email ' ,
44+ 'email_verified_at ' ,
45+ 'created_at ' ,
46+ 'newsletter ' ,
47+ 'organization_id ' ,
48+ 'created_by ' ,
49+ ])
50+ ->with ([
51+ 'donations ' => fn ($ q ) => $ q ->select (['user_id ' , 'amount ' , 'status ' , 'created_at ' ]),
52+ ])
53+ ->withCount (['donations ' ]);
54+ })
55+ ->withColumns ([
56+ Column::make ('id ' )
57+ ->heading ('ID ' ),
58+
59+ Column::make ('name ' )
60+ ->heading (__ ('user.labels.name ' )),
61+
62+ Column::make ('email ' )
63+ ->heading (__ ('user.labels.email ' )),
64+
65+ Column::make ('role ' )
66+ ->heading (__ ('user.labels.role ' ))->formatStateUsing (
67+ fn (User $ record ) => $ record ->role ->label ()
68+ ),
69+
70+ Column::make ('status ' )
71+ ->heading (__ ('user.labels.status ' ))
72+ ->formatStateUsing (
73+ fn (User $ record ) => $ record ->email_verified_at ?
74+ __ ('user.labels.status_display.verified ' ) :
75+ __ ('user.labels.status_display.unverified ' )
76+ ),
77+
78+ Column::make ('created_at ' )
79+ ->heading (__ ('user.labels.created_at ' ))
80+ ->formatStateUsing (
81+ fn (User $ record ) => $ record ->created_at ->toFormattedDateTime ()
82+ ),
83+
84+ Column::make ('newsletter_subscription ' )
85+ ->heading (__ ('user.labels.newsletter_subscription ' ))
86+ ->formatStateUsing (
87+ fn (User $ record ) => $ record ->newsletter ?
8688 $ record ->created_at ->toFormattedDateTime () :
8789 ''
88- ),
89-
90- Column::make ('referrer ' )
91- ->heading (__ ('user.labels.referrer ' )),
92-
93- Column::make ('donations_count ' )
94- ->formatStateUsing (fn (User $ record ) => $ record ->donations ()->whereCharged ()->count () ?? 0 )
95- ->heading (__ ('user.labels.donations_count ' )),
96-
97- Column::make ('donations ' )
98- ->heading (__ ('user.labels.donations_sum ' ))
99- ->formatStateUsing (
100- fn (User $ record ) => $ record ->donations
101- ->reject (fn ($ item ) => $ item ->status === EuPlatescStatus::CHARGED )
102- ->sum ('amount ' )
103- ),
104-
105- Column::make ('comments_count ' )
106- //TODO:: implement comments module and add this column
107- ->formatStateUsing (fn (User $ record ) => 'numarul de comentarii va aparea dupa implementarea modulului ' )
108- ->heading (__ ('user.labels.comments_count ' )),
109-
110- Column::make ('last_donation_date ' )
111- ->heading (__ ('user.labels.last_donation_date ' ))
112- ->formatStateUsing (
113- fn (User $ record ) => $ record ->donations_count ?
90+ ),
91+
92+ Column::make ('referrer ' )
93+ ->heading (__ ('user.labels.referrer ' )),
94+
95+ Column::make ('donations_count ' )
96+ ->formatStateUsing (fn (User $ record ) => $ record ->donations ()->whereCharged ()->count () ?? 0 )
97+ ->heading (__ ('user.labels.donations_count ' )),
98+
99+ Column::make ('donations ' )
100+ ->heading (__ ('user.labels.donations_sum ' ))
101+ ->formatStateUsing (
102+ fn (User $ record ) => $ record ->donations
103+ ->reject (fn ($ item ) => $ item ->status === EuPlatescStatus::CHARGED )
104+ ->sum ('amount ' )
105+ ),
106+
107+ Column::make ('comments_count ' )
108+ //TODO:: implement comments module and add this column
109+ ->formatStateUsing (fn (User $ record ) => 'numarul de comentarii va aparea dupa implementarea modulului ' )
110+ ->heading (__ ('user.labels.comments_count ' )),
111+
112+ Column::make ('last_donation_date ' )
113+ ->heading (__ ('user.labels.last_donation_date ' ))
114+ ->formatStateUsing (
115+ fn (User $ record ) => $ record ->donations_count ?
114116 $ record ->donations
115117 ->reject (fn ($ item ) => $ item ->status === EuPlatescStatus::CHARGED )
116118 ->last ()?->created_at->toFormattedDateTime () : ''
117- ),
118-
119- ])
120- ->queue (),
121- ]);
119+ ),
120+
121+ ])
122+ ->queue (),
123+ ]);
124+ } catch (\Throwable $ exception ) {
125+ logger ()->error ($ exception ->getMessage ());
126+ Notification::make ('export ' )
127+ ->title (__ ('notification.export.error.title ' ))
128+ ->body (__ ('notification.export.error.body ' ))
129+ ->danger ()
130+ ->send ();
131+ }
122132 }
123133}
0 commit comments