coryd.dev/app/Utils/media.php

167 lines
6.5 KiB
PHP

<?php
if (! function_exists('renderMediaGrid')) {
function renderMediaGrid(array $items, int $count = 0, string $loading = 'lazy')
{
$limit = $count > 0 ? $count : count($items);
$firstType = $items[0]['type'] ?? ($items[0]['grid']['type'] ?? '');
$shapeClass = in_array($firstType, ['books', 'movies', 'tv']) ? 'vertical' : 'square';
echo '<div class="media-grid '.$shapeClass.'">';
foreach (array_slice($items, 0, $limit) as $item) {
$grid = $item['grid'] ?? $item;
$alt = htmlspecialchars($grid['alt'] ?? '');
$image = htmlspecialchars($grid['image'] ?? '');
$title = htmlspecialchars($grid['title'] ?? '');
$subtext = htmlspecialchars($grid['subtext'] ?? '');
$url = $grid['url'] ?? null;
$type = $item['type'] ?? '';
$isVertical = in_array($type, ['books', 'movies', 'tv']);
$imageClass = $isVertical ? 'vertical' : 'square';
$width = $isVertical ? 120 : 150;
$height = $isVertical ? 184 : 150;
$openLink = $url ? '<a class="'.htmlspecialchars($type).'" href="'.htmlspecialchars($url).'" title="'.$alt.'">' : '';
$closeLink = $url ? '</a>' : '';
echo $openLink;
echo '<div class="media-grid-item">';
if ($title || $subtext) {
echo '<div class="meta-text media-highlight">';
if ($title) {
echo '<div class="header">'.$title.'</div>';
}
if ($subtext) {
echo '<div class="subheader">'.$subtext.'</div>';
}
echo '</div>';
}
echo '<img
srcset="'.$image.'?class='.$imageClass.'sm&type=webp '.$width.'w, '.
$image.'?class='.$imageClass.'md&type=webp '.($width * 2).'w"
sizes="(max-width: 450px) '.$width.'px, '.($width * 2).'px"
src="'.$image.'?class='.$imageClass.'sm&type=webp"
alt="'.$alt.'"
loading="'.$loading.'"
decoding="async"
width="'.$width.'"
height="'.$height.'"
>';
echo '</div>';
echo $closeLink;
}
echo '</div>';
}
}
if (! function_exists('renderAssociatedMedia')) {
function renderAssociatedMedia(
array $artists = [],
array $books = [],
array $genres = [],
array $movies = [],
array $posts = [],
array $shows = [],
) {
$sections = [
'artists' => ['icon' => 'headphones', 'css_class' => 'music', 'label' => 'Related artist(s)', 'hasGrid' => true],
'books' => ['icon' => 'books', 'css_class' => 'books', 'label' => 'Related book(s)', 'hasGrid' => true],
'genres' => ['icon' => 'headphones', 'css_class' => 'music', 'label' => 'Related genre(s)', 'hasGrid' => false],
'movies' => ['icon' => 'movie', 'css_class' => 'movies', 'label' => 'Related movie(s)', 'hasGrid' => true],
'posts' => ['icon' => 'article', 'css_class' => 'article', 'label' => 'Related post(s)', 'hasGrid' => false],
'shows' => ['icon' => 'device-tv-old', 'css_class' => 'tv', 'label' => 'Related show(s)', 'hasGrid' => true],
];
$allMedia = compact('artists', 'books', 'genres', 'movies', 'posts', 'shows');
echo '<div class="associated-media">';
foreach ($sections as $key => $section) {
$items = $allMedia[$key];
if (empty($items)) {
continue;
}
echo '<h3 id="'.htmlspecialchars($key).'" class="'.htmlspecialchars($section['css_class']).'">';
echo getTablerIcon($section['icon']);
echo htmlspecialchars($section['label']);
echo '</h3>';
if ($section['hasGrid']) {
renderMediaGrid($items);
} else {
echo '<ul>';
foreach ($items as $item) {
echo '<li class="'.htmlspecialchars($section['css_class']).'">';
echo '<a href="'.htmlspecialchars($item['url']).'">'.htmlspecialchars($item['title'] ?? $item['name'] ?? '').'</a>';
if ($key === 'artists' && isset($item['total_plays']) && $item['total_plays'] > 0) {
echo ' ('.htmlspecialchars($item['total_plays']).' play'.($item['total_plays'] > 1 ? 's' : '').')';
} elseif ($key === 'books' && isset($item['author'])) {
echo ' by '.htmlspecialchars($item['author']);
} elseif (($key === 'movies' || $key === 'shows') && isset($item['year'])) {
echo ' ('.htmlspecialchars($item['year']).')';
} elseif ($key === 'posts' && isset($item['date'])) {
echo ' ('.date('F j, Y', strtotime($item['date'])).')';
}
echo '</li>';
}
echo '</ul>';
}
}
echo '</div>';
}
}
if (! function_exists('renderMediaLinks')) {
function renderMediaLinks(array $data, string $type, int $count = 10): string
{
if (empty($data) || empty($type)) {
return '';
}
$slice = array_slice($data, 0, $count);
if (count($slice) === 0) {
return '';
}
$buildLink = function ($item) use ($type) {
switch ($type) {
case 'genre':
return '<a href="'.htmlspecialchars($item['genre_url']).'">'.htmlspecialchars($item['genre_name']).'</a>';
case 'artist':
return '<a href="'.htmlspecialchars($item['url']).'">'.htmlspecialchars($item['name']).'</a>';
case 'book':
return '<a href="'.htmlspecialchars($item['url']).'">'.htmlspecialchars($item['title']).'</a>';
default:
return '';
}
};
if (count($slice) === 1) {
return $buildLink($slice[0]);
}
$links = array_map($buildLink, $slice);
$last = array_pop($links);
return implode(', ', $links).' and '.$last;
}
}
if (! function_exists('sanitizeMediaString')) {
function sanitizeMediaString(string $str): string
{
$sanitizedString = preg_replace("/[^a-zA-Z0-9\s-]/", '', iconv('UTF-8', 'ASCII//TRANSLIT', $str));
return strtolower(trim(preg_replace("/[\s-]+/", '-', $sanitizedString), '-'));
}
}