Spaces:
Running
Running
souliya_pps commited on
Commit ·
4a04c54
1
Parent(s): 0d8d29b
Add E-Tipitaka, Anakame, Uttayarndham results to global search with type badges
Browse files
app/Controllers/SearchController.php
CHANGED
|
@@ -6,9 +6,10 @@ use App\Models\Book;
|
|
| 6 |
use App\Models\Video;
|
| 7 |
use App\Models\Calendar;
|
| 8 |
use App\Models\PdfBook;
|
|
|
|
| 9 |
|
| 10 |
class SearchController {
|
| 11 |
-
private const MAX_PER_TYPE =
|
| 12 |
private const MAX_TOTAL = 100;
|
| 13 |
|
| 14 |
public function search() {
|
|
@@ -40,7 +41,34 @@ class SearchController {
|
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
| 43 |
-
// 2. Search
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
$pdfBooks = PdfBook::getBooks();
|
| 45 |
foreach ($pdfBooks as $book) {
|
| 46 |
if (($countByType['book-page'] ?? 0) >= self::MAX_PER_TYPE) break;
|
|
@@ -60,7 +88,7 @@ class SearchController {
|
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
-
//
|
| 64 |
$books = Book::getAll();
|
| 65 |
foreach ($books as $item) {
|
| 66 |
if (($countByType['book'] ?? 0) >= self::MAX_PER_TYPE) break;
|
|
@@ -76,7 +104,7 @@ class SearchController {
|
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
-
//
|
| 80 |
$videos = Video::getAll();
|
| 81 |
foreach ($videos as $item) {
|
| 82 |
if (($countByType['video'] ?? 0) >= self::MAX_PER_TYPE) break;
|
|
@@ -92,7 +120,7 @@ class SearchController {
|
|
| 92 |
}
|
| 93 |
}
|
| 94 |
|
| 95 |
-
//
|
| 96 |
$events = Calendar::getAll();
|
| 97 |
foreach ($events as $item) {
|
| 98 |
if (($countByType['calendar'] ?? 0) >= self::MAX_PER_TYPE) break;
|
|
@@ -108,6 +136,57 @@ class SearchController {
|
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
header('Content-Type: application/json');
|
| 112 |
echo json_encode(array_slice($results, 0, self::MAX_TOTAL), JSON_UNESCAPED_UNICODE);
|
| 113 |
}
|
|
@@ -116,4 +195,4 @@ class SearchController {
|
|
| 116 |
if (empty($text)) return false;
|
| 117 |
return mb_strpos(mb_strtolower($text), $query) !== false;
|
| 118 |
}
|
| 119 |
-
}
|
|
|
|
| 6 |
use App\Models\Video;
|
| 7 |
use App\Models\Calendar;
|
| 8 |
use App\Models\PdfBook;
|
| 9 |
+
use App\Services\EtipitakaService;
|
| 10 |
|
| 11 |
class SearchController {
|
| 12 |
+
private const MAX_PER_TYPE = 20;
|
| 13 |
private const MAX_TOTAL = 100;
|
| 14 |
|
| 15 |
public function search() {
|
|
|
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
+
// 2. Search E-Tipitaka
|
| 45 |
+
$etipitakaCodes = ['thai', 'thaimm'];
|
| 46 |
+
foreach ($etipitakaCodes as $code) {
|
| 47 |
+
if (($countByType['etipitaka'] ?? 0) >= self::MAX_PER_TYPE) break;
|
| 48 |
+
try {
|
| 49 |
+
$etResults = EtipitakaService::search($code, $q, 10);
|
| 50 |
+
foreach ($etResults as $er) {
|
| 51 |
+
if (($countByType['etipitaka'] ?? 0) >= self::MAX_PER_TYPE) break;
|
| 52 |
+
$volume = (int)$er['volume'];
|
| 53 |
+
$page = (int)$er['page'];
|
| 54 |
+
$content = trim(preg_replace('/\s+/', ' ', $er['content'] ?? ''));
|
| 55 |
+
$detail = mb_strlen($content) > 150 ? mb_substr($content, 0, 150) . '...' : $content;
|
| 56 |
+
$label = EtipitakaService::getLabel($code);
|
| 57 |
+
$results[] = [
|
| 58 |
+
'type' => 'etipitaka',
|
| 59 |
+
'title' => 'ເຫຼັ້ມທີ່ ' . $volume . ' ຫນ້າ ' . $page . ' (' . $label . ')',
|
| 60 |
+
'detail' => $detail,
|
| 61 |
+
'url' => url('/etipitaka/' . $code . '/' . $volume . '/' . $page),
|
| 62 |
+
'category' => 'E-Tipitaka',
|
| 63 |
+
];
|
| 64 |
+
$countByType['etipitaka'] = ($countByType['etipitaka'] ?? 0) + 1;
|
| 65 |
+
}
|
| 66 |
+
} catch (\Throwable $e) {
|
| 67 |
+
error_log('Search etipitaka error: ' . $e->getMessage());
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// 3. Search PDF/DOCX Book Text
|
| 72 |
$pdfBooks = PdfBook::getBooks();
|
| 73 |
foreach ($pdfBooks as $book) {
|
| 74 |
if (($countByType['book-page'] ?? 0) >= self::MAX_PER_TYPE) break;
|
|
|
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
+
// 4. Search Books
|
| 92 |
$books = Book::getAll();
|
| 93 |
foreach ($books as $item) {
|
| 94 |
if (($countByType['book'] ?? 0) >= self::MAX_PER_TYPE) break;
|
|
|
|
| 104 |
}
|
| 105 |
}
|
| 106 |
|
| 107 |
+
// 5. Search Videos
|
| 108 |
$videos = Video::getAll();
|
| 109 |
foreach ($videos as $item) {
|
| 110 |
if (($countByType['video'] ?? 0) >= self::MAX_PER_TYPE) break;
|
|
|
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
+
// 6. Search Calendar
|
| 124 |
$events = Calendar::getAll();
|
| 125 |
foreach ($events as $item) {
|
| 126 |
if (($countByType['calendar'] ?? 0) >= self::MAX_PER_TYPE) break;
|
|
|
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
+
// 7. Search Anakame
|
| 140 |
+
if (($countByType['anakame'] ?? 0) < self::MAX_PER_TYPE) {
|
| 141 |
+
$anakameHtml = @file_get_contents('http://anakame.com/page/1_Sutas/main/1_Sutta_number.htm');
|
| 142 |
+
if ($anakameHtml !== false) {
|
| 143 |
+
preg_match_all('/<a\s+href="([^"]+)"[^>]*>([^<]+)<\/a>/i', $anakameHtml, $aMatches, PREG_SET_ORDER);
|
| 144 |
+
$seen = [];
|
| 145 |
+
foreach ($aMatches as $am) {
|
| 146 |
+
if (($countByType['anakame'] ?? 0) >= self::MAX_PER_TYPE) break;
|
| 147 |
+
$title = trim(strip_tags($am[2]));
|
| 148 |
+
if (empty($title) || !str_contains($am[1], '.htm')) continue;
|
| 149 |
+
$normalized = str_replace('../', '', $am[1]);
|
| 150 |
+
$normalized = preg_replace('/#.*$/', '', $normalized);
|
| 151 |
+
$key = $normalized . '|' . $title;
|
| 152 |
+
if (isset($seen[$key])) continue;
|
| 153 |
+
$seen[$key] = true;
|
| 154 |
+
if ($this->match($q, $title)) {
|
| 155 |
+
$results[] = [
|
| 156 |
+
'type' => 'anakame',
|
| 157 |
+
'title' => $title,
|
| 158 |
+
'detail' => 'ອານາຄົມສູດ ພາສາໄທ',
|
| 159 |
+
'url' => url('/anakame/read?href=' . urlencode($normalized)),
|
| 160 |
+
'category' => 'Anakame',
|
| 161 |
+
];
|
| 162 |
+
$countByType['anakame'] = ($countByType['anakame'] ?? 0) + 1;
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
// 8. Search Uttayarndham
|
| 169 |
+
if (($countByType['uttayarndham'] ?? 0) < self::MAX_PER_TYPE) {
|
| 170 |
+
$uttHtml = @file_get_contents('https://uttayarndham.org/dhamma-sharing');
|
| 171 |
+
if ($uttHtml !== false) {
|
| 172 |
+
preg_match_all('/<h4><a\s+href="\s+(\/[^"]+)"[^>]*>\s*([^<]+?)\s*<\/a><\/h4>/s', $uttHtml, $uMatches, PREG_SET_ORDER);
|
| 173 |
+
foreach ($uMatches as $um) {
|
| 174 |
+
if (($countByType['uttayarndham'] ?? 0) >= self::MAX_PER_TYPE) break;
|
| 175 |
+
$title = trim($um[2]);
|
| 176 |
+
if ($this->match($q, $title)) {
|
| 177 |
+
$results[] = [
|
| 178 |
+
'type' => 'uttayarndham',
|
| 179 |
+
'title' => $title,
|
| 180 |
+
'detail' => 'ອຸດທະຍານທັມ (ທັມມະ)',
|
| 181 |
+
'url' => url('/uttayarndham/read?url=' . urlencode(trim($um[1]))),
|
| 182 |
+
'category' => 'Uttayarndham',
|
| 183 |
+
];
|
| 184 |
+
$countByType['uttayarndham'] = ($countByType['uttayarndham'] ?? 0) + 1;
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
header('Content-Type: application/json');
|
| 191 |
echo json_encode(array_slice($results, 0, self::MAX_TOTAL), JSON_UNESCAPED_UNICODE);
|
| 192 |
}
|
|
|
|
| 195 |
if (empty($text)) return false;
|
| 196 |
return mb_strpos(mb_strtolower($text), $query) !== false;
|
| 197 |
}
|
| 198 |
+
}
|
views/layouts/main.php
CHANGED
|
@@ -760,7 +760,10 @@
|
|
| 760 |
'bg-green-100 text-green-700': result.type === 'book',
|
| 761 |
'bg-red-100 text-red-700': result.type === 'video',
|
| 762 |
'bg-purple-100 text-purple-700': result.type === 'calendar',
|
| 763 |
-
'bg-amber-100 text-amber-700': result.type === 'book-page'
|
|
|
|
|
|
|
|
|
|
| 764 |
}"
|
| 765 |
x-html="highlight(result.category)"></span>
|
| 766 |
<h4 class="text-base sm:text-lg font-bold text-gray-800 font-lao break-words min-w-0" x-html="highlight(result.title)"></h4>
|
|
@@ -835,10 +838,7 @@
|
|
| 835 |
</a>
|
| 836 |
<?php endforeach; ?>
|
| 837 |
<hr class="mx-2 my-2 border-gray-100">
|
| 838 |
-
|
| 839 |
-
<svg xmlns="http://www.w3.org/2000/svg" class="w-8 h-8 sm:w-10 sm:h-10" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" /></svg>
|
| 840 |
-
ລ້າງຖືກໃຈ
|
| 841 |
-
</button>
|
| 842 |
</div>
|
| 843 |
</div>
|
| 844 |
</div>
|
|
@@ -904,10 +904,7 @@
|
|
| 904 |
</a>
|
| 905 |
<?php endforeach; ?>
|
| 906 |
<hr class="mx-2 my-1 border-gray-100">
|
| 907 |
-
|
| 908 |
-
<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" /></svg>
|
| 909 |
-
ລ້າງຖືກໃຈ
|
| 910 |
-
</button>
|
| 911 |
</div>
|
| 912 |
</div>
|
| 913 |
<?php endif; ?>
|
|
|
|
| 760 |
'bg-green-100 text-green-700': result.type === 'book',
|
| 761 |
'bg-red-100 text-red-700': result.type === 'video',
|
| 762 |
'bg-purple-100 text-purple-700': result.type === 'calendar',
|
| 763 |
+
'bg-amber-100 text-amber-700': result.type === 'book-page',
|
| 764 |
+
'bg-emerald-100 text-emerald-700': result.type === 'etipitaka',
|
| 765 |
+
'bg-cyan-100 text-cyan-700': result.type === 'anakame',
|
| 766 |
+
'bg-orange-100 text-orange-700': result.type === 'uttayarndham'
|
| 767 |
}"
|
| 768 |
x-html="highlight(result.category)"></span>
|
| 769 |
<h4 class="text-base sm:text-lg font-bold text-gray-800 font-lao break-words min-w-0" x-html="highlight(result.title)"></h4>
|
|
|
|
| 838 |
</a>
|
| 839 |
<?php endforeach; ?>
|
| 840 |
<hr class="mx-2 my-2 border-gray-100">
|
| 841 |
+
|
|
|
|
|
|
|
|
|
|
| 842 |
</div>
|
| 843 |
</div>
|
| 844 |
</div>
|
|
|
|
| 904 |
</a>
|
| 905 |
<?php endforeach; ?>
|
| 906 |
<hr class="mx-2 my-1 border-gray-100">
|
| 907 |
+
|
|
|
|
|
|
|
|
|
|
| 908 |
</div>
|
| 909 |
</div>
|
| 910 |
<?php endif; ?>
|