From 4de032635c3ce2f2ccc72966702c084c82981743 Mon Sep 17 00:00:00 2001 From: danilopolani Date: Tue, 20 Feb 2024 11:53:42 +0100 Subject: [PATCH 1/2] add tags to templates --- .../9_add_tags_to_templates_table.php | 27 +++++++++++++ src/MailCarrierServiceProvider.php | 1 + src/Models/Template.php | 3 ++ src/Resources/LogResource.php | 3 +- src/Resources/TemplateResource.php | 40 ++++++++++++++----- 5 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 database/migrations/9_add_tags_to_templates_table.php diff --git a/database/migrations/9_add_tags_to_templates_table.php b/database/migrations/9_add_tags_to_templates_table.php new file mode 100644 index 0000000..d5229eb --- /dev/null +++ b/database/migrations/9_add_tags_to_templates_table.php @@ -0,0 +1,27 @@ +json('tags')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('templates', function (Blueprint $table) { + $table->dropColumn('tags'); + }); + } +}; diff --git a/src/MailCarrierServiceProvider.php b/src/MailCarrierServiceProvider.php index bc2c90e..b7c5aa4 100644 --- a/src/MailCarrierServiceProvider.php +++ b/src/MailCarrierServiceProvider.php @@ -75,6 +75,7 @@ public function configurePackage(Package $package): void '6_transform_logs_cc_bcc_array', '7_add_tries_to_logs_table', '8_add_tags_metadata_to_logs_table', + '9_add_tags_to_templates_table', ]) ->runsMigrations(); } diff --git a/src/Models/Template.php b/src/Models/Template.php index 2069ac3..c046021 100644 --- a/src/Models/Template.php +++ b/src/Models/Template.php @@ -15,6 +15,7 @@ * @property string $name * @property string $slug * @property string $content + * @property array|null $tags * @property-read \MailCarrier\Models\User|null $user * @property-read \MailCarrier\Models\Layout|null $layout */ @@ -34,6 +35,7 @@ class Template extends Model 'name', 'slug', 'content', + 'tags', ]; /** @@ -50,6 +52,7 @@ class Template extends Model */ protected $casts = [ 'is_locked' => 'boolean', + 'tags' => 'array', ]; /** diff --git a/src/Resources/LogResource.php b/src/Resources/LogResource.php index e19c324..90b2bc4 100644 --- a/src/Resources/LogResource.php +++ b/src/Resources/LogResource.php @@ -10,7 +10,6 @@ use Filament\Support\Enums\Alignment; use Filament\Tables; use Filament\Tables\Actions\Action as TablesAction; -use Filament\Tables\Enums\FiltersLayout; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\View; @@ -90,7 +89,7 @@ public static function table(Tables\Table $table): Tables\Table ]) ->poll(Config::get('mailcarrier.logs.table_refresh_poll', '5s')) ->recordAction('details') - ->filters(static::getTableFilters(), layout: FiltersLayout::Modal) + ->filters(static::getTableFilters()) ->filtersTriggerAction( fn (TablesAction $action) => $action ->button() diff --git a/src/Resources/TemplateResource.php b/src/Resources/TemplateResource.php index 58863a9..b1d5a72 100644 --- a/src/Resources/TemplateResource.php +++ b/src/Resources/TemplateResource.php @@ -6,14 +6,13 @@ use Filament\Forms\Form; use Filament\Resources\Resource; use Filament\Tables; +use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\URL; use Illuminate\Support\HtmlString; use MailCarrier\Actions\Templates\GenerateSlug; use MailCarrier\Forms\Components\CodeEditor; -use MailCarrier\Models\Layout; use MailCarrier\Models\Template; -use MailCarrier\Models\User; use MailCarrier\Resources\TemplateResource\Pages; use RalphJSmit\Filament\Components\Forms\Timestamps; @@ -47,18 +46,14 @@ public static function table(Tables\Table $table): Tables\Table ->searchable() ->sortable(), - Tables\Columns\TextColumn::make('layout') - ->label('Layout') - ->formatStateUsing(fn (?Layout $state) => $state?->name ?: '-'), - - Tables\Columns\TextColumn::make('user') - ->label('User') - ->formatStateUsing(fn (?User $state) => $state?->getFilamentName() ?: '-'), + Tables\Columns\TextColumn::make('tags') + ->badge(), Tables\Columns\TextColumn::make('created_at') ->dateTime() ->sortable(), ]) + ->filters(static::getTableFilters()) ->actions([ Tables\Actions\EditAction::make(), Tables\Actions\ReplicateAction::make() @@ -101,6 +96,31 @@ public static function getPages(): array ]; } + /** + * Get the table filters. + */ + protected static function getTableFilters(): array + { + return [ + Tables\Filters\SelectFilter::make('tags') + ->searchable() + ->options( + Template::query() + ->pluck('tags') + ->unique() + ->flatten() + ->filter() + ->mapWithKeys(fn (string $tag) => [$tag => $tag]) + ) + ->query(function (Builder $query, array $data): Builder { + return $query->when( + !is_null($data['value'] ?? null), + fn (Builder $query) => $query->whereJsonContains('tags', $data['value']) + ); + }), + ]; + } + /** * Get the form content. */ @@ -175,6 +195,8 @@ protected static function getFormSidebar(): Forms\Components\Section HTML); }), + Forms\Components\TagsInput::make('tags'), + Forms\Components\Placeholder::make('Separator') ->label('') ->content(new HtmlString('
')), From 29267df60a1483e4e7663de92fa409b158a5f865 Mon Sep 17 00:00:00 2001 From: danilopolani Date: Tue, 20 Feb 2024 11:55:44 +0100 Subject: [PATCH 2/2] transform function into arrow function --- src/Resources/TemplateResource.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Resources/TemplateResource.php b/src/Resources/TemplateResource.php index b1d5a72..6d77dcf 100644 --- a/src/Resources/TemplateResource.php +++ b/src/Resources/TemplateResource.php @@ -112,12 +112,10 @@ protected static function getTableFilters(): array ->filter() ->mapWithKeys(fn (string $tag) => [$tag => $tag]) ) - ->query(function (Builder $query, array $data): Builder { - return $query->when( - !is_null($data['value'] ?? null), - fn (Builder $query) => $query->whereJsonContains('tags', $data['value']) - ); - }), + ->query(fn (Builder $query, array $data): Builder => $query->when( + !is_null($data['value'] ?? null), + fn (Builder $query) => $query->whereJsonContains('tags', $data['value']) + )), ]; }