File size: 5,272 Bytes
dfb0e6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2f9f87
 
 
 
 
 
dfb0e6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2f9f87
 
 
 
 
 
 
dfb0e6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbb329e
dfb0e6b
 
 
 
 
 
 
 
 
 
cbb329e
dfb0e6b
 
 
 
 
 
 
 
 
 
 
 
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
<?php
namespace App\Controllers;

use App\Services\EtipitakaService;

class EtipitakaController {
    public function index() {
        $categories = EtipitakaService::getCategories();
        $currentCode = $_GET['code'] ?? 'thai';
        $query = $_GET['q'] ?? '';

        $results = [];
        $volumes = [];
        $groupedResults = [];

        if ($query && mb_strlen($query) >= 2) {
            try {
                $results = EtipitakaService::search($currentCode, $query);
            } catch (\Throwable $e) {
                error_log('Etipitaka index search error: ' . $e->getMessage());
                $results = [];
            }
            foreach ($results as $r) {
                $vol = (int)$r['volume'];
                if (!isset($groupedResults[$vol])) $groupedResults[$vol] = [];
                $groupedResults[$vol][] = $r;
            }
        }

        ksort($groupedResults);

        $seoTitle = 'E-Tipitaka - ' . EtipitakaService::getLabel($currentCode);

        return view('pages.tipitaka.index', [
            'categories' => $categories,
            'currentCode' => $currentCode,
            'query' => $query,
            'results' => $results,
            'groupedResults' => $groupedResults,
            'currentLabel' => EtipitakaService::getLabel($currentCode),
            'seo' => [
                'title' => $seoTitle . ' - ຄຳສອນພຸດທະ',
                'description' => 'ຄົ້ນຫາພຣະໄຕຣປິດກ ຫຼາຍສະບັບພາສາຕ່າງໆ',
                'keywords' => 'tipitaka, tripitaka, ພຣະໄຕຣປິດກ, ພຸດທະສາສະໜາ',
            ]
        ]);
    }

    public function search() {
        $code = $_GET['code'] ?? 'thai';
        $query = $_GET['q'] ?? '';

        if (mb_strlen($query) < 2) {
            $this->json(['results' => []]);
            return;
        }

        try {
            $results = EtipitakaService::search($code, $query);
        } catch (\Throwable $e) {
            error_log('Etipitaka search error: ' . $e->getMessage());
            $this->json(['results' => [], 'error' => 'Search failed']);
            return;
        }
        $items = [];
        foreach ($results as $r) {
            $volume = (int)$r['volume'];
            $page = (int)$r['page'];
            $content = $r['content'];
            $cleaned = preg_replace('/\s+/', ' ', str_replace("\t", ' ', $content));
            $cleaned = trim($cleaned);
            $idx = mb_stripos($cleaned, $query);
            $excerpt = '';
            if ($idx !== false) {
                $start = max(0, $idx - 60);
                $end = min(mb_strlen($cleaned), $idx + mb_strlen($query) + 90);
                $prefix = $start > 0 ? '...' : '';
                $suffix = $end < mb_strlen($cleaned) ? '...' : '';
                $excerpt = $prefix . mb_substr($cleaned, $start, $end - $start) . $suffix;
            } else {
                $excerpt = mb_strlen($cleaned) > 150 ? mb_substr($cleaned, 0, 150) . '...' : $cleaned;
            }
            $items[] = [
                'volume' => $volume,
                'page' => $page,
                'items' => $r['items'],
                'title' => 'ເຫຼັ້ມທີ່ ' . $volume . ' ຫນ້າ ' . $page,
                'excerpt' => $excerpt,
            ];
        }

        $grouped = [];
        foreach ($items as $item) {
            $grouped[$item['volume']][] = $item;
        }
        ksort($grouped);

        $this->json([
            'results' => $items,
            'grouped' => $grouped,
            'total' => count($items),
        ]);
    }

    public function show($code, $volume, $page) {
        $volume = (int)$volume;
        $page = (int)$page;

        if (!EtipitakaService::isAvailable($code)) {
            http_response_code(404);
            echo 'Edition not found';
            return;
        }

        $content = EtipitakaService::getContent($code, $volume, $page);
        if (!$content) {
            http_response_code(404);
            echo 'Content not found';
            return;
        }

        $totalPages = EtipitakaService::getTotalPages($code, $volume);
        $label = EtipitakaService::getLabel($code);
        $prevPage = $page > 1 ? $page - 1 : null;
        $nextPage = $page < $totalPages ? $page + 1 : null;
        $volumes = EtipitakaService::getVolumes($code);

        return view('pages.tipitaka.show', [
            'content' => $content,
            'code' => $code,
            'label' => $label,
            'volume' => $volume,
            'page' => $page,
            'prevPage' => $prevPage,
            'nextPage' => $nextPage,
            'totalPages' => $totalPages,
            'volumes' => $volumes,
            'seo' => [
                'title' => 'E-Tipitaka - ເຫຼັ້ມທີ່ ' . $volume . ' ຫນ້າ ' . $page . ' (' . $label . ')',
                'description' => 'ພຣະໄຕຣປິດກ ເຫຼັ້ມທີ່ ' . $volume . ' ຫນ້າ ' . $page,
            ]
        ]);
    }

    private function json($data) {
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }
}