392 lines
19 KiB
PHP
392 lines
19 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Filament\Tables\Columns\MapperRightColumn;
|
|
use App\Livewire\Mapper\LoadMapper;
|
|
use App\Livewire\Mapper\SearchString;
|
|
use App\Livewire\Mapper\StoreMapped;
|
|
use App\Models\Country;
|
|
use App\Models\Event;
|
|
use App\Models\Mapper;
|
|
use App\Models\Sport;
|
|
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\Notifications\Notification;
|
|
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 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_values = [];
|
|
|
|
public $search_color = 'info';
|
|
|
|
protected $listeners = [
|
|
'refreshMapperRight' => '$refresh',
|
|
];
|
|
|
|
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->dispatch('refreshMapperRight');
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->query(LoadMapper::load($this->search_values))
|
|
->striped()
|
|
->columns([
|
|
MapperRightColumn::make('left')->label('Event')->alignCenter(),
|
|
TextColumn::make('starttime')
|
|
->sortable()
|
|
->time('d. M. y H:i')
|
|
->alignCenter(),
|
|
TextColumn::make('group')
|
|
->wrap()
|
|
->color('primary')
|
|
->tooltip('Filter by group')
|
|
->description(fn ($record): string => $record->country_betsapi?->name.' '.$record->country)
|
|
->action(function ($record) {
|
|
if ($record?->group_id ?? null && ! in_array($record->group_id, $this->tableFilters['events']['group_id'])) {
|
|
$this->tableFilters['events']['group_id'] = (string) $record->group_id;
|
|
$this->dispatch('refreshMapperRight');
|
|
}
|
|
}),
|
|
IconColumn::make('country_magloo.icon')
|
|
->label('')
|
|
->icon(fn (string $state): string => $state)
|
|
->visible(fn () => in_array(auth()->user()->provider_id, [3, 4])),
|
|
IconColumn::make('group_cc')
|
|
->visible(fn () => auth()->user()->provider_id == 2)
|
|
->icon(fn (string $state): string => 'flag-country-'.$state)->label(''),
|
|
IconColumn::make('sport.icon')
|
|
->label('')
|
|
->icon(fn ($record) => $record->sport->icon)
|
|
->color(fn ($record) => $record->sport->color)
|
|
->tooltip(fn ($record) => $record->id)
|
|
->visibleFrom('lg'),
|
|
])->searchable(['home', 'away'])
|
|
->filters([
|
|
Filter::make('events')
|
|
->schema([
|
|
FusedGroup::make([
|
|
Select::make('country_id')
|
|
->visible(fn () => in_array(auth()->user()->provider_id, [3, 4]) && auth()->user()->sport_id != 5)
|
|
->placeholder('Country')
|
|
->hiddenLabel()
|
|
->label('')
|
|
->searchable()
|
|
->preload()
|
|
->live()
|
|
->options(function (Get $get) {
|
|
$query = Mapper::query()->where('sport_id', auth()->user()->sport_id)->where('provider_id', auth()->user()->provider_id)->groupBy('country_id')->orderBy('country');
|
|
|
|
$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);
|
|
}
|
|
}
|
|
|
|
return $query->pluck('country', 'country_id')->toArray();
|
|
})
|
|
->afterStateUpdated(function (callable $set) {
|
|
$set('group_id', null);
|
|
})
|
|
->placeholder('Country'),
|
|
Select::make('group_cc')
|
|
->visible(fn () => auth()->user()->provider_id == 2)
|
|
->placeholder('Country')
|
|
->hiddenLabel()
|
|
->label('')
|
|
->searchable()
|
|
->preload()
|
|
->live()
|
|
->options(function (Get $get) {
|
|
$query = Mapper::query()->where('sport_id', auth()->user()->sport_id)->where('provider_id', auth()->user()->provider_id)->groupBy('group_cc');
|
|
|
|
$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);
|
|
}
|
|
}
|
|
if ($get('country_id') ?? null) {
|
|
$res = $query->where('country_id', $get('country_id'))->pluck('group_cc')->toArray();
|
|
} else {
|
|
$res = $query->pluck('group_cc')->toArray();
|
|
}
|
|
|
|
return Country::query()->whereIn('iso2', $res)->orderBy('name')->pluck('name', 'iso2');
|
|
})
|
|
->reactive()
|
|
->afterStateUpdated(function (callable $set) {
|
|
$set('group_id', null);
|
|
})
|
|
->placeholder('Country'),
|
|
Select::make('group_id')
|
|
->placeholder('League')
|
|
->label('Leagues')
|
|
->hiddenLabel()
|
|
->label('')
|
|
->searchable()
|
|
->preload()
|
|
->options(function (Get $get) {
|
|
if ($get('country_id')) {
|
|
$query = Mapper::query()->where('sport_id', auth()->user()->sport_id)->where('provider_id', auth()->user()->provider_id)->where('country_id', $get('country_id'))->groupBy('group_id');
|
|
} elseif ($get('group_cc')) {
|
|
$query = Mapper::query()->where('sport_id', auth()->user()->sport_id)->where('provider_id', auth()->user()->provider_id)->where('group_cc', $get('group_cc'))->groupBy('group_id');
|
|
} else {
|
|
$query = Mapper::query()->where('sport_id', auth()->user()->sport_id)->where('provider_id', auth()->user()->provider_id)->groupBy('group_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);
|
|
}
|
|
}
|
|
|
|
return $query->pluck('group', 'group_id')->toArray();
|
|
})
|
|
->live(),
|
|
])->columnSpan(2),
|
|
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['country_id'],
|
|
fn (Builder $query, $country_id) => $query->where('country_id', $country_id)
|
|
)->when(
|
|
$data['group_cc'],
|
|
fn (Builder $query, $group_cc) => $query->where('group_cc', $group_cc)
|
|
)->when(
|
|
$data['group_id'],
|
|
function (Builder $query, $group_id) {
|
|
$this->setPage(1);
|
|
|
|
return $query->where('group_id', $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)
|
|
// IconColumn::make('search_home')->icon('phosphor-magnifying-magnifying')->label('')->color('home')->width(30),
|
|
// IconColumn::make('search_away')->icon('phosphor-magnifying-glass')->label('')->color('away')->width(30),
|
|
// ->hiddenFilterIndicators()
|
|
->defaultPaginationPageOption(10)
|
|
->persistFiltersInSession()
|
|
->filtersFormColumns(3)
|
|
->deferFilters(false)
|
|
->toolbarActions([
|
|
Action::make('refresh_right')
|
|
->hiddenLabel()
|
|
->tooltip('Reload')
|
|
->icon('phosphor-arrows-counter-clockwise')
|
|
->size(Size::Small)
|
|
->action(function (Component $livewire) {
|
|
$this->dispatch('refreshMapperRight');
|
|
}),
|
|
Action::make('reload_right')
|
|
->hiddenLabel()
|
|
->icon('phosphor-x')
|
|
->tooltip('Clear Filter and Reload')
|
|
->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('refreshMapperRight');
|
|
}),
|
|
Action::make('empty_mapper')
|
|
->hiddenLabel()
|
|
->color('danger')
|
|
->icon('phosphor-trash')
|
|
->tooltip('Empty Database and Reload from Provider')
|
|
->size(Size::Small)
|
|
->requiresConfirmation()
|
|
->action(function (Component $livewire) {
|
|
|
|
Mapper::query()->where('sport_id', auth()->user()->sport_id)->where('provider_id', auth()->user()->provider_id)->delete();
|
|
|
|
$this->search_values = [];
|
|
$this->tableSearch = ''; // Clears the main search bar
|
|
$livewire->fill(['tableSearch' => '']);
|
|
$livewire->resetTableFiltersForm(); // Clears all filters
|
|
$this->dispatch('refreshMapperRight');
|
|
})->successNotification(
|
|
Notification::make()
|
|
->success()
|
|
->title('Events deleted')
|
|
->body('All events for '.Sport::query()->where('id', auth()->user()->sport_id)->value('name').' has been deleted successfully. It will take up to 5 minutes to reload'),
|
|
),
|
|
]);
|
|
}
|
|
|
|
public function toggle()
|
|
{
|
|
$this->elements_swopped = ! $this->elements_swopped;
|
|
}
|
|
|
|
#[On('search_right')]
|
|
public function searchRight($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('refreshMapperRight');
|
|
}
|
|
|
|
public function rightAction(): Action
|
|
{
|
|
return Action::make('right')
|
|
->modalHeading('Event Mapper')
|
|
->modalDescription('Keyboard: "enter" Save, "c" Close')
|
|
->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()
|
|
->modalSubmitAction(
|
|
fn (Action $action) => $action->keyBindings(['return'])->tooltip('keyboard: "return"')
|
|
);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.mapper-right');
|
|
}
|
|
}
|