<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class newsDAL extends dalBase{
		public $NewsID;
		public $CategoryID;
		public $Title;
		public $QuoteText;
		public $QuoteLink;
		public $Introduction;
		public $Description;
		public $Image;
		public $OnlineTime;
		public $Visible;
		public $OrderNO;
		public $WebTitle;
		public $WebKeyword;
		public $WebDescription;
		public $CreatedTime;
		public $CreatedUserID;
		public $ModifiedTime;
		public $ModifiedUserID;
		//外部欄位
		public $CategoryName;

		public function loadRecord(){
			$sql = "SELECT a.*,b.`CategoryName` FROM `news` a LEFT JOIN `news_category` b ON a.`CategoryID`=b.`CategoryID` WHERE a.`NewsID`='".$this->NewsID."'";
			$reader = $this->data->executeReader($sql);

			$this->NewsID = $reader["NewsID"];
			$this->CategoryID = $reader["CategoryID"];
			$this->Title = $reader["Title"];
			$this->QuoteText = $reader["QuoteText"];
			$this->QuoteLink = $reader["QuoteLink"];
			$this->Introduction = $reader["Introduction"];
			$this->Description = $reader["Description"];
			$this->Image = $reader["Image"];
			$this->OnlineTime = $reader["OnlineTime"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];
			$this->WebTitle = $reader["WebTitle"];
			$this->WebKeyword = $reader["WebKeyword"];
			$this->WebDescription = $reader["WebDescription"];
			$this->CreatedTime = $reader["CreatedTime"];
			$this->CreatedUserID = $reader["CreatedUserID"];
			$this->ModifiedTime = $reader["ModifiedTime"];
			$this->ModifiedUserID = $reader["ModifiedUserID"];
			$this->CategoryName = $reader["CategoryName"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `news` (`CategoryID`, `Title`, `QuoteText`, `QuoteLink`, `Introduction`, `Description`, `Image`, `OnlineTime`, `Visible`, `OrderNO`, `WebTitle`, `WebKeyword`, `WebDescription`, `CreatedTime`, `CreatedUserID`, `ModifiedTime`, `ModifiedUserID`)" .
				" VALUES (@CategoryID,@Title,@QuoteText,@QuoteLink,@Introduction,@Description,@Image,@OnlineTime,@Visible,@OrderNO,@WebTitle,@WebKeyword,@WebDescription,@CreatedTime,@CreatedUserID,@ModifiedTime,@ModifiedUserID)";
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@Title","'".$this->Title."'",$sql);
			$sql = str_replace("@QuoteText","'".$this->QuoteText."'",$sql);
			$sql = str_replace("@QuoteLink","'".$this->QuoteLink."'",$sql);
			$sql = str_replace("@Introduction","'".$this->Introduction."'",$sql);
			$sql = str_replace("@Description","'".$this->Description."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@OnlineTime","'".$this->OnlineTime."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@OrderNO","'".$this->OrderNO."'",$sql);
			$sql = str_replace("@WebTitle","'".$this->WebTitle."'",$sql);
			$sql = str_replace("@WebKeyword","'".$this->WebKeyword."'",$sql);
			$sql = str_replace("@WebDescription","'".$this->WebDescription."'",$sql);
			$sql = str_replace("@CreatedTime","'".date("Y-m-d H:i:s")."'",$sql);
			$sql = str_replace("@CreatedUserID","'".$this->CreatedUserID."'",$sql);
			$sql = str_replace("@ModifiedTime","'".date("Y-m-d H:i:s")."'",$sql);
			$sql = str_replace("@ModifiedUserID","'".$this->ModifiedUserID."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return $this->data->getInsertID();
			return false;
		}
		public function updateRecord(){
			$sql = "UPDATE `news` SET " .
				"`CategoryID`=@CategoryID" .
				",`Title`=@Title" .
				",`QuoteText`=@QuoteText" .
				",`QuoteLink`=@QuoteLink" .
				",`Introduction`=@Introduction" .
				",`Description`=@Description" .
				",`Image`=@Image" .
				",`OnlineTime`=@OnlineTime" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				",`WebTitle`=@WebTitle" .
				",`WebKeyword`=@WebKeyword" .
				",`WebDescription`=@WebDescription" .
				",`ModifiedTime`=@ModifiedTime" .
				",`ModifiedUserID`=@ModifiedUserID" .
				" WHERE `NewsID`=@NewsID";
			$sql = str_replace("@NewsID","'".$this->NewsID."'",$sql);
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@Title","'".$this->Title."'",$sql);
			$sql = str_replace("@QuoteText","'".$this->QuoteText."'",$sql);
			$sql = str_replace("@QuoteLink","'".$this->QuoteLink."'",$sql);
			$sql = str_replace("@Introduction","'".$this->Introduction."'",$sql);
			$sql = str_replace("@Description","'".$this->Description."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@OnlineTime","'".$this->OnlineTime."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@OrderNO","'".$this->OrderNO."'",$sql);
			$sql = str_replace("@WebTitle","'".$this->WebTitle."'",$sql);
			$sql = str_replace("@WebKeyword","'".$this->WebKeyword."'",$sql);
			$sql = str_replace("@WebDescription","'".$this->WebDescription."'",$sql);
			$sql = str_replace("@ModifiedTime","'".date("Y-m-d H:i:s")."'",$sql);
			$sql = str_replace("@ModifiedUserID","'".$this->ModifiedUserID."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return true;
			return false;
		}
		//查詢
		public function executeRecordset($filter="",$order="",$start=0,$length=-1){
			$sql = "SELECT a.*,b.`CategoryName` FROM `news` a LEFT JOIN `news_category` b ON a.`CategoryID`=b.`CategoryID`";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			if(!isset($order) || $order == "")
				$sql .= " ORDER BY a.`OrderNO`,a.`OnlineTime` DESC";
			else
				$sql .= " ORDER BY ".$order;
			return $this->data->executeRecordset($sql,$start,$length);
		}
		//查詢計數
		public function countRecordset($filter=""){
			$sql = "SELECT COUNT(*) FROM `news` a LEFT JOIN `news_category` b ON a.`CategoryID`=b.`CategoryID`";
			if($filter != "")
				$sql .= " WHERE ".$filter;

			return $this->data->executeScalar($sql);
		}

		//刪除
		public function deleteRecord($newsID){
			$sql = "DELETE FROM `news` WHERE `NewsID`='".$newsID."'";
			$this->data->executeNonQuery($sql);

			if(!$this->data->error)
				return true;
			return false;
		}

		//批量刪除，傳入參數為用逗號分隔的id串
		public function batDelete($newsIDs){
			$sql = "DELETE FROM `news` WHERE `NewsID` IN (".$newsIDs.")";
			$this->data->executeNonQuery($sql,true);

			if(!$this->data->error)
				return true;
			return false;
		}

		//批量修改啟用狀態，傳入參數為用逗號分隔的id串和啟用欄位值
		public function batUpdateVisible($newsIDs,$value="1"){
			$sql = "UPDATE `news` SET `Visible`='".$value."' WHERE `NewsID` IN (".$newsIDs.")";
			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return true;
			return false;
		}
	}
?>