2022-07-09 18:06:09 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Filament\Resources\System;
|
|
|
|
|
|
2025-09-21 18:07:38 +08:00
|
|
|
use Filament\Schemas\Schema;
|
|
|
|
|
use Filament\Forms\Components\Select;
|
|
|
|
|
use Filament\Forms\Components\DateTimePicker;
|
2025-12-07 12:57:08 +08:00
|
|
|
use Filament\Forms\Components\Textarea;
|
2025-09-21 18:07:38 +08:00
|
|
|
use Filament\Tables\Columns\TextColumn;
|
|
|
|
|
use Filament\Actions\EditAction;
|
2025-12-07 12:57:08 +08:00
|
|
|
use Filament\Actions\DeleteAction;
|
|
|
|
|
use Filament\Actions\DeleteBulkAction;
|
2025-09-21 18:07:38 +08:00
|
|
|
use App\Filament\Resources\System\TorrentStateResource\Pages\ManageTorrentStates;
|
2022-07-09 18:06:09 +08:00
|
|
|
use App\Models\Torrent;
|
|
|
|
|
use App\Models\TorrentState;
|
2025-12-07 12:57:08 +08:00
|
|
|
use Carbon\Carbon;
|
2022-07-09 18:06:09 +08:00
|
|
|
use Filament\Resources\Resource;
|
2024-12-25 23:09:07 +08:00
|
|
|
use Filament\Tables\Table;
|
2022-07-09 18:06:09 +08:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
|
|
|
|
|
class TorrentStateResource extends Resource
|
|
|
|
|
{
|
|
|
|
|
protected static ?string $model = TorrentState::class;
|
|
|
|
|
|
2025-09-21 18:07:38 +08:00
|
|
|
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-megaphone';
|
2022-07-09 18:06:09 +08:00
|
|
|
|
2025-09-21 18:07:38 +08:00
|
|
|
protected static string | \UnitEnum | null $navigationGroup = 'System';
|
2022-07-09 18:06:09 +08:00
|
|
|
|
2025-05-05 18:24:17 +07:00
|
|
|
protected static ?int $navigationSort = 9;
|
2022-07-09 18:06:09 +08:00
|
|
|
|
2024-12-25 23:09:07 +08:00
|
|
|
public static function getNavigationLabel(): string
|
2022-07-09 18:06:09 +08:00
|
|
|
{
|
|
|
|
|
return __('admin.sidebar.torrent_state');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-09 18:06:09 +08:00
|
|
|
{
|
2025-09-21 18:07:38 +08:00
|
|
|
return $schema
|
|
|
|
|
->components([
|
|
|
|
|
Select::make('global_sp_state')
|
2025-12-07 12:57:08 +08:00
|
|
|
->options(function () {
|
|
|
|
|
$options = Torrent::listPromotionTypes(true);
|
|
|
|
|
unset($options[Torrent::PROMOTION_NORMAL]);
|
|
|
|
|
return $options;
|
|
|
|
|
})
|
2022-07-09 18:06:09 +08:00
|
|
|
->label(__('label.torrent_state.global_sp_state'))
|
|
|
|
|
->required(),
|
2025-09-21 18:07:38 +08:00
|
|
|
DateTimePicker::make('begin')
|
2025-12-07 12:57:08 +08:00
|
|
|
->label(__('label.begin'))
|
|
|
|
|
->required(),
|
2025-09-21 18:07:38 +08:00
|
|
|
DateTimePicker::make('deadline')
|
2025-12-07 12:57:08 +08:00
|
|
|
->label(__('label.deadline'))
|
|
|
|
|
->required()
|
|
|
|
|
->after('begin')
|
|
|
|
|
->validationMessages([
|
|
|
|
|
'after' => __('label.torrent_state.deadline_after_begin'),
|
|
|
|
|
]),
|
2025-12-11 12:19:35 +08:00
|
|
|
Select::make('notice_days')
|
|
|
|
|
->label(__('label.torrent_state.notice_days'))
|
|
|
|
|
->options(TorrentState::noticeOptions())
|
|
|
|
|
->required()
|
|
|
|
|
->default(TorrentState::NOTICE_NONE)
|
|
|
|
|
->dehydrated(true)
|
|
|
|
|
->native(false),
|
2025-12-07 12:57:08 +08:00
|
|
|
Textarea::make('remark')
|
|
|
|
|
->label(__('label.comment'))
|
|
|
|
|
->rows(2)
|
|
|
|
|
->columnSpanFull()
|
|
|
|
|
->maxLength(255),
|
2022-07-09 18:06:09 +08:00
|
|
|
])->columns(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function table(Table $table): Table
|
|
|
|
|
{
|
|
|
|
|
return $table
|
|
|
|
|
->columns([
|
2025-09-21 18:07:38 +08:00
|
|
|
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')),
|
2025-12-07 12:57:08 +08:00
|
|
|
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'),
|
2025-12-11 12:19:35 +08:00
|
|
|
TextColumn::make('notice_days_text')
|
|
|
|
|
->label(__('label.torrent_state.notice_days')),
|
2025-12-07 12:57:08 +08:00
|
|
|
TextColumn::make('remark')->label(__('label.comment'))->limit(50),
|
2022-07-09 18:06:09 +08:00
|
|
|
])
|
|
|
|
|
->filters([
|
|
|
|
|
//
|
|
|
|
|
])
|
2025-09-21 18:07:38 +08:00
|
|
|
->recordActions([
|
2025-12-07 12:57:08 +08:00
|
|
|
EditAction::make(),
|
|
|
|
|
DeleteAction::make(),
|
2022-07-09 18:06:09 +08:00
|
|
|
])
|
2025-09-21 18:07:38 +08:00
|
|
|
->toolbarActions([
|
2025-12-07 12:57:08 +08:00
|
|
|
DeleteBulkAction::make(),
|
2022-07-09 18:06:09 +08:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getPages(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2025-09-21 18:07:38 +08:00
|
|
|
'index' => ManageTorrentStates::route('/'),
|
2022-07-09 18:06:09 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|