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

241 lines
6.0 KiB
PHP

<?php
namespace DJMixHosting;
class DJ
{
private int $id = -1;
private string $name = "";
private string $bio = "";
private string $slug = "";
private string $img = "/img/no-image1.png";
private bool $active = false;
private bool $claimed = false;
private string $email = "";
private array $socials = [];
private string $created = "";
private string $updated = "";
private string $claimedBy = "";
private $db;
private array $mixes = [];
public function __construct($value, $db)
{
$this->db = $db;
if (ctype_digit((string)$value)) {
$this->id = (int)$value;
return $this->loadByID();
} else {
$this->slug = $value;
return $this->loadBySlug();
}
}
private function loadByID(): bool
{
$dj = $this->getDJbyID($this->id);
return $this->buildDJ($dj);
}
private function getDJbyID()
{
$stmt = $this->db->prepare("SELECT * FROM djs WHERE id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$dj = $result->fetch_assoc();
$stmt->close();
return $dj;
}
/**
* @param $dj
* @return bool
*/
private function buildDJ($dj): bool
{
if ($dj) {
if (isset($dj['id'])) {
$this->id = $dj['id'];
}
if (isset($dj['name'])) {
$this->name = $dj['name'];
}
if (isset($dj['bio'])) {
$this->bio = $dj['bio'];
}
if (isset($dj['slug'])) {
$this->slug = $dj['slug'];
}
if (isset($dj['img'])) {
// is this legacy code?
// the code is legacy if it starts with /dj/,
// if it does, prefix with https://www.utahsdjs.com
if (substr($dj['img'], 0, 5) == "/djs/") {
// remove /djs/ from the string
$dj['img'] = substr($dj['img'], 4);
$this->img = "https://cdn.utahsdjs.com" . $dj['img'];
} else {
$this->img = $dj['img'];
}
}
if (isset($dj['email'])) {
$this->email = $dj['email'];
}
if (isset($dj['facebook_url'])) {
$this->socials['facebook'] = $dj['facebook_url'];
}
if (isset($dj['instagram_url'])) {
$this->socials['instagram'] = $dj['instagram_url'];
}
if (isset($dj['twitter_url'])) {
$this->socials['twitter'] = $dj['twitter_url'];
}
if (isset($dj['myspace_url'])) {
$this->socials['myspace'] = $dj['myspace_url'];
}
if (isset($dj['soundcloud_url'])) {
$this->socials['soundcloud'] = $dj['soundcloud_url'];
}
if (isset($dj['mixcloud_url'])) {
$this->socials['mixcloud'] = $dj['mixcloud_url'];
}
if (isset($dj['spotify_url'])) {
$this->socials['spotify'] = $dj['spotify_url'];
}
if (isset($dj['active'])) {
$this->active = $dj['active'];
}
if (isset($dj['claimed_by']) && $dj['claimed_by'] != null) {
// TODO: pull some quick data on the user who claimed this DJ
$this->claimed = true;
}
if (isset($dj['created'])) {
$this->created = $dj['created'];
}
if (isset($dj['lastupdated'])) {
$this->updated = $dj['lastupdated'];
}
$this->loadDJMixes();
return true;
} else {
return false;
}
}
private function loadDJMixes(): void
{
$stmt = $this->db->prepare("SELECT * FROM mix WHERE dj1 = ? OR dj2 = ? OR dj3 = ?");
$stmt->bind_param("iii", $this->id, $this->id, $this->id);
$stmt->execute();
$result = $stmt->get_result();
$mixes = [];
while ($mix = $result->fetch_assoc()) {
$mixes[] = $mix;
}
$stmt->close();
$this->mixes = $mixes;
}
private function loadBySlug(): bool
{
$socials = [];
$dj = $this->getDJbySlug($this->slug);
return $this->buildDJ($dj);
}
private function getDJbySlug($slug)
{
$stmt = $this->db->prepare("SELECT * FROM djs WHERE slug = ?");
$stmt->bind_param("s", $slug);
$stmt->execute();
$result = $stmt->get_result();
$dj = $result->fetch_assoc();
$stmt->close();
return $dj;
}
public function getSlug(): string
{
return $this->slug;
}
public function getID(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function getBio(): string
{
return $this->bio;
}
public function getImg(): string
{
return $this->img;
}
public function getActive(): bool
{
return $this->active;
}
public function getEmail(): string
{
return $this->email;
}
public function getSocials(): array
{
return $this->socials;
}
public function getCreated(): string
{
return $this->created;
}
public function getUpdated(): string
{
return $this->updated;
}
public function getClaimed(): bool
{
return $this->claimed;
}
public function getDJMixes()
{
return $this->mixes;
}
public function getClaimedBy()
{
return $this->claimedBy;
}
}