mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
feat: enhance torrent state scheduling and display
Signed-off-by: Qi HU <github@spcsky.com>
This commit is contained in:
@@ -5,21 +5,18 @@ namespace App\Filament\Resources\System;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use App\Filament\Resources\System\TorrentStateResource\Pages\ManageTorrentStates;
|
||||
use App\Filament\Resources\System\TorrentStateResource\Pages;
|
||||
use App\Filament\Resources\System\TorrentStateResource\RelationManagers;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\TorrentState;
|
||||
use Filament\Forms;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Tables;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class TorrentStateResource extends Resource
|
||||
{
|
||||
@@ -46,13 +43,28 @@ class TorrentStateResource extends Resource
|
||||
return $schema
|
||||
->components([
|
||||
Select::make('global_sp_state')
|
||||
->options(Torrent::listPromotionTypes(true))
|
||||
->options(function () {
|
||||
$options = Torrent::listPromotionTypes(true);
|
||||
unset($options[Torrent::PROMOTION_NORMAL]);
|
||||
return $options;
|
||||
})
|
||||
->label(__('label.torrent_state.global_sp_state'))
|
||||
->required(),
|
||||
DateTimePicker::make('begin')
|
||||
->label(__('label.begin')),
|
||||
->label(__('label.begin'))
|
||||
->required(),
|
||||
DateTimePicker::make('deadline')
|
||||
->label(__('label.deadline')),
|
||||
->label(__('label.deadline'))
|
||||
->required()
|
||||
->after('begin')
|
||||
->validationMessages([
|
||||
'after' => __('label.torrent_state.deadline_after_begin'),
|
||||
]),
|
||||
Textarea::make('remark')
|
||||
->label(__('label.comment'))
|
||||
->rows(2)
|
||||
->columnSpanFull()
|
||||
->maxLength(255),
|
||||
])->columns(1);
|
||||
}
|
||||
|
||||
@@ -63,21 +75,63 @@ class TorrentStateResource extends Resource
|
||||
TextColumn::make('global_sp_state_text')->label(__('label.torrent_state.global_sp_state')),
|
||||
TextColumn::make('begin')->label(__('label.begin')),
|
||||
TextColumn::make('deadline')->label(__('label.deadline')),
|
||||
TextColumn::make('promotion_status')
|
||||
->label(__('label.torrent_state.status'))
|
||||
->state(function (TorrentState $record) {
|
||||
$now = Carbon::now();
|
||||
$begin = $record->begin ? Carbon::parse($record->begin) : null;
|
||||
$deadline = $record->deadline ? Carbon::parse($record->deadline) : null;
|
||||
|
||||
if ($deadline && $deadline->lt($now)) {
|
||||
return 'expired';
|
||||
}
|
||||
if ($begin && $begin->gt($now)) {
|
||||
return 'upcoming';
|
||||
}
|
||||
return 'ongoing';
|
||||
})
|
||||
->formatStateUsing(function (string $state) {
|
||||
return match ($state) {
|
||||
'expired' => __('label.torrent_state.status_expired'),
|
||||
'upcoming' => __('label.torrent_state.status_upcoming'),
|
||||
default => __('label.torrent_state.status_ongoing'),
|
||||
};
|
||||
})
|
||||
->badge()
|
||||
->sortable(query: function (Builder $query, string $direction): Builder {
|
||||
$now = Carbon::now()->toDateTimeString();
|
||||
// expired=0, ongoing=1, upcoming=2
|
||||
return $query->orderByRaw(
|
||||
"CASE
|
||||
WHEN deadline IS NOT NULL AND deadline < ? THEN 0
|
||||
WHEN begin IS NOT NULL AND begin > ? THEN 2
|
||||
ELSE 1
|
||||
END {$direction}",
|
||||
[$now, $now]
|
||||
);
|
||||
})
|
||||
->color(fn (string $state) => match ($state) {
|
||||
'expired' => 'danger',
|
||||
'upcoming' => 'info',
|
||||
default => 'success',
|
||||
})
|
||||
->icon(fn (string $state) => match ($state) {
|
||||
'expired' => 'heroicon-o-x-circle',
|
||||
'upcoming' => 'heroicon-o-clock',
|
||||
default => 'heroicon-o-check-circle',
|
||||
})
|
||||
->iconPosition('before'),
|
||||
TextColumn::make('remark')->label(__('label.comment'))->limit(50),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make()->after(function () {
|
||||
do_log("cache_del: global_promotion_state");
|
||||
NexusDB::cache_del(Setting::TORRENT_GLOBAL_STATE_CACHE_KEY);
|
||||
do_log("publish_model_event: global_promotion_state_updated");
|
||||
publish_model_event("global_promotion_state_updated", 0);
|
||||
}),
|
||||
// Tables\Actions\DeleteAction::make(),
|
||||
EditAction::make(),
|
||||
DeleteAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
// Tables\Actions\DeleteBulkAction::make(),
|
||||
DeleteBulkAction::make(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
namespace App\Filament\Resources\System\TorrentStateResource\Pages;
|
||||
|
||||
use App\Filament\Resources\System\TorrentStateResource;
|
||||
use Filament\Pages\Actions;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ManageRecords;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class ManageTorrentStates extends ManageRecords
|
||||
{
|
||||
@@ -14,7 +13,7 @@ class ManageTorrentStates extends ManageRecords
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
// Actions\CreateAction::make(),
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user