<?php
	require_once "pageBase.php";
	require_once "bin/web/tables/article.php";
	require_once "bin/web/tables/news.php";
	require_once "bin/web/tables/faq.php";
	require_once "bin/web/tables/product.php";
	require_once "bin/web/tables/book.php";
	require_once "bin/web/tables/contact.php";
	require_once "dal/htaccessDAL.php";

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

	class _rss extends pageBase {
		public function load() {
			$dalH = new htaccessDAL();

			$indexTitle = $this->formatspace($this->registry->webTitle);
			$indexDescription = $this->formatspace($this->registry->webDescription);
			$host = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'];
			$time = date("D, d M Y H:i:s O");
			$time2 = date("D, d M Y H:i:s O", $this->registry->ModifiedTime);
	
			$dom = new DomDocument("1.0", "UTF-8");
			$dom->formatOutput = true;
			$rss = $dom->createElement("rss");
			$dom->appendChild($rss);

			$rssVersion = $dom->createAttribute("version");
			$rssVersion->value = "2.0";
			$rss->appendChild($rssVersion);

			$rssAtom = $dom->createAttribute("xmlns:atom");
			$rssAtom->value = "https://www.w3.org/2005/Atom";
			$rss->appendChild($rssAtom);

			$channel = $dom->createElement("channel");
			$rss->appendChild($channel);
	
			$channelTitle = $dom->createElement("title");
			$channelTitle->appendChild($dom->createTextNode($indexTitle));
			$channel->appendChild($channelTitle);

			$channelLink = $dom->createElement("link", $host."/");
			$channel->appendChild($channelLink);
	
			$channelDescription = $dom->createElement("description");
			$channelDescription->appendChild($dom->createTextNode($indexDescription));
			$channel->appendChild($channelDescription);

			$channelAtom = $dom->createElement("atom:link");
			$channel->appendChild($channelAtom);
			$atomhref = $dom->createAttribute("href");
			$atomhref->value = $host."/rss.xml";
			$channelAtom->appendChild($atomhref);
			$atomrel = $dom->createAttribute("rel");
			$atomrel->value = "self";
			$channelAtom->appendChild($atomrel);
			$atomtype = $dom->createAttribute("type");
			$atomtype->value = "application/rss+xml";
			$channelAtom->appendChild($atomtype);
			
			$item = $this->additem($dom, "/", $indexTitle, $indexDescription, $time);
			$channel->appendChild($item);

			$dalH->ID = "1";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalAC = new article_categoryDAL();
				$dalA = new articleDAL();
				$article = new article();
				$item = $this->additem($dom, $article->linkArticleList(), $indexTitle, $indexDescription, $time);
				$channel->appendChild($item);

				$dataAC = $dalAC->executeRecordset([], "a.`Visible`=1");
				foreach ($dataAC as $rows) {
					$webTitle = (!empty($rows["WebTitle"])) ? $this->formatspace($rows["WebTitle"]) : $indexTitle;
					$webDescription = (!empty($rows["WebDescription"])) ? $this->formatspace($rows["WebDescription"]) : $indexDescription;
					$item = $this->additem($dom, $article->linkArticleList($rows["CategoryID"]), $webTitle, $webDescription, $time);
					$channel->appendChild($item);

					$intoAry = [parent::addArray(":categoryID", $rows["CategoryID"], "int")];
					$dataA = $dalA->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1", "a.`OrderNO`, a.`ArticleID`");
					foreach ($dataA as $rows2) {
						$webTitle = (!empty($rows2["WebTitle"])) ? $this->formatspace($rows2["WebTitle"]) : $indexTitle;
						$webDescription = (!empty($rows2["WebDescription"])) ? $this->formatspace($rows2["WebDescription"]) : $indexDescription;
						$item = $this->additem($dom, $article->linkArticleDetail($rows2["ArticleID"]), $webTitle, $webDescription, date("D, d M Y H:i:s O", $rows2["ModifiedTime"]));
						$channel->appendChild($item);
					}
				}
			}

			$dalH->ID = "2";
			$dalH->loadRecord();
			if($dalH->Visible == "1"){
				$dalNC = new news_categoryDAL();
				$dalN = new newsDAL();
				$news = new news();
				$item = $this->additem($dom, $news->linkNewsList(), $indexTitle, $indexDescription, $time);
				$channel->appendChild($item);

				$dataNC = $dalNC->executeRecordset([], "a.`Visible`=1");
				foreach ($dataNC as $rows) {
					$webTitle = (!empty($rows["WebTitle"])) ? $this->formatspace($rows["WebTitle"]) : $indexTitle;
					$webDescription = (!empty($rows["WebDescription"])) ? $this->formatspace($rows["WebDescription"]) : $indexDescription;
					$item = $this->additem($dom, $news->linkNewsList($rows["CategoryID"]), $webTitle, $webDescription, $time);
					$channel->appendChild($item);

					$intoAry = [
						parent::addArray(":categoryID", $rows["CategoryID"], "int"),
						parent::addArray(":onlineTime", time(), "int")
					];
					$dataN = $dalN->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`OnlineTime`<=:onlineTime AND a.`Visible`=1", "a.`OrderNO`, a.`OnlineTime` DESC, a.`NewsID` DESC");
					foreach ($dataN as $rows2) {
						$webTitle = (!empty($rows["WebTitle"])) ? $this->formatspace($rows["WebTitle"]) : $indexTitle;
						$webDescription = (!empty($rows["WebDescription"])) ? $this->formatspace($rows["WebDescription"]) : $indexDescription;
						$item = $this->additem($dom, $news->linkNewsDetail($rows2["NewsID"]), $webTitle, $webDescription, date("D, d M Y H:i:s O", $rows2["OnlineTime"]));
						$channel->appendChild($item);
					}
				}
			}

			$dalH->ID = "3";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalFC = new faq_categoryDAL();
				$dalF = new faqDAL();
				$faq = new faq();
				$item = $this->additem($dom, $faq->linkFaqList(), $indexTitle, $indexDescription, $time);
				$channel->appendChild($item);

				$dataFC = $dalFC->executeRecordset([], "a.`Visible`=1");
				foreach ($dataFC as $rows) {
					$webTitle = (!empty($rows["WebTitle"])) ? $this->formatspace($rows["WebTitle"]) : $indexTitle;
					$webDescription = (!empty($rows["WebDescription"])) ? $this->formatspace($rows["WebDescription"]) : $indexDescription;
					$item = $this->additem($dom, $faq->linkFaqList($rows["CategoryID"]), $webTitle, $webDescription, $time);
					$channel->appendChild($item);
				}
			}

			$dalH->ID = "4";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalC = new categoryDAL();
				$dalP = new productDAL();
				$product = new product();
				$item = $this->additem($dom, $product->linkProductList(), $indexTitle, $indexDescription, $time);
				$channel->appendChild($item);

				$dataC = $dalC->executeRecordset([], "a.`ParentID`=0 AND a.`Visible`=1", "a.`OrderNO`, a.`CategoryID`");
				foreach ($dataC as $rowsC) {
					$webTitle = (!empty($rowsC["WebTitle"])) ? $this->formatspace($rowsC["WebTitle"]) : $indexTitle;
					$webDescription = (!empty($rowsC["WebDescription"])) ? $this->formatspace($rowsC["WebDescription"]) : $indexDescription;
					$item = $this->additem($dom, $product->linkProductList($rowsC["CategoryID"]), $webTitle, $webDescription, $time);
					$channel->appendChild($item);

					$this->renderProductList($rowsC["CategoryID"], $dom, $channel, $product, $dalC, $dalP, $indexTitle, $indexDescription, $time);
				}
			}

			$dalH->ID = "5";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalBC = new book_categoryDAL();
				$dalB = new bookDAL();
				$book = new book();
				$item = $this->additem($dom, $book->linkBookList(), $indexTitle, $indexDescription, $time);
				$channel->appendChild($item);
				
				$dataBC = $dalBC->executeRecordset([], "a.`ParentID`=0 AND a.`Visible`=1", "a.`OrderNO`, a.`CategoryID`");
				foreach ($dataBC as $rowsC) {
					$webTitle = (!empty($rowsC["WebTitle"])) ? $this->formatspace($rowsC["WebTitle"]) : $indexTitle;
					$webDescription = (!empty($rowsC["WebDescription"])) ? $this->formatspace($rowsC["WebDescription"]) : $indexDescription;
					$item = $this->additem($dom, $book->linkBookList($rowsC["CategoryID"]), $webTitle, $webDescription, $time);
					$channel->appendChild($item);

					$this->renderBookList($rowsC["CategoryID"], $dom, $channel, $book, $dalBC, $dalB, $indexTitle, $indexDescription, $time);
				}
			}

			$dalH->ID = "6";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$contact = new contact();
				$item = $this->additem($dom, $contact->linkContact(), $indexTitle, $indexDescription, $time2);
				$channel->appendChild($item);
			}
			
			$xmlCode = $dom->saveXML();
			echo $xmlCode;
			
			parent::load();
		}

		private function renderProductList($categoryId, $dom, $channel, $product, $dalC, $dal, $indexTitle, $indexDescription, $time) {
			$intoAry = [parent::addArray(":categoryID", $categoryId, "int")];
		
			// 取得產品資料
			$data = $dal->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1", "a.`OrderNO`,a.`ModifiedTime` DESC");
			foreach ($data as $rows) {
				$webTitle = (!empty($rows["WebTitle"])) ? $this->formatspace($rows["WebTitle"]) : $indexTitle;
				$webDescription = (!empty($rows["WebDescription"])) ? $this->formatspace($rows["WebDescription"]) : $indexDescription;
				$item = $this->additem($dom, $product->linkProductDetail($rows["ProductID"]), $webTitle, $webDescription, date("D, d M Y H:i:s O", $rows["ModifiedTime"]));
				$channel->appendChild($item);
			}
		
			// 取得子分類資料
			$dataC2 = $dalC->executeRecordset($intoAry, "a.`ParentID`=:categoryID AND a.`Visible`=1", "a.`OrderNO`, a.`CategoryID`");
			foreach ($dataC2 as $rowsC2) {
				$webTitle = (!empty($rowsC2["WebTitle"])) ? $this->formatspace($rowsC2["WebTitle"]) : $indexTitle;
				$webDescription = (!empty($rowsC2["WebDescription"])) ? $this->formatspace($rowsC2["WebDescription"]) : $indexDescription;
				$item = $this->additem($dom, $product->linkProductList($rowsC2["CategoryID"]), $webTitle, $webDescription, $time);
				$channel->appendChild($item);
				
				$this->renderProductList($rowsC2["CategoryID"], $dom, $channel, $product, $dalC, $dal, $indexTitle, $indexDescription, $time);
			}
		}

		private function renderBookList($categoryId, $dom, $channel, $book, $dalC, $dal, $indexTitle, $indexDescription, $time) {
			$intoAry = [parent::addArray(":categoryID", $categoryId, "int")];
		
			// 取得相本資料
			$data = $dal->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1", "a.`OrderNO`,a.`ModifiedTime` DESC");
			foreach ($data as $rows) {
				$webTitle = (!empty($rows["WebTitle"])) ? $this->formatspace($rows["WebTitle"]) : $indexTitle;
				$webDescription = (!empty($rows["WebDescription"])) ? $this->formatspace($rows["WebDescription"]) : $indexDescription;
				$item = $this->additem($dom, $book->linkBookDetail($rows["BookID"]), $webTitle, $webDescription, date("D, d M Y H:i:s O", $rows["ModifiedTime"]));
				$channel->appendChild($item);
			}
		
			// 取得子分類資料
			$dataC2 = $dalC->executeRecordset($intoAry, "a.`ParentID`=:categoryID AND a.`Visible`=1", "a.`OrderNO`, a.`CategoryID`");
			foreach ($dataC2 as $rowsC2) {
				$webTitle = (!empty($rowsC2["WebTitle"])) ? $this->formatspace($rowsC2["WebTitle"]) : $indexTitle;
				$webDescription = (!empty($rowsC2["WebDescription"])) ? $this->formatspace($rowsC2["WebDescription"]) : $indexDescription;
				$item = $this->additem($dom, $book->linkBookList($rowsC2["CategoryID"]), $webTitle, $webDescription, $time);
				$channel->appendChild($item);
				
				$this->renderBookList($rowsC2["CategoryID"], $dom, $channel, $book, $dalC, $dal, $indexTitle, $indexDescription, $time);
			}
		}

		private function formatspace($text) {
			$text = trim($text);
			$text = preg_replace("/\s+/", " ", $text);
			return $text;
		}

		private function additem($dom, $link, $title, $description, $time) {
			$link = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $link;
			$item = $dom->createElement("item");

			$itemLink = $dom->createElement("link", $link);
			$item->appendChild($itemLink);

			$itemTitle = $dom->createElement("title");
			$itemTitle->appendChild($dom->createTextNode($title));
			$item->appendChild($itemTitle);

			$itemDescription = $dom->createElement("description");
			$itemDescription->appendChild($dom->createTextNode($description));
			$item->appendChild($itemDescription);

			$updatePeriod = $dom->createElement("pubDate", $time);
			$item->appendChild($updatePeriod);
			$sortOrder = $dom->createElement("guid", $link);
			$item->appendChild($sortOrder);
			$resourceOf = $dom->createAttribute("isPermaLink");
			$resourceOf->value = "false";
			$sortOrder->appendChild($resourceOf);
			return $item;
		}
	}
	$page = new _rss();
	$page->load();
?>