dj_mix_hosting_software/classes/Genre.php
Cody Cook 70c8a87e15
All checks were successful
SonarQube Scan / SonarQube Trigger (push) Successful in 30s
Changes.
2024-05-19 20:01:13 -07:00

127 lines
3.0 KiB
PHP

<?php
namespace DJMixHosting;
class Genre
{
private $id = -1;
private $enabled = "";
private $count = 0;
private $name = "";
private $slug = "";
private $db = "";
private $mixes = [];
public function __construct($value, $db)
{
$this->db = $db;
if (ctype_digit((string)$value)) {
$this->id = (int)$value;
return $this->load_by_id();
} else {
$this->slug = $value;
return $this->load_by_slug();
}
}
private function load_by_id(): bool
{
$genre = $this->get_genre_by_id();
if ($genre && $genre['name'] != "") {
$this->enabled = $genre['enabled'];
$this->count = $genre['count'];
$this->name = $genre['name'];
$this->slug = $genre['slug'];
$this->mixes = $this->load_mixes();
return true;
} else {
return false;
}
}
private function get_genre_by_id()
{
$stmt = $this->db->prepare("SELECT * FROM genres WHERE id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$dj = $result->fetch_assoc();
$stmt->close();
return $dj;
}
private function load_by_slug(): bool
{
$genre = $this->get_genre_by_slug();
if ($genre && $genre['name'] != "") {
$this->id = $genre['id'];
$this->enabled = $genre['enabled'];
$this->count = $genre['count'];
$this->name = $genre['name'];
$this->mixes = $this->load_mixes();
return true;
}
return false;
}
private function get_genre_by_slug()
{
$stmt = $this->db->prepare("SELECT * FROM genres WHERE slug = ?");
$stmt->bind_param("s", $this->slug);
$stmt->execute();
$result = $stmt->get_result();
$dj = $result->fetch_assoc();
$stmt->close();
return $dj;
}
public function get_slug(): string
{
return $this->slug;
}
public function get_id(): int
{
return $this->id;
}
public function get_name(): string
{
return $this->name;
}
public function get_img(): string
{
return "/img/no-image1.png";
}
public function get_count(): int
{
return $this->count;
}
public function get_mixes(): array
{
return $this->mixes;
}
private function load_mixes(): array
{
$stmt = $this->db->prepare("SELECT mix_id FROM mix_meta WHERE attribute = 'genre' AND value = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$mixes = [];
while ($row = $result->fetch_assoc()) {
$mixes[] = $row['mix_id'];
}
$stmt->close();
return $mixes;
}
}