<?php
	require_once "pageBase.php";
	require_once "../dal/count_lastDAL.php";
	require_once "../dal/count_totalDAL.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/registryDAL.php";

	class _countAnalyze extends pageBase {
		//覆蓋父類load()方法
		public function load() {
			$this->limitID = "42";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				if (registryDAL::getRegistryValue("isAnalyze") == 0) {
					$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
					$this->locationHref = "index.php";
				} else {
					$this->loadData();
				}
			}

			parent::loadTemplateFile(dirname(__FILE__) . "/templates/countAnalyze.html");
			parent::load();
		}
		public function loadData(){
			$dalL = new count_lastDAL();
			$dalT = new count_totalDAL();

			// 搜尋引擎分析
			$intoAry = [ parent::addArray(":time", "%".date("Y-m")."%", "str") ];
			$dataL = $dalL->executeRecordset($intoAry, "a.`Time` LIKE :time");
			
			$aryC = [
				["title" => "總瀏覽人次", "type" => "txt_tertiary", "count" => 0, "NO" => 1, "percent" => ""],
				["title" => "來自Yahoo", "type" => "txt_purple", "count" => 0, "NO" => 2, "percent" => ""],
				["title" => "來自Google", "type" => "txt_orange", "count" => 0, "NO" => 3, "percent" => ""],
				["title" => "來自百度", "type" => "txt_info", "count" => 0, "NO" => 4, "percent" => ""]
			];

			foreach ($dataL as $entry) {
				$aryC[0]["count"]++;
				if (mb_strpos($entry["RefererHost"], "yahoo.com", 0, "utf-8") !== false) {
					$aryC[1]["count"]++;
				} elseif (mb_strpos($entry["RefererHost"], "google.com", 0, "utf-8") !== false) {
					$aryC[2]["count"]++;
				} elseif (mb_strpos($entry["RefererHost"], "baidu.com", 0, "utf-8") !== false) {
					$aryC[3]["count"]++;
				}
			}

			foreach ($aryC as &$item) {
				if ($item["NO"] !== 1)  $item["percent"] = ($aryC[0]["count"] !== 0) ? (round($item["count"] / $aryC[0]["count"] * 10000) / 100) . "&#37;" : "";
			}
			$listC = new repeater();
			$listC->id = "listC";
			$listC->dataSource = $aryC;
			parent::addControl($listC);
			$listC2 = new repeater();
			$listC2->id = "listC2";
			$listC2->dataSource = $aryC;
			parent::addControl($listC2);

			// 統計圖表
			$num1 = $dalT->countRecordset();
			$intoAry2 = [ parent::addArray(":date", date("Y") . "%", "str") ];
			$num2 = $dalT->countRecordset($intoAry2, "a.`Date` LIKE :date");
			$intoAry3 = [ parent::addArray(":date", date("Y-m") . "%", "str") ];
			$num3 = $dalT->countRecordset($intoAry3, "a.`Date` LIKE :date");
			$intoAry4 = [ parent::addArray(":date", date("Y-m-d"), "str") ];
			$num4 = $dalT->countRecordset($intoAry4, "a.`Date`=:date");
			parent::addParameter("num1", (string) $num1);
			parent::addParameter("num2", (string) $num2);
			parent::addParameter("num3", (string) $num3);
			parent::addParameter("num4", (string) $num4);
			
			$date2 = date("Y年");
			$date3 = date("Y年m月");
			$date4 = date("Y年m月d日");
			parent::addParameter("date2", $date2);
			parent::addParameter("date3", $date3);
			parent::addParameter("date4", $date4);
			
			$ylistD = $ylistN = [];
			for ($i = 0; $i < 12; $i++) {
				$ydate = date("Y-m", mktime(0, 0, 0, ($i + 1), 1, date("Y")));
				$ylistD[] = "'" . date("m月", mktime(0, 0, 0, ($i + 1), 1, date("Y"))) . "'";
				$intoAryY = [ parent::addArray(":date", $ydate . "%", "str") ];
				$ylistN[] = $dalT->countRecordset($intoAryY, "a.`Date` LIKE :date");
			}
			$ylistDtxt = count($ylistD) ? implode(",", $ylistD) : "";
			$ylistNtxt = count($ylistN) ? implode(",", $ylistN) : "";
			parent::addParameter("ylistD", $ylistDtxt);
			parent::addParameter("ylistN", $ylistNtxt);
			
			$mlistD = $mlistN = [];
			for ($i = 0; $i < 31; $i++) {
				if (mktime(0, 0, 0, date("m"), ($i + 1), date("Y")) > mktime(0, 0, 0, date("m") + 1, 0, date("Y"))) {
					break;
				} else {
					$mdate = date("Y-m-d", mktime(0, 0, 0, date("m"), ($i + 1), date("Y")));
					$mlistD[] = "'" . date("d日", mktime(0, 0, 0, date("m"), ($i + 1), date("Y"))) . "'";
					$intoAryM = [ parent::addArray(":date", $mdate . "%", "str") ];
					$mlistN[] = $dalT->countRecordset($intoAryM, "a.`Date` LIKE :date");
				}
			}
			$mlistDtxt = count($mlistD) ? implode(",", $mlistD) : "";
			$mlistNtxt = count($mlistN) ? implode(",", $mlistN) : "";
			parent::addParameter("mlistD", $mlistDtxt);
			parent::addParameter("mlistN", $mlistNtxt);

			$dlistD = $dlistN = [];
			for ($i = 0; $i < 24; $i++) {
				$ddate = date("Y-m-d H", mktime($i, 0, 0, date("m"), date("d"), date("Y")));
				$dlistD[] = "'" . date("H時", mktime($i, 0, 0, date("m"), date("d"), date("Y"))) . "'";
				$intoAryD = [ parent::addArray(":time", $ddate . "%", "str") ];
				$dlistN[] = $dalL->countRecordset($intoAryD, "a.`Time` LIKE :time");
			}
			$dlistDtxt = count($dlistD) ? implode(",", $dlistD) : "";
			$dlistNtxt = count($dlistN) ? implode(",", $dlistN) : "";
			parent::addParameter("dlistD", $dlistDtxt);
			parent::addParameter("dlistN", $dlistNtxt);

			// 最後100位訪客
			$dataLH = $dalL->executeRecordset([], "", "a.`CountID` DESC", 0, 10);
			foreach ($dataLH as &$entry) {
				$entry["dis"] = ($entry["RefererHost"] == "") ? "d_none" : "";
			}
			$listLH = new repeater();
			$listLH->id = "listLH";
			$listLH->dataSource = $dataLH;
			parent::addControl($listLH);

			// IP點擊次數
			$ym = date("Y-m");
			$intoAryIP = [ parent::addArray(":time", "%" . $ym . "%", "str") ];
			$dataLIP = $dalL->executeRecordset($intoAryIP, "a.`Time` LIKE :time", "a.`IP`", 0, 10);
			$aryL = [];
			foreach ($dataLIP as $entry) {
				if (empty($aryL)) {
					$aryL[] = ["IP" => $entry["IP"], "IP2" => urlencode($entry["IP"]), "ym" => $ym, "Count" => 1];
				} elseif ($entry["IP"] == $aryL[count($aryL) - 1]["IP"]) {
					$aryL[count($aryL) - 1]["Count"]++;
				} else {
					$aryL[] = ["IP" => $entry["IP"], "IP2" => urlencode($entry["IP"]), "ym" => $ym, "Count" => 1];
				}
			}
			$listLIP = new repeater();
			$listLIP->id = "listLIP";
			$listLIP->dataSource = $aryL;
			parent::addControl($listLIP);
			
			$intoAryU = [ parent::addArray(":time", "%" . date("Y-m-d H:i") . "%", "str") ];
			$nownum = $dalL->countRecordset($intoAryU, "a.`Time` LIKE :time");
			parent::addParameter("nownum", (string) $nownum);
			
			parent::addParameter("Y", date("Y"));
			
			parent::loadData();
		}
	}

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