<?php
	require_once "pageBase.php";
	require_once "bin/web/repeater.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 "dal/htaccessDAL.php";
	require_once "dal/pictureDAL.php";

	class _webmap extends pageBase{
		public function load() {
			$this->loadData();
			if($this->registry->sitemapType == "2") {
				parent::loadTemplateFile("templates/".$this->registry->webTemplates."/webmap2.html");
			}else if($this->registry->sitemapType == "3") {
				parent::loadTemplateFile("templates/".$this->registry->webTemplates."/webmap3.html");
			}else{
				parent::loadTemplateFile("templates/".$this->registry->webTemplates."/webmap.html");
			}
			parent::load();
		}
		public function loadData() {
			//橫幅廣告
			$this->picturetype = "100";
			//
			$dalH = new htaccessDAL();
			
			//
			$aryA1 = [];
			$aryA2 = [];
			$aryA3 = [];
			$dalH->ID = "1";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalAC = new article_categoryDAL();
				$dalA = new articleDAL();
				$article = new article();
				$dataAC = $dalAC->executeRecordset([], "a.`Visible`=1 AND a.`Disable`=0");
				foreach ($dataAC as $rows) {
					$link = $article->linkArticleList($rows["CategoryID"]);
					$aryA1[] = [ "title" => $rows["CategoryName"], "link" => $link, "type" => "L" ];
					$aryA2[] = [ "title" => $rows["CategoryName"], "link" => $link ];
					$intoAry = [parent::addArray(":categoryID", $rows["CategoryID"], "int")];
					$dataA = $dalA->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0");
					$aryS = [];
					foreach ($dataA as $rows2) {
						$link = $article->linkArticleDetail($rows2["ArticleID"]);
						$aryA1[] = [ "title" => $rows2["Title"], "link" => $link, "type" => "D" ];
						$aryS[] = [ "title" => urlencode($rows2["Title"]), "link" => urlencode($link) ];
					}
					$aryA3[] = [ "title" => urlencode($rows["CategoryName"]), "link" => urlencode($link), "aryS" => $aryS ];
				}
			}
			$listA = new repeater();
			$listA->id = "listA";
			$listA->dataSource = $aryA1;
			parent::addControl($listA);
			$listA2 = new repeater();
			$listA2->id = "listA2";
			$listA2->dataSource = $aryA2;
			parent::addControl($listA2);
			$this->processVisibility($dalH->Visible, "article", "A");
			parent::addParameter("jsonA", urldecode(json_encode($aryA3)));

			//
			$aryN1 = [];
			$aryN2 = [];
			$aryN3 = [];
			$dalH->ID = "2";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalNC = new news_categoryDAL();
				$dalN = new newsDAL();
				$news = new news();
				$dataNC = $dalNC->executeRecordset([], "a.`Visible`=1 AND a.`Disable`=0");
				foreach ($dataNC as $rows) {
					$link = $news->linkNewsList($rows["CategoryID"]);
					$aryN1[] = [ "title" => $rows["CategoryName"], "link" => $link, "type" => "L" ];
					$aryN2[] = [ "title" => $rows["CategoryName"], "link" => $link ];
					$intoAry = [ parent::addArray(":categoryID", $rows["CategoryID"], "int") ];
					$dataN = $dalN->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0");
					$aryS = [];
					foreach ($dataN as $rows2) {
						$link = $news->linkNewsDetail($rows2["NewsID"]);
						$aryN1[] = [ "title" => $rows2["Title"], "link" => $link, "type" => "D" ];
						$aryS[] = [ "title" => urlencode($rows2["Title"]), "link" => urlencode($link) ];
					}
					$aryN3[] = [ "title" => urlencode($rows["CategoryName"]), "link" => urlencode($link), "aryS" => $aryS ];
				}
			}
			$listN = new repeater();
			$listN->id = "listN";
			$listN->dataSource = $aryN1;
			parent::addControl($listN);
			$listN2 = new repeater();
			$listN2->id = "listN2";
			$listN2->dataSource = $aryN2;
			parent::addControl($listN2);
			$this->processVisibility($dalH->Visible, "news", "N");
			parent::addParameter("jsonN", urldecode(json_encode($aryN3)));

			//
			$aryF1 = [];
			$aryF2 = [];
			$aryF3 = [];
			$dalH->ID = "3";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalFC = new faq_categoryDAL();
				$dalF = new faqDAL();
				$faq = new faq();
				$dataFC = $dalFC->executeRecordset([], "a.`Visible`=1 AND a.`Disable`=0");
				foreach ($dataFC as $rows) {
					$link = $faq->linkFaqList($rows["CategoryID"]);
					$aryF1[] = [ "title" => $rows["CategoryName"], "link" => $link, "type" => "L" ];
					$aryF2[] = [ "title" => $rows["CategoryName"], "link" => $link ];
					$intoAry = [ parent::addArray(":categoryID", $rows["CategoryID"], "int") ];
					$dataF = $dalF->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0");
					$aryS = [];
					foreach ($dataF as $rows2) {
						$link = $faq->linkFaqDetail($rows2["FaqID"]);
						$aryF1[] = [ "title" => $rows2["Title"], "link" => $link, "type" => "D" ];
						$aryS[] = [ "title" => urlencode($rows2["Title"]), "link" => urlencode($link) ];
					}
					$aryF3[] = [ "title" => urlencode($rows["CategoryName"]), "link" => urlencode($link), "aryS" => $aryS ];
				}
			}
			$listF = new repeater();
			$listF->id = "listF";
			$listF->dataSource = $aryF1;
			parent::addControl($listF);
			$listF2 = new repeater();
			$listF2->id = "listF2";
			$listF2->dataSource = $aryF2;
			parent::addControl($listF2);
			$this->processVisibility($dalH->Visible, "faq", "F");
			parent::addParameter("jsonF", urldecode(json_encode($aryF3)));

			//
			$aryP1 = [];
			$aryP2 = [];
			$aryP3 = [];
			$dalH->ID = "4";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalC = new categoryDAL();
				$dalP = new productDAL();
				$product = new product();
				$dataC = $dalC->executeRecordset([], "a.`ParentID`=0 AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`CategoryID`");
				foreach ($dataC as $rows) {
					$link1 = $product->linkProductList($rows["CategoryID"]);
					$aryP1[] = [ "title" => $rows["CategoryName"], "link" => $link1, "type" => "L", "lv" => "1" ];
					$aryP2[] = [ "title" => $rows["CategoryName"], "link" => $link1, "lv" => "1" ];
					$intoAry = [ parent::addArray(":categoryID", $rows["CategoryID"], "int") ];
					$dataP = $dalP->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`ModifiedTime` DESC");
					$arySP = [];
					foreach ($dataP as $rowsP) {
						$link2 = $product->linkProductDetail($rowsP["ProductID"]);
						$aryP1[] = [ "title" => $rowsP["ProductName"], "link" => $link2, "type" => "D", "lv" => "1" ];
						$arySP[] = [ "title" => urlencode($rowsP["ProductName"]), "link" => urlencode($link2) ];
					}
					$dataC2 = $dalC->executeRecordset($intoAry, "a.`ParentID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`CategoryID`");
					$arySC = [];
					foreach ($dataC2 as $rows2) {
						$link2 = $product->linkProductList($rows2["CategoryID"]);
						$aryP1[] = [ "title" => $rows2["CategoryName"], "link" => $link2, "type" => "L", "lv" => "2" ];
						$aryP2[] = [ "title" => $rows2["CategoryName"], "link" => $link2, "lv" => "2" ];
						$intoAry = [ parent::addArray(":categoryID", $rows2["CategoryID"], "int") ];
						$dataP = $dalP->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`ModifiedTime` DESC");
						$arySP2 = [];
						foreach ($dataP as $rowsP) {
							$link3 = $product->linkProductDetail($rowsP["ProductID"]);
							$aryP1[] = [ "title" => $rowsP["ProductName"], "link" => $link3, "type" => "D", "lv" => "2" ];
							$arySP2[] = [ "title" => urlencode($rowsP["ProductName"]), "link" => urlencode($link3) ];
						}
						$dataC3 = $dalC->executeRecordset($intoAry, "a.`ParentID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`CategoryID`");
						$arySC2 = [];
						foreach ($dataC3 as $rows3) {
							$link3 = $product->linkProductList($rows3["CategoryID"]);
							$aryP1[] = [ "title" => $rows3["CategoryName"], "link" => $link3, "type" => "L", "lv" => "3" ];
							$aryP2[] = [ "title" => $rows3["CategoryName"], "link" => $link3, "lv" => "3" ];
							$intoAry = [ parent::addArray(":categoryID", $rows3["CategoryID"], "int") ];
							$dataP = $dalP->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`ModifiedTime` DESC");
							$arySP3 = [];
							foreach ($dataP as $rowsP) {
								$link4 = $product->linkProductDetail($rowsP["ProductID"]);
								$aryP1[] = [ "title" => $rowsP["ProductName"], "link" => $link4, "type" => "D", "lv" => "3" ];
								$arySP3[] = [ "title" => urlencode($rowsP["ProductName"]), "link" => urlencode($link4) ];
							}
							$arySC2[] = [ "title" => urlencode($rows3["CategoryName"]), "link" => urlencode($link3), "arySP" => $arySP3 ];
						}
						$arySC[] = [ "title" => urlencode($rows2["CategoryName"]), "link" => urlencode($link2), "arySP" => $arySP2, "arySC" => $arySC2 ];
					}
					$aryP3[] = [ "title" => urlencode($rows["CategoryName"]), "link" => urlencode($link1), "arySP" => $arySP, "arySC" => $arySC ];
				}
			}
			$listP = new repeater();
			$listP->id = "listP";
			$listP->dataSource = $aryP1;
			parent::addControl($listP);
			$listP2 = new repeater();
			$listP2->id = "listP2";
			$listP2->dataSource = $aryP2;
			parent::addControl($listP2);
			$this->processVisibility($dalH->Visible, "product", "P");
			parent::addParameter("jsonP", urldecode(json_encode($aryP3)));

			//
			$aryB1 = [];
			$aryB2 = [];
			$aryB3 = [];
			$dalH->ID = "5";
			$dalH->loadRecord();
			if ($dalH->Visible == "1") {
				$dalBC = new book_categoryDAL();
				$dalB = new bookDAL();
				$book = new book();
				$dataBC = $dalBC->executeRecordset([], "a.`ParentID`=0 AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`CategoryID`");
				foreach ($dataBC as $rows) {
					$link1 = $book->linkBookList($rows["CategoryID"]);
					$aryB1[] = [ "title" => $rows["CategoryName"], "link" => $link1, "type" => "L", "lv" => "1" ];
					$aryB2[] = [ "title" => $rows["CategoryName"], "link" => $link1, "lv" => "1" ];
					$intoAry = [ parent::addArray(":categoryID", $rows["CategoryID"], "int") ];
					$dataB = $dalB->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`ModifiedTime` DESC");
					$arySB = [];
					foreach ($dataB as $rowsB) {
						$link2 = $book->linkBookDetail($rowsB["BookID"]);
						$aryB1[] = [ "title" => $rowsB["BookName"], "link" => $link2, "type" => "D", "lv" => "1" ];
						$arySB[] = [ "title" => urlencode($rowsB["BookName"]), "link" => urlencode($link2) ];
					}
					$dataBC2 = $dalBC->executeRecordset($intoAry, "a.`ParentID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`CategoryID`");
					$arySC = [];
					foreach ($dataBC2 as $rows2) {
						$link2 = $book->linkBookList($rows2["CategoryID"]);
						$aryB1[] = [ "title" => $rows2["CategoryName"], "link" => $link2, "type" => "L", "lv" => "2" ];
						$aryB2[] = [ "title" => $rows2["CategoryName"], "link" => $link2, "lv" => "2" ];
						$intoAry = [ parent::addArray(":categoryID", $rows2["CategoryID"], "int") ];
						$dataB = $dalB->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`ModifiedTime` DESC");
						$arySB2 = [];
						foreach ($dataB as $rowsB) {
							$link3 = $book->linkBookDetail($rowsB["BookID"]);
							$aryB1[] = [ "title" => $rowsB["BookName"], "link" => $link3, "type" => "D", "lv" => "2" ];
							$arySB2[] = [ "title" => urlencode($rowsB["BookName"]), "link" => urlencode($link3) ];
						}
						$dataBC3 = $dalBC->executeRecordset($intoAry, "a.`ParentID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`CategoryID`");
						$arySC2 = [];
						foreach ($dataBC3 as $rows3) {
							$link3 = $book->linkBookList($rows3["CategoryID"]);
							$aryB1[] = [ "title" => $rows3["CategoryName"], "link" => $link3, "type" => "L", "lv" => "3" ];
							$aryB2[] = [ "title" => $rows3["CategoryName"], "link" => $link3, "lv" => "3" ];
							$intoAry = [ parent::addArray(":categoryID", $rows3["CategoryID"], "int") ];
							$dataB = $dalB->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0", "a.`OrderNO`,a.`ModifiedTime` DESC");
							$arySB3 = [];
							foreach ($dataB as $rowsB) {
								$link4 = $book->linkBookDetail($rowsB["BookID"]);
								$aryB1[] = [ "title" => $rowsB["BookName"], "link" => $link4, "type" => "D", "lv" => "3" ];
								$arySB3[] = [ "title" => urlencode($rowsB["BookName"]), "link" => urlencode($link4) ];
							}
							$arySC2[] = [ "title" => urlencode($rows3["CategoryName"]), "link" => urlencode($link3), "arySB" => $arySB3 ];
						}
						$arySC[] = [ "title" => urlencode($rows2["CategoryName"]), "link" => urlencode($link2), "arySB" => $arySB2, "arySC" => $arySC2 ];
					}
					$aryB3[] = [ "title" => urlencode($rows["CategoryName"]), "link" => urlencode($link1), "arySB" => $arySB, "arySC" => $arySC ];
				}
			}
			$listB = new repeater();
			$listB->id = "listB";
			$listB->dataSource = $aryB1;
			parent::addControl($listB);
			$listB2 = new repeater();
			$listB2->id = "listB2";
			$listB2->dataSource = $aryB2;
			parent::addControl($listB2);
			$this->processVisibility($dalH->Visible, "book", "B");
			parent::addParameter("jsonB", urldecode(json_encode($aryB3)));

			//
			$dalH->ID = "6";
			$dalH->loadRecord();
			$this->processVisibility($dalH->Visible, "contact", "C");
			parent::loadData();
		}

		// 建立模組的顯示方式
		public function processVisibility($visible, $sectionName, $sectionSub) {
			$visibility = $visible == "1" ? "" : " style='display:none;'";
			$flag = $visible == "1" ? "1" : "0";
			
			parent::addParameter($sectionName . "Style", $visibility);
			parent::addParameter("flag" . $sectionSub, $flag);
		}
	}

	$page = new _webmap();
	$page->load();
	$page->render();
?>