<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class bookDAL extends dalBase{
		public $BookID;
		public $CategoryID;
		public $BookName;
		public $Image;
		public $Description;
		public $IsIndex;
		public $Visible;
		public $OrderNO;
		public $WebTitle;
		public $WebKeyword;
		public $WebDescription;
		//外部欄位
		public $CategoryName;

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

			$this->BookID = $reader["BookID"];
			$this->CategoryID = $reader["CategoryID"];
			$this->BookName = $reader["BookName"];
			$this->Image = $reader["Image"];
			$this->Description = $reader["Description"];
			$this->IsIndex = $reader["IsIndex"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];
			$this->WebTitle = $reader["WebTitle"];
			$this->WebKeyword = $reader["WebKeyword"];
			$this->WebDescription = $reader["WebDescription"];
			$this->CategoryName = $reader["CategoryName"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `book` (`CategoryID`, `BookName`, `Image`, `Description`, `IsIndex`, `Visible`, `OrderNO`, `WebTitle`, `WebKeyword`, `WebDescription`)" .
					" VALUES (@CategoryID,@BookName,@Image,@Description,@IsIndex,@Visible,@OrderNO,@WebTitle,@WebKeyword,@WebDescription)";
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@BookName","'".$this->BookName."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@Description","'".$this->Description."'",$sql);
			$sql = str_replace("@IsIndex","'".$this->IsIndex."'",$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);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return $this->data->getInsertID();
			return false;
		}
		public function updateRecord(){
			$sql = "UPDATE `book` SET " .
				"`CategoryID`=@CategoryID" .
				",`BookName`=@BookName" .
				",`Image`=@Image" .
				",`Description`=@Description" .
				",`IsIndex`=@IsIndex" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				",`WebTitle`=@WebTitle" .
				",`WebKeyword`=@WebKeyword" .
				",`WebDescription`=@WebDescription" .
				" WHERE `BookID`=@BookID";
			$sql = str_replace("@BookID","'".$this->BookID."'",$sql);
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@BookName","'".$this->BookName."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@Description","'".$this->Description."'",$sql);
			$sql = str_replace("@IsIndex","'".$this->IsIndex."'",$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);

			$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 `book` a LEFT JOIN `book_category` b ON a.`CategoryID`=b.`CategoryID`";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			if(!isset($order) || $order == "")
				$sql .= " ORDER BY a.`OrderNO`";
			else
				$sql .= " ORDER BY ".$order;
			return $this->data->executeRecordset($sql,$start,$length);
		}
		//查詢計數
		public function countRecordset($filter=""){
			$sql = "SELECT COUNT(*) FROM `book` a LEFT JOIN `book_category` b ON a.`CategoryID`=b.`CategoryID`";
			if($filter != "")
				$sql .= " WHERE ".$filter;
			return $this->data->executeScalar($sql);
		}
		//刪除
		public function deleteRecord($bookID){
			$sql="DELETE a,b FROM `book` a LEFT JOIN `photo` b ON a.`BookID`=b.`BookID` WHERE a.`BookID`='".$bookID."'";
			$this->data->executeNonQuery($sql);

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