Files
nuhodds_fi4/app/Livewire/MapperLeft.php
T
2026-02-03 10:57:50 +01:00

218 lines
8.5 KiB
PHP

<?php
namespace App\Livewire;
use App\Filament\Tables\Columns\MapperLeftColumn;
use App\Livewire\Mapper\LoadEvents;
use App\Livewire\Mapper\StoreMapped;
use App\Models\CompetitionGroup;
use App\Models\Country;
use App\Models\Event;
use App\Models\Mapper;
use App\Models\User;
use Carbon\Carbon;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Components\Select;
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\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_left = false;
public $user;
protected $listeners = [
'refreshMapperLeft' => '$refresh',
];
public function removeTableFilter(string $filterName, ?string $field = null, bool $shouldTriggerUpdatedFiltersHook = true): void
{
$searchValues = preg_split('/\s+/', $this->search_left, -1, PREG_SPLIT_NO_EMPTY);
if (($key = array_search($field, $searchValues)) !== false) {
unset($searchValues[$key]);
}
if ($searchValues) {
$this->search_left = implode(' ', $searchValues);
} else {
$this->search_left = '';
}
$this->dispatch('refreshMapperLeft');
}
public function table(Table $table): Table
{
$this->user = User::getUser();
return $table
->query(LoadEvents::load($this->search_left, $this->user->sport_id))
->columns([
MapperLeftColumn::make('left')->label(''),
])
->searchable(['home.name', 'away.name'])
->defaultPaginationPageOption(5)
->filters([
Filter::make('events')
->schema([
Select::make('country_id')
->placeholder('Country')
->hiddenLabel()
->multiple()
->columnSpan(2)
->searchable()
->preload()
->options(function () {
$res = Event::query()->where('sport_id', $this->user->sport_id)->where('betradar_id', 0)->where('starttime', '>', Carbon::now())->groupBy('country_id')->pluck('country_id')->toArray();
return Country::query()->whereIn('id', $res)->pluck('name', 'id');
})
->live()
->reactive()
->afterStateUpdated(function (callable $set) {
$set('competition_group_id', null);
}),
Select::make('competition_group_id')
->placeholder('League')
->hiddenLabel()
->label('')
->columnSpan(2)
->multiple()
->searchable()
->options(function (Get $get) {
if ($get('country_id')) {
$res = Event::query()->where('sport_id', $this->user->sport_id)->where('betradar_id', 0)->where('starttime', '>', Carbon::now())->whereIn('country_id', $get('country_id'))->groupBy('competition_group_id')->pluck('competition_group_id')->toArray();
} else {
$res = Event::query()->where('sport_id', $this->user->sport_id)->where('betradar_id', 0)->where('starttime', '>', Carbon::now())->groupBy('competition_group_id')->pluck('competition_group_id')->toArray();
}
$comps = CompetitionGroup::with('country')->whereIn('id', $res)->get();
$arr = [];
foreach ($comps as $val) {
if (isset($val->country->name)) {
$arr[$val->id] = $val->country->name.' - '.$val->name;
} else {
$arr[$val->id] = $val->name;
}
}
asort($arr);
return $arr;
})
->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'],
fn (Builder $query, $competition_group_id) => $query->whereIn('competition_group_id', $competition_group_id)
);
})->indicateUsing(function (): array {
$indicators = [];
if ($this->search_left) {
$searchValues = preg_split('/\s+/', $this->search_left, -1, PREG_SPLIT_NO_EMPTY);
foreach ($searchValues as $value) {
$indicators[] = Indicator::make($value)->removeField($value);
}
}
return $indicators;
})
->columns(4)->columnSpan(4),
], layout: FiltersLayout::AboveContent)
->filtersFormColumns(3)
->persistFiltersInSession()
->deferFilters(false)
->toolbarActions([
Action::make('refreshMapperLeft')
->hiddenLabel()
->icon('phosphor-x')
->color('danger')
->size(Size::Small)
->action(function (Component $livewire) {
$this->search_left = '';
$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($str)
{
$this->search_left = $str;
$this->dispatch('refreshMapperLeft');
}
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_right = '';
$this->search_left = '';
$this->dispatch('refreshMapperLeft');
$this->dispatch('refreshMapperRight');
})
->closeModalByEscaping(false)
->modalCloseButton(false)
->closeModalByClickingAway(false)
->modalAlignment(Alignment::Center)
->modalFooterActionsAlignment(Alignment::Center)
->modalWidth('2xl')
->modalSubmitActionLabel('Confirm')
->modalSubmitAction();
}
public function render()
{
return view('livewire.mapper-left');
}
}