<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class photoDAL extends dalBase{
		public $PhotoID;
		public $BookID;
		public $PhotoName;
		public $Introduction;
		public $PhotoType;
		public $Image;
		public $Movie;
		public $Visible;
		public $OrderNO;
		//外部欄位
		public $BookName;
		public $CategoryID;
		public $CategoryName;

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

			$this->PhotoID = $reader["PhotoID"];
			$this->BookID = $reader["BookID"];
			$this->PhotoName = $reader["PhotoName"];
			$this->Introduction = $reader["Introduction"];
			$this->PhotoType = $reader["PhotoType"];
			$this->Image = $reader["Image"];
			$this->Movie = $reader["Movie"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];
			$this->BookName = $reader["BookName"];
			$this->CategoryID = $reader["CategoryID"];
			$this->CategoryName = $reader["CategoryName"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `photo` (`BookID`, `PhotoName`, `Introduction`, `PhotoType`, `Image`, `Movie`, `Visible`, `OrderNO`)" .
					" VALUES (@BookID,@PhotoName,@Introduction,@PhotoType,@Image,@Movie,@Visible,@OrderNO)";
			$sql = str_replace("@BookID","'".$this->BookID."'",$sql);
			$sql = str_replace("@PhotoName","'".$this->PhotoName."'",$sql);
			$sql = str_replace("@Introduction","'".$this->Introduction."'",$sql);
			$sql = str_replace("@PhotoType","'".$this->PhotoType."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@Movie","'".$this->Movie."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@OrderNO","'".$this->OrderNO."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return $this->data->getInsertID();
			return false;
		}
		public function updateRecord(){
			$sql = "UPDATE `photo` SET " .
				"`BookID`=@BookID" .
				",`PhotoName`=@PhotoName" .
				",`Introduction`=@Introduction" .
				",`PhotoType`=@PhotoType" .
				",`Image`=@Image" .
				",`Movie`=@Movie" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				" WHERE `PhotoID`=@PhotoID";
			$sql = str_replace("@PhotoID","'".$this->PhotoID."'",$sql);
			$sql = str_replace("@BookID","'".$this->BookID."'",$sql);
			$sql = str_replace("@PhotoName","'".$this->PhotoName."'",$sql);
			$sql = str_replace("@Introduction","'".$this->Introduction."'",$sql);
			$sql = str_replace("@PhotoType","'".$this->PhotoType."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@Movie","'".$this->Movie."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@OrderNO","'".$this->OrderNO."'",$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.`BookName`,b.`CategoryID`,c.`CategoryName` FROM `photo` a LEFT JOIN `book` b ON a.`BookID`=b.`BookID` LEFT JOIN `book_category` c ON b.`CategoryID`=c.`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 `photo` a LEFT JOIN `book` b ON a.`BookID`=b.`BookID` LEFT JOIN `book_category` c ON b.`CategoryID`=c.`CategoryID`";
			if($filter != "")
				$sql .= " WHERE ".$filter;
			return $this->data->executeScalar($sql);
		}
		//刪除
		public function deleteRecord($photoID){
			$sql="DELETE FROM `photo` WHERE `PhotoID`='".$photoID."'";
			$this->data->executeNonQuery($sql);

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