<?php
require_once dirname(dirname(__FILE__))."/page.php";
require_once dirname(dirname(dirname(dirname(__FILE__))))."/dal/news_categoryDAL.php";
require_once dirname(dirname(dirname(dirname(__FILE__))))."/dal/newsDAL.php";
class news extends page{
	/*
	 * 取得最新消息列表網址
	 * @param $id 最新消息分類ID
	 * @param $p 頁數
	 */
	public function linkNewsList($id = 0, $p = 0) {
		if ($this->htVisible[2] === 1) {
			$dalN = new newsDAL();
			$dal = new news_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->newsPageSize ?? "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 = $dalN->countRecordset($intoAry, $filter);
			$max = ceil($rows / $pz);
			$p = (int)$p;
			if ($p > $max) $p = $max;
			
			$link = "/{$this->htLink[2]}";
			if ($this->registry->webLinkType === "2") {
				if ($id != 0) $link .= "/cate/{$dal->TwPathName}";
				if ($p != 0) $link .= "/page/{$p}";
				$link .= "/";
			} else {
				if ($id != 0) $link .= $id;
				if ($p != 0) $link .= "_{$p}";
				$link .= ".html";
			}
		} else {
			$link = "/";
		}
		return $link;
	}
	
	/*
	 * 取得最新消息網址
	 * @param $id 最新消息ID
	 */
	public function linkNewsDetail($id = 0) {
		if ($this->htVisible[2] === 1) {
			$dal = new newsDAL();
			$dal->NewsID = $id;
			$dal->loadRecord();
			if (($dal->Visible != 1 && $dal->Disable != 0) || ($dal->CategoryVisible != 1 && $dal->CategoryDisable != 0)) $id = 0;
			
			$link = "/{$this->htLink[2]}";
			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 news_categoryDAL();
			$dal->CategoryID = $id;
			$dal->loadRecord();
			if ($dal->OGImage != "" && $this->image_exists($dal->OGImage)) $image = $dal->OGImage;
			if ($dal->IsShowJason === 1) $txt .= $this->reviewHtml($image, $dal->CategoryName, $dal->RatingValue, $dal->BestRating);
			if ($dal->IsShowStars === 1) $txt .= $this->isShowStars($dal->RatingValue, $dal->ReviewCount);
		} else if ($type === 1) {
			$dal = new NewsDAL(); // Assuming "NewsDAL" is the updated class name
			$dal->NewsID = $id;
			$dal->loadRecord();
			if ($dal->Image != "" && $this->image_exists($dal->Image)) $image = $dal->Image;
			// 按照設定取得分數
			if ($this->registry->isScore === 2 && $this->registry->isCustomer === 1) {	// 啟用真實評分功能，必須同時啟用會員功能
				$dalS->TargetType = 4;
				$dalS->TargetID = $id;
				$ratingValue = $dalS->getScore;
			} else {
				$ratingValue = $dal->RatingValue;
			}
			if ($dal->IsShowJason === 1) $txt .= $this->reviewHtml($image, $dal->Title, $dal->RatingValue, $dal->BestRating);
			if ($dal->IsShowStars === 1) $txt .= $this->isShowStars($dal->RatingValue, $dal->ReviewCount);
		}
		return $txt;
	}
}
?>