Spaces:
Running
Running
File size: 13,532 Bytes
7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 282827b 7731906 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | <?php
namespace App\Controllers;
use App\Models\Sutra;
require_once __DIR__ . '/../Helpers/cache.php';
class SutraController {
/**
* Read a derived sutra cache. The cache is stale as soon as the source
* sutra_api.json is refreshed (sync), even if the 24h TTL has not elapsed.
*/
private function readSutraCache($file, $maxAge = 86400) {
$cached = readCache($file, $maxAge);
if ($cached === null) return null;
$path = getCachePath($file);
$source = getCachePath('sutra_api.json');
if (file_exists($source) && file_exists($path) && filemtime($source) > filemtime($path)) {
return null;
}
return $cached;
}
public function index() {
/* The list page only needs category names. Caching them avoids decoding
the ~6 MB sutra store on every visit (big TTFB win on mobile). */
$cacheFile = 'sutra_categories_v1.json';
$categories = null;
$cached = $this->readSutraCache($cacheFile);
if ($cached !== null) {
$decoded = json_decode($cached, true);
if (is_array($decoded) && !empty($decoded)) {
$categories = $decoded;
}
}
if ($categories === null) {
$sutras = Sutra::getAll();
$categoriesFromData = [];
foreach ($sutras as $item) {
$category = $item['ໝວດທັມ'] ?? '';
if ($category !== '' && !in_array($category, $categoriesFromData)) {
$categoriesFromData[] = $category;
}
}
$imagesDir = __DIR__ . '/../../public/images/sutra/';
$availableCategories = [];
if (is_dir($imagesDir)) {
$files = scandir($imagesDir);
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'jpg') {
$availableCategories[] = pathinfo($file, PATHINFO_FILENAME);
}
}
}
$categories = array_values(array_intersect($categoriesFromData, $availableCategories));
if (empty($categories)) {
$categories = $availableCategories;
}
if (!empty($categories)) {
writeCache($cacheFile, json_encode($categories, JSON_UNESCAPED_UNICODE));
}
}
$canonicalUrl = canonicalUrl();
$siteUrl = getSiteUrl();
$jsonLd = [
[
'@context' => 'https://schema.org',
'@type' => 'WebSite',
'name' => 'ຄຳສອນພຸດທະ',
'url' => $siteUrl,
'description' => 'ເວັບໄຊທ໌ສຳລັບຮຽນຮູ້ພຣະສູດ ແລະ ຄຳສອນຂອງພຣະພຸດທະເຈົ້າ',
'inLanguage' => 'lo',
'potentialAction' => [
'@type' => 'SearchAction',
'target' => [
'@type' => 'EntryPoint',
'urlTemplate' => absoluteUrl('/api/search') . '?keyword={search_term}',
],
'query-input' => 'required name=search_term',
],
],
[
'@context' => 'https://schema.org',
'@type' => 'CollectionPage',
'name' => 'ພຣະສູດ - ຄຳສອນພຸດທະ',
'description' => 'ລວມພຣະສູດ ແລະ ຄຳສອນຂອງພຣະພຸດທະເຈົ້າ',
'url' => $canonicalUrl,
'inLanguage' => 'lo',
'isPartOf' => [
'@type' => 'WebSite',
'name' => 'ຄຳສອນພຸດທະ',
'url' => $siteUrl,
],
],
];
return view('pages.sutra.index', [
'categories' => $categories,
'seo' => [
'title' => 'ຄຳສອນພຸດທະ - ຮຽນຮູ້ພຣະສູດ ແລະ ທັມມະ',
'description' => 'ເວັບໄຊທ໌ສຳລັບຮຽນຮູ້ພຣະສູດ ພຣະທັມ ຄຳສອນຂອງພຣະພຸດທະເຈົ້າ ພ້ອມທັງປຶ້ມ, ວິດີໂອ ແລະ ປະຕິທິນກິດຈະກຳ',
'json_ld' => $jsonLd,
]
]);
}
public function category($category) {
/* Cache a slim list per category: the list only renders 4 fields, so we
never need to ship the full sutra text to the browser. */
$cacheFile = 'sutra_cat_' . md5($category) . '.json';
$filteredSutras = null;
$cached = $this->readSutraCache($cacheFile);
if ($cached !== null) {
$decoded = json_decode($cached, true);
if (is_array($decoded)) {
$filteredSutras = $decoded;
}
}
if ($filteredSutras === null) {
$sutras = Sutra::getAll();
$filteredSutras = array_values(array_map(function($item) {
return [
'ID' => $item['ID'] ?? '',
'ຊື່ພຣະສູດ' => $item['ຊື່ພຣະສູດ'] ?? '',
'ສຽງ' => $item['ສຽງ'] ?? '',
'ໝວດທັມ' => $item['ໝວດທັມ'] ?? '',
];
}, array_filter($sutras, function($item) use ($category) {
return ($item['ໝວດທັມ'] ?? '') === $category;
})));
writeCache($cacheFile, json_encode($filteredSutras, JSON_UNESCAPED_UNICODE));
}
$canonicalUrl = canonicalUrl();
$count = count($filteredSutras);
$jsonLd = [
'@context' => 'https://schema.org',
'@type' => 'CollectionPage',
'name' => $category . ' - ຄຳສອນພຸດທະ',
'description' => 'ພຣະສູດໃນໝວດ ' . $category . ' ຈຳນວນ ' . $count . ' ພຣະສູດ',
'url' => $canonicalUrl,
'inLanguage' => 'lo',
'about' => $category,
];
return view('pages.sutra.category', [
'category' => $category,
'sutras' => $filteredSutras,
'seo' => [
'title' => $category . ' - ຄຳສອນພຸດທະ',
'description' => 'ພຣະສູດໃນໝວດ ' . $category . ' ຈຳນວນ ' . $count . ' ພຣະສູດ',
'keywords' => $category . ', ພຣະສູດ, ຄຳສອນພຸດທະ, ທັມ',
'json_ld' => $jsonLd,
]
]);
}
public function show($id) {
/* Cache one sutra by id so opening an article does not decode the whole store. */
$cacheFile = 'sutra_item_' . md5((string) $id) . '.json';
$sutra = null;
$cached = $this->readSutraCache($cacheFile);
if ($cached !== null) {
$decoded = json_decode($cached, true);
if (is_array($decoded) && !empty($decoded)) {
$sutra = $decoded;
}
}
if (!$sutra) {
$sutras = Sutra::getAll();
foreach ($sutras as $item) {
if ($item['ID'] == $id) {
$sutra = $item;
break;
}
}
if (!$sutra) {
$sutras = Sutra::getAll(true);
foreach ($sutras as $item) {
if ($item['ID'] == $id) {
$sutra = $item;
break;
}
}
}
if ($sutra) {
writeCache($cacheFile, json_encode($sutra, JSON_UNESCAPED_UNICODE));
}
}
if (!$sutra) {
http_response_code(404);
return view('pages.sutra.show', [
'sutra' => [
'ID' => $id,
'ຊື່ພຣະສູດ' => 'ບໍ່ພົບພຣະສູດ',
'ພຣະສູດ' => 'ຂໍອະໄພ, ບໍ່ພົບພຣະສູດທີ່ທ່ານກຳລັງຊອກຫາ.',
'ໝວດທັມ' => '',
'ສຽງ' => '',
'ຮູບ' => ''
],
'prevID' => null,
'nextID' => null,
'seo' => [
'title' => 'ບໍ່ພົບໜ້ານີ້ - ຄຳສອນພຸດທະ',
'description' => 'ຂໍອະໄພ, ບໍ່ພົບໜ້າທີ່ທ່ານກຳລັງຊອກຫາ.',
'robots' => 'noindex, follow',
]
]);
}
$prevID = null;
$nextID = null;
if (!empty($sutra['ໝວດທັມ'])) {
$category = $sutra['ໝວດທັມ'];
$categorySutras = null;
$catCached = $this->readSutraCache('sutra_cat_' . md5($category) . '.json');
if ($catCached !== null) {
$decoded = json_decode($catCached, true);
if (is_array($decoded)) {
$categorySutras = $decoded;
}
}
if ($categorySutras === null) {
$allSutras = Sutra::getAll();
$categorySutras = array_values(array_map(function($item) {
return [
'ID' => $item['ID'] ?? '',
'ຊື່ພຣະສູດ' => $item['ຊື່ພຣະສູດ'] ?? '',
'ສຽງ' => $item['ສຽງ'] ?? '',
'ໝວດທັມ' => $item['ໝວດທັມ'] ?? '',
];
}, array_filter($allSutras, function($item) use ($category) {
return ($item['ໝວດທັມ'] ?? '') === $category;
})));
if (!empty($categorySutras)) {
writeCache('sutra_cat_' . md5($category) . '.json', json_encode($categorySutras, JSON_UNESCAPED_UNICODE));
}
}
foreach ($categorySutras as $index => $item) {
if ($item['ID'] == $id) {
$prevID = $categorySutras[$index - 1]['ID'] ?? null;
$nextID = $categorySutras[$index + 1]['ID'] ?? null;
break;
}
}
}
$description = strip_tags($sutra['ພຣະສູດ'] ?? '');
$description = mb_strlen($description) > 160 ? mb_substr($description, 0, strrpos(mb_substr($description, 0, 160), ' ') ?: 157) . '...' : $description;
$canonicalUrl = canonicalUrl();
$sutraTitle = $sutra['ຊື່ພຣະສູດ'] ?? 'ພຣະສູດ';
$category = $sutra['ໝວດທັມ'] ?? '';
$jsonLd = [
'@context' => 'https://schema.org',
'@type' => 'Article',
'headline' => $sutraTitle,
'description' => $description,
'url' => $canonicalUrl,
'inLanguage' => 'lo',
'isPartOf' => [
'@type' => 'Collection',
'name' => 'ຄຳສອນພຸດທະ',
],
'breadcrumb' => [
'@type' => 'BreadcrumbList',
'itemListElement' => [
['@type' => 'ListItem', 'position' => 1, 'name' => 'ໜ້າຫຼັກ', 'item' => absoluteUrl('')],
['@type' => 'ListItem', 'position' => 2, 'name' => 'ພຣະສູດ', 'item' => absoluteUrl('/sutra')],
['@type' => 'ListItem', 'position' => 3, 'name' => $sutraTitle],
],
],
];
if ($category) {
$jsonLd['about'] = $category;
$jsonLd['keywords'] = $category . ', ພຣະສູດ, ຄຳສອນພຸດທະ, ທັມ';
$jsonLd['articleSection'] = $category;
} else {
$jsonLd['keywords'] = 'ພຣະສູດ, ຄຳສອນພຸດທະ, ທັມ';
}
if (!empty($sutra['ຮູບ'])) {
$jsonLd['image'] = $sutra['ຮູບ'];
}
$jsonLd['datePublished'] = $sutra['datePublished'] ?? date('Y-m-d');
$jsonLd['author'] = [
'@type' => 'Organization',
'name' => 'ຄຳສອນພຸດທະ',
];
$publishedTime = $sutra['datePublished'] ?? date('Y-m-d');
return view('pages.sutra.show', [
'sutra' => $sutra,
'prevID' => $prevID,
'nextID' => $nextID,
'seo' => [
'title' => $sutraTitle . ' - ຄຳສອນພຸດທະ',
'description' => $description,
'keywords' => ($category ? $category . ', ' : '') . 'ພຣະສູດ, ຄຳສອນພຸດທະ, ທັມ',
'image' => !empty($sutra['ຮູບ']) ? $sutra['ຮູບ'] : absoluteUrl('assets/images/logo_shared.png'),
'json_ld' => $jsonLd,
'og_type' => 'article',
'article_published_time' => $publishedTime,
]
]);
}
}
|