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']; 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->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 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['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; } 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(): string { 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(): string { 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; } }