Files
nexusphp/app/Filament/Resources/User/HitAndRunResource.php

196 lines
7.5 KiB
PHP
Raw Normal View History

2022-06-27 01:39:01 +08:00
<?php
namespace App\Filament\Resources\User;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Filters\SelectFilter;
use Filament\Forms\Components\DatePicker;
use Filament\Actions\ViewAction;
use Filament\Actions\BulkAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Schemas\Schema;
use Filament\Infolists\Components\TextEntry;
use App\Filament\Resources\User\HitAndRunResource\Pages\ListHitAndRuns;
use App\Filament\Resources\User\HitAndRunResource\Pages\ViewHitAndRun;
2022-06-27 01:39:01 +08:00
use App\Filament\Resources\User\HitAndRunResource\Pages;
use App\Filament\Resources\User\HitAndRunResource\RelationManagers;
use App\Models\HitAndRun;
2025-01-27 14:07:44 +08:00
use App\Models\User;
2022-06-27 01:39:01 +08:00
use App\Repositories\HitAndRunRepository;
use Filament\Forms;
2024-12-25 23:09:07 +08:00
use Filament\Forms\Form;
2022-06-27 01:39:01 +08:00
use Filament\Resources\Resource;
2024-12-25 23:09:07 +08:00
use Filament\Tables\Table;
2022-06-27 01:39:01 +08:00
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\HtmlString;
2025-01-27 14:07:44 +08:00
use Filament\Infolists\Components;
use Filament\Infolists;
2025-07-21 20:55:30 +07:00
use Nette\Utils\Html;
2022-06-27 01:39:01 +08:00
class HitAndRunResource extends Resource
{
protected static ?string $model = HitAndRun::class;
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-beaker';
2022-06-27 01:39:01 +08:00
protected static string | \UnitEnum | null $navigationGroup = 'User';
2022-06-27 01:39:01 +08:00
2022-07-02 15:08:23 +08:00
protected static ?int $navigationSort = 3;
2024-12-25 23:09:07 +08:00
public static function getNavigationLabel(): string
2022-06-27 01:39:01 +08:00
{
return __('admin.sidebar.hit_and_runs');
}
2022-06-29 17:00:15 +08:00
public static function getBreadcrumb(): string
{
return self::getNavigationLabel();
}
2022-06-27 01:39:01 +08:00
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')->sortable(),
TextColumn::make('uid')->searchable(),
TextColumn::make('user.username')
->searchable()
->label(__('label.username'))
2022-10-07 02:51:52 +08:00
->formatStateUsing(fn ($record) => new HtmlString(get_username($record->uid, false, true, true, true)))
,
TextColumn::make('torrent.name')->limit(30)->label(__('label.torrent.label')),
TextColumn::make('snatch.uploadText')->label(__('label.uploaded')),
TextColumn::make('snatch.downloadText')->label(__('label.downloaded')),
TextColumn::make('snatch.shareRatio')->label(__('label.ratio')),
TextColumn::make('seedTimeRequired')->label(__('label.seed_time_required')),
TextColumn::make('inspectTimeLeft')->label(__('label.inspect_time_left')),
TextColumn::make('statusText')->label(__('label.status')),
TextColumn::make('created_at')->label(__('label.created_at')),
2022-06-27 01:39:01 +08:00
])
2022-07-02 16:36:44 +08:00
->defaultSort('id', 'desc')
2022-06-27 01:39:01 +08:00
->filters([
Filter::make('uid')
->schema([
TextInput::make('uid')
->label('UID')
->placeholder('UID')
,
])
->query(function (Builder $query, array $data) {
return $query->when($data['uid'], fn (Builder $query, $uid) => $query->where("uid", $uid));
})
,
SelectFilter::make('status')->options(HitAndRun::listStatus(true))->label(__('label.status')),
Filter::make('created_at_begin')
->schema([
DatePicker::make('created_at_begin')
->maxDate(now())
->label(__('label.created_at_begin'))
,
])->query(function (Builder $query, array $data) {
return $query->when($data['created_at_begin'], fn (Builder $query, $value) => $query->where("created_at", '>=', $value));
})
,
Filter::make('created_at_end')
->schema([
DatePicker::make('created_at_end')
->maxDate(now())
->label(__('label.created_at_end'))
,
])->query(function (Builder $query, array $data) {
return $query->when($data['created_at_end'], fn (Builder $query, $value) => $query->where("created_at", '<=', $value));
})
,
2022-06-27 01:39:01 +08:00
])
->recordActions([
ViewAction::make(),
2022-06-27 01:39:01 +08:00
])
->groupedBulkActions([
BulkAction::make('Pardon')->action(function (Collection $records) {
2022-06-27 01:39:01 +08:00
$idArr = $records->pluck('id')->toArray();
$rep = new HitAndRunRepository();
$rep->bulkPardon(['id' => $idArr], Auth::user());
})
->deselectRecordsAfterCompletion()
2022-07-02 15:08:23 +08:00
->label(__('admin.resources.hit_and_run.bulk_action_pardon'))
2024-12-25 23:09:07 +08:00
->icon('heroicon-o-x-mark')
,
DeleteBulkAction::make('bulkDelete')
2022-06-27 01:39:01 +08:00
]);
}
public static function infolist(Schema $schema): Schema
2025-01-27 14:07:44 +08:00
{
return $schema
->components([
TextEntry::make('id'),
TextEntry::make('statusText')
2025-01-27 14:07:44 +08:00
->label(__("label.status"))
,
TextEntry::make('uid')
2025-01-27 14:07:44 +08:00
->formatStateUsing(fn ($record) => username_for_admin($record->uid))
->label(__("label.username"))
,
TextEntry::make('torrent_id')
2025-01-27 14:07:44 +08:00
->formatStateUsing(fn ($record) => $record->torrent->name)
->label(__("label.torrent.label"))
,
TextEntry::make('snatch.uploadedText')
2025-01-27 14:07:44 +08:00
->label(__("label.uploaded"))
,
TextEntry::make('snatch.downloadedText')
2025-01-27 14:07:44 +08:00
->label(__("label.downloaded"))
,
TextEntry::make('snatch.shareRatio')
2025-01-27 14:07:44 +08:00
->label(__("label.ratio"))
,
TextEntry::make('seedTimeRequired')
2025-01-27 14:07:44 +08:00
->label(__("label.seed_time_required"))
,
TextEntry::make('inspectTimeLeft')
2025-01-27 14:07:44 +08:00
->label(__("label.inspect_time_left"))
,
TextEntry::make('comment')
2025-07-21 20:55:30 +07:00
->formatStateUsing(fn ($record) => new HtmlString(nl2br($record->comment)))
2025-01-27 14:07:44 +08:00
->label(__("label.comment"))
,
TextEntry::make('created_at')
2025-01-27 14:07:44 +08:00
->label(__("label.created_at"))
,
TextEntry::make('updated_at')
2025-01-27 14:07:44 +08:00
->label(__("label.updated_at"))
,
])->columns(4);
}
2022-07-02 16:58:07 +08:00
public static function getEloquentQuery(): Builder
{
2022-10-01 01:58:18 +08:00
return parent::getEloquentQuery()->with(['user', 'torrent', 'snatch', 'torrent.basic_category']);
2022-07-02 16:58:07 +08:00
}
2022-06-27 01:39:01 +08:00
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => ListHitAndRuns::route('/'),
2022-06-27 01:39:01 +08:00
// 'create' => Pages\CreateHitAndRun::route('/create'),
// 'edit' => Pages\EditHitAndRun::route('/{record}/edit'),
'view' => ViewHitAndRun::route('/{record}'),
2022-06-27 01:39:01 +08:00
];
}
}