diff --git a/classes/DJ.php b/classes/DJ.php index 0fe5a41..56514fe 100644 --- a/classes/DJ.php +++ b/classes/DJ.php @@ -19,21 +19,43 @@ class DJ private $db = null; - public function __construct($slug, $db) + public function __construct($value, $db) { - $this->slug = $slug; $this->db = $db; - if (!$this->load_from_slug()) { - return false; + if (ctype_digit((string)$value)) { + $this->id = (int)$value; + return $this->load_from_id(); + } else { - return true; + $this->slug = $value; + return $this->load_from_slug(); } } - private function load_from_slug(): bool + private function load_from_id(): bool { $socials = []; - $dj = $this->get_dj_by_slug($this->slug); + $dj = $this->get_dj_by_id($this->id); + return $this->build_dj($dj); + } + + private function get_dj_by_id() + { + $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 build_dj($dj): bool + { if ($dj) { if (isset($dj['id'])) { $this->id = $dj['id']; @@ -44,6 +66,10 @@ class DJ if (isset($dj['bio'])) { $this->bio = $dj['bio']; } + if (isset($dj['slug'])) { + $this->slug = $dj['slug']; + } + if (isset($dj['img'])) { // is this legacy code? @@ -109,6 +135,13 @@ class DJ } } + private function load_from_slug(): bool + { + $socials = []; + $dj = $this->get_dj_by_slug($this->slug); + return $this->build_dj($dj); + } + private function get_dj_by_slug($slug) { $stmt = $this->db->prepare("SELECT * FROM djs WHERE slug = ?"); diff --git a/classes/Genre.php b/classes/Genre.php index 8a74623..f724797 100644 --- a/classes/Genre.php +++ b/classes/Genre.php @@ -3,9 +3,9 @@ class Genre { - private $id = "" ; + private $id = -1 ; private $enabled = ""; - private $count = ""; + private $count = 0; private $name = ""; private $slug = ""; private $db = ""; @@ -13,25 +13,15 @@ class Genre public function __construct($value, $db) { $this->db = $db; - if (intval($value)) { - $this->id = $value; - if ($this->load_by_id()) { - return true; - } else { - return false; - } + + if (ctype_digit((string)$value)) { + $this->id = (int)$value; + return $this->load_by_id(); } else { $this->slug = $value; - if ($this->load_by_slug()) { - return true; - } else { - return false; - } + return $this->load_by_slug(); } - - } - private function load_by_id(): bool { $genre = $this->get_genre_by_id(); @@ -98,5 +88,14 @@ class Genre return $this->name; } + public function get_img(): string + { + return "img/no-image1.png"; + } + + public function get_count(): int + { + return $this->count; + } } \ No newline at end of file diff --git a/classes/Genres.php b/classes/Genres.php new file mode 100644 index 0000000..b881859 --- /dev/null +++ b/classes/Genres.php @@ -0,0 +1,53 @@ +db = $db; + if (!$this->load_all_genres()) { + return false; + } else { + return true; + } + + } + + private function load_all_genres(): bool + { + $genres = $this->get_all_genres(); + if ($genres) { + $this->genres = $genres; + return true; + } else { + return false; + } + + + } + + public function get_all_genres($order = "ASC") + { + $stmt = $this->db->prepare("SELECT * FROM genres ORDER BY name $order"); + $stmt->execute(); + $result = $stmt->get_result(); + $genres = $result->fetch_all(MYSQLI_ASSOC); + $stmt->close(); + return $genres; + } + + public function get_nonzero_genres() + { + $stmt = $this->db->prepare("SELECT * FROM genres WHERE count > 0 ORDER BY name ASC"); + $stmt->execute(); + $result = $stmt->get_result(); + $genres = $result->fetch_all(MYSQLI_ASSOC); + $stmt->close(); + return $genres; + } +} \ No newline at end of file diff --git a/classes/Mix.php b/classes/Mix.php new file mode 100644 index 0000000..079718d --- /dev/null +++ b/classes/Mix.php @@ -0,0 +1,195 @@ +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; + } + +} + diff --git a/dj.php b/dj.php index ae9438a..e8a319a 100644 --- a/dj.php +++ b/dj.php @@ -30,7 +30,7 @@ if (isset($_GET['dj']) && $_GET['dj'] != "") {