Files
nexusphp/app/Filament/Resources/Torrent/TorrentResource.php

475 lines
19 KiB
PHP
Raw Normal View History

2022-07-03 14:00:07 +08:00
<?php
namespace App\Filament\Resources\Torrent;
2022-07-05 15:53:06 +08:00
use App\Filament\OptionsTrait;
2022-07-03 14:00:07 +08:00
use App\Filament\Resources\Torrent\TorrentResource\Pages;
use App\Filament\Resources\Torrent\TorrentResource\RelationManagers;
2022-11-05 18:43:49 +08:00
use App\Models\Category;
use App\Models\SearchBox;
use App\Models\Setting;
2022-07-03 14:00:07 +08:00
use App\Models\Tag;
use App\Models\Torrent;
use App\Models\TorrentTag;
2022-10-02 21:14:11 +08:00
use App\Models\User;
2022-07-03 14:00:07 +08:00
use App\Repositories\TagRepository;
use App\Repositories\TorrentRepository;
2022-07-19 14:15:35 +08:00
use Filament\Facades\Filament;
2022-07-03 14:00:07 +08:00
use Filament\Forms;
2024-12-17 13:12:58 +08:00
use Filament\Pages\Actions\Action;
2024-12-25 23:09:07 +08:00
use Filament\Forms\Form;
2022-07-03 14:00:07 +08:00
use Filament\Resources\Resource;
2024-12-25 23:09:07 +08:00
use Filament\Tables\Table;
2022-07-03 14:00:07 +08:00
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
2022-07-04 04:03:11 +08:00
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
2022-10-02 21:14:11 +08:00
use Illuminate\Validation\Rule;
use Nexus\Database\NexusDB;
2022-07-03 14:00:07 +08:00
class TorrentResource extends Resource
{
2022-07-05 15:53:06 +08:00
use OptionsTrait;
2022-07-03 14:00:07 +08:00
protected static ?string $model = Torrent::class;
2024-12-25 23:09:07 +08:00
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
2022-07-03 14:00:07 +08:00
protected static ?string $navigationGroup = 'Torrent';
protected static ?int $navigationSort = 1;
2022-08-26 17:35:49 +08:00
private static ?TorrentRepository $rep;
2024-12-25 23:09:07 +08:00
public static function getNavigationLabel(): string
2022-07-03 14:00:07 +08:00
{
return __('admin.sidebar.torrent_list');
}
public static function getBreadcrumb(): string
{
return self::getNavigationLabel();
}
public static function form(Form $form): Form
{
return $form
->schema([
//
]);
}
2022-08-26 17:35:49 +08:00
public static function getRep(): ?TorrentRepository
{
if (self::$rep === null) {
self::$rep = new TorrentRepository();
}
return self::$rep;
}
2022-07-03 14:00:07 +08:00
public static function table(Table $table): Table
{
$showApproval = self::shouldShowApproval();
2022-07-03 14:00:07 +08:00
return $table
->columns([
Tables\Columns\TextColumn::make('id')->sortable(),
2022-07-04 04:21:31 +08:00
Tables\Columns\TextColumn::make('basic_category.name')->label(__('label.torrent.category')),
Tables\Columns\TextColumn::make('name')->formatStateUsing(fn ($record) => torrent_name_for_admin($record, true))
->label(__('label.name'))
->searchable(query: function (Builder $query, string $search) {
return $query->where("name", "like", "%{$search}%")->orWhere("small_descr", "like", "%{$search}%");
}),
2022-07-04 04:21:31 +08:00
Tables\Columns\TextColumn::make('posStateText')->label(__('label.torrent.pos_state')),
Tables\Columns\TextColumn::make('spStateText')->label(__('label.torrent.sp_state')),
2022-10-02 21:14:11 +08:00
Tables\Columns\TextColumn::make('pickInfoText')
->label(__('label.torrent.picktype'))
->formatStateUsing(fn ($record) => $record->pickInfo['text'])
,
Tables\Columns\IconColumn::make('hr')
2022-10-02 21:14:11 +08:00
->label(__('label.torrent.hr'))
->boolean()
2022-10-02 21:14:11 +08:00
,
Tables\Columns\TextColumn::make('size')
->label(__('label.torrent.size'))
->formatStateUsing(fn ($state) => mksize($state))
->sortable()
,
Tables\Columns\TextColumn::make('seeders')->label(__('label.torrent.seeders'))->sortable(),
Tables\Columns\TextColumn::make('leechers')->label(__('label.torrent.leechers'))->sortable(),
2022-07-03 14:00:07 +08:00
Tables\Columns\BadgeColumn::make('approval_status')
->visible($showApproval)
2022-07-03 14:00:07 +08:00
->label(__('label.torrent.approval_status'))
->colors(array_flip(Torrent::listApprovalStatus(true, 'badge_color')))
->formatStateUsing(fn ($record) => $record->approvalStatusText),
Tables\Columns\TextColumn::make('added')->label(__('label.added'))->dateTime(),
Tables\Columns\TextColumn::make('user.username')
2022-07-04 14:41:27 +08:00
->label(__('label.torrent.owner'))
->formatStateUsing(fn ($record) => username_for_admin($record->owner))
2022-07-03 14:00:07 +08:00
,
])
->defaultSort('id', 'desc')
->filters(self::getFilters())
->actions(self::getActions())
2024-12-17 13:12:58 +08:00
->bulkActions(self::getBulkActions())
;
2022-07-03 14:00:07 +08:00
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with(['user', 'basic_category', 'tags']);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListTorrents::route('/'),
'create' => Pages\CreateTorrent::route('/create'),
'edit' => Pages\EditTorrent::route('/{record}/edit'),
];
}
2022-07-04 04:03:11 +08:00
private static function getBulkActions(): array
{
2022-10-02 21:14:11 +08:00
$user = Auth::user();
$actions = [];
2022-08-20 21:07:29 +08:00
if (user_can('torrentsticky')) {
$actions[] = Tables\Actions\BulkAction::make('posState')
->label(__('admin.resources.torrent.bulk_action_pos_state'))
->form([
Forms\Components\Select::make('pos_state')
->label(__('label.torrent.pos_state'))
->options(Torrent::listPosStates(true))
->required()
2022-09-18 03:33:13 +08:00
,
Forms\Components\DateTimePicker::make('pos_state_until')
->label(__('label.deadline'))
,
])
2024-12-25 23:09:07 +08:00
->icon('heroicon-o-arrow-up-circle')
->action(function (Collection $records, array $data) {
$idArr = $records->pluck('id')->toArray();
2022-07-19 14:15:35 +08:00
try {
$torrentRep = new TorrentRepository();
2022-09-18 03:33:13 +08:00
$torrentRep->setPosState($idArr, $data['pos_state'], $data['pos_state_until']);
2022-07-19 14:15:35 +08:00
} catch (\Exception $exception) {
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
2022-07-19 14:15:35 +08:00
Filament::notify('danger', class_basename($exception));
}
})
->deselectRecordsAfterCompletion();
}
2022-10-02 21:14:11 +08:00
if (user_can('torrentonpromotion')) {
$actions[] = Tables\Actions\BulkAction::make('sp_state')
->label(__('admin.resources.torrent.bulk_action_sp_state'))
->form([
Forms\Components\Select::make('sp_state')
->label(__('label.torrent.sp_state'))
->options(Torrent::listPromotionTypes(true))
->required()
,
Forms\Components\Select::make('promotion_time_type')
->label(__('label.torrent.promotion_time_type'))
->options(Torrent::listPromotionTimeTypes(true))
->required()
,
Forms\Components\DateTimePicker::make('promotion_until')
->label(__('label.deadline'))
,
])
2024-12-25 23:09:07 +08:00
->icon('heroicon-o-megaphone')
2022-10-02 21:14:11 +08:00
->action(function (Collection $records, array $data) {
$idArr = $records->pluck('id')->toArray();
try {
$torrentRep = new TorrentRepository();
$torrentRep->setSpState($idArr, $data['sp_state'], $data['promotion_time_type'], $data['promotion_until']);
} catch (\Exception $exception) {
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
Filament::notify('danger', $exception->getMessage());
}
})
->deselectRecordsAfterCompletion();
}
if (user_can('torrentmanage') && ($user->picker == 'yes' || $user->class >= User::CLASS_SYSOP)) {
$actions[] = Tables\Actions\BulkAction::make('recommend')
->label(__('admin.resources.torrent.bulk_action_recommend'))
->form([
Forms\Components\Radio::make('picktype')
->label(__('admin.resources.torrent.bulk_action_recommend'))
->inline()
->options(Torrent::listPickInfo(true))
->required(),
])
->icon('heroicon-o-fire')
->action(function (Collection $records, array $data) {
if (empty($data['picktype'])) {
return;
}
$idArr = $records->pluck('id')->toArray();
try {
$torrentRep = new TorrentRepository();
$torrentRep->setPickType($idArr, $data['picktype']);
} catch (\Exception $exception) {
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
Filament::notify('danger', class_basename($exception));
}
})
->deselectRecordsAfterCompletion();
}
2022-08-20 21:07:29 +08:00
if (user_can('torrentmanage')) {
$actions[] = Tables\Actions\BulkAction::make('remove_tag')
->label(__('admin.resources.torrent.bulk_action_remove_tag'))
->requiresConfirmation()
->icon('heroicon-o-minus-circle')
->action(function (Collection $records) {
$idArr = $records->pluck('id')->toArray();
2022-07-19 14:15:35 +08:00
try {
$torrentRep = new TorrentRepository();
$torrentRep->syncTags($idArr);
} catch (\Exception $exception) {
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
2022-07-19 14:15:35 +08:00
Filament::notify('danger', class_basename($exception));
}
})
->deselectRecordsAfterCompletion();
$actions[] = Tables\Actions\BulkAction::make('attach_tag')
->label(__('admin.resources.torrent.bulk_action_attach_tag'))
->form([
Forms\Components\Checkbox::make('remove')->label(__('admin.resources.torrent.bulk_action_attach_tag_remove_old')),
Forms\Components\CheckboxList::make('tags')
->label(__('label.tag.label'))
->columns(4)
->options(TagRepository::createBasicQuery()->pluck('name', 'id')->toArray())
->required(),
])
->icon('heroicon-o-tag')
->action(function (Collection $records, array $data) {
if (empty($data['tags'])) {
return;
}
2022-07-19 14:15:35 +08:00
$idArr = $records->pluck('id')->toArray();
try {
$torrentRep = new TorrentRepository();
$torrentRep->syncTags($idArr, $data['tags'], $data['remove'] ?? false);
2022-07-19 14:15:35 +08:00
} catch (\Exception $exception) {
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
2022-07-19 14:15:35 +08:00
Filament::notify('danger', class_basename($exception));
}
})
->deselectRecordsAfterCompletion();
2022-10-02 21:14:11 +08:00
$actions[] = Tables\Actions\BulkAction::make('hr')
->label(__('admin.resources.torrent.bulk_action_hr'))
->form([
Forms\Components\Radio::make('hr')
->label(__('admin.resources.torrent.bulk_action_hr'))
->inline()
->options(self::getYesNoOptions())
->required(),
])
->icon('heroicon-o-sparkles')
->action(function (Collection $records, array $data) {
2024-05-08 02:36:52 +08:00
if (!isset($data['hr'])) {
2022-10-02 21:14:11 +08:00
return;
}
$idArr = $records->pluck('id')->toArray();
try {
$torrentRep = new TorrentRepository();
$torrentRep->setHr($idArr, $data['hr']);
} catch (\Exception $exception) {
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
Filament::notify('danger', class_basename($exception));
}
})
->deselectRecordsAfterCompletion();
}
if (user_can('torrent-delete')) {
$actions[] = Tables\Actions\DeleteBulkAction::make('bulk-delete')->using(function (Collection $records) {
deletetorrent($records->pluck('id')->toArray());
});
}
return $actions;
}
2022-07-19 14:15:35 +08:00
private static function getActions(): array
{
$actions = [];
2022-08-26 17:35:49 +08:00
if (self::shouldShowApproval() && user_can('torrent-approval')) {
$actions[] = Tables\Actions\Action::make('approval')
->label(__('admin.resources.torrent.action_approval'))
->form([
Forms\Components\Radio::make('approval_status')
->label(__('label.torrent.approval_status'))
->inline()
->required()
->options(Torrent::listApprovalStatus(true))
,
Forms\Components\Textarea::make('comment')->label(__('label.comment')),
])
->action(function (Torrent $record, array $data) {
$torrentRep = new TorrentRepository();
try {
$data['torrent_id'] = $record->id;
$torrentRep->approval(Auth::user(), $data);
} catch (\Exception $exception) {
do_log($exception->getMessage(), 'error');
}
});
2022-08-26 17:35:49 +08:00
}
2022-10-02 21:14:11 +08:00
if (user_can('torrent-delete')) {
2022-08-26 17:35:49 +08:00
$actions[] = Tables\Actions\DeleteAction::make('delete')->using(function ($record) {
deletetorrent($record->id);
});
2024-12-17 13:12:58 +08:00
// $actions[] = Tables\Actions\Action::make('view')
// ->action(function (Torrent $record) {
// return [
// 'modelContent' => new HtmlString("ssss")
// ];
// })
// ;
}
return $actions;
}
private static function shouldShowApproval(): bool
{
2022-11-05 18:43:49 +08:00
return false;
// return Setting::get('torrent.approval_status_none_visible') == 'no' || Setting::get('torrent.approval_status_icon_enabled') == 'yes';
}
private static function getFilters()
{
$filters = [
Tables\Filters\Filter::make('owner')
->form([
Forms\Components\TextInput::make('owner')
->label(__('label.torrent.owner'))
->placeholder('UID')
,
])->query(function (Builder $query, array $data) {
return $query->when($data['owner'], fn (Builder $query, $owner) => $query->where("owner", $owner));
})
,
Tables\Filters\SelectFilter::make('visible')
->options(self::$yesOrNo)
->label(__('label.torrent.visible'))
,
Tables\Filters\SelectFilter::make('hr')
->options(self::getYesNoOptions())
->label(__('label.torrent.hr'))
,
Tables\Filters\SelectFilter::make('pos_state')
->options(Torrent::listPosStates(true))
->label(__('label.torrent.pos_state'))
->multiple()
,
Tables\Filters\SelectFilter::make('sp_state')
->options(Torrent::listPromotionTypes(true))
->label(__('label.torrent.sp_state'))
->multiple()
,
Tables\Filters\SelectFilter::make('picktype')
->options(Torrent::listPickInfo(true))
->label(__('label.torrent.picktype'))
->multiple()
,
Tables\Filters\SelectFilter::make('approval_status')
->options(Torrent::listApprovalStatus(true))
->label(__('label.torrent.approval_status'))
->multiple()
,
Tables\Filters\SelectFilter::make('tags')
->relationship('tags', 'name')
->label(__('label.tag.label'))
->multiple()
,
Tables\Filters\SelectFilter::make('category')
->options(Category::query()->pluck('name', 'id')->toArray())
->label(__('label.torrent.category'))
->multiple()
,
];
foreach (SearchBox::$taxonomies as $torrentField => $tableModel) {
$filters[] = Tables\Filters\SelectFilter::make($torrentField)
->options(NexusDB::table($tableModel['table'])->orderBy('sort_index')->orderBy('id')->pluck('name', 'id'))
->multiple()
;
}
$filters[] = Tables\Filters\Filter::make('added_begin')
->form([
Forms\Components\DatePicker::make('added_begin')
->maxDate(now())
->label(__('label.torrent.added_begin'))
,
])->query(function (Builder $query, array $data) {
return $query->when($data['added_begin'], fn (Builder $query, $value) => $query->where("added", '>=', $value));
})
;
$filters[] = Tables\Filters\Filter::make('added_end')
->form([
Forms\Components\DatePicker::make('added_end')
->maxDate(now())
->label(__('label.torrent.added_end'))
,
])->query(function (Builder $query, array $data) {
return $query->when($data['added_end'], fn (Builder $query, $value) => $query->where("added", '<=', $value));
})
;
$filters[] = Tables\Filters\Filter::make('size_begin')
->form([
Forms\Components\TextInput::make('size_begin')
->numeric()
->placeholder('GB')
->label(__('label.torrent.size_begin'))
,
])->query(function (Builder $query, array $data) {
return $query->when($data['size_begin'], fn (Builder $query, $value) => $query->where("size", '>=', $value * 1024 * 1024 * 1024));
})
;
$filters[] = Tables\Filters\Filter::make('size_end')
->form([
Forms\Components\TextInput::make('size_end')
->numeric()
->placeholder('GB')
->label(__('label.torrent.size_end'))
,
])->query(function (Builder $query, array $data) {
return $query->when($data['size_end'], fn (Builder $query, $value) => $query->where("size", '<=', $value * 1024 * 1024 * 1024));
})
;
return $filters;
}
2022-07-03 14:00:07 +08:00
}