<?php

include("inc/dbcom.inc.php");
include("inc/functions.inc.php");
include("inc/website-config.inc.php");

$baseUrl = 'http://'.$this_website_root;

header("Content-Type:text/xml");

echo '<?xml version="1.0" encoding="UTF-8"?>';

?>

<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
  <loc><?=$baseUrl?>/</loc>
  <changefreq>weekly</changefreq>
</url>
<url>
  <loc><?=$baseUrl?>/career-center</loc>
  <changefreq>weekly</changefreq>
</url>
<?
$query = mysqli_query($db,"SELECT full_permalink FROM pages WHERE website_id='".$this_website_id."' AND status=1");
while($result = mysqli_fetch_array($query)) {
	?>
<url>
  <loc><?=$baseUrl?><?=$result['full_permalink']?></loc>
  <changefreq>weekly</changefreq>
</url>
	<?
}

// MODULES
$queryM = mysqli_query($db,"SELECT id, module_table, search_field FROM modules WHERE status=1");
while($resultM = mysqli_fetch_array($queryM)) {

	$queryP = mysqli_query($db,"SELECT full_permalink FROM pages WHERE include_module=1 AND module_id='".$resultM['id']."' AND website_id='".$this_website_id."' LIMIT 1");
	$gotParent = mysqli_num_rows($queryP);
	if ($gotParent) {
		$basePage = '/';
		while($resultP = mysqli_fetch_array($queryP)) {
			$basePage = $resultP['full_permalink'];
		}
		$sql = "SELECT id, ".$resultM['search_field']." FROM ".$resultM['module_table']." WHERE status=1";
		if (in_array($resultM['module_table'],array('users','photos_albums'))) {
			$sql .= " AND website_access LIKE '%ID".$this_website_id."%'";
		}
		else {
			$sql .= " AND website_id='".$this_website_id."'";
		}
		$query = mysqli_query($db,$sql);
		$e = mysqli_error($db);
		if (!$e) {
			while($result = mysqli_fetch_array($query)) {
				?>
<url>
  <loc><?=$baseUrl?><?=$basePage?>?rec_id=<?=$result['id']?></loc>
  <changefreq>weekly</changefreq>
</url>
				<?
			}
		}
	}
}

// TESTIMONIALS (only need one record, testimonials are bundled into single page)
$query = mysqli_query($db,"SELECT id FROM testimonials WHERE status=1 AND website_id='".$this_website_id."' LIMIT 1");
while($result = mysqli_fetch_array($query)) {
	?>
<url>
  <loc><?=$baseUrl?>/testimonials</loc>
  <changefreq>weekly</changefreq>
</url>
	<?
}

?>
<url>
  <loc><?=$baseUrl?>/event-calendar</loc>
  <changefreq>weekly</changefreq>
</url>
<?

// CAREER CENTER
$query = mysqli_query($db,"SELECT id FROM jobs WHERE status=1 AND website_id='".$this_website_id."'");
$gotJobs = mysqli_num_rows($query);
if ($gotJobs) {
		?>
<url>
  <loc><?=$baseUrl?>/career-center</loc>
  <changefreq>weekly</changefreq>
</url>
		<?
	while($result = mysqli_fetch_array($query)) {
		?>
<url>
  <loc><?=$baseUrl?>/career-center?job_id=<?=$result['id']?></loc>
  <changefreq>weekly</changefreq>
</url>
		<?
	}
}

?>
<url>
  <loc><?=$baseUrl?>/products</loc>
  <changefreq>weekly</changefreq>
</url>
</urlset>