Files

535 lines
21 KiB
PHP
Raw Permalink Normal View History

2022-07-03 14:00:07 +08:00
<?php
namespace App\Filament\Resources\Torrent;
2025-09-21 18:07:38 +08:00
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\BadgeColumn;
use App\Filament\Resources\Torrent\TorrentResource\Pages\ListTorrents;
use App\Filament\Resources\Torrent\TorrentResource\Pages\CreateTorrent;
use App\Filament\Resources\Torrent\TorrentResource\Pages\EditTorrent;
use Filament\Actions\BulkAction;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\DateTimePicker;
use Exception;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\CheckboxList;
use Filament\Actions\DeleteBulkAction;
use Filament\Forms\Components\Textarea;
use Filament\Actions\DeleteAction;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Tables\Filters\Filter;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Filters\SelectFilter;
use Filament\Forms\Components\DatePicker;
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;
2022-12-08 03:48:20 +08:00
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;
2025-09-11 20:08:43 +07:00
use App\Repositories\SearchBoxRepository;
2022-07-03 14:00:07 +08:00
use App\Repositories\TagRepository;
use App\Repositories\TorrentRepository;
2025-09-17 04:05:36 +07:00
use Elasticsearch\Endpoints\Search;
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;
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;
2022-12-08 03:48:20 +08:00
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;
2025-09-21 18:07:38 +08:00
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-rectangle-stack';
2022-07-03 14:00:07 +08:00
2025-09-21 18:07:38 +08:00
protected static string | \UnitEnum | null $navigationGroup = 'Torrent';
2022-07-03 14:00:07 +08:00
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();
}
2025-09-21 18:07:38 +08:00
public static function form(Schema $schema): Schema
2022-07-03 14:00:07 +08:00
{
2025-09-21 18:07:38 +08:00
return $schema
->components([
2022-07-03 14:00:07 +08:00
//
]);
}
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([
2025-09-21 18:07:38 +08:00
TextColumn::make('id')->sortable(),
TextColumn::make('basic_category.name')->label(__('label.torrent.category')),
TextColumn::make('name')->formatStateUsing(fn ($record) => torrent_name_for_admin($record, true))
2022-12-08 03:48:20 +08:00
->label(__('label.name'))
2023-09-23 03:04:58 +08:00
->searchable(query: function (Builder $query, string $search) {
return $query->where("name", "like", "%{$search}%")->orWhere("small_descr", "like", "%{$search}%");
}),
2025-09-21 18:07:38 +08:00
TextColumn::make('posStateText')->label(__('label.torrent.pos_state')),
TextColumn::make('spStateText')->label(__('label.torrent.sp_state')),
TextColumn::make('pickInfoText')
2022-10-02 21:14:11 +08:00
->label(__('label.torrent.picktype'))
->formatStateUsing(fn ($record) => $record->pickInfo['text'])
,
2025-09-21 18:07:38 +08:00
IconColumn::make('hr')
2022-10-02 21:14:11 +08:00
->label(__('label.torrent.hr'))
2022-12-08 03:48:20 +08:00
->boolean()
2022-10-02 21:14:11 +08:00
,
2025-09-21 18:07:38 +08:00
TextColumn::make('size')
->label(__('label.torrent.size'))
->formatStateUsing(fn ($state) => mksize($state))
->sortable()
,
2025-09-21 18:07:38 +08:00
TextColumn::make('seeders')->label(__('label.torrent.seeders'))->sortable(),
TextColumn::make('leechers')->label(__('label.torrent.leechers'))->sortable(),
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),
2025-09-21 18:07:38 +08:00
TextColumn::make('added')->label(__('label.added'))->dateTime(),
TextColumn::make('user.username')
2022-07-04 14:41:27 +08:00
->label(__('label.torrent.owner'))
2022-12-08 03:48:20 +08:00
->formatStateUsing(fn ($record) => username_for_admin($record->owner))
2022-07-03 14:00:07 +08:00
,
])
->defaultSort('id', 'desc')
2022-12-08 03:48:20 +08:00
->filters(self::getFilters())
2025-09-21 18:07:38 +08:00
->recordActions(self::getActions())
->toolbarActions(self::getBulkActions())
2024-12-17 13:12:58 +08:00
;
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 [
2025-09-21 18:07:38 +08:00
'index' => ListTorrents::route('/'),
'create' => CreateTorrent::route('/create'),
'edit' => EditTorrent::route('/{record}/edit'),
2022-07-03 14:00:07 +08:00
];
}
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')) {
2025-09-21 18:07:38 +08:00
$actions[] = BulkAction::make('posState')
->label(__('admin.resources.torrent.bulk_action_pos_state'))
->form([
2025-09-21 18:07:38 +08:00
Select::make('pos_state')
->label(__('label.torrent.pos_state'))
->options(Torrent::listPosStates(true))
->required()
2022-09-18 03:33:13 +08:00
,
2025-09-21 18:07:38 +08:00
DateTimePicker::make('pos_state_until')
2022-09-18 03:33:13 +08:00
->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']);
2025-09-21 18:07:38 +08:00
} catch (Exception $exception) {
2022-09-12 22:02:57 +08:00
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')) {
2025-09-21 18:07:38 +08:00
$actions[] = BulkAction::make('sp_state')
2022-10-02 21:14:11 +08:00
->label(__('admin.resources.torrent.bulk_action_sp_state'))
->form([
2025-09-21 18:07:38 +08:00
Select::make('sp_state')
2022-10-02 21:14:11 +08:00
->label(__('label.torrent.sp_state'))
->options(Torrent::listPromotionTypes(true))
->required()
,
2025-09-21 18:07:38 +08:00
Select::make('promotion_time_type')
2022-10-02 21:14:11 +08:00
->label(__('label.torrent.promotion_time_type'))
->options(Torrent::listPromotionTimeTypes(true))
->required()
,
2025-09-21 18:07:38 +08:00
DateTimePicker::make('promotion_until')
2022-10-02 21:14:11 +08:00
->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']);
2025-09-21 18:07:38 +08:00
} catch (Exception $exception) {
2022-10-02 21:14:11 +08:00
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)) {
2025-09-21 18:07:38 +08:00
$actions[] = BulkAction::make('recommend')
2022-10-02 21:14:11 +08:00
->label(__('admin.resources.torrent.bulk_action_recommend'))
->form([
2025-09-21 18:07:38 +08:00
Radio::make('picktype')
2022-10-02 21:14:11 +08:00
->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']);
2025-09-21 18:07:38 +08:00
} catch (Exception $exception) {
2022-10-02 21:14:11 +08:00
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')) {
2025-09-21 18:07:38 +08:00
$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);
2025-09-21 18:07:38 +08:00
} catch (Exception $exception) {
2022-09-12 22:02:57 +08:00
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
2022-07-19 14:15:35 +08:00
Filament::notify('danger', class_basename($exception));
}
})
->deselectRecordsAfterCompletion();
2025-09-21 18:07:38 +08:00
$actions[] = BulkAction::make('attach_tag')
->label(__('admin.resources.torrent.bulk_action_attach_tag'))
->form([
2025-09-21 18:07:38 +08:00
Checkbox::make('remove')->label(__('admin.resources.torrent.bulk_action_attach_tag_remove_old')),
CheckboxList::make('tags')
->label(__('label.tag.label'))
->columns(4)
->options(TagRepository::createBasicQuery()->pluck('name', 'id')->toArray())
->required(),
2022-09-12 22:02:57 +08:00
])
->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();
2022-09-12 22:02:57 +08:00
$torrentRep->syncTags($idArr, $data['tags'], $data['remove'] ?? false);
2025-09-21 18:07:38 +08:00
} catch (Exception $exception) {
2022-09-12 22:02:57 +08:00
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
2025-09-21 18:07:38 +08:00
$actions[] = BulkAction::make('hr')
2022-10-02 21:14:11 +08:00
->label(__('admin.resources.torrent.bulk_action_hr'))
->form([
2025-09-21 18:07:38 +08:00
Radio::make('hr')
2022-10-02 21:14:11 +08:00
->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']);
2025-09-21 18:07:38 +08:00
} catch (Exception $exception) {
2022-10-02 21:14:11 +08:00
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
Filament::notify('danger', class_basename($exception));
}
})
->deselectRecordsAfterCompletion();
}
2025-09-17 04:05:36 +07:00
// $actions[] = self::getBulkActionChangeCategory();
2022-10-02 21:14:11 +08:00
if (user_can('torrent-delete')) {
2025-09-21 18:07:38 +08:00
$actions[] = DeleteBulkAction::make('bulk-delete')->using(function (Collection $records) {
2022-10-02 21:14:11 +08:00
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')) {
2025-09-21 18:07:38 +08:00
$actions[] = \Filament\Actions\Action::make('approval')
->label(__('admin.resources.torrent.action_approval'))
2025-09-21 18:07:38 +08:00
->schema([
Radio::make('approval_status')
->label(__('label.torrent.approval_status'))
->inline()
->required()
->options(Torrent::listApprovalStatus(true))
,
2025-09-21 18:07:38 +08:00
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);
2025-09-21 18:07:38 +08:00
} 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')) {
2025-09-21 18:07:38 +08:00
$actions[] = DeleteAction::make('delete')->using(function ($record) {
2022-08-26 17:35:49 +08:00
deletetorrent($record->id);
});
}
return $actions;
}
2025-09-21 18:07:38 +08:00
private static function getBulkActionChangeCategory(): BulkAction
2025-09-11 20:08:43 +07:00
{
2025-09-15 13:38:38 +07:00
$searchBoxRep = new SearchBoxRepository();
2025-09-21 18:07:38 +08:00
return BulkAction::make('changeCategory')
2025-09-11 20:08:43 +07:00
->label(__('admin.resources.torrent.bulk_action_change_category'))
->form([
2025-09-21 18:07:38 +08:00
Select::make('section_id')
2025-09-11 20:08:43 +07:00
->label(__('searchbox.section'))
2025-09-15 13:38:38 +07:00
->helperText(new HtmlString(__('admin.resources.torrent.bulk_action_change_category_section_help')))
2025-09-11 20:08:43 +07:00
->options(function() {
$rep = new SearchBoxRepository();
2025-09-17 04:05:36 +07:00
$list = $rep->listSections(SearchBox::listAllSectionId(), false);
2025-09-11 20:08:43 +07:00
$result = [];
foreach ($list as $section) {
$result[$section->id] = $section->displaySectionName;
}
return $result;
})
->reactive()
->required()
,
2025-09-17 04:05:36 +07:00
$searchBoxRep->buildSearchBoxFormSchema(SearchBox::getBrowseSearchBox(), 'section_info')
2025-09-21 18:07:38 +08:00
->hidden(function (Get $get) {
2025-09-15 13:38:38 +07:00
return $get('section_id') != SearchBox::getBrowseMode();
})
,
2025-09-17 04:05:36 +07:00
$searchBoxRep->buildSearchBoxFormSchema(SearchBox::getSpecialSearchBox(), 'section_info')
2025-09-21 18:07:38 +08:00
->hidden(function (Get $get) {
2025-09-15 13:38:38 +07:00
return $get('section_id') != SearchBox::getSpecialMode();
2025-09-11 20:08:43 +07:00
})
,
])
->action(function (Collection $records, array $data) {
2025-09-14 00:47:09 +07:00
$torrentRep = new TorrentRepository();
2025-09-17 04:05:36 +07:00
$newSectionId = $data['section_id'];
2025-09-14 00:47:09 +07:00
try {
2025-09-17 04:05:36 +07:00
$torrentRep->changeCategory($records, $newSectionId, $data['section_info']['section'][$newSectionId]);
2025-09-21 18:07:38 +08:00
} catch (Exception $exception) {
2025-09-14 00:47:09 +07:00
do_log($exception->getMessage(), 'error');
}
2025-09-11 20:08:43 +07:00
});
}
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';
}
2022-12-08 03:48:20 +08:00
private static function getFilters()
{
$filters = [
2025-09-21 18:07:38 +08:00
Filter::make('owner')
->schema([
TextInput::make('owner')
2022-12-08 03:48:20 +08:00
->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));
})
,
2025-09-21 18:07:38 +08:00
SelectFilter::make('visible')
2022-12-08 03:48:20 +08:00
->options(self::$yesOrNo)
->label(__('label.torrent.visible'))
,
2025-09-21 18:07:38 +08:00
SelectFilter::make('hr')
2022-12-08 03:48:20 +08:00
->options(self::getYesNoOptions())
->label(__('label.torrent.hr'))
,
2025-09-21 18:07:38 +08:00
SelectFilter::make('pos_state')
2022-12-08 03:48:20 +08:00
->options(Torrent::listPosStates(true))
->label(__('label.torrent.pos_state'))
->multiple()
,
2025-09-21 18:07:38 +08:00
SelectFilter::make('sp_state')
2022-12-08 03:48:20 +08:00
->options(Torrent::listPromotionTypes(true))
->label(__('label.torrent.sp_state'))
->multiple()
,
2025-09-21 18:07:38 +08:00
SelectFilter::make('picktype')
2022-12-08 03:48:20 +08:00
->options(Torrent::listPickInfo(true))
->label(__('label.torrent.picktype'))
->multiple()
,
2025-09-21 18:07:38 +08:00
SelectFilter::make('approval_status')
2022-12-08 03:48:20 +08:00
->options(Torrent::listApprovalStatus(true))
->label(__('label.torrent.approval_status'))
->multiple()
,
2025-09-21 18:07:38 +08:00
SelectFilter::make('tags')
2022-12-08 03:48:20 +08:00
->relationship('tags', 'name')
->label(__('label.tag.label'))
->multiple()
,
2025-09-21 18:07:38 +08:00
SelectFilter::make('category')
2022-12-08 03:48:20 +08:00
->options(Category::query()->pluck('name', 'id')->toArray())
->label(__('label.torrent.category'))
->multiple()
,
];
foreach (SearchBox::$taxonomies as $torrentField => $tableModel) {
2025-09-21 18:07:38 +08:00
$filters[] = SelectFilter::make($torrentField)
2022-12-08 03:48:20 +08:00
->options(NexusDB::table($tableModel['table'])->orderBy('sort_index')->orderBy('id')->pluck('name', 'id'))
->multiple()
;
}
2025-09-21 18:07:38 +08:00
$filters[] = Filter::make('added_begin')
->schema([
DatePicker::make('added_begin')
2022-12-08 03:48:20 +08:00
->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));
})
;
2025-09-21 18:07:38 +08:00
$filters[] = Filter::make('added_end')
->schema([
DatePicker::make('added_end')
2022-12-08 03:48:20 +08:00
->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));
})
;
2025-09-21 18:07:38 +08:00
$filters[] = Filter::make('size_begin')
->schema([
TextInput::make('size_begin')
2022-12-08 03:48:20 +08:00
->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));
})
;
2025-09-21 18:07:38 +08:00
$filters[] = Filter::make('size_end')
->schema([
TextInput::make('size_end')
2022-12-08 03:48:20 +08:00
->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
}