144 lines
6.4 KiB
PHP
144 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Clusters\StaticData\Resources\Elements\Tables;
|
|
|
|
use App\Filament\Exports\ElementExporter;
|
|
use App\Models\CompetitionGroup;
|
|
use App\Models\Country;
|
|
use App\Models\ElementGroup;
|
|
use App\Models\ElementProvider;
|
|
use App\Models\Event;
|
|
use App\Models\Sport;
|
|
use Filament\Actions\ActionGroup;
|
|
use Filament\Actions\BulkActionGroup;
|
|
use Filament\Actions\DeleteAction;
|
|
use Filament\Actions\DeleteBulkAction;
|
|
use Filament\Actions\EditAction;
|
|
use Filament\Actions\ExportAction;
|
|
use Filament\Actions\Exports\Enums\ExportFormat;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Schemas\Components\Utilities\Get;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use Filament\Tables\Columns\ImageColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Enums\FiltersLayout;
|
|
use Filament\Tables\Filters\Filter;
|
|
use Filament\Tables\Filters\SelectFilter;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class ElementsTable
|
|
{
|
|
public static function configure(Table $table): Table
|
|
{
|
|
return $table
|
|
->striped()
|
|
->columns([
|
|
IconColumn::make('sport.icon')
|
|
->label('')
|
|
->grow(false)
|
|
->color('primary')
|
|
->width(30)
|
|
->icon(fn ($record) => $record->sport->icon)
|
|
->tooltip(fn ($record) => $record->id)
|
|
->visibleFrom('lg'),
|
|
TextColumn::make('id')
|
|
->tooltip('click to copy')
|
|
->alignCenter()
|
|
->copyable(),
|
|
TextColumn::make('country.name')
|
|
->wrap()
|
|
->color('gray')
|
|
->icon(fn ($record) => $record->country->icon),
|
|
TextColumn::make('element_group.competition_group_id')->label('Group Id')
|
|
->alignCenter()
|
|
->tooltip('click to copy')
|
|
->copyable(),
|
|
TextColumn::make('element_group.competition_group.name')->label('Group'),
|
|
// TextColumn::make('element_group.group.name')->label('Group'),
|
|
ImageColumn::make('betsapi_image_id')
|
|
->label('Logo')
|
|
->defaultImageUrl(fn ($state) => url('https://assets.b365api.com/images/team/m/'.$state.'.png')),
|
|
TextColumn::make('name')->searchable(),
|
|
])->filters([
|
|
SelectFilter::make('sport_id')
|
|
->label('Sport')
|
|
->options(Sport::where('is_active', 1)->pluck('name', 'id')),
|
|
Filter::make('competition_group')
|
|
->columns(3)->columnSpan(3)
|
|
->schema([
|
|
Select::make('country_id')
|
|
->searchable()
|
|
->label('Country')
|
|
->options(Country::orderBy('name')->pluck('name', 'id'))
|
|
->live(),
|
|
Select::make('competition_group_id')
|
|
->searchable()
|
|
->label('Competition Group')
|
|
->options(function (Get $get) {
|
|
if ($get('country_id')) {
|
|
return CompetitionGroup::where('country_id', $get('country_id'))->orderBy('name')->pluck('name', 'id');
|
|
} else {
|
|
return CompetitionGroup::orderBy('name')->pluck('name', 'id');
|
|
}
|
|
}),
|
|
])
|
|
->query(function (Builder $query, array $data): Builder {
|
|
return $query->when(
|
|
$data['country_id'],
|
|
fn (Builder $query, $country_id) => $query->where('country_id', $country_id)
|
|
)->when(
|
|
$data['competition_group_id'],
|
|
function (Builder $query, $competition_group_id) {
|
|
return $query->whereHas('element_group', function (Builder $query) use ($competition_group_id) {
|
|
return $query->where('competition_group_id', $competition_group_id);
|
|
});
|
|
}
|
|
);
|
|
}),
|
|
], layout: FiltersLayout::AboveContent)
|
|
->deferFilters(false)
|
|
->recordActions([
|
|
ActionGroup::make([
|
|
EditAction::make(),
|
|
DeleteAction::make()
|
|
->tooltip('All elements connected to this group, including active events, will be deleted')
|
|
->before(function ($record) {
|
|
Event::where('home_id', $record->id)->delete();
|
|
Event::where('away_id', $record->id)->delete();
|
|
ElementProvider::where('element_id', $record->id)->delete();
|
|
ElementGroup::where('element_id', $record->id)->delete();
|
|
})
|
|
->successNotification(
|
|
Notification::make()
|
|
->success()
|
|
->title('Element deleted')
|
|
->body('The element with all relations in other tables has been deleted successfully.'),
|
|
),
|
|
])->iconButton()
|
|
->icon('phosphor-dots-three-vertical'),
|
|
])
|
|
->headerActions([
|
|
ExportAction::make()
|
|
->modalHeading('Download Teams/Player')
|
|
->color('warning')
|
|
->label('Export')
|
|
->icon('phosphor-download')
|
|
->modalDescription('This will download the TxOdds Teams/Player with "sport_id" and "country_id" for the sport selected.')
|
|
->modalIcon('phosphor-microsoft-excel-logo')
|
|
->exporter(ElementExporter::class)
|
|
->columnMapping(false)
|
|
->formats([
|
|
ExportFormat::Xlsx,
|
|
ExportFormat::Csv,
|
|
]),
|
|
])
|
|
->toolbarActions([
|
|
BulkActionGroup::make([
|
|
DeleteBulkAction::make(),
|
|
]),
|
|
]);
|
|
}
|
|
}
|