Files
nuhodds_fi4/app/Actions/Statistics/LoadEvents.php
T
2026-04-30 09:16:30 +02:00

190 lines
6.1 KiB
PHP

<?php
namespace App\Actions\Statistics;
use App\Models\StatistikoEvent;
class LoadEvents
{
public static $last_query = [];
public static function load($filter)
{
if (self::$last_query) {
return self::$last_query;
}
$league_id = (int) $filter['league_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$team_position = $filter['team_position'];
if (! $league_id) {
return [];
}
if (! $team_id) {
return [];
}
switch ($filter['team_position']) {
case 'home':
$res = StatistikoEvent::where('league_id', $league_id)->where('home_id', '=', $team_id)->get();
break;
case 'away':
$res = StatistikoEvent::where('league_id', $league_id)->where('away_id', '=', $team_id)->get();
break;
default:
$res = StatistikoEvent::where('league_id', $league_id)
->where(function ($query) use ($team_id) {
$query->where('home_id', '=', $team_id)->orWhere('away_id', '=', $team_id);
})->get();
break;
}
$datasets = [];
$labels = [];
$statistic_type = $filter['statistic_type'];
if (! $statistic_type || $statistic_type == 'none') {
$statistic_type = 'gls';
}
foreach ($res as $value) {
$labels[] = $value['starttime']->format('d. M.y');
// dd($filter);
// $tmp['exp_handicap'] = (float) $value['exp_handicap'];
// $tmp['exp_total'] = (float) $value['exp_total'];
// $tmp['exp_away'] = (float) $value['expgoalsaway_value'];
// $tmp['exp_home'] = (float) $value['expgoalshome_value'];
if ($team_position == 'home') {
switch ($statistic_type) {
case 'gls':
$val = (int) $value['gls_h'];
break;
case 'crn':
$val = (int) $value['crn_h'];
break;
case 'fls':
$val = (int) $value['fls_h'];
break;
case 'ofs':
$val = (int) $value['ofs_h'];
break;
case 'crd':
$val = (int) $value['crd_h'];
break;
case 'snt':
$val = (int) $value['snt_h'];
break;
case 'tsh':
$val = (int) $value['tsh_h'];
break;
}
} elseif ($team_position == 'away') {
switch ($statistic_type) {
case 'gls':
$val = (int) $value['gls_a'];
break;
case 'crn':
$val = (int) $value['crn_a'];
break;
case 'fls':
$val = (int) $value['fls_a'];
break;
case 'ofs':
$val = (int) $value['ofs_a'];
break;
case 'crd':
$val = (int) $value['crd_a'];
break;
case 'snt':
$val = (int) $value['snt_a'];
break;
case 'tsh':
$val = (int) $value['tsh_a'];
break;
}
} else {
switch ($statistic_type) {
case 'gls':
if ($team_id == $value['home_id']) {
$val = (int) $value['gls_a'];
} else {
$val = (int) $value['gls_h'];
}
break;
case 'crn':
if ($team_id == $value['home_id']) {
$val = (int) $value['crn_a'];
} else {
$val = (int) $value['crn_h'];
}
break;
case 'fls':
if ($team_id == $value['home_id']) {
$val = (int) $value['fls_a'];
} else {
$val = (int) $value['fls_h'];
}
break;
case 'ofs':
if ($team_id == $value['home_id']) {
$val = (int) $value['ofs_a'];
} else {
$val = (int) $value['ofs_h'];
}
break;
case 'crd':
if ($team_id == $value['home_id']) {
$val = (int) $value['crd_a'];
} else {
$val = (int) $value['crd_h'];
}
break;
case 'snt':
if ($team_id == $value['home_id']) {
$val = (int) $value['snt_a'];
} else {
$val = (int) $value['snt_h'];
}
break;
case 'tsh':
if ($team_id == $value['home_id']) {
$val = (int) $value['tsh_a'];
} else {
$val = (int) $value['tsh_h'];
}
break;
}
}
$datasets[] = $val;
}
$data = [
'datasets' => [
[
'label' => ucfirst($team_position),
'data' => $datasets,
'borderColor' => '#36A2EB',
],
],
'labels' => $labels,
];
self::$last_query = $data;
return $data;
}
}