<?php
	require_once "dal/articleDAL.php";
	require_once "dal/bookDAL.php";
	require_once "dal/book_categoryDAL.php";
	require_once "dal/categoryDAL.php";
	require_once "dal/faq_categoryDAL.php";
	require_once "dal/htaccessDAL.php";
	require_once "dal/newsDAL.php";
	require_once "dal/news_categoryDAL.php";
	require_once "dal/productDAL.php";

	ob_start();
	header('Content-Type: text/xml');

	$dalH = new htaccessDAL();
	$dalH->ID = "1";
	$dalH->loadRecord();
	$htLink1 = $dalH->HtmlName;
	$htVisible1 = $dalH->Visible;
	$dalH->ID = "2";
	$dalH->loadRecord();
	$htLink2 = $dalH->HtmlName;
	$htVisible2 = $dalH->Visible;
	$dalH->ID = "3";
	$dalH->loadRecord();
	$htLink3 = $dalH->HtmlName;
	$htVisible3 = $dalH->Visible;
	$dalH->ID = "4";
	$dalH->loadRecord();
	$htLink4 = $dalH->HtmlName;
	$htVisible4 = $dalH->Visible;
	$dalH->ID = "5";
	$dalH->loadRecord();
	$htLink5 = $dalH->HtmlName;
	$htVisible5 = $dalH->Visible;
	$dalH->ID = "6";
	$dalH->loadRecord();
	$htLink6 = $dalH->HtmlName;
	$htVisible6 = $dalH->Visible;

	$domS = new DomDocument("1.0","UTF-8");
	$domS->formatOutput = true;
	$urlset = $domS->createElement("urlset");
	$domS->appendChild($urlset);

	$xmlns1 = $domS->createAttribute("xmlns");
	$xmlns1->value = "https://www.sitemaps.org/schemas/sitemap/0.9";
	$urlset->appendChild($xmlns1);

	$xmlns2 = $domS->createAttribute("xmlns:xsi");
	$xmlns2->value = "https://www.w3.org/2001/XMLSchema-instance";
	$urlset->appendChild($xmlns2);

	$xsi = $domS->createAttribute("xsi:schemaLocation");
	$xsi->value = "https://www.sitemaps.org/schemas/sitemap/0.9             https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd";
	$urlset->appendChild($xsi);

	$url = $domS->createElement("url");
	$urlset->appendChild($url);
	$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/");
	$url->appendChild($loc);

	if($htVisible1 == "1"){
		$dalA = new articleDAL();
		$dataA = $dalA->executeRecordset("a.`Visible`='1'","b.`OrderNO`,b.`CategoryID`,a.`OrderNO`,a.`ArticleID`");
		for($i=0;$i<count($dataA);$i++){
			$url = $domS->createElement("url");
			$urlset->appendChild($url);

			$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink1."-".$dataA[$i]["PathName"].".html");
			$url->appendChild($loc);
		}
	}

	if($htVisible2 == "1"){
		$url = $domS->createElement("url");
		$urlset->appendChild($url);

		$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink2.".html");
		$url->appendChild($loc);

		$dalN = new newsDAL();
		$dalNC = new news_categoryDAL();
		$dataNC = $dalNC->executeRecordset("a.`Visible`='1'");
		for($i=0;$i<count($dataNC);$i++){
			$url = $domS->createElement("url");
			$urlset->appendChild($url);

			$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink2.$dataNC[$i]["CategoryID"].".html");
			$url->appendChild($loc);

			$dataN = $dalN->executeRecordset("a.`CategoryID`='".$dataNC[$i]["CategoryID"]."' AND a.`Visible`='1' AND a.`OnlineTime`<='".date("Y-m-d H:i:s")."'","a.`OrderNO`,a.`OnlineTime` DESC");
			for($j=0;$j<count($dataN);$j++){
				$url = $domS->createElement("url");
				$urlset->appendChild($url);

				$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink2."-".$dataN[$j]["NewsID"].".html");
				$url->appendChild($loc);
			}
		}
	}

	if($htVisible3 == "1"){
		$url = $domS->createElement("url");
		$urlset->appendChild($url);

		$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink3.".html");
		$url->appendChild($loc);

		$dalFC = new faq_categoryDAL();
		$dataFC = $dalFC->executeRecordset("a.`Visible`='1'");
		for($i=0;$i<count($dataFC);$i++){
			$url = $domS->createElement("url");
			$urlset->appendChild($url);

			$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink3.$dataFC[$i]["CategoryID"].".html");
			$url->appendChild($loc);
		}
	}

	if($htVisible4 == "1"){
		$url = $domS->createElement("url");
		$urlset->appendChild($url);

		$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink4.".html");
		$url->appendChild($loc);

		$dalC = new categoryDAL();
		$dalP = new productDAL();
		$dataC = $dalC->executeRecordset("a.`Visible`='1' AND (a.`ParentID`='0' OR b.`Visible`='1')","b.`OrderNO`,a.`OrderNO`");
		for($i=0;$i<count($dataC);$i++){
			$url = $domS->createElement("url");
			$urlset->appendChild($url);

			$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink4.$dataC[$i]["CategoryID"].".html");
			$url->appendChild($loc);

			$dataP = $dalP->executeRecordset("a.`CategoryID`='".$dataC[$i]["CategoryID"]."' AND a.`Visible`='1'","a.`OrderNO`,a.`ModifiedTime` DESC");
			for($j=0;$j<count($dataP);$j++){
				$url = $domS->createElement("url");
				$urlset->appendChild($url);

				$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink4."-".$dataP[$j]["ProductID"].".html");
				$url->appendChild($loc);
			}
		}
	}

	if($htVisible5 == "1"){
		$url = $domS->createElement("url");
		$urlset->appendChild($url);

		$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink5.".html");
		$url->appendChild($loc);

		$dalB = new bookDAL();
		$dalBC = new book_categoryDAL();
		$dataBC = $dalBC->executeRecordset("a.`Visible`='1'");
		for($i=0;$i<count($dataBC);$i++){
			$url = $domS->createElement("url");
			$urlset->appendChild($url);

			$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink5.$dataBC[$i]["CategoryID"].".html");
			$url->appendChild($loc);

			$dataB = $dalB->executeRecordset("a.`CategoryID`='".$dataBC[$i]["CategoryID"]."' AND a.`Visible`='1'","a.`OrderNO`,a.`BookID` DESC");
			for($j=0;$j<count($dataB);$j++){
				$url = $domS->createElement("url");
				$urlset->appendChild($url);

				$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink5."-".$dataB[$j]["BookID"].".html");
				$url->appendChild($loc);
			}
		}
	}

	if($htVisible6 == "1"){
		$url = $domS->createElement("url");
		$urlset->appendChild($url);

		$loc = $domS->createElement("loc","https://".$_SERVER['HTTP_HOST']."/".$htLink6.".html");
		$url->appendChild($loc);
	}

	$xmlCode = $domS->saveXML();
	echo $xmlCode;
?>