dj_mix_hosting_software/classes/Mix.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

321 lines
7.9 KiB
PHP

<?php
namespace DJMixHosting;
class Mix
{
private $id = -1;
private $enabled = false;
private $name = "";
private $slug = "";
private $db = null;
private $description = "";
private $cover = "";
private $url = "";
private $seconds = 0;
private $download_only = true;
private $djs = [];
private $genres = [];
private $recorded;
private $downloads = 0;
private $created;
private $updated;
private $playcount = 0;
private $tracklist = [];
private $loadDJs = true;
private $related_mixes = [];
private $duration = [];
private $mixshow = [];
public function __construct($value, $db, $loadDJs = true)
{
$this->db = $db;
// echo the type of value
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['title'] != "") {
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;
}
/**
* @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'];
$this->cover = $this->legacyFix($mix['cover']);
$this->url = $this->legacyFix($mix['url']);
$this->seconds = $mix['seconds'];
$this->duration = $this->configure_duration();
$this->download_only = $mix['mediaplayer'];
$this->recorded = $mix['recorded'];
$this->created = $mix['created'];
$this->updated = $mix['lastupdated'];
$this->enabled = $mix['pending'];
if ($this->loadDJs) {
require_once '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);
}
$this->djs = array_filter($this->djs);
}
$this->load_mix_meta();
$this->tracklist = $this->evaluate_tracklist();
return true;
}
private function legacyFix(mixed $item)
{
if (str_starts_with($item, "/djs/")) {
return "https://cdn.utahsdjs.com" . substr($item, 4);
} else {
return $item;
}
}
private function configure_duration(): array
{
$seconds = $this->seconds;
$hours = floor($seconds / 3600);
$minutes = floor(($seconds / 60) % 60);
$seconds = $seconds % 60;
// for 't', we need to show it as 01:02:03
if ($hours < 10) {
$hours0 = "0" . $hours;
} else {
$hours0 = $hours;
}
if ($minutes < 10) {
$minutes0 = "0" . $minutes;
} else {
$minutes0 = $minutes;
}
if ($seconds < 10) {
$seconds0 = "0" . $seconds;
} else {
$seconds0 = $seconds;
}
// if hours is 0, we don't need to show it
$time = $hours > 0 ? $hours0 . ":" . $minutes0 . ":" . $seconds0 : $minutes0 . ":" . $seconds0;
return ['h' => $hours, 'm' => $minutes, 's' => $seconds, 't' => $time, 'S' => $this->seconds];
}
private function load_mix_meta(): void
{
$stmt = $this->db->prepare("SELECT attribute,value FROM mix_meta WHERE mix_id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$meta = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
foreach ($meta as $key => $value) {
if ($value['attribute'] == "genre") {
$this->genres[] = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "related") {
$this->related_mixes[] = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "playcount") {
$this->playcount = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "downloads") {
$this->downloads = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "tracklist") {
$this->tracklist = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "mixshow") {
$this->mixshow[] = $value['value'];
unset($meta[$key]);
}
}
}
private function evaluate_tracklist()
{
if (empty($this->tracklist)) {
return [];
} else {
// if the first item in the array is also an array, then return it
if (is_array($this->tracklist)) {
return $this->tracklist;
} else {
return explode("\n", (string)$this->tracklist);
}
}
}
private function load_by_slug(): bool
{
$mix = $this->get_mix_by_slug();
if ($mix) {
if ($mix['title'] != "") {
return $this->build_mix($mix);
} else {
return false;
}
} 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;
}
public function get_recorded()
{
return $this->recorded;
}
public function get_created()
{
return $this->created;
}
public function get_updated()
{
return $this->updated;
}
public function get_img()
{
return $this->cover;
}
public function get_djs()
{
return $this->djs;
}
public function get_description()
{
return $this->description;
}
public function get_tracklist(): array
{
return $this->tracklist;
}
public function get_genres()
{
return $this->genres;
}
public function get_seconds(): int
{
return $this->seconds;
}
public function is_download_only(): bool
{
return $this->download_only;
}
public function get_url(): string
{
return $this->url;
}
public function get_cover()
{
return $this->cover;
}
public function get_downloads(): int
{
return $this->downloads;
}
public function get_plays(): int
{
return $this->playcount;
}
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_duration(): array
{
return $this->duration;
}
public function get_mixshow(): array
{
return $this->mixshow;
}
}