Clean code and added some code methods comments
This commit is contained in:
parent
f1bd63c7ea
commit
a0a548550b
4 changed files with 25 additions and 281 deletions
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
namespace App\Controllers;
|
namespace App\Controllers;
|
||||||
use App\Models\CarCatsModel;
|
use App\Models\CarCatsModel;
|
||||||
use App\Models\TracksModel;
|
|
||||||
use App\Models\BestLapsModel;
|
|
||||||
use CodeIgniter\API\ResponseTrait;
|
use CodeIgniter\API\ResponseTrait;
|
||||||
|
|
||||||
class Home extends BaseController
|
class Home extends BaseController
|
||||||
|
@ -14,13 +12,9 @@ class Home extends BaseController
|
||||||
{
|
{
|
||||||
$tplData = [];
|
$tplData = [];
|
||||||
$carCatModel = new CarCatsModel;
|
$carCatModel = new CarCatsModel;
|
||||||
$bestLapsModel = new BestLapsModel;
|
|
||||||
|
|
||||||
// select interested period
|
// select interested period
|
||||||
if(array_key_exists('period', $_COOKIE))
|
if(array_key_exists('period', $_COOKIE)) $period = $_COOKIE['period'];
|
||||||
{
|
|
||||||
$period = $_COOKIE['period'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if($this->request->getGet('period'))
|
if($this->request->getGet('period'))
|
||||||
{
|
{
|
||||||
|
@ -33,40 +27,31 @@ class Home extends BaseController
|
||||||
|
|
||||||
switch ($period)
|
switch ($period)
|
||||||
{
|
{
|
||||||
case 'today': //today
|
case 'today': // Today
|
||||||
$datediff = (1*24*60*60);
|
|
||||||
$backto = time() - $datediff;
|
|
||||||
$tplData['periodString'] = 'In the last day';
|
$tplData['periodString'] = 'In the last day';
|
||||||
break;
|
break;
|
||||||
case 'week': //last week
|
case 'week': // Last week
|
||||||
$datediff = (7*24*60*60);
|
|
||||||
$backto = time() - $datediff;
|
|
||||||
$tplData['periodString'] = 'In the last week';
|
$tplData['periodString'] = 'In the last week';
|
||||||
break;
|
break;
|
||||||
case 'month': //last month
|
case 'month': // Last month
|
||||||
$datediff = (30*24*60*60);
|
|
||||||
$backto = time()-$datediff;
|
|
||||||
$tplData['periodString'] = 'In the last month';
|
$tplData['periodString'] = 'In the last month';
|
||||||
break;
|
break;
|
||||||
case 'year': //last year
|
case 'year': // Last year
|
||||||
$datediff = (365*24*60*60);
|
|
||||||
$backto = time()-$datediff;
|
|
||||||
$tplData['periodString'] = 'In the last year';
|
$tplData['periodString'] = 'In the last year';
|
||||||
break;
|
break;
|
||||||
case 'allTime'://always
|
case 'allTime': // Always
|
||||||
$datediff = (50000*24*60*60);
|
|
||||||
$backto = time() - $datediff;
|
|
||||||
$tplData['periodString'] = 'all time';
|
$tplData['periodString'] = 'all time';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
|
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
//select the category to display
|
// Select the category to display
|
||||||
$catId = $this->request->getGet('cat');
|
$catId = $this->request->getGet('cat');
|
||||||
|
|
||||||
if ($catId)
|
if ($catId)
|
||||||
{
|
{
|
||||||
// Check if the cat exists
|
// Check if the car's category exists
|
||||||
$exists = $carCatModel->find($catId);
|
$exists = $carCatModel->find($catId);
|
||||||
if (!$exists) throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
|
if (!$exists) throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
|
||||||
|
|
||||||
|
@ -84,98 +69,9 @@ class Home extends BaseController
|
||||||
$categoriesList = $carCatModel->select('id, name, count(carId) as totalCars')->groupBy('id')->findAll();
|
$categoriesList = $carCatModel->select('id, name, count(carId) as totalCars')->groupBy('id')->findAll();
|
||||||
$currCat = $carCatModel->find($carCatId);
|
$currCat = $carCatModel->find($carCatId);
|
||||||
|
|
||||||
//$carsCatList = $carCatModel->select('carId')->where('id', $carCatId)->findAll();
|
|
||||||
$carsCatIds = $carCatModel->getCarsInCat($carCatId);
|
|
||||||
|
|
||||||
$tplData['currCat'] = $currCat;
|
$tplData['currCat'] = $currCat;
|
||||||
$tplData['carCategoriesList'] = $categoriesList;
|
$tplData['carCategoriesList'] = $categoriesList;
|
||||||
|
|
||||||
//$carsCatIds = [];
|
|
||||||
//foreach ($carsCatList as $car) $carsCatIds[] = $car->carId;
|
|
||||||
|
|
||||||
//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';
|
|
||||||
if($carsql == ''){
|
|
||||||
$carsql = " B.car_id='NonExistentCarIdFindThisIfYouCan'";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
################################
|
|
||||||
## 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');
|
|
||||||
$builder->orderBy('count DESC');
|
|
||||||
|
|
||||||
$tplData['users'] = [];
|
|
||||||
$query = $builder->get();
|
|
||||||
if ($query && $query->getNumRows() > 0) $tplData['users'] = $query->getResult();
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
################################
|
|
||||||
## SELECT THE BEST LAPS FOR EACH TRACK
|
|
||||||
## WITH A CAR OFT HIS CATEGORY
|
|
||||||
################################
|
|
||||||
*/
|
|
||||||
//list($tplData['mylaps'], $_) = $bestLapsModel->getBests($period, $carCatId, 0, 0);
|
|
||||||
/*
|
|
||||||
$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');
|
|
||||||
$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;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
$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');
|
|
||||||
$builder->orderBy('count DESC');
|
|
||||||
|
|
||||||
$query = $builder->get();
|
|
||||||
if ($query && $query->getNumRows() > 0) $tplData['cars'] = $query->getResult();
|
|
||||||
*/
|
|
||||||
echo get_header('Home', ['minidt.css']);
|
echo get_header('Home', ['minidt.css']);
|
||||||
echo view('main', $tplData);
|
echo view('main', $tplData);
|
||||||
echo get_footer(['minidt.js', 'home_tables.js']);
|
echo get_footer(['minidt.js', 'home_tables.js']);
|
||||||
|
|
|
@ -8,6 +8,14 @@ class CarsModel extends BaseModel
|
||||||
|
|
||||||
protected $allowedFields = ['id', 'name', 'img', 'category', 'width', 'length', 'mass', 'fueltank', 'engine', 'drivetrain'];
|
protected $allowedFields = ['id', 'name', 'img', 'category', 'width', 'length', 'mass', 'fueltank', 'engine', 'drivetrain'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the most used cars in the category
|
||||||
|
* @param array $carsCatIds List with the car's ID on the category
|
||||||
|
* @param string $period The period of time between the current date and when you want to obtain the list.
|
||||||
|
* @param int $page Current page
|
||||||
|
* @param int $limit The limit of results to be obtained
|
||||||
|
* @return array An array with the list and the total number of unpaginated results.
|
||||||
|
*/
|
||||||
public function getMostUsedCars(array $carsCatIds, string $period='today', int $page=0, int $limit=20)
|
public function getMostUsedCars(array $carsCatIds, string $period='today', int $page=0, int $limit=20)
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
|
@ -42,52 +50,4 @@ class CarsModel extends BaseModel
|
||||||
|
|
||||||
return [$list, $total];
|
return [$list, $total];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
public function __construct($car=null)
|
|
||||||
{
|
|
||||||
if ($car) $this->data = $this->data($car);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLink(string $text=null): string
|
|
||||||
{
|
|
||||||
if(!$text) $text = $this->data->name;
|
|
||||||
return "<a href='". base_url() . "'/car/{$this->data->id}'>$text</a>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function card() {
|
|
||||||
return $this->data->name . $this->data->img;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function imgTag()
|
|
||||||
{
|
|
||||||
return "<img width='80' src='" . base_url() . "/{$this->img}' alt='{$this->name}' title='{$this->name}'>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function imgTagFull()
|
|
||||||
{
|
|
||||||
return "<img src='" . base_url() . "/{$this->img}' class='car-img' alt='{$this->name}'>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function clickableName()
|
|
||||||
{
|
|
||||||
return $this->linkTag($this->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function clickableImgTag()
|
|
||||||
{
|
|
||||||
return $this->linkTag($this->imgTag());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function linkTag($content)
|
|
||||||
{
|
|
||||||
return "<a href='" . base_url() . "/car/{$this->id}'>$content</a>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function linkTitleImgTag()
|
|
||||||
{
|
|
||||||
$content = $this->name . '<br />' . $this->imgTag();
|
|
||||||
return "<a href='" . base_url() . "/car/{$this->id}'>$content</a>";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,14 @@ class TracksModel extends BaseModel
|
||||||
protected $db;
|
protected $db;
|
||||||
protected $allowedFields = ['id', 'name', 'img', 'category', 'author', 'description'];
|
protected $allowedFields = ['id', 'name', 'img', 'category', 'author', 'description'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the most used tracks in the category
|
||||||
|
* @param array $carsCatIds List with the car's ID on the category
|
||||||
|
* @param string $period The period of time between the current date and when you want to obtain the list.
|
||||||
|
* @param int $page Current page
|
||||||
|
* @param int $limit The limit of results to be obtained
|
||||||
|
* @return array An array with the list and the total number of unpaginated results.
|
||||||
|
*/
|
||||||
public function getMostUsedTracks(array $carsCatIds, string $period='today', int $page=0, int $limit=20)
|
public function getMostUsedTracks(array $carsCatIds, string $period='today', int $page=0, int $limit=20)
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
|
@ -45,51 +53,4 @@ class TracksModel extends BaseModel
|
||||||
|
|
||||||
return [$list, $total];
|
return [$list, $total];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
public function __construct($track=null)
|
|
||||||
{
|
|
||||||
if ($track) $this->import($track);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function import($properties)
|
|
||||||
{
|
|
||||||
foreach($properties as $key => $value) $this->{$key} = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLink($text='')
|
|
||||||
{
|
|
||||||
if($text == '') $text = $this->username;
|
|
||||||
return '<a href="track/'.$this->id.'">'.$text.'</a>';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function card($text='')
|
|
||||||
{
|
|
||||||
return $this->name.$this->img;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function imgTag() {
|
|
||||||
return "<img width='80' src='". base_url() ."/".$this->img."' alt='".$this->name."' title='".$this->name."'>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function imgTagFull() {
|
|
||||||
return "<img src='". base_url() ."/".$this->img."' class='track-img' alt='".$this->name."'>";
|
|
||||||
}
|
|
||||||
public function clickableName() {
|
|
||||||
return $this->linkTag($this->name);
|
|
||||||
|
|
||||||
}
|
|
||||||
public function clickableImgTag() {
|
|
||||||
return $this->linkTag($this->imgTag());
|
|
||||||
}
|
|
||||||
public function linkTag($content) {
|
|
||||||
return "<a href='". base_url() ."/track/".$this->id."'>".$content."</a>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function linkTitleImgTag()
|
|
||||||
{
|
|
||||||
$content = $this->name . '<br />' . $this->imgTag();
|
|
||||||
return "<a href='" . base_url() . "/track/{$this->id}'>$content</a>";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,28 +39,6 @@ class UsersModel extends BaseModel
|
||||||
$this->raceTypes->race = 2;
|
$this->raceTypes->race = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
public function initialize()
|
|
||||||
{
|
|
||||||
$this->session = session();
|
|
||||||
$this->carsModel = new CarsModel;
|
|
||||||
$this->tracksModel = new TracksModel;
|
|
||||||
|
|
||||||
$this->raceTypes = new \stdClass;
|
|
||||||
$this->raceTypes->practice = 0;
|
|
||||||
$this->raceTypes->qualify = 1;
|
|
||||||
$this->raceTypes->race = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getUsers()
|
|
||||||
{
|
|
||||||
$query = $this->db->table('users')->get();
|
|
||||||
$users = [];
|
|
||||||
|
|
||||||
if ($query && $query->getNumRows() > 0) $users = $query->getResult();
|
|
||||||
return $users;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
public function getUser($username)
|
public function getUser($username)
|
||||||
{
|
{
|
||||||
$builder = $this->builder();
|
$builder = $this->builder();
|
||||||
|
@ -101,57 +79,6 @@ class UsersModel extends BaseModel
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
private function import($properties)
|
|
||||||
{
|
|
||||||
foreach($properties as $key => $value){
|
|
||||||
$this->{$key} = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the flag filename
|
|
||||||
$this->flag = str_replace(' ', '_', $this->nation) . '.png';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getFromDb()
|
|
||||||
{
|
|
||||||
$builder = $this->db->table('users');
|
|
||||||
$builder->where('id', $this->id);
|
|
||||||
$query = $builder->get(1);
|
|
||||||
|
|
||||||
if ($query && $query->getNumRows() == 1)
|
|
||||||
{
|
|
||||||
$this->import($query->getRowArray());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//no valid result found! create a fake user
|
|
||||||
$this->username = '<i>guest</i>';
|
|
||||||
$this->nation = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLink($text='')
|
|
||||||
{
|
|
||||||
if ($text == '') $text=$this->username;
|
|
||||||
return "<a href='" . base_url() . "user/{$this->username}'>$text</a>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSmallFlagImg()
|
|
||||||
{
|
|
||||||
return "<img src='" . base_url() . "img/flags/flags_small/{$this->flag}' alt='{$this->nation}'>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMediumFlagImg()
|
|
||||||
{
|
|
||||||
return "<img src='" . base_url() . "img/flags/flags_medium/{$this->flag}' alt='{$this->nation}'>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getImgFile()
|
|
||||||
{
|
|
||||||
return "<img class='avatar' src='" . base_url() . "img/users/{$this->img}' alt='{$this->username}'>";
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
public function getRaceSessions()
|
public function getRaceSessions()
|
||||||
{
|
{
|
||||||
$builder = $this->db->table('races r');
|
$builder = $this->db->table('races r');
|
||||||
|
|
Loading…
Reference in a new issue