dj_mix_hosting_software/classes/Mix.php
2024-04-30 01:07:22 -07:00

195 lines
5 KiB
PHP

<?php
class Mix
{
private $id = -1;
private $enabled = false;
private $name = "";
private $slug = "";
private $genre = [];
private $db = null;
private $description = "";
private $cover = "";
private $url = "";
private $seconds = 0;
private $mediaplayer = false;
private $djs = [];
private $genres = [];
private $recorded;
private $created;
private $updated;
private $playcount = 0;
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
{
$mix = $this->get_mix_by_id();
if ($mix && $mix['name'] != "") {
return $this->build_mix($mix);
} else {
return false;
}
}
private function get_mix_by_id()
{
$stmt = $this->db->prepare("SELECT * FROM mix WHERE id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$mix = $result->fetch_assoc();
$stmt->close();
return $mix;
}
private function get_mix_genres()
{
$stmt = $this->db->prepare("SELECT * FROM mix_meta WHERE attribute = 'genre' AND mix_id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$genres = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $genres;
}
private function get_playcount()
{
$stmt = $this->db->prepare("SELECT value FROM mix_meta WHERE attribute = 'playcount' AND mix_id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$genres = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $genres;
}
private function load_by_slug(): bool
{
$mix = $this->get_mix_by_slug();
if ($mix['title'] != "") {
return $this->build_mix($mix);
} else {
return false;
}
}
private function get_mix_by_slug()
{
$stmt = $this->db->prepare("SELECT * FROM mix WHERE slug = ?");
$stmt->bind_param("s", $this->slug);
$stmt->execute();
$result = $stmt->get_result();
$mix = $result->fetch_assoc();
$stmt->close();
return $mix;
}
private function load_mix_genres()
{
$genres = $this->get_mix_genres();
if (count($genres) == 0) {
$this->genres = [];
} else {
// iterate through the genres; add each of the rest to the array
$this->genres = [];
require_once 'Genre.php';
foreach ($genres as $genre) {
$genre = new Genre($genre['value'], $this->db);
if ($genre->get_id() != -1) {
$this->genres[] = ['id' => $genre->get_id(), 'name' => $genre->get_name(), 'slug' => $genre->get_slug()];
}
}
}
}
public function get_img(): string
{
return $this->cover;
}
public function get_id(): int
{
return $this->id;
}
public function get_name(): string
{
return $this->name;
}
public function get_slug(): string
{
return $this->slug;
}
public function get_djs(){
return $this->djs;
}
/**
* @param $mix
* @return true
*/
private function build_mix($mix): bool
{
$this->id = $mix['id'];
$this->name = $mix['title'];
$this->slug = $mix['slug'];
$this->description = $mix['description'];
if (isset($mix['cover'])) {
// is this legacy code?
// the code is legacy if it starts with /dj/,
// if it does, prefix with https://www.utahsdjs.com
if (substr($mix['cover'], 0, 5) == "/djs/") {
$mix['cover'] = substr($mix['cover'], 4);
$this->cover = "https://cdn.utahsdjs.com" . $mix['cover'];
} else {
$this->cover = $mix['cover'];
}
}
$this->url = $mix['url'];
$this->seconds = $mix['seconds'];
$this->mediaplayer = $mix['mediaplayer'];
$this->recorded = $mix['recorded'];
$this->created = $mix['created'];
$this->updated = $mix['lastupdated'];
$this->enabled = $mix['pending'];
require 'DJ.php';
$this->djs[] = new DJ($mix['dj1'], $this->db);
if ($mix['dj2'] != null)
$this->djs[] = new DJ($mix['dj2'], $this->db);
if ($mix['dj3'] != null)
$this->djs[] = new DJ($mix['dj3'], $this->db);
// delete any nulls from the array
$this->djs = array_filter($this->djs);
$this->genre = $this->get_mix_genres();
$this->playcount = $this->get_playcount();
return true;
}
}