sdwebserver/app/Models/CarsModel.php

94 lines
2.4 KiB
PHP
Raw Normal View History

2022-03-14 19:13:10 +01:00
<?php
namespace App\Models;
use App\Models\BaseModel;
2022-03-14 19:13:10 +01:00
class CarsModel extends BaseModel
2022-03-14 19:13:10 +01:00
{
protected $table = 'cars';
protected $allowedFields = ['id', 'name', 'img', 'category', 'width', 'length', 'mass', 'fueltank', 'engine', 'drivetrain'];
public function getMostUsedCars(array $carsCatIds, string $period='today', int $page=0, int $limit=20)
{
$list = [];
$total = 0;
$offset = $page * $limit;
$backto = getDateDiff($period);
$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) $total = $query->getNumRows();
if ($total == 0) return [[], 0];
$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');
if ($limit > 0) $builder->limit($limit, $offset);
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $list = $query->getResult();
return [$list, $total];
}
/*
public function __construct($car=null)
2022-03-14 19:13:10 +01:00
{
if ($car) $this->data = $this->data($car);
2022-03-14 19:13:10 +01:00
}
public function getLink(string $text=null): string
2022-03-14 19:13:10 +01:00
{
if(!$text) $text = $this->data->name;
return "<a href='". base_url() . "'/car/{$this->data->id}'>$text</a>";
2022-03-14 19:13:10 +01:00
}
public function card() {
return $this->data->name . $this->data->img;
2022-03-14 19:13:10 +01:00
}
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>";
}
*/
2022-03-14 19:13:10 +01:00
}