File size: 6,205 Bytes
7731906
 
 
 
 
 
 
 
 
 
 
 
 
4de7755
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
<?php
namespace App\Controllers;

use App\Models\Sutra;
use App\Models\Book;
use App\Models\Video;
use App\Models\Calendar;
use App\Models\PdfBook;

class SitemapController {
    public function generate() {
        header('Content-Type: application/xml; charset=utf-8');

        $siteUrl = rtrim(getenv('SITE_URL') ?: 'https://buddhaword-web.hf.space', '/');

        $urls = [];
        $today = date('Y-m-d');

        $staticPages = [
            ['loc' => '/', 'priority' => '1.0', 'changefreq' => 'daily', 'lastmod' => $today],
            ['loc' => '/book', 'priority' => '0.8', 'changefreq' => 'weekly', 'lastmod' => $today],
            ['loc' => '/video', 'priority' => '0.8', 'changefreq' => 'weekly', 'lastmod' => $today],
            ['loc' => '/calendar', 'priority' => '0.8', 'changefreq' => 'weekly', 'lastmod' => $today],
            ['loc' => '/favorites', 'priority' => '0.5', 'changefreq' => 'monthly', 'lastmod' => $today],
            ['loc' => '/about', 'priority' => '0.5', 'changefreq' => 'monthly', 'lastmod' => $today],
            ['loc' => '/privacy', 'priority' => '0.3', 'changefreq' => 'monthly', 'lastmod' => $today],
            ['loc' => '/search-books', 'priority' => '0.7', 'changefreq' => 'weekly', 'lastmod' => $today],
        ];
        foreach ($staticPages as $page) {
            $urls[] = [
                'loc' => $siteUrl . $page['loc'],
                'lastmod' => $page['lastmod'],
                'changefreq' => $page['changefreq'],
                'priority' => $page['priority'],
            ];
        }

        try {
            $sutras = Sutra::getAll();
            $categories = [];
            foreach ($sutras as $sutra) {
                $id = $sutra['ID'] ?? '';
                $category = $sutra['ໝວດທັມ'] ?? '';
                $sutraDate = $sutra['datePublished'] ?? $today;
                if ($id) {
                    $urls[] = [
                        'loc' => $siteUrl . '/sutra/details/' . $id,
                        'lastmod' => $sutraDate,
                        'changefreq' => 'weekly',
                        'priority' => '0.9',
                    ];
                }
                if ($category && !in_array($category, $categories)) {
                    $categories[] = $category;
                }
            }
            foreach ($categories as $cat) {
                $urls[] = [
                    'loc' => $siteUrl . '/sutra/' . rawurlencode($cat),
                    'lastmod' => $today,
                    'changefreq' => 'weekly',
                    'priority' => '0.7',
                ];
            }
        } catch (\Exception $e) {
        }

        try {
            $books = Book::getAll();
            foreach ($books as $book) {
                $id = $book['ID'] ?? '';
                $bookDate = $book['datePublished'] ?? $today;
                if ($id) {
                    $urls[] = [
                        'loc' => $siteUrl . '/book/view/' . $id,
                        'lastmod' => $bookDate,
                        'changefreq' => 'monthly',
                        'priority' => '0.8',
                    ];
                }
            }
        } catch (\Exception $e) {
        }

        try {
            $videos = Video::getAll();
            foreach ($videos as $video) {
                $id = $video['ID'] ?? '';
                $videoDate = $video['datePublished'] ?? $today;
                if ($id) {
                    $urls[] = [
                        'loc' => $siteUrl . '/video/view/' . $id,
                        'lastmod' => $videoDate,
                        'changefreq' => 'monthly',
                        'priority' => '0.7',
                    ];
                }
            }
        } catch (\Exception $e) {
        }

        try {
            $events = Calendar::getAll();
            foreach ($events as $event) {
                $id = $event['ID'] ?? '';
                $eventDate = $event['startDateISO'] ?? $today;
                if ($id) {
                    $urls[] = [
                        'loc' => $siteUrl . '/calendar/view/' . $id,
                        'lastmod' => $eventDate,
                        'changefreq' => 'monthly',
                        'priority' => '0.6',
                    ];
                }
            }
        } catch (\Exception $e) {
        }

        try {
            $pdfBooks = PdfBook::getBooks();
            foreach ($pdfBooks as $pdfBook) {
                $slug = $pdfBook['slug'] ?? '';
                $totalPages = $pdfBook['totalPages'] ?? 0;
                if ($slug) {
                    $urls[] = [
                        'loc' => $siteUrl . '/search-books/' . $slug,
                        'lastmod' => $today,
                        'changefreq' => 'monthly',
                        'priority' => '0.6',
                    ];
                    for ($n = 1; $n <= $totalPages; $n++) {
                        $urls[] = [
                            'loc' => $siteUrl . '/search-books/' . $slug . '/page/' . $n,
                            'lastmod' => $today,
                            'changefreq' => 'monthly',
                            'priority' => '0.5',
                        ];
                    }
                }
            }
        } catch (\Exception $e) {
        }

        $urls = array_map(function($url) {
            $url['lastmod'] = date('Y-m-d\TH:i:s.v\Z', strtotime($url['lastmod']));
            return $url;
        }, $urls);

        $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
        $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

        foreach ($urls as $url) {
            $xml .= '  <url>' . "\n";
            $xml .= '    <loc>' . htmlspecialchars($url['loc'], ENT_XML1, 'UTF-8') . '</loc>' . "\n";
            $xml .= '    <lastmod>' . $url['lastmod'] . '</lastmod>' . "\n";
            $xml .= '    <changefreq>' . $url['changefreq'] . '</changefreq>' . "\n";
            $xml .= '    <priority>' . $url['priority'] . '</priority>' . "\n";
            $xml .= '  </url>' . "\n";
        }

        $xml .= '</urlset>' . "\n";

        echo $xml;
    }
}