<?php
if (isset($section_1) && $section_1 =='sitemap.xml') {
  //  All Site
  header("Content-Type: application/xml; charset=UTF-8");
  echo '<?xml version="1.0" encoding="UTF-8"?>';
  ?>
  <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php
    $sitemaps = [ 
      'page-sitemap.xml',
      'post-sitemap.xml',
      'gallery-sitemap.xml',
      'author-sitemap.xml',
      'webstory-sitemap.xml',
      'tag-sitemap.xml'
    ];

    foreach ($sitemaps as $file) {
      echo "<sitemap>
        <loc>" . rtrim($site_domain, '/') . "/sitemaps/$file</loc>
        <lastmod>" . date('Y-m-d') . "</lastmod>
      </sitemap>";
    }
    ?>
  </sitemapindex>
  <?php
}elseif (isset($section_1) && $section_1 =='page-sitemap.xml') {
  //Page Sitemap
  header("Content-type: application/xml");
  echo '<?xml version="1.0" encoding="UTF-8" ?>
          <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">';
          $menuItems = [
              "আর্কাইভ"       => $site_domain . "/archive/",
              "ফটো গ্যালারি"       => $site_domain . "/photos/",
              "ভিডিও"       => $site_domain . "/videos/",
              "ওয়েব স্টোরি"       => $site_domain . "/photo-stories/",
              "আমাদের পরিবার" => $site_domain . "/reporters/",
              "কনভার্টার"    => $site_domain . "/bangla-converter/",
              "লগইন"         => $site_domain . "/login/",
              "নিবন্ধন"       => $site_domain . "/register/",
              "অনলাইন জরিপ"  => $site_domain . "/opinion-poll/"
          ];
          foreach ($menuItems as $mtitle => $mlink) {
            ?>
            <url>
              <loc><?php echo $mlink ?></loc>
              <news:news>
               <news:publication>
                 <news:name><?php echo $mtitle ?></news:name>
                 <news:language>bn</news:language>
               </news:publication>
               <news:publication_date><?php echo date('Y-m-d'); ?></news:publication_date>
               <news:title><?php echo $mtitle ?></news:title>
              </news:news>
            </url>
            <?php
          }
          $tt_web_page = showAllttdata("tt_page", "WHERE page_status ='published' ORDER BY  id  DESC ");
          foreach ($tt_web_page as $the_page) {
            ?>
            <url>
              <loc><?php echo $site_domain.$the_page['page_url'] ?>/</loc>
              <news:news>
               <news:publication>
                 <news:name><?php echo $advancenews['Website_title'] ?></news:name>
                 <news:language>bn</news:language>
               </news:publication>
               <news:publication_date><?php echo date('Y-m-d', strtotime($the_page['page_added'])); ?></news:publication_date>
               <news:title><?php echo $the_page['page_title'] ?></news:title>
              </news:news>
            </url>
            <?php
          }
  echo '</urlset>';

}elseif (isset($section_1) && $section_1 =='gallery-sitemap.xml') {
  //Page Sitemap
  header("Content-type: application/xml");
  echo '<?xml version="1.0" encoding="UTF-8" ?>
          <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">';
          $tt_pg = getGallery();
          foreach ($tt_pg as $the_ph) {
            ?>
            <url>
              <loc><?php echo $site_domain.'photos/'.$the_ph['gallery_url'] ?>/</loc>
              <news:news>
               <news:publication>
                 <news:name><?php echo $advancenews['Website_title'] ?></news:name>
                 <news:language>bn</news:language>
               </news:publication>
               <news:publication_date><?php echo date('Y-m-d', strtotime($the_ph['gallery_date'])); ?></news:publication_date>
               <news:title><?php echo $the_ph['gallery_title'] ?></news:title>
              </news:news>
            </url>
            <?php
          }
  echo '</urlset>';

}elseif (isset($section_1) && $section_1 =='post-sitemap.xml') {
  header("Content-type: text/xml");
  echo '<?xml version="1.0" encoding="UTF-8" ?>';
  $get_post_data = allArticles();
  ?>
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
      <?php
      foreach ($get_post_data as $the_post_data) {
        ?>
        <url>
            <loc><?php echo articleLink($the_post_data['PostId']) ?>/</loc>
            <news:news>
                <news:publication>
                    <news:name><?php echo htmlentities($advancenews['Website_title'], ENT_XML1, 'UTF-8'); ?></news:name>
                    <news:language>bn</news:language>
                </news:publication>
                <news:publication_date><?php $date = date_create($the_post_data['PostDatePublic']); echo date_format($date, "Y-m-d"); ?></news:publication_date>
                <news:title><?php echo htmlentities($the_post_data['PostTitle'], ENT_XML1, 'UTF-8'); ?></news:title>
            </news:news>
        </url>
        <?php
      }
      ?>
  </urlset>
  <?php
}elseif (isset($section_1) && $section_1 =='author-sitemap.xml') {
  header("Content-type: text/xml");
  echo '<?xml version="1.0" encoding="UTF-8" ?>';
  $get_autor_data = showAllttdata("tt_accounts", "WHERE AccountStatus ='active' AND UserProfile ='active' ORDER BY  JoinDate  DESC ");
  ?>
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php
    foreach ($get_autor_data as $the_author) {
      ?>
      <url>
        <loc><?php echo userProfile($the_author['UserID']) ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($the_author['UserLastUpdate'])); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
      </url>
      <?php
    }
    ?>
  </urlset>
  <?php
}elseif (isset($section_1) && $section_1 =='websotry-sitemap.xml') {
  header("Content-type: text/xml");
  echo '<?xml version="1.0" encoding="UTF-8" ?>';
  $get_webstory_data = showAllttdata("mahamudfutureit_photostories", "WHERE story_status ='Published'  ORDER BY  story_date  DESC ");
  ?>
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
      <?php
      foreach ($get_webstory_data as $the_story_data) {
        if (isPublished($the_story_data['story_date']) == true) {
          ?>
          <url>
              <loc><?php echo $site_domain."photo-stories/".$the_story_data['story_url'] ?>/</loc>
              <news:news>
                  <news:publication>
                      <news:name><?php echo htmlentities($advancenews['Website_title'], ENT_XML1, 'UTF-8'); ?></news:name>
                      <news:language>bn</news:language>
                  </news:publication>
                  <news:publication_date><?php $date = date_create($the_story_data['story_date']); echo date_format($date, "Y-m-d"); ?></news:publication_date>
                  <news:title><?php echo htmlentities($the_story_data['story_title'], ENT_XML1, 'UTF-8'); ?></news:title>
              </news:news>
          </url>
          <?php
        }
      }
      ?>
  </urlset>
  <?php
}elseif (isset($section_1) && $section_1 =='tag-sitemap.xml') {
  header("Content-type: text/xml");
  echo '<?xml version="1.0" encoding="UTF-8" ?>';
  $all_topic = showArticleTopic();
  ?>
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php
    foreach ($all_topic as $the_topic) {
      ?>
      <url>
        <loc><?php echo getTopicsUrl($the_topic) ?></loc>
        <lastmod><?php echo date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
      </url>
      <?php
    }
    ?>
  </urlset>
  <?php
}else {
  // 404 Page
  require_once 'includes/404.php';
  exit;
}
?>
