Language updates. New upload form. new classes.
This commit is contained in:
parent
4c2857b445
commit
8f3061ab99
62 changed files with 3107 additions and 1883 deletions
456
classes/Mix.php
456
classes/Mix.php
|
@ -2,343 +2,277 @@
|
|||
|
||||
namespace DJMixHosting;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
|
||||
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 int $id = -1;
|
||||
private bool $enabled = false;
|
||||
private string $name = "";
|
||||
private string $slug = "";
|
||||
private $db;
|
||||
private string $description = "";
|
||||
private string $cover = "";
|
||||
private string $url = "";
|
||||
private int $seconds = 0;
|
||||
private bool $download_only = true;
|
||||
private array $djs = [];
|
||||
private array $genres = [];
|
||||
private ?string $recorded = null;
|
||||
private int $downloads = 0;
|
||||
private ?string $created = null;
|
||||
private ?string $updated = null;
|
||||
private int $playcount = 0;
|
||||
private $tracklist = [];
|
||||
private $loadDJs = true;
|
||||
private $related_mixes = [];
|
||||
private $duration = [];
|
||||
private $mixshow = [];
|
||||
private bool $loadDJs = true;
|
||||
private array $related_mixes = [];
|
||||
private array $duration = [];
|
||||
private array $mixshow = [];
|
||||
|
||||
|
||||
public function __construct($value, $db, $loadDJs = true)
|
||||
/**
|
||||
* Construct a Mix object using either an ID or slug.
|
||||
*
|
||||
* @param mixed $value The mix identifier (ID or slug).
|
||||
* @param mixed $db Database connection.
|
||||
* @param bool $loadDJs Whether to load DJ objects.
|
||||
*/
|
||||
public function __construct($value, $db, bool $loadDJs = true)
|
||||
{
|
||||
$this->db = $db;
|
||||
|
||||
// echo the type of value
|
||||
$this->loadDJs = $loadDJs;
|
||||
|
||||
if (ctype_digit((string)$value)) {
|
||||
$this->id = (int)$value;
|
||||
return $this->load_by_id();
|
||||
|
||||
$loaded = $this->loadById();
|
||||
} else {
|
||||
$this->slug = $value;
|
||||
return $this->load_by_slug();
|
||||
$loaded = $this->loadBySlug();
|
||||
}
|
||||
|
||||
if (!$loaded) {
|
||||
throw new Exception("Mix not found.");
|
||||
}
|
||||
}
|
||||
|
||||
private function load_by_id(): bool
|
||||
/**
|
||||
* Loads mix data by its ID.
|
||||
*/
|
||||
private function loadById(): bool
|
||||
{
|
||||
$mix = $this->get_mix_by_id();
|
||||
if ($mix && $mix['title'] != "") {
|
||||
return $this->build_mix($mix);
|
||||
} else {
|
||||
return false;
|
||||
$mix = $this->getMixById();
|
||||
if ($mix && !empty($mix['title'])) {
|
||||
return $this->buildMix($mix);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function get_mix_by_id()
|
||||
/**
|
||||
* Loads mix data by its slug.
|
||||
*/
|
||||
private function loadBySlug(): bool
|
||||
{
|
||||
// Check if current user is admin
|
||||
$isAdmin = (isset($_SESSION['user']) && isset($_SESSION['user']['role']) && $_SESSION['user']['role'] === 'admin');
|
||||
|
||||
if ($isAdmin) {
|
||||
$stmt = $this->db->prepare("SELECT * FROM mix WHERE id = ?");
|
||||
} else {
|
||||
// Only return approved mixes (pending = 0) for non-admins
|
||||
$stmt = $this->db->prepare("SELECT * FROM mix WHERE id = ? AND pending = 0");
|
||||
$mix = $this->getMixBySlug();
|
||||
if ($mix && !empty($mix['title'])) {
|
||||
return $this->buildMix($mix);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve mix data by ID.
|
||||
*/
|
||||
private function getMixById(): ?array
|
||||
{
|
||||
// If the current user is admin, show all mixes;
|
||||
// Otherwise, only return mixes with pending = 0.
|
||||
$isAdmin = isset($_SESSION['user']) &&
|
||||
isset($_SESSION['user']['role']) &&
|
||||
$_SESSION['user']['role'] === 'admin';
|
||||
$query = $isAdmin
|
||||
? "SELECT * FROM mix WHERE id = ?"
|
||||
: "SELECT * FROM mix WHERE id = ? AND pending = 0";
|
||||
|
||||
$stmt = $this->db->prepare($query);
|
||||
$stmt->bind_param("i", $this->id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
$mix = $result->fetch_assoc();
|
||||
$stmt->close();
|
||||
return $mix;
|
||||
return $mix ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mix
|
||||
* @return true
|
||||
* Retrieve mix data by slug.
|
||||
*/
|
||||
private function build_mix($mix): bool
|
||||
private function getMixBySlug(): ?array
|
||||
{
|
||||
$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'];
|
||||
$isAdmin = isset($_SESSION['user']) &&
|
||||
isset($_SESSION['user']['role']) &&
|
||||
$_SESSION['user']['role'] === 'admin';
|
||||
$query = $isAdmin
|
||||
? "SELECT * FROM mix WHERE slug = ?"
|
||||
: "SELECT * FROM mix WHERE slug = ? AND pending = 0";
|
||||
|
||||
$stmt = $this->db->prepare($query);
|
||||
$stmt->bind_param("s", $this->slug);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
$mix = $result->fetch_assoc();
|
||||
$stmt->close();
|
||||
return $mix ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the mix object from database data.
|
||||
*/
|
||||
private function buildMix(array $mix): bool
|
||||
{
|
||||
$this->id = (int)$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 = (int)($mix['seconds'] ?? 0);
|
||||
$this->duration = $this->configureDuration();
|
||||
$this->download_only = (bool)($mix['mediaplayer'] ?? true);
|
||||
$this->recorded = $mix['recorded'] ?? null;
|
||||
$this->created = $mix['created'] ?? null;
|
||||
$this->updated = $mix['lastupdated'] ?? null;
|
||||
$this->enabled = (bool)($mix['pending'] ?? false);
|
||||
|
||||
if ($this->loadDJs) {
|
||||
require_once 'DJ.php';
|
||||
$this->djs[] = new DJ($mix['dj1'], $this->db);
|
||||
if ($mix['dj2'] != null) {
|
||||
if (!empty($mix['dj2'])) {
|
||||
$this->djs[] = new DJ($mix['dj2'], $this->db);
|
||||
}
|
||||
if ($mix['dj3'] != null) {
|
||||
if (!empty($mix['dj3'])) {
|
||||
$this->djs[] = new DJ($mix['dj3'], $this->db);
|
||||
}
|
||||
|
||||
$this->djs = array_filter($this->djs);
|
||||
}
|
||||
|
||||
$this->load_mix_meta();
|
||||
$this->tracklist = $this->evaluate_tracklist();
|
||||
$this->loadMixMeta();
|
||||
$this->tracklist = $this->evaluateTracklist();
|
||||
return true;
|
||||
}
|
||||
|
||||
private function legacyFix(mixed $item)
|
||||
/**
|
||||
* Fix legacy URL paths.
|
||||
*/
|
||||
private function legacyFix(string $item): string
|
||||
{
|
||||
if (str_starts_with($item, "/djs/")) {
|
||||
return "https://cdn.utahsdjs.com" . substr($item, 4);
|
||||
} else {
|
||||
return $item;
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
|
||||
private function configure_duration(): array
|
||||
/**
|
||||
* Configure a formatted duration based on seconds.
|
||||
*/
|
||||
private function configureDuration(): 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];
|
||||
$secs = $seconds % 60;
|
||||
$time = ($hours > 0)
|
||||
? sprintf("%02d:%02d:%02d", $hours, $minutes, $secs)
|
||||
: sprintf("%02d:%02d", $minutes, $secs);
|
||||
return [
|
||||
'h' => $hours,
|
||||
'm' => $minutes,
|
||||
's' => $secs,
|
||||
't' => $time,
|
||||
'S' => $this->seconds
|
||||
];
|
||||
}
|
||||
|
||||
private function load_mix_meta(): void
|
||||
/**
|
||||
* Load mix meta data.
|
||||
*/
|
||||
private function loadMixMeta(): void
|
||||
{
|
||||
$stmt = $this->db->prepare("SELECT attribute,value FROM mix_meta WHERE mix_id = ?");
|
||||
$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]);
|
||||
foreach ($meta as $entry) {
|
||||
switch ($entry['attribute']) {
|
||||
case "genre":
|
||||
$this->genres[] = $entry['value'];
|
||||
break;
|
||||
case "related":
|
||||
$this->related_mixes[] = $entry['value'];
|
||||
break;
|
||||
case "playcount":
|
||||
$this->playcount = (int)$entry['value'];
|
||||
break;
|
||||
case "downloads":
|
||||
$this->downloads = (int)$entry['value'];
|
||||
break;
|
||||
case "tracklist":
|
||||
$this->tracklist = $entry['value'];
|
||||
break;
|
||||
case "mixshow":
|
||||
$this->mixshow[] = $entry['value'];
|
||||
break;
|
||||
default:
|
||||
// Handle additional meta attributes if needed.
|
||||
break;
|
||||
}
|
||||
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()
|
||||
/**
|
||||
* Evaluate the tracklist data.
|
||||
*/
|
||||
private function evaluateTracklist()
|
||||
{
|
||||
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;
|
||||
if (is_array($this->tracklist)) {
|
||||
return $this->tracklist;
|
||||
}
|
||||
return explode("\n", (string)$this->tracklist);
|
||||
}
|
||||
|
||||
// Getter methods for mix properties.
|
||||
|
||||
private function get_mix_by_slug()
|
||||
public function getId(): int { return $this->id; }
|
||||
public function getName(): string { return $this->name; }
|
||||
public function getSlug(): string { return $this->slug; }
|
||||
public function getDescription(): string { return $this->description; }
|
||||
public function getCover(): string { return $this->cover; }
|
||||
public function getUrl(): string { return $this->url; }
|
||||
public function getSeconds(): int { return $this->seconds; }
|
||||
public function isDownloadOnly(): bool { return $this->download_only; }
|
||||
public function getDJs(): array { return $this->djs; }
|
||||
public function getGenres(): array { return $this->genres; }
|
||||
public function getRecorded(): ?string { return $this->recorded; }
|
||||
public function getDownloads(): int { return $this->downloads; }
|
||||
public function getCreated(): ?string { return $this->created; }
|
||||
public function getUpdated(): ?string { return $this->updated; }
|
||||
public function getPlaycount(): int { return $this->playcount; }
|
||||
public function getTracklist(): array { return $this->evaluateTracklist(); }
|
||||
public function getRelatedMixes(): array { return $this->related_mixes; }
|
||||
public function getDuration(): array { return $this->duration; }
|
||||
public function getMixshow(): array { return $this->mixshow; }
|
||||
|
||||
public function getDownloadUrl(): string
|
||||
{
|
||||
// Check if current user is admin
|
||||
$isAdmin = (isset($_SESSION['user']) && isset($_SESSION['user']['role']) && $_SESSION['user']['role'] === 'admin');
|
||||
|
||||
if ($isAdmin) {
|
||||
$stmt = $this->db->prepare("SELECT * FROM mix WHERE slug = ?");
|
||||
} else {
|
||||
$stmt = $this->db->prepare("SELECT * FROM mix WHERE slug = ? AND pending = 0");
|
||||
}
|
||||
$stmt->bind_param("s", $this->slug);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
$mix = $result->fetch_assoc();
|
||||
$stmt->close();
|
||||
return $mix;
|
||||
return "https://beta.utahsdjs.com/mix/{$this->slug}/download";
|
||||
}
|
||||
|
||||
public function get_recorded()
|
||||
public function getPageUrl(): string
|
||||
{
|
||||
return $this->recorded;
|
||||
return "https://beta.utahsdjs.com/mix/{$this->slug}";
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function get_download_url()
|
||||
{
|
||||
return "https://beta.utahsdjs.com/mix/" . "$this->slug" . "/download";
|
||||
}
|
||||
|
||||
public function get_page_url()
|
||||
{
|
||||
return "https://beta.utahsdjs.com/mix/" . "$this->slug";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue