ifnex/04_Laravel/app/Filament/Resources/AuditLogResource.php

175 lines
7.7 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\AuditLogResource\Pages;
use App\Models\AuditLog;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
class AuditLogResource extends Resource
{
protected static ?string $model = AuditLog::class;
protected static ?string $navigationIcon = 'heroicon-o-clock';
protected static ?string $navigationGroup = 'گزارش‌ها';
protected static ?string $navigationLabel = 'تاریخچه تغییرات';
protected static ?string $modelLabel = 'لاگ تغییرات';
protected static ?string $pluralModelLabel = 'تاریخچه تغییرات';
protected static ?int $navigationSort = 2;
protected static ?string $navigationBadgeColor = 'warning';
public static function form(Form $form): Form
{
return $form->schema([
Forms\Components\Section::make('اطلاعات لاگ')
->schema([
Forms\Components\Placeholder::make('event')
->label('نوع رویداد')
->content(fn ($record) => match($record->event) {
'created' => '🟢 ایجاد',
'updated' => '🟡 به‌روزرسانی',
'deleted' => '🔴 حذف',
default => $record->event,
}),
Forms\Components\Placeholder::make('user')
->label('کاربر')
->content(fn ($record) => $record->user?->name ?? 'سیستم'),
Forms\Components\Placeholder::make('auditable')
->label('رکورد')
->content(fn ($record) => $record->auditable_type . ' #' . $record->auditable_id),
Forms\Components\Placeholder::make('created_at')
->label('تاریخ و زمان')
->content(fn ($record) => $record->created_at->format('Y-m-d H:i:s')),
])->columns(2),
Forms\Components\Section::make('تغییرات')
->schema([
Forms\Components\Placeholder::make('old_values')
->label('مقادیر قبلی')
->content(fn ($record) => $record->old_values ? json_encode($record->old_values, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : '-'),
Forms\Components\Placeholder::make('new_values')
->label('مقادیر جدید')
->content(fn ($record) => $record->new_values ? json_encode($record->new_values, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : '-'),
])->columns(2),
Forms\Components\Section::make('اطلاعات فنی')
->schema([
Forms\Components\Placeholder::make('description')
->label('توضیحات')
->content(fn ($record) => $record->description ?? '-'),
Forms\Components\Placeholder::make('ip_address')
->label('آدرس IP')
->content(fn ($record) => $record->ip_address ?? '-'),
Forms\Components\Placeholder::make('user_agent')
->label('مرورگر')
->content(fn ($record) => $record->user_agent ?? '-'),
])->columns(2),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id')
->label('شناسه')
->sortable(),
Tables\Columns\TextColumn::make('user.name')
->label('کاربر')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('event')
->label('رویداد')
->formatStateUsing(fn ($state) => match($state) {
'created' => '🟢 ایجاد',
'updated' => '🟡 به‌روزرسانی',
'deleted' => '🔴 حذف',
default => $state,
})
->sortable(),
Tables\Columns\TextColumn::make('auditable_type')
->label('نوع مدل')
->formatStateUsing(fn ($state) => class_basename($state))
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('auditable_id')
->label('شناسه رکورد')
->sortable(),
Tables\Columns\TextColumn::make('description')
->label('توضیحات')
->limit(50)
->wrap(),
Tables\Columns\TextColumn::make('created_at')
->label('تاریخ')
->dateTime()
->sortable(),
])
->filters([
Tables\Filters\SelectFilter::make('event')
->label('نوع رویداد')
->options([
'created' => 'ایجاد',
'updated' => 'به‌روزرسانی',
'deleted' => 'حذف',
]),
Tables\Filters\SelectFilter::make('user_id')
->label('کاربر')
->relationship('user', 'name')
->searchable(),
Tables\Filters\SelectFilter::make('auditable_type')
->label('نوع مدل')
->options([
'App\Models\Shipment' => 'سفارش',
'App\Models\User' => 'کاربر',
'App\Models\Wallet' => 'کیف پول',
'App\Models\WalletTransaction' => 'تراکنش کیف پول',
'App\Models\ShipmentChecklist' => 'چک‌لیست',
]),
Tables\Filters\Filter::make('created_at')
->label('تاریخ ایجاد')
->form([
Forms\Components\DatePicker::make('created_from')
->label('از تاریخ'),
Forms\Components\DatePicker::make('created_until')
->label('تا تاریخ'),
])
->query(function ($query, array $data): Builder {
return $query
->when($data['created_from'], fn ($query, $date) => $query->where('created_at', '>=', $date))
->when($data['created_until'], fn ($query, $date) => $query->where('created_at', '<=', $date));
}),
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->defaultSort('created_at', 'desc');
}
public static function getRelations(): array
{
return [];
}
public static function getPages(): array
{
return [
'index' => Pages\ListAuditLogs::route('/'),
'edit' => Pages\EditAuditLog::route('/{record}/edit'),
];
}
public static function getNavigationBadge(): ?string
{
return static::getModel()::where('created_at', '>=', now()->subDays(7))->count();
}
}