File size: 4,010 Bytes
7731906
 
 
03bdc4a
 
7731906
 
 
 
 
 
 
 
 
03bdc4a
7731906
 
03bdc4a
 
 
 
 
 
7731906
03bdc4a
 
 
 
 
 
 
 
 
 
7731906
 
03bdc4a
7731906
 
03bdc4a
 
 
 
 
 
 
 
7731906
03bdc4a
 
 
 
 
 
7731906
 
03bdc4a
 
 
 
 
 
 
 
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
<?php
namespace App\Models;

require_once __DIR__ . '/../Helpers/cache.php';

class Sutra {
    public static function getAll($refresh = false) {
        $sutraData = self::fetchSutraApi($refresh);
        $buddhaNatureData = self::fetchBuddhaNatureJson();
        
        return array_merge($sutraData, $buddhaNatureData);
    }

    private static function fetchSutraApi($refresh = false) {
        $cacheFile = 'sutra_api.json';
        $cacheTime = 86400; // 24 hours

        if (!$refresh) {
            $json = readCache($cacheFile, $cacheTime);
            if ($json !== null) {
                $data = json_decode($json, true);
                if (isset($data['values']) && !empty($data['values'])) {
                    return self::transformData($data);
                }
            }
        }

        $url = $_ENV['SUTRA_API_URL'] ?? getenv('SUTRA_API_URL') ?: null;
        if (!$url) {
            $json = readCache($cacheFile, $cacheTime * 365);
            if ($json !== null) {
                $data = json_decode($json, true);
                if (isset($data['values']) && !empty($data['values'])) {
                    return self::transformData($data);
                }
            }
            return [];
        }

        $json = httpGet($url);
        if ($json !== null) {
            writeCache($cacheFile, $json);
            $data = json_decode($json, true);
            if (isset($data['values']) && !empty($data['values'])) {
                return self::transformData($data);
            }
        }

        $json = readCache($cacheFile, $cacheTime * 365);
        if ($json !== null) {
            $data = json_decode($json, true);
            if (isset($data['values']) && !empty($data['values'])) {
                return self::transformData($data);
            }
        }

        if ($refresh) {
            throw new \Exception("ບໍ່ສາມາດເຊື່ອມຕໍ່ກັບ API ໄດ້");
        }

        return [];
    }

    private static function transformData($data) {
        $headers = array_shift($data['values']);
        $rows = $data['values'];

        $transformed = [];
        foreach ($rows as $row) {
            $rowObject = [];
            foreach ($headers as $index => $header) {
                $rowObject[$header] = trim($row[$index] ?? "");
            }
            if (!isset($rowObject['ສຽງ'])) {
                $rowObject['ສຽງ'] = "";
            }
            
            if (!empty(array_filter($rowObject))) {
                $transformed[] = $rowObject;
            }
        }

        return $transformed;
    }

    private static function fetchBuddhaNatureJson() {
        $filePath = __DIR__ . '/../../public/assets/buddha-nature.json';
        if (!file_exists($filePath)) return [];

        $json = file_get_contents($filePath);
        $data = json_decode($json, true);

        $transformed = [];
        foreach ($data as $item) {
            $categoryName = "";
            switch ($item['category']) {
                case "627515988b61fc33c0d0ea97":
                    $categoryName = "ທໍາໃນເບື້ອງຕົ້ນ";
                    break;
                case "627515918b61fc33c0d0ea94":
                    $categoryName = "ທໍາໃນທ່າມກາງ";
                    break;
                case "627515888b61fc33c0d0ea91":
                    $categoryName = "ທໍາໃນທີສຸດ";
                    break;
                default:
                    $categoryName = $item['category'];
                    break;
            }

            $transformed[] = [
                'ID' => $item['_id'],
                'ຊື່ພຣະສູດ' => $item['title'],
                'ພຣະສູດ' => $item['content'],
                'ຮູບ' => $item['thumbnail'] ?? "",
                'ໝວດທັມ' => $categoryName,
                'ສຽງ' => "",
            ];
        }

        return $transformed;
    }
}