dj_mix_hosting_software/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

472 lines
23 KiB
PHP

<?php
require_once 'includes/globals.php';
require_once 'vendor/autoload.php';
use DJMixHosting\Database;
use DJMixHosting\Genre;
use DJMixHosting\Mix;
use DJMixHosting\Mixshow;
use DJMixHosting\Playcount;
$db = new Database($config);
$mixFound = false;
if (isset($_GET['mix']) && $_GET['mix'] != "") {
$mix = new Mix($_GET['mix'], $db);
if ($mix->get_name() != "") {
$mixFound = true;
$title = $mix->get_name();
} else {
$title = $locale['notfound'];
}
} else {
$title = $locale['notfound'];
}
// if this is a playcount PUT request, update the playcount
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['mix']) && $data['mix'] != "") {
$playcount = new Playcount($data['mix'], $db);
$playcount->updatePlaycount();
}
}
require_once 'includes/header.php'; ?>
<section>
<div class="container py-5">
<div class="row">
<div class="col">
<nav aria-label="breadcrumb" class="bg-body-tertiary rounded-3 p-3 mb-4">
<ol class="breadcrumb mb-0">
<li class="breadcrumb-item"><a href="/"><?php echo $locale['home']; ?></a></li>
<li class="breadcrumb-item"><a href="/mixes.php"><?php echo $locale['mixes']; ?></a></li>
<li class="breadcrumb-item active"
aria-current="page"><?php
if (isset($mix) && $mix->get_name() != "") {
echo $mix->get_name();
} else {
echo $locale['notfound'];
}
?></li>
</ol>
</nav>
</div>
</div>
<?php if ($mixFound): ?>
<div class="row">
<div class="col-lg-4">
<div class="card mb-4">
<div class="card-body bg-body-secondary text-center">
<?php
if ($mix->get_img() != "") {
echo "<img src='" . $mix->get_img() . "' alt='avatar' class='img-fluid' style='width: 150px;'>";
} ?>
<h1 class="my-3 fs-4"><?php echo $mix->get_name();
?></h1>
<?php
if ($mix->get_description() != "") {
echo "<h2 class='text-muted mb-4 fs-6'>" . $mix->get_description() . "</h2>";
}
?>
</p>
</div>
</div>
<div class="card mb-4">
<div class="card-body bg-body-secondary text-center">
<?php
if ($mix->is_download_only()) {
echo "<a href='/mix/" . $mix->get_slug() . "/download" . "' class='btn btn-primary'>" . $locale['download'] . "</a>";
} else {
echo "<audio controls controlsList='nodownload'>";
$url = $mix->get_url();
echo "<source src='" . $url . "' type='audio/mpeg'>";
echo $locale['audioNotSupported'];
echo "</audio>";
}
?>
</div>
</div>
<div class="card mb-4">
<div class="card-body bg-body-secondary text-center">
<a href="#" class="btn btn-secondary w-100 mb-2"
id="shareBtn"><?php echo $locale['share']; ?></a>
<?php if (!$mix->is_download_only()) : ?>
<a href="<?php
echo "/mix/" . $mix->get_slug() . "/download";
?>"
class="btn btn-primary w-100 mb-2"><?php echo $locale['download']; ?></a>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-lg-8">
<div class="card mb-4">
<div class="card-body bg-body-secondary">
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['mixname'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0"><?php echo $mix->get_name(); ?></p>
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['djs'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php
// loop through the $mix['djs'] array and output them in comma separated format
$djs = $mix->get_djs();
$djCount = count($djs);
$i = 0;
foreach ($djs as $dj) {
echo "<a href='/dj/";
echo $dj->getSlug();
echo "'>" . $dj->getName() . "</a>";
if ($i < $djCount - 1) {
echo ", ";
}
$i++;
}
?>
</p>
</div>
</div>
<?php
$genres = $mix->get_genres();
$genreCount = count($genres);
if ($genreCount > 0) {
?>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['genres'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php
$i = 0;
foreach ($genres as $genre) {
$genre = new Genre($genre, $db);
echo "<a href='/genre/";
echo $genre->get_slug();
echo "'>" . $genre->get_name() . "</a>";
if ($i < $genreCount - 1) {
echo ", ";
}
$i++;
}
?>
</div>
</div><?php } ?>
<?php
$mixshows = $mix->get_mixshow();
$mixshowsCount = count($mixshows);
if ($mixshowsCount > 0) {
?>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['mixshow'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php
$i = 0;
foreach ($mixshows as $mixshow) {
$mixshow = new Mixshow($mixshow, $db);
echo "<a href='/mixshow/";
echo $mixshow->get_slug();
echo "'>" . $mixshow->get_name() . "</a>";
if ($i < $mixshowsCount - 1) {
echo ", ";
}
$i++;
}
?>
</div>
</div><?php } ?>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['duration'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php
$time = $mix->get_duration();
// Decide the correct singular or plural term
$hour_text = $time['h'] == 1 ? $locale['hour'] : $locale['hours'];
$minute_text = $time['m'] == 1 ? $locale['minute'] : $locale['minutes'];
$second_text = $time['s'] == 1 ? $locale['second'] : $locale['seconds'];
// Output the time, skipping hours if it is less than one hour
if ($time['h'] > 0) {
// If there are hours, always show hours.
echo $time['h'] . " " . $hour_text;
if ($time['m'] > 0) {
// Show minutes only if they are greater than 0.
echo ", " . $time['m'] . " " . $minute_text;
}
// Always show seconds, regardless of minutes.
echo ", " . $time['s'] . " " . $second_text;
} else {
// No hours, check minutes.
if ($time['m'] != 0) {
echo $time['m'] . " " . $minute_text . ", " . $time['s'] . " " . $second_text;
} else {
// Only seconds to show.
echo $time['s'] . " " . $second_text;
}
}
echo " (" . $time['t'] . ")";
?>
</div>
</div>
<?php if ($mix->get_recorded() != ""): ?>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['recorded'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php echo $mix->get_recorded(); ?>
</div>
</div>
<?php endif; ?>
<?php if ($mix->get_created() != ""): ?>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['added'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php echo $mix->get_created(); ?>
</div>
</div>
<?php endif; ?>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['downloads'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php echo $mix->get_downloads(); ?>
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['plays'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php echo $mix->get_plays(); ?>
</div>
</div>
<?php if ($mix->get_updated() != ""): ?>
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0"><?php echo $locale['lastupdated'] ?></p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">
<?php echo $mix->get_updated(); ?>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const audioElement = document.querySelector('audio');
if ('mediaSession' in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: '<?php echo addslashes($mix->get_name()); ?>',
artist: '<?php
$djs = $mix->get_djs();
$djCount = count($djs);
$i = 0;
$djnamelist = [];
foreach ($djs as $dj) {
$djnamelist[] = $dj->getName();
$i++;
}
echo addslashes(implode(", ", $djnamelist));?>',
album: '<?php echo addslashes($mix->get_name()); ?>',
artwork: [
{
src: '<?php echo $mix->get_cover('small'); ?>',
sizes: '96x96',
type: 'image/jpeg'
},
{
src: '<?php echo $mix->get_cover('large'); ?>',
sizes: '128x128',
type: 'image/jpeg'
}
]
});
// Define action handlers
navigator.mediaSession.setActionHandler('play', function () {
audioElement.play();
});
navigator.mediaSession.setActionHandler('pause', function () {
audioElement.pause();
});
audioElement.addEventListener('timeupdate', () => {
// Update the position state
navigator.mediaSession.setPositionState({
duration: audioElement.duration,
playbackRate: audioElement.playbackRate,
position: audioElement.currentTime
});
});
}
});
</script>
<?php endif; ?>
</div>
</div>
</div>
<?php
if ($mix->get_tracklist() != []) {
echo "<div class='card mb-4 bg-body-secondary'>";
echo "<div class='card-body '>";
echo "<p class='mb-4'><span class='text-primary font-italic me-1'>" . $locale['tracklist'] . "</span></p>";
echo "<ul class='list-group list-group-flush rounded-3 bg-body-secondary'>";
$tracklist = $mix->get_tracklist();
foreach ($tracklist as $track) {
echo "<li class='list-group-item bg-body-secondary d-flex justify-content-between align-items-center'>";
echo $track;
echo "</li>";
}
echo "</ul>";
echo "</div>";
echo "</div>";
}
?>
</div>
</div>
</div>
<div id="shareModal" class="modal">
<div class="modal-content">
<span class="close">&times;</span>
<h2><?php echo $locale['share']; ?></h2>
<a href="#" id="copyLinkBtn" class="btn btn-secondary w-100 mb-2">Copy URL</a>
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode('https://utahsdjs.com/mix/' . $_GET['mix']); ?>"
target="_blank" class="btn btn-primary w-100 mb-2">Share to Facebook</a>
<a href="https://twitter.com/intent/tweet?url=<?php echo urlencode('https://utahsdjs.com/mix/' . $_GET['mix']); ?>"
target="_blank" class="btn btn-info w-100 mb-2">Share to X (Twitter)</a>
<a href="https://www.instagram.com/" target="_blank" class="btn btn-danger w-100">Share to Instagram</a>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById("shareModal");
// Get the button that opens the modal
var btn = document.getElementById("shareBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// Get the copy link button
var copyLinkBtn = document.getElementById("copyLinkBtn");
// Get the URL to be copied
var urlToCopy = window.location.href;
// When the user clicks the button, open the modal
btn.onclick = function () {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
// When the user clicks the copy link button, copy the URL to the clipboard
copyLinkBtn.onclick = function () {
navigator.clipboard.writeText(urlToCopy).then(function () {
alert('URL copied to clipboard');
}, function (err) {
alert('Failed to copy URL: ', err);
});
}
</script>
<?php else: ?>
<div class="row">
<div class="col">
<div class="alert alert-danger" role="alert">
<?php echo $locale['mixNotFound']; ?>
</div>
</div>
</div>
<?php endif;
?>
</div>
</section>
<script>
// when the audio is played, send a PUT request to the page to update the playcount
document.addEventListener('DOMContentLoaded', function () {
const audioElement = document.querySelector('audio');
audioElement.addEventListener('play', function () {
fetch(window.location.href, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({mix: '<?php echo $mix->get_id(); ?>'}),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
});
});
</script>
<?php require_once 'includes/footer.php'; ?>