345 lines
16 KiB
PHP
345 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Filament\Tables\Columns\MapperLeftColumn;
|
|
use App\Livewire\Mapper\LoadEvents;
|
|
use App\Livewire\Mapper\SearchString;
|
|
use App\Livewire\Mapper\StoreMapped;
|
|
use App\Models\CompetitionGroup;
|
|
use App\Models\Country;
|
|
use App\Models\Event;
|
|
use App\Models\Mapper;
|
|
use Carbon\Carbon;
|
|
use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;
|
|
use Filament\Actions\Action;
|
|
use Filament\Actions\Concerns\InteractsWithActions;
|
|
use Filament\Actions\Contracts\HasActions;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Schemas\Components\FusedGroup;
|
|
use Filament\Schemas\Components\Utilities\Get;
|
|
use Filament\Schemas\Concerns\InteractsWithSchemas;
|
|
use Filament\Schemas\Contracts\HasSchemas;
|
|
use Filament\Support\Enums\Alignment;
|
|
use Filament\Support\Enums\Size;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Concerns\InteractsWithTable;
|
|
use Filament\Tables\Contracts\HasTable;
|
|
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 Livewire\Attributes\Lazy;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Component;
|
|
|
|
#[Lazy]
|
|
class MapperLeft extends Component implements HasActions, HasSchemas, HasTable
|
|
{
|
|
use InteractsWithActions;
|
|
use InteractsWithSchemas;
|
|
use InteractsWithTable;
|
|
|
|
public $elements_swopped = false;
|
|
|
|
public $search_values = [];
|
|
|
|
public $search_color = 'info';
|
|
|
|
protected $listeners = [
|
|
'refreshMapperLeft' => '$refresh',
|
|
];
|
|
|
|
public function isRemovingAllFilters() {}
|
|
|
|
public function removeTableFilter(string $filterName, ?string $field = null, bool $shouldTriggerUpdatedFiltersHook = true): void
|
|
{
|
|
if (($key = array_search($field, $this->search_values)) !== false) {
|
|
unset($this->search_values[$key]);
|
|
}
|
|
|
|
$this->search_values = [];
|
|
$this->tableSearch = ''; // Clears the main search bar
|
|
$this->fill(['tableSearch' => '']);
|
|
$this->resetTableFiltersForm(); // Clears all filters
|
|
|
|
$this->dispatch('refreshMapperLeft');
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->query(LoadEvents::load($this->search_values))
|
|
->striped()
|
|
->columns([
|
|
IconColumn::make('sport.icon')
|
|
->label('')
|
|
->icon(fn ($record) => $record->sport->icon)
|
|
->color(fn ($record) => $record->sport->color)
|
|
->tooltip(fn ($record) => $record->id)
|
|
->visibleFrom('lg'),
|
|
IconColumn::make('country.icon')->icon(fn (string $state): string => $state)->label(''),
|
|
TextColumn::make('competition_group.name')->label('Group')
|
|
->wrap()
|
|
->color('primary')
|
|
->tooltip('Filter by group')
|
|
->description(fn ($record): string => $record->country?->name ?? '')
|
|
->action(function ($record) {
|
|
if ($record?->competition_group_id ?? null && ! in_array($record->competition_group_id, $this->tableFilters['events']['competition_group_id'])) {
|
|
$this->tableFilters['events']['competition_group_id'] = (string) $record->competition_group_id;
|
|
$this->dispatch('refreshMapperLeft');
|
|
}
|
|
}),
|
|
TextColumn::make('starttime')
|
|
->sortable()
|
|
->time('d. M. y H:i')
|
|
->alignCenter(),
|
|
MapperLeftColumn::make('left')->label('Event')->alignCenter(),
|
|
])
|
|
->defaultSort('starttime')
|
|
->searchable(['home.name', 'away.name'])
|
|
->defaultPaginationPageOption(10)
|
|
->filters([
|
|
Filter::make('events')
|
|
->columns(4)
|
|
->schema([
|
|
FusedGroup::make([
|
|
Select::make('country_id')
|
|
->placeholder('Country')
|
|
->visible(fn () => auth()->user()->sport_id != 5)
|
|
->hiddenLabel()
|
|
->searchable()
|
|
->preload()
|
|
->options(function (Get $get) {
|
|
$query = Event::query()->where('sport_id', auth()->user()->sport_id)->where('betradar_id', 0)->whereDate('starttime', '>', Carbon::now())->groupBy('country_id');
|
|
|
|
$timeframe = $get('starttime_period') ?? [];
|
|
|
|
if ($timeframe['start'] || $timeframe['end']) {
|
|
if (isset($timeframe['start']) && isset($timeframe['end'])) {
|
|
$from = Carbon::createFromDate($timeframe['start']);
|
|
$end = Carbon::createFromDate($timeframe['end']);
|
|
|
|
$query->whereDate('starttime', '>=', $from)->whereDate('starttime', '<=', $end);
|
|
} elseif (isset($timeframe['start'])) {
|
|
$from = Carbon::createFromDate($timeframe['start']);
|
|
|
|
$query->whereDate('starttime', '>=', $from);
|
|
} elseif (isset($timeframe['end'])) {
|
|
$end = Carbon::createFromDate($timeframe['end']);
|
|
|
|
$query->whereDate('starttime', '<=', $end);
|
|
}
|
|
}
|
|
|
|
$res = $query->pluck('country_id')->toArray();
|
|
|
|
return Country::query()->whereIn('id', $res)->orderBy('name')->pluck('name', 'id');
|
|
})
|
|
->afterStateUpdated(function (callable $set) {
|
|
$set('competition_group_id', null);
|
|
})
|
|
->live(),
|
|
Select::make('competition_group_id')
|
|
->placeholder('League')
|
|
->hiddenLabel()
|
|
->label('')
|
|
->searchable()
|
|
->options(function (Get $get) {
|
|
|
|
$query = CompetitionGroup::where('sport_id', auth()->user()->sport_id)
|
|
->whereHas('event', function ($query) {
|
|
$query->whereDate('starttime', '>', now())
|
|
->where('betradar_id', 0);
|
|
});
|
|
|
|
if ($get('country_id') ?? null) {
|
|
$res = $query->where('country_id', $get('country_id'))->get();
|
|
} else {
|
|
|
|
$timeframe = $get('starttime_period') ?? null;
|
|
|
|
if ($timeframe['start'] || $timeframe['end']) {
|
|
$query->whereHas('event', function ($query) {
|
|
if (isset($timeframe['start']) && isset($timeframe['end'])) {
|
|
$from = Carbon::createFromDate($timeframe['start']);
|
|
$end = Carbon::createFromDate($timeframe['end']);
|
|
|
|
$query->whereDate('starttime', '>=', $from)->whereDate('starttime', '<=', $end);
|
|
} elseif (isset($timeframe['start'])) {
|
|
$from = Carbon::createFromDate($timeframe['start']);
|
|
|
|
$query->whereDate('starttime', '>=', $from);
|
|
} elseif (isset($timeframe['end'])) {
|
|
$end = Carbon::createFromDate($timeframe['end']);
|
|
|
|
$query->whereDate('starttime', '<=', $end);
|
|
}
|
|
});
|
|
}
|
|
if (auth()->user()->sport_id != 5) {
|
|
$res = $query->groupBy('country_id')->get();
|
|
} else {
|
|
$res = $query->get();
|
|
}
|
|
}
|
|
$arr = [];
|
|
$map = Country::all()->pluck('name', 'id')->toArray();
|
|
foreach ($res as $value) {
|
|
if (isset($map[$value->country_id])) {
|
|
$arr[$value->id] = $map[$value->country_id].' '.$value->name;
|
|
} else {
|
|
$arr[$value->id] = $value->name;
|
|
}
|
|
}
|
|
asort($arr);
|
|
|
|
return $arr;
|
|
})
|
|
->live(),
|
|
])->columnSpan(2),
|
|
DateRangePicker::make('starttime_period')
|
|
->displayFormat('d M Y')
|
|
->columnSpan(2)
|
|
->minDate(now())
|
|
->hiddenLabel()
|
|
->label('')
|
|
->live(),
|
|
])
|
|
->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) {
|
|
$this->setPage(1);
|
|
|
|
return $query->where('competition_group_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
|
|
->whereDate('starttime', '>=', $from)
|
|
->whereDate('starttime', '<=', $end);
|
|
} elseif (isset($starttime_period['start'])) {
|
|
$from = Carbon::createFromDate($starttime_period['start']);
|
|
|
|
return $query
|
|
->whereDate('starttime', '>=', $from);
|
|
} elseif (isset($starttime_period['end'])) {
|
|
$end = Carbon::createFromDate($starttime_period['end']);
|
|
|
|
return $query
|
|
->whereDate('starttime', '<=', $end);
|
|
}
|
|
});
|
|
})->indicateUsing(function (): array {
|
|
$indicators = [];
|
|
if ($this->search_values) {
|
|
foreach ($this->search_values as $value) {
|
|
$indicators[] = Indicator::make($value)->removeField($value)->color($this->search_color);
|
|
}
|
|
}
|
|
|
|
return $indicators;
|
|
})
|
|
->columns(4)->columnSpan(4),
|
|
], layout: FiltersLayout::AboveContent)
|
|
->filtersFormColumns(3)
|
|
->persistFiltersInSession()
|
|
->deferFilters(false)
|
|
->toolbarActions([
|
|
Action::make('refresh_left')
|
|
->hiddenLabel()
|
|
->icon('phosphor-arrows-counter-clockwise')
|
|
->size(Size::Small)
|
|
->action(function (Component $livewire) {
|
|
$this->dispatch('refreshMapperLeft');
|
|
}),
|
|
Action::make('reload_left')
|
|
->hiddenLabel()
|
|
->icon('phosphor-x')
|
|
->size(Size::Small)
|
|
->action(function (Component $livewire) {
|
|
$this->search_values = [];
|
|
$this->tableSearch = ''; // Clears the main search bar
|
|
$livewire->fill(['tableSearch' => '']);
|
|
$livewire->resetTableFiltersForm(); // Clears all filters
|
|
$this->dispatch('refreshMapperLeft');
|
|
}),
|
|
]);
|
|
}
|
|
|
|
public function toggle()
|
|
{
|
|
$this->elements_swopped = ! $this->elements_swopped;
|
|
}
|
|
|
|
#[On('search_left')]
|
|
public function searchLeft($string)
|
|
{
|
|
$this->search_values = [];
|
|
$this->tableSearch = ''; // Clears the main search bar
|
|
$this->fill(['tableSearch' => '']);
|
|
$this->resetTableFiltersForm(); // Clears all filters
|
|
|
|
$t = explode('|', $string);
|
|
|
|
$this->search_values = SearchString::explode($t[0]);
|
|
|
|
$this->search_color = $t[1];
|
|
|
|
$this->dispatch('refreshMapperLeft');
|
|
$this->dispatch('refreshMapperRight');
|
|
}
|
|
|
|
public function leftAction(): Action
|
|
{
|
|
return Action::make('left')
|
|
->modalHeading('Event Mapper')
|
|
->modalContent(function (array $arguments) {
|
|
|
|
$event = Event::where('id', $arguments['id'])->first();
|
|
|
|
$mapper = Mapper::where('event_id', $arguments['event_id'])->first();
|
|
|
|
return view('livewire.confirm-mapping', ['event' => $event, 'mapper' => $mapper]);
|
|
})
|
|
->action(function ($arguments) {
|
|
StoreMapped::store($arguments, $this->elements_swopped);
|
|
$this->search_values = [];
|
|
$this->dispatch('refreshMapperLeft');
|
|
$this->dispatch('refreshMapperRight');
|
|
|
|
})
|
|
->closeModalByEscaping(false)
|
|
->modalCloseButton(false)
|
|
->modalCancelAction(fn (Action $action) => $action->keyBindings(['c'])->tooltip('keyboard: "c"'))
|
|
->closeModalByClickingAway(false)
|
|
->modalAlignment(Alignment::Center)
|
|
->modalFooterActionsAlignment(Alignment::Center)
|
|
->modalWidth('2xl')
|
|
->modalSubmitActionLabel('Save')
|
|
->modalSubmitAction(
|
|
fn (Action $action) => $action->keyBindings(['return'])->tooltip('keyboard: "return"')
|
|
);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.mapper-left');
|
|
}
|
|
}
|