Files
nuhodds_fi4/app/Filament/Clusters/Statistic/Resources/StatisticEvents/Tables/StatisticEventsTable.php
T
2026-04-27 07:42:30 +02:00

353 lines
18 KiB
PHP

<?php
namespace App\Filament\Clusters\Statistic\Resources\StatisticEvents\Tables;
use App\Models\CompetitionGroup;
use App\Models\Country;
use App\Models\Event;
use App\Models\Sport;
use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\Indicator;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\HtmlString;
class StatisticEventsTable
{
public static function configure(Table $table): Table
{
return $table
->striped()
->columns([
IconColumn::make('sport_id')
->label('')
->icon(fn (string $state): string => match ($state) {
'1' => 'icon-soccerball',
'2' => 'icon-icehockey',
'3' => 'icon-basketball',
'5' => 'icon-tennisball',
}),
TextColumn::make('league.name'),
TextColumn::make('starttime')->dateTime('M j, Y')->label('Date'),
TextColumn::make('home.name')->color('primary'),
TextColumn::make('away.name')->color('success'),
])
->filters([
Filter::make('events')
->columns(5)->columnSpan(5)
->schema([
Select::make('sport_id')
->default(1)
->hiddenLabel()
->label('')
->label('Sport')
->options(Sport::whereIn('id', [1, 2, 3, 5])->pluck('name', 'id')),
Select::make(name: 'country_id')
->hidden(fn (Get $get) => $get('sport_id') == 5)
->placeholder('Country')
->prefixIcon('phosphor-flag')
->hiddenLabel()
->label('')
->multiple()
->searchable()
->preload()
->options(function (Get $get) {
$res = Event::where('has_markets', 1)
->where('sport_id', $get('sport_id'))
->where('starttime', '>=', Carbon::now())
->groupBy('country_id')
->pluck('country_id')->toArray();
return Country::query()->whereIn('id', $res)->orderBy('name')->pluck('name', 'id');
})
->live()
->reactive()
->afterStateUpdated(function (callable $set) {
$set('competition_group_id', null);
}),
Select::make('competition_group_id')
->hidden(fn (Get $get) => $get('sport_id') == 5)
->placeholder('League')
->prefixIcon('phosphor-trophy')
->hiddenLabel()
->label('')
->multiple()
->searchable()
->options(function (Get $get) {
$country_id = $get('country_id') ?? 0;
if ($country_id) {
$res = Event::with('country')
->with('competition_group')
->where('sport_id', $get('sport_id'))
->whereIn('country_id', $country_id)
->where('starttime', '>', Carbon::now())
->groupBy('competition_group_id')->get();
} else {
$res = Event::with('country')
->with('competition_group')
->where('sport_id', $get('sport_id'))
->where('starttime', '>', Carbon::now())
->groupBy('competition_group_id')->get();
}
$arr = [];
foreach ($res as $val) {
$arr[$val->competition_group_id] = $val->country?->name.' '.$val->competition_group?->name;
}
asort($arr);
return $arr;
})
->live(),
Select::make('competition_group_id')
->visible(fn (Get $get) => $get('sport_id') == 5)
->placeholder('Tournament')
->prefixIcon('phosphor-trophy')
->hiddenLabel()
->label('')
->multiple()
->searchable()
->options(function () {
$res = Event::with('competition_group')
->where('sport_id', 5)
->where('starttime', '>', Carbon::now())
->groupBy('competition_group_id')
->get();
$arr = [];
foreach ($res as $val) {
$arr[$val->competition_group_id] = $val->competition_group->name;
}
// asort($arr);
return $arr;
})
->live(),
DatePicker::make('starttime_from')
->prefix(fn () => new HtmlString('<string class="text-info-500">From</string>'))
->displayFormat('d M Y')
->hiddenLabel()
->label(''),
DatePicker::make('starttime_to')
->prefix(fn () => new HtmlString('<string class="text-success-500">To:</string>'))
->displayFormat('d M Y')
->hiddenLabel()
->label(''),
])
->query(function (Builder $query, array $data): Builder {
return $query->when(
$data['sport_id'],
function (Builder $query, $sport_id) {
$query->where('sport_id', (int) $sport_id);
}
)->when(
$data['starttime_from'],
function (Builder $query, $starttime_from) {
if (isset($starttime_from)) {
$from = Carbon::createFromDate($starttime_from);
return $query->where('starttime', '>=', $starttime_from);
}
}
)->when(
$data['starttime_to'],
function (Builder $query, $starttime_to) {
if (isset($starttime_to)) {
$to = Carbon::createFromDate($starttime_to);
return $query->where('starttime', '<=', $starttime_to);
}
}
);
})->indicateUsing(function (array $data): array {
$indicators = [];
if ($data['starttime_from'] ?? null) {
$starttime_from = $data['starttime_from'];
if ($starttime_from ?? null) {
$text = Carbon::parse($starttime_from)->format('d. M. Y');
$indicators[] = Indicator::make('From: '.$text)->removeField('starttime_from')->color('info');
}
}
if ($data['starttime_to'] ?? null) {
$starttime_to = $data['starttime_to'];
if ($starttime_to ?? null) {
$text = Carbon::parse($starttime_to)->format('d. M. Y');
$indicators[] = Indicator::make('To: '.$text)->removeField('starttime_to')->color('success');
}
}
// if ($data['competition_group_id'] ?? null) {
// $competition_groups = CompetitionGroup::whereIn('id', $data['competition_group_id'])->pluck('name')->toArray();
// if (count($competition_groups) > 1) {
// $indicators[] = Indicator::make(implode(', ', $competition_groups))->removeField('competition_group_id')->color('info');
// } else {
// $indicators[] = Indicator::make($competition_groups[0])->removeField('competition_group_id')->color('info')->color('info');
// }
// }
return $indicators;
}),
], layout: FiltersLayout::AboveContent)
->deferFilters(false)
// ->filters([
// Filter::make('events')
// ->columns(7)->columnSpan(7)
// ->schema([
// Select::make('sport_id')
// ->default(1)
// ->hiddenLabel()
// ->label('')
// ->label('Sport')
// ->options(Sport::where('is_active', 1)->pluck('name', 'id')),
// // Select::make('competition_group_id')
// // ->hidden(fn (Get $get) => $get('sport_id') == 5)
// // ->placeholder('League')
// // ->prefixIcon('phosphor-trophy')
// // ->hiddenLabel()
// // ->label('')
// // ->multiple()
// // ->searchable()
// // ->options(function (Get $get) {
// // $res = Event::where('has_markets', 1)
// // ->with('country')
// // ->with('competition_group')
// // ->where('sport_id', $get('sport_id'))
// // ->where('starttime', '>', Carbon::now())
// // ->whereIn('country_id', $get('country_id'))
// // ->groupBy('competition_group_id')->get();
// // $arr = [];
// // foreach ($res as $val) {
// // $arr[$val->competition_group_id] = $val->country?->name.' '.$val->competition_group?->name;
// // }
// // asort($arr);
// // return $arr;
// // })
// // ->live(),
// // Select::make('competition_group_id')
// // ->visible(fn (Get $get) => $get('sport_id') == 5)
// // ->placeholder('Tournament')
// // ->prefixIcon('phosphor-trophy')
// // ->hiddenLabel()
// // ->label('')
// // ->multiple()
// // ->searchable()
// // ->options(function () {
// // $res = Event::where('has_markets', 1)
// // ->with('competition_group')
// // ->where('sport_id', auth()->user()->sport_id)
// // ->where('starttime', '>', Carbon::now())
// // ->groupBy('competition_group_id')
// // ->get();
// // $arr = [];
// // foreach ($res as $val) {
// // $arr[$val->competition_group_id] = $val->competition_group->name;
// // }
// // asort($arr);
// // return $arr;
// // })
// // ->live(),
// // DateRangePicker::make('starttime_period')
// // ->columnSpan(2)
// // ->displayFormat('d M Y')
// // ->minDate(now())
// // ->hiddenLabel()
// // ->label('')
// // ->live(),
// ]),
// // ->query(function (Builder $query, array $data): Builder {
// // return $query->when(
// // $data['sport_id'],
// // function (Builder $query, $sport_id) {
// // $query->where('sport_id', $sport_id);
// // }
// // )->when(
// // $data['competition_group_id'],
// // fn (Builder $query, $competition_group_id) => $query->whereIn('sport_id', $competition_group_id)
// // )->when(
// // $data['starttime_period'],
// // function (Builder $query, $starttime_period) {
// // // dd($starttime_period);
// // if (isset($starttime_period['start']) && isset($starttime_period['end'])) {
// // $from = Carbon::createFromDate($starttime_period['start']);
// // $end = Carbon::createFromDate($starttime_period['end']);
// // return $query
// // ->where('starttime', '>=', $from)
// // ->where('starttime', '<=', $end);
// // } elseif (isset($starttime_period['start'])) {
// // $from = Carbon::createFromDate($starttime_period['start']);
// // return $query
// // ->where('starttime', '>=', $from);
// // } elseif (isset($starttime_period['end'])) {
// // $end = Carbon::createFromDate($starttime_period['end']);
// // return $query
// // ->where('starttime', '<=', $end);
// // }
// // }
// // );
// // })->indicateUsing(function (array $data): array {
// // $indicators = [];
// // if ($data['starttime_period'] ?? null) {
// // $span = $data['starttime_period'];
// // $text = '';
// // if ($span['start'] ?? null) {
// // $text .= Carbon::parse($span['start'])->format('d. M.');
// // }
// // if ($span['end'] ?? null) {
// // $text .= ' - '.Carbon::parse($span['end'])->format('d. M.');
// // }
// // if ($text) {
// // $indicators[] = Indicator::make($text)->removeField('starttime_period.start')->color('info');
// // }
// // }
// // if ($data['competition_group_id'] ?? null) {
// // $competition_groups = CompetitionGroup::whereIn('id', $data['competition_group_id'])->pluck('name')->toArray();
// // if (count($competition_groups) > 1) {
// // $indicators[] = Indicator::make(implode(', ', $competition_groups))->removeField('competition_group_id')->color('info');
// // } else {
// // $indicators[] = Indicator::make($competition_groups[0])->removeField('competition_group_id')->color('info')->color('info');
// // }
// // }
// // return $indicators;
// // }),
// ], layout: FiltersLayout::AboveContent)
// ->deferFilters(false)
// ->hiddenFilterIndicators(false)
// ->persistFiltersInSession()
->recordActions([
EditAction::make(),
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}
}