<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class pictureDAL extends dalBase{
		public $PictureID;
		public $PictureType;
		public $TypeOrderNO;
		public $Image;
		public $Title;
		public $Link;
		public $BanColor;
		public $DanDesc;

		public function loadRecord(){
			$sql = "SELECT a.* FROM `picture` a WHERE ";
			if($this->PictureType != "" && $this->PictureType != "0" && $this->TypeOrderNO != ""){
				$sql .= "a.`PictureType`='".$this->PictureType."' AND a.`TypeOrderNO`='".$this->TypeOrderNO."'";
			}else if($this->PictureID != "" && $this->PictureID != "0"){
				$sql .= "a.`PictureID`='".$this->PictureID."'";
			}else{
				$sql .= "1=2";
			}
			$reader = $this->data->executeReader($sql);

			$this->PictureID = $reader["PictureID"];
			$this->PictureType = $reader["PictureType"];
			$this->TypeOrderNO = $reader["TypeOrderNO"];
			$this->Image = $reader["Image"];
			$this->Title = $reader["Title"];
			$this->Link = $reader["Link"];
			$this->BanColor = $reader["BanColor"];
			$this->DanDesc = $reader["DanDesc"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `picture` (`PictureType`, `TypeOrderNO`, `Image`, `Title`, `Link`, `BanColor`, `DanDesc`)" .
				" VALUES (@PictureType,@TypeOrderNO,@Image,@Title,@Link,@BanColor,@DanDesc)";
			$sql = str_replace("@PictureType","'".$this->PictureType."'",$sql);
			$sql = str_replace("@TypeOrderNO","'".$this->TypeOrderNO."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@Title","'".$this->Title."'",$sql);
			$sql = str_replace("@Link","'".$this->Link."'",$sql);
			$sql = str_replace("@BanColor","'".$this->BanColor."'",$sql);
			$sql = str_replace("@DanDesc","'".$this->DanDesc."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return $this->data->getInsertID();
			return false;
		}
		public function updateRecord(){
			$sql = "UPDATE `picture` SET " .
				"`Image`=@Image" .
				",`Title`=@Title" .
				",`Link`=@Link" .
				",`BanColor`=@BanColor" .
				",`DanDesc`=@DanDesc" .
				" WHERE `PictureType`=@PictureType AND `TypeOrderNO`=@TypeOrderNO";
			$sql = str_replace("@PictureType","'".$this->PictureType."'",$sql);
			$sql = str_replace("@TypeOrderNO","'".$this->TypeOrderNO."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@Title","'".$this->Title."'",$sql);
			$sql = str_replace("@Link","'".$this->Link."'",$sql);
			$sql = str_replace("@BanColor","'".$this->BanColor."'",$sql);
			$sql = str_replace("@DanDesc","'".$this->DanDesc."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return true;
			return false;
		}
		public function replaceRecord(){
			$sql = "REPLACE INTO `picture` (`PictureType`, `TypeOrderNO`, `Image`, `Title`, `Link`, `BanColor`, `DanDesc`)" .
				" VALUES (@PictureType,@TypeOrderNO,@Image,@Title,@Link,@BanColor,@DanDesc)";
			$sql = str_replace("@PictureType","'".$this->PictureType."'",$sql);
			$sql = str_replace("@TypeOrderNO","'".$this->TypeOrderNO."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@Title","'".$this->Title."'",$sql);
			$sql = str_replace("@Link","'".$this->Link."'",$sql);
			$sql = str_replace("@BanColor","'".$this->BanColor."'",$sql);
			$sql = str_replace("@DanDesc","'".$this->DanDesc."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return true;
			return false;
		}
		//查詢
		public function executeRecordset($filter="",$orderExp="", $start=0,$length=-1){
			$sql = "SELECT a.* FROM `picture` a";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;

			if(trim($orderExp) != "")
				$sql .= " ORDER BY ".$orderExp;
			else
				$sql .= " ORDER BY a.`TypeOrderNO`";

			return $this->data->executeRecordset($sql,$start,$length);
		}

		public function countRecordset($filter=""){
			$sql = "SELECT COUNT(*) FROM `picture` a";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			return $this->data->executeScalar($sql);
		}

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

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