Clean code and added some code methods comments

This commit is contained in:
Alfonso Saavedra "Son Link" 2024-12-31 12:10:28 +01:00
parent f1bd63c7ea
commit a0a548550b
No known key found for this signature in database
GPG key ID: D3594BCF897F74D8
4 changed files with 25 additions and 281 deletions

View file

@ -2,8 +2,6 @@
namespace App\Controllers;
use App\Models\CarCatsModel;
use App\Models\TracksModel;
use App\Models\BestLapsModel;
use CodeIgniter\API\ResponseTrait;
class Home extends BaseController
@ -14,13 +12,9 @@ class Home extends BaseController
{
$tplData = [];
$carCatModel = new CarCatsModel;
$bestLapsModel = new BestLapsModel;
// select interested period
if(array_key_exists('period', $_COOKIE))
{
$period = $_COOKIE['period'];
}
if(array_key_exists('period', $_COOKIE)) $period = $_COOKIE['period'];
if($this->request->getGet('period'))
{
@ -33,40 +27,31 @@ class Home extends BaseController
switch ($period)
{
case 'today': //today
$datediff = (1*24*60*60);
$backto = time() - $datediff;
case 'today': // Today
$tplData['periodString'] = 'In the last day';
break;
case 'week': //last week
$datediff = (7*24*60*60);
$backto = time() - $datediff;
case 'week': // Last week
$tplData['periodString'] = 'In the last week';
break;
case 'month': //last month
$datediff = (30*24*60*60);
$backto = time()-$datediff;
case 'month': // Last month
$tplData['periodString'] = 'In the last month';
break;
case 'year': //last year
$datediff = (365*24*60*60);
$backto = time()-$datediff;
case 'year': // Last year
$tplData['periodString'] = 'In the last year';
break;
case 'allTime'://always
$datediff = (50000*24*60*60);
$backto = time() - $datediff;
case 'allTime': // Always
$tplData['periodString'] = 'all time';
break;
default:
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
//select the category to display
// Select the category to display
$catId = $this->request->getGet('cat');
if ($catId)
{
// Check if the cat exists
// Check if the car's category exists
$exists = $carCatModel->find($catId);
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();
$currCat = $carCatModel->find($carCatId);
//$carsCatList = $carCatModel->select('carId')->where('id', $carCatId)->findAll();
$carsCatIds = $carCatModel->getCarsInCat($carCatId);
$tplData['currCat'] = $currCat;
$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 view('main', $tplData);
echo get_footer(['minidt.js', 'home_tables.js']);

View file

@ -8,6 +8,14 @@ class CarsModel extends BaseModel
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)
{
$list = [];
@ -42,52 +50,4 @@ class CarsModel extends BaseModel
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>";
}
*/
}

View file

@ -11,6 +11,14 @@ class TracksModel extends BaseModel
protected $db;
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)
{
$list = [];
@ -45,51 +53,4 @@ class TracksModel extends BaseModel
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>";
}
*/
}

View file

@ -39,28 +39,6 @@ class UsersModel extends BaseModel
$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)
{
$builder = $this->builder();
@ -101,57 +79,6 @@ class UsersModel extends BaseModel
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()
{
$builder = $this->db->table('races r');