sdwebserver/app/Controllers/Home.php

197 lines
5.5 KiB
PHP
Raw Normal View History

2022-03-14 19:13:10 +01:00
<?php
namespace App\Controllers;
use App\Models\CarCatsModel;
use App\Models\TracksModel;
use CodeIgniter\API\ResponseTrait;
2022-03-14 19:13:10 +01:00
class Home extends BaseController
{
use ResponseTrait;
2022-03-14 19:13:10 +01:00
public function index()
{
$tplData = [];
$carCatModel = new CarCatsModel();
2022-03-14 19:13:10 +01:00
// select interested period
if(array_key_exists('period', $_COOKIE))
{
$period = $_COOKIE['period'];
}
if($this->request->getGet('period'))
2022-03-14 19:13:10 +01:00
{
$period = $this->request->getGet('period');
setcookie( "period", $period, time()+(60*60*24*30) );
2022-03-14 19:13:10 +01:00
}
else $period = 'today';
2022-03-14 19:13:10 +01:00
$tplData['period'] = $period;
switch ($period)
{
case 'today': //today
$datediff = (1*24*60*60);
$backto = time() - $datediff;
$tplData['periodString'] = 'In the last day';
break;
case 'week': //last week
$datediff = (7*24*60*60);
$backto = time() - $datediff;
$tplData['periodString'] = 'In the last week';
break;
case 'month': //last month
$datediff = (30*24*60*60);
$backto = time()-$datediff;
$tplData['periodString'] = 'In the last month';
break;
case 'year': //last year
$datediff = (365*24*60*60);
$backto = time()-$datediff;
$tplData['periodString'] = 'In the last year';
break;
case 'allTime'://always
$datediff = (50000*24*60*60);
$backto = time() - $datediff;
$tplData['periodString'] = 'all time';
break;
default:
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
2022-03-14 19:13:10 +01:00
}
//select the category to display
$catId = $this->request->getGet('cat');
if ($catId)
2022-03-14 19:13:10 +01:00
{
// Check if the cat exists
$exists = $carCatModel->find($catId);
if (!$exists) throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
$carCatId = $catId;
}
else
{
$first = $carCatModel->select('id')->findAll(1);
$carCatId = $first[0]->id;
2022-03-14 19:13:10 +01:00
}
$tplData['carCatId'] = $carCatId;
2022-03-14 19:13:10 +01:00
// Get cars categories
$categoriesList = $carCatModel->select('id, name, count(carId) as totalCars')->groupBy('id')->findAll();
$currCat = $carCatModel->find($carCatId);
2022-03-14 19:13:10 +01:00
$carsCatList = $carCatModel->select('carId')->where('id', $carCatId)->findAll();
2022-03-14 19:13:10 +01:00
$tplData['currCat'] = $currCat;
$tplData['carCategoriesList'] = $categoriesList;
2022-03-14 19:13:10 +01:00
$carsCatIds = [];
foreach ($carsCatList as $car) $carsCatIds[] = $car->carId;
2022-03-14 19:13:10 +01:00
//UGLY: there is some category that have no car assigned so create a fake $carsql for them
//to prevent errors in the generated queries
/*
$carsql = '0';
2022-03-14 19:13:10 +01:00
if($carsql == ''){
$carsql = " B.car_id='NonExistentCarIdFindThisIfYouCan'";
}
*/
2022-03-14 19:13:10 +01:00
/*
################################
## MOST ACTIVE USER OF THIS CATEGORY BASED ON LAPS RUN
## WITH A CAR OF THIS CATEGORY
################################
*/
$builder = $this->db->table('races r');
$builder->select('r.user_id, COUNT(*) AS count, u.username');
$builder->join('users u', 'u.id = r.user_id');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->whereIn('r.car_id', $carsCatIds);
$builder->groupBy('r.user_id');
2022-03-14 19:13:10 +01:00
$builder->orderBy('count DESC');
$tplData['users'] = [];
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $tplData['users'] = $query->getResult();
2022-03-14 19:13:10 +01:00
/*
################################
## SELECT THE BEST LAPS FOR EACH TRACK
## WITH A CAR OFT HIS CATEGORY
################################
*/
$builder = $this->db->table('laps l');
$builder->select('l.race_id, r.track_id, r.car_id, r.user_id, r.timestamp, l.wettness, min(l.laptime) as bestlap, c.name AS car_name, t.name AS track_name, u.username');
$builder->join('races r', 'l.race_id = r.id');
$builder->join('cars c', 'c.id = r.car_id');
$builder->join('tracks t', 't.id = r.track_id');
$builder->join('users u', 'u.id = r.user_id');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->whereIn('r.car_id', $carsCatIds);
$builder->groupBy(['r.track_id', 'l.wettness']);
2022-03-14 19:13:10 +01:00
$tplData['mylaps'] = [];
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $tplData['mylaps'] = $query->getResult();
2022-03-14 19:13:10 +01:00
$tplData['tracks'] = [];
$builder = $this->db->table('races');
$builder->select('track_id, COUNT(*) AS count');
$builder->where('UNIX_TIMESTAMP(timestamp) >', $backto);
$builder->whereIn('car_id', $carsCatIds);
$builder->groupBy('track_id');
2022-03-14 19:13:10 +01:00
$builder->orderBy('count DESC');
$query = $builder->get();
if ($query && $query->getNumRows() > 0)
{
$tracks = $query->getResult();
$tracksIds = [];
foreach($tracks as $track) $tracksIds[] = $track->track_id;
$tracksModel = new TracksModel();
$tracksNames = [];
$tracksNamesList = $tracksModel->select('id, name')->whereIn('id', $tracksIds)->findAll();
foreach($tracksNamesList as $name) $tracksNames[$name->id] = $name->name;
foreach($tracksIds as $id)
{
if (!key_exists($id, $tracksNames)) $tracksNames[$id] = "$id (Modded)";
}
$tplData['tracks'] = $tracks;
$tplData['tracksNames'] = $tracksNames;
}
2022-03-14 19:13:10 +01:00
$tplData['cars'] = [];
$builder = $this->db->table('races r');
$builder->join('cars c', 'c.id = r.car_id');
$builder->select('r.car_id, COUNT(r.car_id) as count, c.name');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->whereIn('r.car_id', $carsCatIds);
$builder->groupBy('r.car_id');
2022-03-14 19:13:10 +01:00
$builder->orderBy('count DESC');
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $tplData['cars'] = $query->getResult();
2022-03-14 19:13:10 +01:00
echo get_header('Home');
echo view('main', $tplData);
echo get_footer();
}
public function error404()
{
return view('404');
}
2022-03-14 19:13:10 +01:00
}