Skip to content

Commit

Permalink
Add resource
Browse files Browse the repository at this point in the history
  • Loading branch information
andreia committed Apr 10, 2024
1 parent e623b7a commit bc11fbb
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 8 deletions.
43 changes: 37 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.


## Installation

You can install the package via composer:
Expand All @@ -33,20 +32,52 @@ This is the contents of the published config file:

```php
return [
'amazon-ses' => [
'configuration-set' => null,
],

'resources' => [
'MaiLogResource' => \Tapp\FilamentMailLog\Resources\MailLogResource::class,
],

'navigation' => [
'maillog' => [
'register' => true,
'sort' => 1,
'icon' => 'heroicon-o-rectangle-stack',
],
],

'sort' => [
'column' => 'created_at',
'direction' => 'desc',
],
];
```

Optionally, you can publish the views using
Optionally, you can publish the translations files with:

```bash
php artisan vendor:publish --tag="filament-maillog-views"
php artisan vendor:publish --tag="filament-authentication-log-translations"
```

## Usage
## Using the Resource

Add this plugin to a panel on `plugins()` method.
E.g. in `app/Providers/Filament/AdminPanelProvider.php`:

```php
$FilamentMailLog = new Tapp\FilamentMailLog();
echo $FilamentMailLog->echoPhrase('Hello, Tapp!');
use Tapp\FilamentMailLog\FilamentMailLogPlugin;

public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugins([
FilamentMailLogPlugin::make(),
//...
]);
}
```

## Testing
Expand Down
17 changes: 17 additions & 0 deletions config/filament-maillog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,21 @@
'amazon-ses' => [
'configuration-set' => null,
],

'resources' => [
'MaiLogResource' => \Tapp\FilamentMailLog\Resources\MailLogResource::class,
],

'navigation' => [
'maillog' => [
'register' => true,
'sort' => 1,
'icon' => 'heroicon-o-rectangle-stack',
],
],

'sort' => [
'column' => 'created_at',
'direction' => 'desc',
],
];
29 changes: 29 additions & 0 deletions resources/lang/en/filament-maillog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return [
'navigation.group' => 'Logs',

'navigation.maillog.label' => 'Mail Log',
'navigation.maillog.plural-label' => 'Mail Logs',

'table.heading' => 'Mail Logs',

'column.status' => 'Status',
'column.date' => 'Date',
'column.subject' => 'Subject',
'column.to' => 'To',
'column.from' => 'From',
'column.cc' => 'CC',
'column.bcc' => 'BCC',
'column.message_id' => 'Message ID',
'column.delivered' => 'Delivered',
'column.opened' => 'Opened',
'column.bounced' => 'Bounced',
'column.complaint' => 'Complaint',
'column.body' => 'Body',
'column.headers' => 'Headers',
'column.attachments' => 'Attachments',
'column.data' => 'Log',
'column.created_at' => 'Created At',
'column.updated_at' => 'Updated At',
];
2 changes: 1 addition & 1 deletion src/Events/MailLogEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function handleMessageSending(MessageSending $event): void
'body' => $message->getHtmlBody(),
'headers' => $message->getHeaders()->toString(),
'attachments' => $this->saveAttachments($message),
'message_id' => Str::uuid(),
'message_id' => (string) Str::uuid(),
]);

if (config('filament-maillog.amazon-ses.configuration-set') !== null) {
Expand Down
32 changes: 32 additions & 0 deletions src/FilamentMailLogPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tapp\FilamentMailLog;

use Filament\Contracts\Plugin;
use Filament\Panel;

class FilamentMailLogPlugin implements Plugin
{
public static function make(): static
{
return app(static::class);
}

public function getId(): string
{
return 'filament-maillog';
}

public function register(Panel $panel): void
{
$panel
->resources(
config('filament-maillog.resources')
);
}

public function boot(Panel $panel): void
{
//
}
}
3 changes: 2 additions & 1 deletion src/FilamentMailLogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public function configurePackage(Package $package): void
->name('filament-maillog')
->hasConfigFile()
->hasViews()
->hasMigration('create_filament-mail_log_table');
->hasTranslations()
->hasMigration('create_filament_mail_log_table');
}

public function packageBooted(): void
Expand Down
185 changes: 185 additions & 0 deletions src/Resources/MailLogResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace Tapp\FilamentMailLog\Resources;

use Tapp\FilamentMailLog\Resources\MailLogResource\Pages;
use Tapp\FilamentMailLog\Models\MailLog;
use Filament\Forms;
use Filament\Infolists;
use Filament\Infolists\Infolist;
use Filament\Resources\Resource;
use Filament\Tables\Table;
use Filament\Tables;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Illuminate\Database\Eloquent\Builder;

class MailLogResource extends Resource
{
protected static ?string $model = MailLog::class;

public static function shouldRegisterNavigation(): bool
{
return config('filament-maillog.navigation.maillog.register', true);
}

public static function getNavigationIcon(): string
{
return config('filament-maillog.navigation.maillog.icon');
}

public static function getNavigationSort(): ?int
{
return config('filament-maillog.navigation.maillog.sort');
}

public static function getNavigationGroup(): ?string
{
return __('filament-maillog::filament-maillog.navigation.group');
}

public static function getLabel(): string
{
return __('filament-maillog::filament-maillog.navigation.maillog.label');
}

public static function getPluralLabel(): string
{
return __('filament-maillog::filament-maillog.navigation.maillog.plural-label');
}

public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Infolists\Components\TextEntry::make('message_id')
->label(trans('filament-maillog::filament-maillog.column.message_id')),
Infolists\Components\TextEntry::make('subject')
->label(trans('filament-maillog::filament-maillog.column.subject')),
Infolists\Components\TextEntry::make('date')
->label(trans('filament-maillog::filament-maillog.column.date'))
->datetime(),
Infolists\Components\TextEntry::make('to')
->label(trans('filament-maillog::filament-maillog.column.to')),
Infolists\Components\TextEntry::make('from')
->label(trans('filament-maillog::filament-maillog.column.from')),
Infolists\Components\TextEntry::make('cc')
->label(trans('filament-maillog::filament-maillog.column.cc')),
Infolists\Components\TextEntry::make('bcc')
->label(trans('filament-maillog::filament-maillog.column.bcc')),
Infolists\Components\TextEntry::make('status')
->label(trans('filament-maillog::filament-maillog.column.status'))
->badge(),
Infolists\Components\TextEntry::make('delivered')
->label(trans('filament-maillog::filament-maillog.column.delivered')),
Infolists\Components\TextEntry::make('opened')
->label(trans('filament-maillog::filament-maillog.column.opened')),
Infolists\Components\TextEntry::make('bounced')
->label(trans('filament-maillog::filament-maillog.column.bounced')),
Infolists\Components\TextEntry::make('complaint')
->label(trans('filament-maillog::filament-maillog.column.complaint')),
Infolists\Components\TextEntry::make('body')
->label(trans('filament-maillog::filament-maillog.column.body'))
->html()
->columnSpanFull(),
Infolists\Components\TextEntry::make('headers')
->label(trans('filament-maillog::filament-maillog.column.headers'))
->columnSpanFull(),
Infolists\Components\TextEntry::make('attachments')
->label(trans('filament-maillog::filament-maillog.column.attachments'))
->columnSpanFull(),
Infolists\Components\Section::make('Log')
->label(trans('filament-maillog::filament-maillog.column.data'))
->icon('heroicon-m-list-bullet')
->schema([
Infolists\Components\TextEntry::make('data')
->label(false),
])
->columnSpanFull(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('status')
->label(trans('filament-maillog::filament-maillog.column.status'))
->sortable(),
Tables\Columns\TextColumn::make('date')
->label(trans('filament-maillog::filament-maillog.column.date'))
->dateTime()
->sortable(),
Tables\Columns\TextColumn::make('subject')
->label(trans('filament-maillog::filament-maillog.column.subject'))
->limit(25)
->tooltip(function (Tables\Columns\TextColumn $column): ?string {
$state = $column->getState();

if (strlen($state) <= $column->getCharacterLimit()) {
return null;
}

// Only render the tooltip if the column content exceeds the length limit.
return $state;
})
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('to')
->label(trans('filament-maillog::filament-maillog.column.to'))
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('from')
->label(trans('filament-maillog::filament-maillog.column.from'))
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->label(trans('filament-maillog::filament-maillog.column.created_at'))
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->label(trans('filament-maillog::filament-maillog.column.updated_at'))
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
SelectFilter::make('status')
->options(MailLog::distinct('status')->pluck('status', 'status')->filter()->toArray()),
Filter::make('created_at')
->form([
Forms\Components\DatePicker::make('created_from'),
Forms\Components\DatePicker::make('created_until'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
)
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
);
}),
])
->actions([
Tables\Actions\ViewAction::make(),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListMailLogs::route('/'),
//'view' => Pages\ViewMailLog::route('/{record}'),
];
}
}
11 changes: 11 additions & 0 deletions src/Resources/MailLogResource/Pages/ListMailLogs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tapp\FilamentMailLog\Resources\MailLogResource\Pages;

use Tapp\FilamentMailLog\Resources\MailLogResource;
use Filament\Resources\Pages\ListRecords;

class ListMailLogs extends ListRecords
{
protected static string $resource = MailLogResource::class;
}
11 changes: 11 additions & 0 deletions src/Resources/MailLogResource/Pages/ViewMailLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tapp\FilamentMailLog\Resources\MailLogResource\Pages;

use Filament\Resources\Pages\ViewRecord;
use Tapp\FilamentMailLog\Resources\MailLogResource;

class ViewMailLog extends ViewRecord
{
protected static string $resource = MailLogResource::class;
}

0 comments on commit bc11fbb

Please sign in to comment.