<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class searchItemDAL extends dalBase {
		//查詢
		public function executeRecordset(array $intoAry, string $kw, int $start = 0, int $length = -1) {
			$sql = "(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '1' AS `ItemLINK` FROM `article_category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `ArticleID` AS `ID`, `Title`, '' AS `NO`, `Image`, `Description`, '2' AS `ItemType`, '1' AS `ItemLINK` FROM `article` WHERE (`Title` LIKE '%".$kw."%' OR `Description` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '2' AS `ItemLINK` FROM `news_category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `NewsID` AS `ID`, `Title`, '' AS `NO`, `Image`, `Description`, '2' AS `ItemType`, '2' AS `ItemLINK` FROM `news` WHERE (`Title` LIKE '%".$kw."%' OR `Description` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '3' AS `ItemLINK` FROM `faq_category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `FaqID` AS `ID`, `Title`, '' AS `NO`, `BannerImage` AS `Image`, `Description`, '2' AS `ItemType`, '3' AS `ItemLINK` FROM `faq` WHERE (`Title` LIKE '%".$kw."%' OR `Description` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '4' AS `ItemLINK` FROM `category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `ProductID` AS `ID`, `ProductName` AS `Title`, `ProductNO` AS `NO`, `IconImage` AS `Image`, `Description1`, '2' AS `ItemType`, '4' AS `ItemLINK` FROM `product` WHERE (`ProductName` LIKE '%".$kw."%' OR `ProductNO` LIKE '%".$kw."%' OR `Description1` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '5' AS `ItemLINK` FROM `book_category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `BookID` AS `ID`, `BookName` AS `Title`, '' AS `NO`, `BookImage` AS `Image`, `Description`, '2' AS `ItemType`, '5' AS `ItemLINK` FROM `book` WHERE (`BookName` LIKE '%".$kw."%' OR `Description` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" ORDER BY `ItemLINK`,`ItemType`,`ID`";
		
			return $this->data->executeRecordset($sql, $intoAry, $start, $length);
		}
		//查詢計數
		public function countRecordset(array $intoAry, string $kw): int {
			$sql = "SELECT COUNT(*) FROM (";
			$sql .= "(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '1' AS `ItemLINK` FROM `article_category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `ArticleID` AS `ID`, `Title`, '' AS `NO`, `Image`, `Description`, '2' AS `ItemType`, '1' AS `ItemLINK` FROM `article` WHERE (`Title` LIKE '%".$kw."%' OR `Description` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '2' AS `ItemLINK` FROM `news_category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `NewsID` AS `ID`, `Title`, '' AS `NO`, `Image`, `Description`, '2' AS `ItemType`, '2' AS `ItemLINK` FROM `news` WHERE (`Title` LIKE '%".$kw."%' OR `Description` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '3' AS `ItemLINK` FROM `faq_category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `FaqID` AS `ID`, `Title`, '' AS `NO`, `BannerImage` AS `Image`, `Description`, '2' AS `ItemType`, '3' AS `ItemLINK` FROM `faq` WHERE (`Title` LIKE '%".$kw."%' OR `Description` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '4' AS `ItemLINK` FROM `category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `ProductID` AS `ID`, `ProductName` AS `Title`, `ProductNO` AS `NO`, `IconImage` AS `Image`, `Description1`, '2' AS `ItemType`, '4' AS `ItemLINK` FROM `product` WHERE (`ProductName` LIKE '%".$kw."%' OR `ProductNO` LIKE '%".$kw."%' OR `Description1` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `CategoryID` AS `ID`, `CategoryName` AS `Title`, '' AS `NO`, `BannerImage` AS `Image`, '' AS `Description`, '1' AS `ItemType`, '5' AS `ItemLINK` FROM `book_category` WHERE `CategoryName` LIKE '%".$kw."%' AND `Visible`=1 AND `Disable`=0)" .
			" UNION " .
			"(SELECT `BookID` AS `ID`, `BookName` AS `Title`, '' AS `NO`, `BookImage` AS `Image`, `Description`, '2' AS `ItemType`, '5' AS `ItemLINK` FROM `book` WHERE (`BookName` LIKE '%".$kw."%' OR `Description` LIKE '%".$kw."%') AND `Visible`=1 AND `Disable`=0)";
			$sql .= ") x";
			
			return $this->data->executeScalar($sql, $intoAry);
		}
	}
?>