web / app /Controllers /VideoController.php
souliya_pps
Devin
feat: update UI and add video 404 page
282827b
Raw History Blame Contribute Delete
6.83 kB
<?php
namespace App\Controllers;
use App\Models\Video;
class VideoController {
public function index() {
$videos = Video::getAll();
$jsonLd = [
'@context' => 'https://schema.org',
'@type' => 'CollectionPage',
'name' => 'ວິດີໂອ - ຄຳສອນພຸດທະ',
'description' => 'ລວມວິດີໂອທັມມະ ແລະ ຄຳສອນຂອງພຣະພຸດທະເຈົ້າ',
'url' => canonicalUrl(),
'inLanguage' => 'lo',
];
return view('pages.video.index', [
'videos' => $videos,
'seo' => [
'title' => 'ວິດີໂອ - ຄຳສອນພຸດທະ',
'description' => 'ລວມວິດີໂອທັມມະ ແລະ ຄຳສອນຂອງພຣະພຸດທະເຈົ້າ',
'keywords' => 'ວິດີໂອ, ທັມມະ, ຄຳສອນພຸດທະ, ພຸດທະສາສະໜາ',
'json_ld' => $jsonLd,
]
]);
}
public function show($id) {
$videos = Video::getAll();
$video = null;
$ytId = null;
foreach ($videos as $item) {
$itemKey = $item['_key'] ?? ($item['_ytId'] ?? '');
if ($itemKey === '' || $itemKey === null) {
$itemKey = (string) ($item['ID'] ?? '');
}
if ((string) $itemKey === (string) $id || (string) ($item['ID'] ?? '') === (string) $id) {
$video = $item;
$ytId = $item['_ytId'] ?? '';
break;
}
}
if (!$video) {
http_response_code(404);
return view('pages.video.404', [
'videoId' => $id,
'seo' => [
'title' => 'ວິດີໂອບໍ່ພົບ - ຄຳສອນພຸດທະ',
'description' => 'ວິດີໂອທີ່ທ່ານຄົ້ນຫາບໍ່ພົບໃນລະບົບ',
]
]);
}
$currentKey = $video['_key'] ?? ($ytId ?: (string) ($video['ID'] ?? ''));
$provider = $video['_provider'] ?? 'youtube';
$videoId = $video['_videoId'] ?? $ytId;
$sourceUrl = $video['link'] ?? '';
$isReel = !empty($video['_isReel']);
$embedUrl = '';
$watchUrl = $sourceUrl;
$sourceLabel = 'ວິດີໂອ';
if ($provider === 'youtube' && $videoId) {
$embedUrl = 'https://www.youtube.com/embed/' . rawurlencode($videoId) . '?autoplay=1&rel=0';
$watchUrl = 'https://www.youtube.com/watch?v=' . rawurlencode($videoId);
$sourceLabel = 'YouTube';
} elseif ($provider === 'facebook' && $sourceUrl) {
/* Official plugin embed: works for public reels/watch/share links
without any app token or session cookie. */
$embedUrl = 'https://www.facebook.com/plugins/video.php?href=' . rawurlencode($sourceUrl)
. '&show_text=false&autoplay=true&width=720';
$watchUrl = $sourceUrl;
$sourceLabel = 'Facebook';
} elseif ($provider === 'drive' && $videoId) {
$embedUrl = 'https://drive.google.com/file/d/' . rawurlencode($videoId) . '/preview';
$watchUrl = 'https://drive.google.com/file/d/' . rawurlencode($videoId) . '/view';
$sourceLabel = 'Google Drive';
} elseif ($provider === 'vimeo' && $videoId) {
$embedUrl = 'https://player.vimeo.com/video/' . rawurlencode($videoId) . '?autoplay=1';
$watchUrl = 'https://vimeo.com/' . rawurlencode($videoId);
$sourceLabel = 'Vimeo';
} elseif ($provider === 'file') {
$embedUrl = $sourceUrl;
$sourceLabel = 'ວິດີໂອ';
}
$otherVideos = array_filter($videos, function($item) use ($currentKey) {
$key = $item['_key'] ?? ($item['_ytId'] ?? '');
return $key !== '' && $key !== $currentKey;
});
$canonicalUrl = canonicalUrl();
$videoTitle = $video['ຊື່ພຣະສູດ'] ?? 'Video';
$thumbnailUrl = $video['_thumbnail'] ?? '';
if ($thumbnailUrl === '' && $ytId) {
// Use default thumbnail for YouTube videos
$thumbnailUrl = 'https://img.youtube.com/vi/' . $ytId . '/hqdefault.jpg';
}
if ($thumbnailUrl === '') {
$thumbnailUrl = absoluteUrl('/assets/images/logo.png');
}
$jsonLd = [
'@context' => 'https://schema.org',
'@type' => 'VideoObject',
'name' => $videoTitle,
'description' => 'ວິດີໂອ: ' . $videoTitle,
'url' => $canonicalUrl,
'inLanguage' => 'lo',
'embedUrl' => $embedUrl !== '' ? $embedUrl : $watchUrl,
'thumbnailUrl' => $thumbnailUrl,
'isPartOf' => [
'@type' => 'Collection',
'name' => 'ຄຳສອນພຸດທະ',
],
'breadcrumb' => [
'@type' => 'BreadcrumbList',
'itemListElement' => [
['@type' => 'ListItem', 'position' => 1, 'name' => 'ໜ້າຫຼັກ', 'item' => absoluteUrl('')],
['@type' => 'ListItem', 'position' => 2, 'name' => 'ວິດີໂອ', 'item' => absoluteUrl('/video')],
['@type' => 'ListItem', 'position' => 3, 'name' => $videoTitle],
],
],
];
$videoDescription = strip_tags($video['description'] ?? $video['detail'] ?? '');
$videoDescription = !empty($videoDescription)
? (mb_strlen($videoDescription) > 160 ? mb_substr($videoDescription, 0, 157) . '...' : $videoDescription)
: 'ວິດີໂອ: ' . $videoTitle;
return view('pages.video.show', [
'video' => $video,
'ytId' => $ytId,
'provider' => $provider,
'sourceLabel' => $sourceLabel,
'embedUrl' => $embedUrl,
'watchUrl' => $watchUrl,
'isReel' => $isReel,
'otherVideos' => array_values($otherVideos),
'seo' => [
'title' => $videoTitle . ' - ຄຳສອນພຸດທະ',
'description' => $videoDescription,
'keywords' => ($video['ໝວດທັມ'] ?? '') . ', ວິດີໂອ, ທັມມະ, ຄຳສອນພຸດທະ',
'image' => $thumbnailUrl,
'json_ld' => $jsonLd,
'og_type' => 'video.other',
'video_published_time' => $video['datePublished'] ?? date('Y-m-d'),
]
]);
}
}