<?php
/**
 * Dynamic Sitemap Generator for SLiMS
 *
 * Generates XML sitemap for better SEO indexing
 * This file should be accessed via web browser to generate sitemap.xml
 * Or setup as cron job to regenerate periodically
 */

// Bootstrap SLiMS
define('INDEX_AUTH', 1);
require_once 'sysconfig.inc.php';

// Set header as XML
header('Content-Type: application/xml; charset=utf-8');

// Get base URL
$base_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$base_url = rtrim($base_url, '/') . '/';

// Start XML output
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// 1. Homepage - Highest priority
echo "  <url>\n";
echo "    <loc>{$base_url}index.php</loc>\n";
echo "    <changefreq>daily</changefreq>\n";
echo "    <priority>1.0</priority>\n";
echo "  </url>\n";

// 2. Static pages
$static_pages = [
    'libinfo' => ['priority' => '0.8', 'changefreq' => 'monthly'],
    'services' => ['priority' => '0.8', 'changefreq' => 'monthly'],
    'memberarea' => ['priority' => '0.7', 'changefreq' => 'weekly'],
    'contact' => ['priority' => '0.7', 'changefreq' => 'monthly'],
];

foreach ($static_pages as $page => $config) {
    echo "  <url>\n";
    echo "    <loc>{$base_url}index.php?p={$page}</loc>\n";
    echo "    <changefreq>{$config['changefreq']}</changefreq>\n";
    echo "    <priority>{$config['priority']}</priority>\n";
    echo "  </url>\n";
}

// 3. Bibliographic records (books/collections)
// Only include published and not deleted items
$biblio_query = "SELECT biblio_id, last_update
                 FROM biblio
                 WHERE opac_hide = 0
                 ORDER BY last_update DESC
                 LIMIT 5000";

$result = $dbs->query($biblio_query);

if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $biblio_id = $row['biblio_id'];
        $last_update = $row['last_update'];

        // Format date to W3C format (YYYY-MM-DD)
        $lastmod = date('Y-m-d', strtotime($last_update));

        echo "  <url>\n";
        echo "    <loc>{$base_url}index.php?p=show_detail&amp;id={$biblio_id}</loc>\n";
        echo "    <lastmod>{$lastmod}</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.6</priority>\n";
        echo "  </url>\n";
    }
}

// 4. Popular subjects/topics
$subject_query = "SELECT DISTINCT topic
                  FROM mst_topic
                  ORDER BY topic
                  LIMIT 100";

$result = $dbs->query($subject_query);

if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $topic = urlencode($row['topic']);

        echo "  <url>\n";
        echo "    <loc>{$base_url}index.php?search=Search&amp;keywords={$topic}</loc>\n";
        echo "    <changefreq>weekly</changefreq>\n";
        echo "    <priority>0.5</priority>\n";
        echo "  </url>\n";
    }
}

// 5. Authors (if you want to include author pages)
$author_query = "SELECT DISTINCT author_name
                 FROM mst_author
                 ORDER BY author_name
                 LIMIT 200";

$result = $dbs->query($author_query);

if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $author = urlencode($row['author_name']);

        echo "  <url>\n";
        echo "    <loc>{$base_url}index.php?search=Search&amp;keywords={$author}</loc>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.4</priority>\n";
        echo "  </url>\n";
    }
}

// Close XML
echo '</urlset>';

// Optional: Save to file for static serving
// Uncomment below to save sitemap to file
/*
$xml_content = ob_get_contents();
file_put_contents(__DIR__ . '/sitemap.xml', $xml_content);
*/
