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

230 lines
9.7 KiB
PHP

<?php
namespace App\Livewire;
use App\Filament\Tables\Columns\MapperRightColumn;
use App\Livewire\Mapper\LoadProvider;
use App\Livewire\Mapper\StoreMapped;
use App\Models\Country;
use App\Models\Event;
use App\Models\Mapper;
use App\Models\User;
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 Illuminate\Support\Carbon;
use Livewire\Attributes\Lazy;
use Livewire\Attributes\On;
use Livewire\Component;
#[Lazy]
class MapperRight extends Component implements HasActions, HasSchemas, HasTable
{
use InteractsWithActions;
use InteractsWithSchemas;
use InteractsWithTable;
public $elements_swopped = false;
public $search_right = false;
protected $listeners = [
'refreshMapperRight' => '$refresh',
];
public $user;
public function removeTableFilter(string $filterName, ?string $field = null, bool $shouldTriggerUpdatedFiltersHook = true): void
{
$searchValues = preg_split('/\s+/', $this->search_right, -1, PREG_SPLIT_NO_EMPTY);
if (($key = array_search($field, $searchValues)) !== false) {
unset($searchValues[$key]);
}
if ($searchValues) {
$this->search_right = implode(' ', $searchValues);
} else {
$this->search_right = '';
}
$this->dispatch('refreshMapperRight');
}
public function table(Table $table): Table
{
$this->user = User::getUser();
return $table
->query(LoadProvider::load($this->search_right, $this->user))
->columns([
MapperRightColumn::make('right')->label(''),
])->searchable(['home', 'away'])
->defaultPaginationPageOption(5)
->filters([
Filter::make('events')
->schema([
Select::make('country_id')
->visible(fn () => in_array($this->user->provider_id, [3, 4]))
->placeholder('Country')
->hiddenLabel()
->label('')
->searchable()
->preload()
->multiple()
->columnSpan(2)
->live()
->options(function (Get $get) {
return Mapper::query()->where('sport_id', $this->user->sport_id)->where('provider_id', $this->user->provider_id)->where('starttime', '>', Carbon::now())->groupBy('country_id')->pluck('country', 'country_id')->toArray();
})
->reactive()
->afterStateUpdated(function (callable $set) {
$set('group_id', null);
})
->placeholder('Country'),
Select::make('group_cc')
->visible(fn () => $this->user->provider_id == 2)
->placeholder('Country')
->hiddenLabel()
->label('')
->searchable()
->preload()
->multiple()
->columnSpan(2)
->live()
->options(function () {
$res = Mapper::query()->where('sport_id', $this->user->sport_id)->where('provider_id', $this->user->provider_id)->where('starttime', '>', Carbon::now())->groupBy('group_cc')->pluck('group_cc')->toArray();
return Country::query()->whereIn('iso2', $res)->pluck('name', 'iso2');
})
->reactive()
->afterStateUpdated(function (callable $set) {
$set('group_id', null);
})
->placeholder('Country'),
Select::make('group_id')
->placeholder('League')
->label('Leagues')
->hiddenLabel()
->label('')
->columnSpan(2)
->searchable()
->preload()
->multiple()
->options(function (Get $get) {
if ($get('country_id')) {
return Mapper::query()->where('sport_id', $this->user->sport_id)->where('provider_id', $this->user->provider_id)->where('starttime', '>', Carbon::now())->whereIn('country_id', $get('country_id'))->groupBy('group_id')->pluck('group', 'group_id')->toArray();
} elseif ($get('group_cc')) {
return Mapper::query()->where('sport_id', $this->user->sport_id)->where('provider_id', $this->user->provider_id)->where('starttime', '>', Carbon::now())->whereIn('group_cc', $get('group_cc'))->groupBy('group_id')->pluck('group', 'group_id')->toArray();
} else {
return Mapper::query()->where('sport_id', $this->user->sport_id)->where('provider_id', $this->user->provider_id)->where('starttime', '>', Carbon::now())->groupBy('group_id')->pluck('group', 'group_id')->toArray();
}
})
->live(),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['country_id'],
fn (Builder $query, $country_id) => $query->whereIn('country_id', $country_id)
)->when(
$data['group_cc'],
fn (Builder $query, $group_cc) => $query->whereIn('group_cc', $group_cc)
)->when(
$data['group_id'],
fn (Builder $query, $group_id) => $query->whereIn('group_id', $group_id)
);
})->indicateUsing(function (): array {
$indicators = [];
if ($this->search_right) {
$searchValues = preg_split('/\s+/', $this->search_right, -1, PREG_SPLIT_NO_EMPTY);
foreach ($searchValues as $value) {
$indicators[] = Indicator::make($value)->removeField($value);
}
}
return $indicators;
})
->columns(4)->columnSpan(4),
], layout: FiltersLayout::AboveContent)
// ->hiddenFilterIndicators()
->persistFiltersInSession()
->filtersFormColumns(3)
->deferFilters(false)
->toolbarActions([
Action::make('refreshMapperRight')
->hiddenLabel()
->icon('phosphor-x')
->color('danger')
->size(Size::Small)
->action(function (Component $livewire) {
$this->search_right = '';
$this->tableSearch = ''; // Clears the main search bar
$livewire->fill(['tableSearch' => '']);
$livewire->resetTableFiltersForm(); // Clears all filters
$this->dispatch('refreshMapperRight');
$this->dispatch('refreshMapperLeft');
}),
]);
}
public function toggle()
{
$this->elements_swopped = ! $this->elements_swopped;
}
#[On('search_right')]
public function searchRight($str)
{
$this->search_right = $str;
$this->dispatch('refreshMapperRight');
}
public function rightAction(): Action
{
return Action::make('right')
->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');
}
}