2
2
3
3
namespace Vormkracht10 \FilamentMails \Resources ;
4
4
5
- use Filament \Forms \Components \TextInput ;
5
+ use Filament \Tables ;
6
+ use Illuminate \View \View ;
7
+ use Filament \Tables \Table ;
8
+ use Illuminate \Support \Carbon ;
9
+ use Filament \Infolists \Infolist ;
10
+ use Filament \Resources \Resource ;
11
+ use Illuminate \Support \Collection ;
12
+ use Filament \Tables \Actions \Action ;
13
+ use Vormkracht10 \Mails \Models \Mail ;
6
14
use Filament \Infolists \Components \Grid ;
7
- use Filament \Infolists \Components \RepeatableEntry ;
8
- use Filament \Infolists \Components \Section ;
9
15
use Filament \Infolists \Components \Tabs ;
16
+ use Vormkracht10 \Mails \Enums \EventType ;
17
+ use Filament \Forms \Components \TagsInput ;
18
+ use Filament \Notifications \Notification ;
19
+ use Vormkracht10 \Mails \Models \MailEvent ;
20
+ use Filament \Infolists \Components \Section ;
21
+ use Vormkracht10 \Mails \Actions \ResendMail ;
10
22
use Filament \Infolists \Components \Tabs \Tab ;
11
23
use Filament \Infolists \Components \TextEntry ;
12
24
use Filament \Infolists \Components \ViewEntry ;
13
- use Filament \Infolists \Infolist ;
14
- use Filament \Notifications \Notification ;
15
- use Filament \Resources \Resource ;
16
- use Filament \Tables ;
17
- use Filament \Tables \Actions \Action ;
18
- use Filament \Tables \Table ;
19
- use Illuminate \Support \Carbon ;
20
- use Illuminate \Support \Collection ;
21
- use Illuminate \View \View ;
22
- use Vormkracht10 \FilamentMails \Resources \MailResource \Pages \ListMails ;
25
+ use Filament \Infolists \Components \RepeatableEntry ;
23
26
use Vormkracht10 \FilamentMails \Resources \MailResource \Pages \ViewMail ;
27
+ use Vormkracht10 \FilamentMails \Resources \MailResource \Pages \ListMails ;
24
28
use Vormkracht10 \FilamentMails \Resources \MailResource \Widgets \MailStatsWidget ;
25
- use Vormkracht10 \Mails \Actions \ResendMail ;
26
- use Vormkracht10 \Mails \Enums \EventType ;
27
- use Vormkracht10 \Mails \Models \Mail ;
28
- use Vormkracht10 \Mails \Models \MailEvent ;
29
29
30
30
class MailResource extends Resource
31
31
{
@@ -84,22 +84,22 @@ public static function infolist(Infolist $infolist): Infolist
84
84
->label (__ ('Subject ' )),
85
85
TextEntry::make ('from ' )
86
86
->label (__ ('From ' ))
87
- ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->from )),
87
+ ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->from )),
88
88
TextEntry::make ('to ' )
89
89
->label (__ ('Recipient ' ))
90
- ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->to )),
90
+ ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->to )),
91
91
TextEntry::make ('cc ' )
92
92
->label (__ ('CC ' ))
93
93
->default ('- ' )
94
- ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->cc ?? [])),
94
+ ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->cc ?? [])),
95
95
TextEntry::make ('bcc ' )
96
96
->label (__ ('BCC ' ))
97
97
->default ('- ' )
98
- ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->bcc ?? [])),
98
+ ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->bcc ?? [])),
99
99
TextEntry::make ('reply_to ' )
100
100
->default ('- ' )
101
101
->label (__ ('Reply To ' ))
102
- ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->reply_to ?? [])),
102
+ ->getStateUsing (fn (Mail $ record ) => self ::formatMailState ($ record ->reply_to ?? [])),
103
103
]),
104
104
]),
105
105
Tab::make (__ ('Statistics ' ))
@@ -171,11 +171,11 @@ public static function infolist(Infolist $infolist): Infolist
171
171
TextEntry::make ('type ' )
172
172
->label (__ ('Type ' ))
173
173
->badge ()
174
- ->url (fn (MailEvent $ record ) => route ('filament. ' . filament ()->getCurrentPanel ()?->getId() . '.resources.mails.events.view ' , [
174
+ ->url (fn (MailEvent $ record ) => route ('filament. ' . filament ()->getCurrentPanel ()?->getId() . '.resources.mails.events.view ' , [
175
175
'record ' => $ record ,
176
176
'tenant ' => filament ()->getTenant ()?->id,
177
177
]))
178
- ->color (fn (EventType $ state ): string => match ($ state ) {
178
+ ->color (fn (EventType $ state ): string => match ($ state ) {
179
179
EventType::DELIVERED => 'success ' ,
180
180
EventType::CLICKED => 'clicked ' ,
181
181
EventType::OPENED => 'info ' ,
@@ -189,7 +189,7 @@ public static function infolist(Infolist $infolist): Infolist
189
189
return ucfirst ($ state ->value );
190
190
}),
191
191
TextEntry::make ('occurred_at ' )
192
- ->url (fn (MailEvent $ record ) => route ('filament. ' . filament ()->getCurrentPanel ()?->getId() . '.resources.mails.events.view ' , [
192
+ ->url (fn (MailEvent $ record ) => route ('filament. ' . filament ()->getCurrentPanel ()?->getId() . '.resources.mails.events.view ' , [
193
193
'record ' => $ record ,
194
194
'tenant ' => filament ()->getTenant ()?->id,
195
195
]))
@@ -219,7 +219,7 @@ public static function infolist(Infolist $infolist): Infolist
219
219
->hiddenLabel ()
220
220
->label (__ ('HTML Content ' ))
221
221
->extraAttributes (['class ' => 'overflow-x-auto ' ])
222
- ->formatStateUsing (fn (string $ state , Mail $ record ): View => view (
222
+ ->formatStateUsing (fn (string $ state , Mail $ record ): View => view (
223
223
'filament-mails::mails.preview ' ,
224
224
['html ' => $ state , 'mail ' => $ record ],
225
225
)),
@@ -229,7 +229,7 @@ public static function infolist(Infolist $infolist): Infolist
229
229
TextEntry::make ('html ' )
230
230
->hiddenLabel ()
231
231
->extraAttributes (['class ' => 'overflow-x-auto ' ])
232
- ->formatStateUsing (fn (string $ state , Mail $ record ): View => view (
232
+ ->formatStateUsing (fn (string $ state , Mail $ record ): View => view (
233
233
'filament-mails::mails.html ' ,
234
234
['html ' => $ state , 'mail ' => $ record ],
235
235
))
@@ -260,12 +260,12 @@ public static function infolist(Infolist $infolist): Infolist
260
260
TextEntry::make ('attachments ' )
261
261
->hiddenLabel ()
262
262
->label (__ ('Attachments ' ))
263
- ->visible (fn (Mail $ record ) => $ record ->attachments ->count () == 0 )
263
+ ->visible (fn (Mail $ record ) => $ record ->attachments ->count () == 0 )
264
264
->default (__ ('Email has no attachments ' )),
265
265
RepeatableEntry::make ('attachments ' )
266
266
->hiddenLabel ()
267
267
->label (__ ('Attachments ' ))
268
- ->visible (fn (Mail $ record ) => $ record ->attachments ->count () > 0 )
268
+ ->visible (fn (Mail $ record ) => $ record ->attachments ->count () > 0 )
269
269
->schema ([
270
270
Grid::make (3 )
271
271
->schema ([
@@ -277,7 +277,7 @@ public static function infolist(Infolist $infolist): Infolist
277
277
->label (__ ('Mime Type ' )),
278
278
ViewEntry::make ('uuid ' )
279
279
->label (__ ('Download ' ))
280
- ->getStateUsing (fn ($ record ) => $ record )
280
+ ->getStateUsing (fn ($ record ) => $ record )
281
281
->view ('filament-mails::mails.download ' ),
282
282
]),
283
283
]),
@@ -297,7 +297,7 @@ public static function table(Table $table): Table
297
297
->label (__ ('Status ' ))
298
298
->sortable ()
299
299
->badge ()
300
- ->color (fn (string $ state ): string => match ($ state ) {
300
+ ->color (fn (string $ state ): string => match ($ state ) {
301
301
__ ('Soft Bounced ' ) => 'warning ' ,
302
302
__ ('Hard Bounced ' ) => 'danger ' ,
303
303
__ ('Complained ' ) => 'danger ' ,
@@ -317,27 +317,27 @@ public static function table(Table $table): Table
317
317
Tables \Columns \IconColumn::make ('attachments ' )
318
318
->label ('' )
319
319
->alignLeft ()
320
- ->getStateUsing (fn (Mail $ record ) => $ record ->attachments ->count () > 0 )
321
- ->icon (fn (string $ state ): string => $ state ? 'heroicon-o-paper-clip ' : '' ),
320
+ ->getStateUsing (fn (Mail $ record ) => $ record ->attachments ->count () > 0 )
321
+ ->icon (fn (string $ state ): string => $ state ? 'heroicon-o-paper-clip ' : '' ),
322
322
Tables \Columns \TextColumn::make ('to ' )
323
323
->label (__ ('Recipient ' ))
324
324
->limit (50 )
325
- ->getStateUsing (fn (Mail $ record ) => self ::formatMailState (emails: $ record ->to , mailOnly: true ))
325
+ ->getStateUsing (fn (Mail $ record ) => self ::formatMailState (emails: $ record ->to , mailOnly: true ))
326
326
->sortable ()
327
327
->searchable (),
328
328
Tables \Columns \TextColumn::make ('opens ' )
329
329
->label (__ ('Opens ' ))
330
- ->tooltip (fn (Mail $ record ) => __ ('Last opened at :date ' , ['date ' => $ record ->last_opened_at ?->format('d-m-Y H:i ' )]))
330
+ ->tooltip (fn (Mail $ record ) => __ ('Last opened at :date ' , ['date ' => $ record ->last_opened_at ?->format('d-m-Y H:i ' )]))
331
331
->sortable (),
332
332
Tables \Columns \TextColumn::make ('clicks ' )
333
333
->label (__ ('Clicks ' ))
334
- ->tooltip (fn (Mail $ record ) => __ ('Last clicked at :date ' , ['date ' => $ record ->last_clicked_at ?->format('d-m-Y H:i ' )]))
334
+ ->tooltip (fn (Mail $ record ) => __ ('Last clicked at :date ' , ['date ' => $ record ->last_clicked_at ?->format('d-m-Y H:i ' )]))
335
335
->sortable (),
336
336
Tables \Columns \TextColumn::make ('sent_at ' )
337
337
->label (__ ('Sent At ' ))
338
338
->dateTime ('d-m-Y H:i ' )
339
339
->since ()
340
- ->tooltip (fn (Mail $ record ) => $ record ->sent_at ?->format('d-m-Y H:i ' ))
340
+ ->tooltip (fn (Mail $ record ) => $ record ->sent_at ?->format('d-m-Y H:i ' ))
341
341
->sortable ()
342
342
->searchable (),
343
343
])
@@ -357,35 +357,16 @@ public static function table(Table $table): Table
357
357
->modalDescription (__ ('Are you sure you want to resend this mail? ' ))
358
358
->hiddenLabel ()
359
359
->tooltip (__ ('Resend ' ))
360
- ->form ([
361
- TextInput::make ('to ' )
362
- ->label (__ ('Recipient ' ))
363
- ->helperText (__ ('You can add multiple email addresses separated by commas. ' ))
364
- ->required (),
365
- TextInput::make ('cc ' )
366
- ->label (__ ('CC ' )),
367
- TextInput::make ('bcc ' )
368
- ->label (__ ('BCC ' )),
369
- ])
360
+ ->form (self ::getResendForm ())
370
361
->fillForm (function (Mail $ record ) {
371
362
return [
372
- 'to ' => implode ( ' , ' , array_keys ($ record ->to ) ),
373
- 'cc ' => is_array ($ record ->cc ) ? implode ( ' , ' , array_keys ($ record ->cc ) ) : null ,
374
- 'bcc ' => is_array ($ record ->bcc ) ? implode ( ' , ' , array_keys ($ record ->bcc ) ) : null ,
363
+ 'to ' => array_keys ($ record ->to ),
364
+ 'cc ' => is_array ($ record ->cc ) ? array_keys ($ record ->cc ) : null ,
365
+ 'bcc ' => is_array ($ record ->bcc ) ? array_keys ($ record ->bcc ) : null ,
375
366
];
376
367
})
377
368
->action (function (Mail $ record , array $ data ) {
378
- $ to = explode (', ' , $ data ['to ' ]);
379
-
380
- if ($ data ['cc ' ]) {
381
- $ data ['cc ' ] = explode (', ' , $ data ['cc ' ]);
382
- }
383
-
384
- if ($ data ['bcc ' ]) {
385
- $ data ['bcc ' ] = explode (', ' , $ data ['bcc ' ]);
386
- }
387
-
388
- (new ResendMail )->handle ($ record , $ to , $ data ['cc ' ] ?? [], $ data ['bcc ' ] ?? []);
369
+ (new ResendMail )->handle ($ record , $ data ['to ' ], $ data ['cc ' ] ?? [], $ data ['bcc ' ] ?? []);
389
370
390
371
Notification::make ()
391
372
->title (__ ('Mail will be resent in the background ' ))
@@ -400,38 +381,10 @@ public static function table(Table $table): Table
400
381
->icon ('heroicon-o-arrow-uturn-right ' )
401
382
->requiresConfirmation ()
402
383
->modalDescription (__ ('Are you sure you want to resend the selected mails? ' ))
403
- ->hiddenLabel ()
404
- ->tooltip (__ ('Resend ' ))
405
- ->form ([
406
- TextInput::make ('to ' )
407
- ->label (__ ('Recipient ' ))
408
- ->helperText (__ ('You can add multiple email addresses separated by commas. ' ))
409
- ->required (),
410
- TextInput::make ('cc ' )
411
- ->label (__ ('CC ' )),
412
- TextInput::make ('bcc ' )
413
- ->label (__ ('BCC ' )),
414
- ])
415
- ->fillForm (function () {
416
- return [
417
- 'to ' => '' ,
418
- 'cc ' => '' ,
419
- 'bcc ' => '' ,
420
- ];
421
- })
384
+ ->form (self ::getResendForm ())
422
385
->action (function (Collection $ records , array $ data ) {
423
386
foreach ($ records as $ record ) {
424
- $ to = explode (', ' , $ data ['to ' ]);
425
-
426
- if ($ data ['cc ' ]) {
427
- $ data ['cc ' ] = explode (', ' , $ data ['cc ' ]);
428
- }
429
-
430
- if ($ data ['bcc ' ]) {
431
- $ data ['bcc ' ] = explode (', ' , $ data ['bcc ' ]);
432
- }
433
-
434
- (new ResendMail )->handle ($ record , $ to , $ data ['cc ' ] ?? [], $ data ['bcc ' ] ?? []);
387
+ (new ResendMail )->handle ($ record , $ data ['to ' ], $ data ['cc ' ] ?? [], $ data ['bcc ' ] ?? []);
435
388
}
436
389
437
390
Notification::make ()
@@ -444,6 +397,22 @@ public static function table(Table $table): Table
444
397
]);
445
398
}
446
399
400
+ private static function getResendForm (): array
401
+ {
402
+ return [
403
+ TagsInput::make ('to ' )
404
+ ->placeholder (__ ('Recipient ' ))
405
+ ->label (__ ('Recipient ' ))
406
+ ->required (),
407
+ TagsInput::make ('cc ' )
408
+ ->placeholder (__ ('Recipient ' ))
409
+ ->label (__ ('CC ' )),
410
+ TagsInput::make ('bcc ' )
411
+ ->placeholder (__ ('Recipient ' ))
412
+ ->label (__ ('BCC ' )),
413
+ ];
414
+ }
415
+
447
416
public static function getPages (): array
448
417
{
449
418
return [
@@ -455,8 +424,8 @@ public static function getPages(): array
455
424
private static function formatMailState (array $ emails , bool $ mailOnly = false ): string
456
425
{
457
426
return collect ($ emails )
458
- ->mapWithKeys (fn ($ value , $ key ) => [$ key => $ value ?? $ key ])
459
- ->map (fn ($ value , $ key ) => $ mailOnly ? $ key : ($ value === null ? $ key : "$ value < $ key> " ))
427
+ ->mapWithKeys (fn ($ value , $ key ) => [$ key => $ value ?? $ key ])
428
+ ->map (fn ($ value , $ key ) => $ mailOnly ? $ key : ($ value === null ? $ key : "$ value < $ key> " ))
460
429
->implode (', ' );
461
430
}
462
431
0 commit comments