<?php
require_once dirname(dirname(__FILE__))."/page.php";
require_once dirname(dirname(dirname(dirname(__FILE__))))."/dal/faq_categoryDAL.php";
require_once dirname(dirname(dirname(dirname(__FILE__))))."/dal/faqDAL.php";
class faq extends page{
	/*
	 * 取得問與答列表網址
	 * @param $id 問與答分類ID
	 * @param $p 頁數
	 */
	public function linkFaqList($id = 0, $p = 0) {
		if ($this->htVisible[3] === 1) {
			$dalF = new faqDAL();
			$dal = new faq_categoryDAL();
			if (!empty($id) && $id != 0) {
				$dal->CategoryID = $id;
				$dal->loadRecord();
				if ($dal->Visible != 1 && $dal->Disable != 0) {
					$id = 0;
					$p = 0;
				}
			}
			
			$pz = $this->registry->faqPageSize;
			$pz = $pz ?? "10";
			$pz = (int)$pz;
	
			$intoAry = [];
			$filter = "a.`Visible`=1 AND a.`Disable`=0 AND b.`Visible`=1 AND b.`Disable`=0";
			if ($id != 0) {
				$intoAry[] = $this->addArray(":categoryID", $id, "int");
				$filter .= " AND a.`CategoryID`=:categoryID";
			}
			$rows = $dalF->countRecordset($intoAry, $filter);
			$max = ceil($rows / $pz);
			$p = (int)$p;
			if ($p > $max) $p = $max;
	
			$link = "/{$this->htLink[3]}";
			if ($this->registry->webLinkType === "2") {
				if ($id != 0) $link .= "/cate/{$dal->TwPathName}";
				if ($p != 0) $link .= "/page/{$p}";
				$link .= "/";
			} else {
				$link .= ($id != 0) ? $id : "";
				$link .= ($p != 0) ? "_{$p}" : "";
				$link .= ".html";
			}
		} else {
			$link = "/";
		}
		return $link;
	}
	
	/*
	 * 取得問與答網址
	 * @param $id 問與答ID
	 */
	public function linkFaqDetail($id = 0) {
		if ($this->htVisible[3] === 1) {
			$dal = new faqDAL();
			$dal->FaqID = $id;
			$dal->loadRecord();
			if (($dal->Visible != 1 && $dal->Disable != 0) || ($dal->CategoryVisible != 1 && $dal->CategoryDisable != 0)) $id = 0;
			
			$link = "/{$this->htLink[3]}";
			switch ($this->registry->webLinkType) {
				case "2":
					$link .= ($id != 0) ? "/{$dal->TwPathName}/" : "/";
					break;
				case "3":
					$link .= ($id != 0) ? "-{$dal->PathName}.html" : ".html";
					break;
				default:
					$link .= ($id != 0) ? "-{$id}.html" : ".html";
			}
		} else {
			$link = "/";
		}
		return $link;
	}
	
	/* 結構化資料
	 * @param $type 列表/內頁
	 * @param $id 分類ID/文章ID
	 */
	public function seoGoogleSearch($id = 0, $type = 0) {
		// 分數(真)
		$dalS = new scoreDAL();
		$txt = "";
		$image = $this->getLogo();
	
		if ($type === 0) {
			$dal = new faq_categoryDAL();
			$dal->CategoryID = $id;
			$dal->loadRecord();
			$txt .= $this->faqHtml($id);
			if ($dal->IsShowStars === 1) $txt .= $this->isShowStars($dal->RatingValue, $dal->ReviewCount);
		} else if ($type === 1) {
			$dal = new faqDAL();
			$dal->FaqID = $id;
			$dal->loadRecord();
			// 按照設定取得分數
			if ($this->registry->isScore === 2 && $this->registry->isCustomer === 1) {	// 啟用真實評分功能，必須同時啟用會員功能
				$dalS->TargetType = 5;
				$dalS->TargetID = $id;
				$ratingValue = $dalS->getScore;
			} else {
				$ratingValue = $dal->RatingValue;
			}
			if ($dal->IsShowJason === 1) $txt .= $this->reviewHtml("", $dal->Title, $dal->RatingValue, $dal->BestRating);
			if ($dal->IsShowStars === 1) $txt .= $this->isShowStars($dal->RatingValue, $dal->ReviewCount);
		}
		return $txt;
	}
}
?>