<?php
	require_once dirname(__FILE__)."/../bin/web/repeater.php";
	require_once dirname(__FILE__)."/../dal/book_categoryDAL.php";
	require_once dirname(__FILE__)."/../dal/categoryDAL.php";
	require_once dirname(__FILE__)."/../dal/count_lastDAL.php";
	require_once dirname(__FILE__)."/../dal/count_totalDAL.php";
	require_once dirname(__FILE__)."/../dal/faq_categoryDAL.php";
	require_once dirname(__FILE__)."/../dal/htaccessDAL.php";
	require_once dirname(__FILE__)."/../dal/news_categoryDAL.php";
	require_once dirname(__FILE__)."/../dal/pictureDAL.php";
	require_once dirname(__FILE__)."/../dal/webmenuDAL.php";

	class _footer extends webPartBase{
		public function load(){
			$this->loadData();
			parent::loadTemplateFile(dirname(__FILE__)."/../templates/webParts/".$this->registry->webTemplates."/footer.html");
			parent::load();
		}
		private function loadData(){
			if(isset($_SESSION["CustomerID"])){
				parent::addParameter("topMenu1","<a class='login' href='memberlogout.html'>Đăng xuất</a>");
				parent::addParameter("topMenu2","");
			}else{
				parent::addParameter("topMenu1","<a class='login' href='memberlogin.html'>Đăng nhập</a>");
				parent::addParameter("topMenu2","<a class='member' href='newmember.html'>Đăng ký</a>｜ ");
			}

			//網頁網址設定
			$dalHT = new htaccessDAL();
			$dalHT->ID = "1";
			$dalHT->loadRecord();
			$htLink1 = $dalHT->HtmlName;
			$dalHT->ID = "2";
			$dalHT->loadRecord();
			$htLink2 = $dalHT->HtmlName;
			$dalHT->ID = "3";
			$dalHT->loadRecord();
			$htLink3 = $dalHT->HtmlName;
			$dalHT->ID = "4";
			$dalHT->loadRecord();
			$htLink4 = $dalHT->HtmlName;
			$dalHT->ID = "5";
			$dalHT->loadRecord();
			$htLink5 = $dalHT->HtmlName;
			$dalHT->ID = "6";
			$dalHT->loadRecord();
			$htLink6 = $dalHT->HtmlName;
			$dalHT->ID = "7";
			$dalHT->loadRecord();
			$htLink7 = $dalHT->HtmlName;
			
			if($this->registry->LineOpen == "0"){
				parent::addParameter("LineClose"," style='display:none;'");
			}

			parent::addParameter("webName",$this->registry->webName);
			parent::addParameter("webFooter",$this->registry->webFooter);
			parent::addParameter("webPhone",$this->registry->webPhone);
			parent::addParameter("webMobile",$this->registry->webMobile);
			parent::addParameter("webEmail",$this->registry->webEmail);
			parent::addParameter("webAddress",$this->registry->webAddress);
			parent::addParameter("webFax",$this->registry->webFax);
			parent::addParameter("fbFansURL",$this->registry->fbFansURL);
			parent::addParameter("webCopy",$this->registry->webCopy);
			parent::addParameter("webMarquee",$this->registry->webMarquee);
			parent::addParameter("webH1",$this->registry->webH1);
			parent::addParameter("webTitle",$this->registry->webTitle);
			parent::addParameter("webKeyword",$this->registry->webKeyword);
			parent::addParameter("lineTitle",$this->registry->lineTitle);
			if(trim($this->registry->lineQRCode) == "" || !file_exists(".".$this->registry->lineQRCode)){
				parent::addParameter("QRstyle","display:none;");
				parent::addParameter("lineQRCode","/images/no-pic.jpg");
			}else{
				parent::addParameter("QRstyle","");
				parent::addParameter("lineQRCode",$this->registry->lineQRCode);
			}

			$jsonGM = preg_replace("/\s+/", " ", $jsonGM);
			parent::addParameter("jsonGM", $jsonGM);
			if($this->registry->webPhone == ''){
				parent::addParameter("webPhoneStyle"," style='display:none;'");
			}else{
				parent::addParameter("webPhoneStyle","");
			}
			if($this->registry->webMobile == ''){
				parent::addParameter("webMobileStyle"," style='display:none;'");
			}else{
				parent::addParameter("webMobileStyle","");
			}
			if($this->registry->webEmail == ''){
				parent::addParameter("webEmailStyle"," style='display:none;'");
			}else{
				parent::addParameter("webEmailStyle","");
			}
			if($this->registry->webAddress == ''){
				parent::addParameter("webAddressStyle"," style='display:none;'");
			}else{
				parent::addParameter("webAddressStyle","");
			}
			if($this->registry->webFax == ''){
				parent::addParameter("webFaxStyle"," style='display:none;'");
			}else{
				parent::addParameter("webFaxStyle","");
			}
			if($this->registry->lineTitle == ''){
				parent::addParameter("lineTitleStyle"," style='display:none;'");
			}else{
				parent::addParameter("lineTitleStyle","");
			}

			////logo
			$dalP = new pictureDAL();
			$dalP->PictureType = "20";
			$dalP->TypeOrderNO = "0";
			$dalP->loadRecord();
			if(trim($dalP->Image) == ''){
				parent::addParameter("logoImage","/images/no-pic.jpg");
				parent::addParameter("logoStyle"," style='display:none;'");
			}else{
				if(file_exists(".".$dalP->Image)){
					parent::addParameter("logoImage",$dalP->Image);
					parent::addParameter("logoStyle","");
				}else{
					parent::addParameter("logoImage","/images/no-pic.jpg");
					parent::addParameter("logoStyle"," style='display:none;'");
				}
			}
			if(trim($dalP->Title) == ''){
				parent::addParameter("logoTitle",$this->registry->webName);
			}else{
				parent::addParameter("logoTitle",$dalP->Title);
			}
			if(trim($dalP->Link) == ''){
				parent::addParameter("logoLink","/");
			}else{
				parent::addParameter("logoLink",$dalP->Link);
			}
			
			//open grapy
			$dataP = $dalP->executeRecordset("a.`PictureType`='30'","a.`TypeOrderNO`",0,1);
			if(count($dataP) == 1){
				parent::addParameter("ogImage","<meta property=\"og:image\" content=\"http://".$_SERVER['HTTP_HOST'].$dataP[0]["Image"]."\"/>");
			}else{
				parent::addParameter("ogImage","");
			}

			//主選單
			$dalWM = new webmenuDAL();
			$dalC = new categoryDAL();
			$dalNC = new news_categoryDAL();
			$dalFC = new faq_categoryDAL();
			$dalBC = new book_categoryDAL();
			$dataWM = $dalWM->executeRecordset("a.`Visible`='1'");
			for($i=0;$i<count($dataWM);$i++){
				switch($dataWM[$i]["MenuType"]){
					case "0":
						$dataWM[$i]["htLink"] = $htLink1;
						$dataWM[$i]["SubList"] = "";
						break;
					case "1":
						$dataWM[$i]["htLink"] = $htLink4;
						if($dataWM[$i]["SubMenu"] == "0"){
							$dataWM[$i]["SubList"] = "";
						}else{
							$dataC = $dalC->executeRecordset("a.`ParentID`='0' AND a.`Visible`='1'");
							for($j=0;$j<count($dataC);$j++){
								$dataSC = $dalC->executeRecordset("a.`ParentID`='".$dataC[$j]["CategoryID"]."' AND a.`Visible`='1'");
								if(count($dataSC) == 0){
									$dataWM[$i]["SubList"] .= "<li";
									if($j == (count($dataC) - 1)){
										$dataWM[$i]["SubList"] .= " class='last'";
									}
									$dataWM[$i]["SubList"] .= "><a href='".$htLink4.$dataC[$j]["CategoryID"].".html'>".$dataC[$j]["CategoryName"]."</a></li>";
								}else{
									$dataWM[$i]["SubList"] .= "<li";
									if($j == (count($dataC) - 1)){
										$dataWM[$i]["SubList"] .= " class='last'";
									}
									$dataWM[$i]["SubList"] .= "><p>".$dataC[$j]["CategoryName"]."</p><ul class='subnav'>";
									for($k=0;$k<count($dataSC);$k++){
										$dataWM[$i]["SubList"] .= "<li";
										if($k == (count($dataSC) - 1)){
											$dataWM[$i]["SubList"] .= " class='last'";
										}
										$dataWM[$i]["SubList"] .= "><a href='".$htLink4.$dataSC[$k]["CategoryID"].".html'>".$dataSC[$k]["CategoryName"]."</a></li>";
									}
									$dataWM[$i]["SubList"] .= "</ul></li>";
								}
							}
						}
						break;
					case "2":
						$dataWM[$i]["htLink"] = $htLink2;
						if($dataWM[$i]["SubMenu"] == "0"){
							$dataWM[$i]["SubList"] = "";
						}else{
							$dataNC = $dalNC->executeRecordset("a.`Visible`='1'");
							for($j=0;$j<count($dataNC);$j++){
								$dataWM[$i]["SubList"] .= "<li";
								if($j == (count($dataNC) - 1)){
									$dataWM[$i]["SubList"] .= " class='last'";
								}
								$dataWM[$i]["SubList"] .= "><a href='".$htLink2.$dataNC[$j]["CategoryID"].".html'>".$dataNC[$j]["CategoryName"]."</a></li>";
							}
						}
						break;
					case "3":
						$dataWM[$i]["htLink"] = $htLink3;
						if($dataWM[$i]["SubMenu"] == "0"){
							$dataWM[$i]["SubList"] = "";
						}else{
							$dataFC = $dalFC->executeRecordset("a.`Visible`='1'");
							for($j=0;$j<count($dataFC);$j++){
								$dataWM[$i]["SubList"] .= "<li";
								if($j == (count($dataFC) - 1)){
									$dataWM[$i]["SubList"] .= " class='last'";
								}
								$dataWM[$i]["SubList"] .= "><a href='".$htLink3.$dataFC[$j]["CategoryID"].".html'>".$dataFC[$j]["CategoryName"]."</a></li>";
							}
						}
						break;
					case "4":
						$dataWM[$i]["htLink"] = $htLink5;
						if($dataWM[$i]["SubMenu"] == "0"){
							$dataWM[$i]["SubList"] = "";
						}else{
							$dataBC = $dalBC->executeRecordset("a.`Visible`='1'");
							for($j=0;$j<count($dataBC);$j++){
								$dataWM[$i]["SubList"] .= "<li";
								if($j == (count($dataBC) - 1)){
									$dataWM[$i]["SubList"] .= " class='last'";
								}
								$dataWM[$i]["SubList"] .= "><a href='".$htLink5.$dataBC[$j]["CategoryID"].".html'>".$dataBC[$j]["CategoryName"]."</a></li>";
							}
						}
						break;
					case "5":
						$dataWM[$i]["htLink"] = $htLink6;
						$dataWM[$i]["SubList"] = "";
						break;
				}
			}
			$listWM = new repeater();
			$listWM->id = "listWM";
			$listWM->dataSource = $dataWM;
			parent::addControl($listWM);

			//rel link
			parent::addParameter("RelLink","http://".$_SERVER['HTTP_HOST']);
			parent::addParameter("HTTPHOST",$_SERVER['HTTP_HOST']);
			parent::addParameter("REQUESTURI",$_SERVER['REQUEST_URI']);
			
			//臉書粉絲團
			if(trim($this->registry->fbFansURL) != ''){
				if($this->registry->fbFansFriendFace == '0'){
					$facepile = 'false';
				}else{
					$facepile = 'true';
				}
				if($this->registry->fbFansCoverPhoto == '1'){
					$hidecover = 'false';
				}else{
					$hidecover = 'true';
				}
				if($this->registry->fbFansPagePost == '0'){
					$showpost = 'false';
				}else{
					$showpost = 'true';
				}
				$fbFans = '<div class="fb-page" data-href="'.$this->registry->fbFansURL.'" data-hide-cover="'.$hidecover.'" data-show-facepile="'.$facepile.'" data-show-posts="'.$showpost.'"><div class="fb-xfbml-parse-ignore"><blockquote cite="'.$this->registry->fbFansURL.'"><a href="'.$this->registry->fbFansURL.'">Facebook</a></blockquote></div></div>';
			}else{
				$fbFans = '';
			}
			parent::addParameter("fbFans",$fbFans);
			
			//計數器
			$dalDL = new count_lastDAL();
			$dalDT = new count_totalDAL();
			$ip = "";
			if(!empty($_SERVER['HTTP_CLIENT_IP'])){
				$ip = $_SERVER['HTTP_CLIENT_IP'];
			}else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
				$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
			}else{
				$ip = $_SERVER['REMOTE_ADDR'];
			}
			$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$refererURL = $_SERVER['HTTP_REFERER'];
			$referer = parse_url($_SERVER['HTTP_REFERER']);
			if(!$referer){
				$refererHost = "";
				$refererPath = "";
				$refererQuery = "";
			}else{
				$refererHost = $referer["host"];
				$refererPath = $referer["path"];
				$refererQuery = $referer["query"];
			}

			$flagDT = $dalDT->countRecordset("a.`IP`='".$ip."' AND a.`Date`='".date("Y-m-d")."'");
			if($flagDT == 0){
				$dalDL->IP = $ip;
				$dalDL->Time = date("Y-m-d H:i:s");
				$dalDL->URL = $url;
				$dalDL->RefererURL = $refererURL;
				$dalDL->RefererHost = $refererHost;
				$dalDL->RefererPath = $refererPath;
				$dalDL->RefererQuery = $refererQuery;
				$dalDL->insertRecord();

				$dalDT->IP = $ip;
				$dalDT->Date = date("Y-m-d");
				$dalDT->insertRecord();
			}
			//$lastT = date("Y-m-d H:i:s",mktime(0,0,0,date("n"),((int)date("j")-7),date("Y")));
			$lastT = date("Y-m-d H:i:s",mktime(0,0,0,((int)date("n")-3),1,date("Y")));
			$dalDL->deleteRecord("`Time`<'".$lastT."'");

			$count1 = $dalDT->countRecordset();
			$count2 = $dalDT->countRecordset("a.`Date`='".date("Y-m-d")."'");
			parent::addParameter("countTotal",$count1);
			parent::addParameter("countToday",$count2);
		}
	}
?>