<?php
	require_once "dalBase.php";
	class contactDAL extends dalBase{
		public $ContactID;
		public $Name;
		public $Phone;
		public $Email;
		public $ProductID;
		public $ProductName;
		public $Mobile;
		public $Company;
		public $Address;
		public $Url;
		public $Title;
		public $Content;
		public $ReMail;
		public $ReTitle;
		public $ReContent;
		public $IsView;
		public $IsReply;
		public $CreatedTime;
		public $CreatedIP;
		public $ModifiedTime;
		public $ModifiedUserID;

		public function loadRecord(){
			$sql = "SELECT a.* FROM `contact` a WHERE a.`ContactID`='".$this->ContactID."'";
			$reader = $this->data->executeReader($sql);

			$this->ContactID = $reader["ContactID"];
			$this->Name = $reader["Name"];
			$this->Phone = $reader["Phone"];
			$this->Email = $reader["Email"];
			$this->ProductID = $reader["ProductID"];
			$this->ProductName = $reader["ProductName"];
			$this->Mobile = $reader["Mobile"];
			$this->Company = $reader["Company"];
			$this->Address = $reader["Address"];
			$this->Url = $reader["Url"];
			$this->Title = $reader["Title"];
			$this->Content = $reader["Content"];
			$this->ReMail = $reader["ReMail"];
			$this->ReTitle = $reader["ReTitle"];
			$this->ReContent = $reader["ReContent"];
			$this->IsView = $reader["IsView"];
			$this->IsReply = $reader["IsReply"];
			$this->CreatedTime = $reader["CreatedTime"];
			$this->CreatedIP = $reader["CreatedIP"];
			$this->ModifiedTime = $reader["ModifiedTime"];
			$this->ModifiedUserID = $reader["ModifiedUserID"];

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

		public function insertRecord(){
			$sql = "INSERT INTO `contact` (`Name`, `Phone`, `Email`, `ProductID`, `ProductName`, `Mobile`, `Company`, `Address`, `Url`, `Title`, `Content`, `ReMail`, `ReTitle`, `ReContent`, `IsView`, `IsReply`, `CreatedTime`, `CreatedIP`, `ModifiedTime`, `ModifiedUserID`)" .
				" VALUES (@Name,@Phone,@Email,@ProductID,@ProductName,@Mobile,@Company,@Address,@Url,@Title,@Content,@ReMail,@ReTitle,@ReContent,@IsView,@IsReply,@CreatedTime,@CreatedIP,@ModifiedTime,@ModifiedUserID)";
			$sql = str_replace("@Name","'".$this->Name."'",$sql);
			$sql = str_replace("@Phone","'".$this->Phone."'",$sql);
			$sql = str_replace("@Email","'".$this->Email."'",$sql);
			$sql = str_replace("@ProductID","'".$this->ProductID."'",$sql);
			$sql = str_replace("@ProductName","'".$this->ProductName."'",$sql);
			$sql = str_replace("@Mobile","'".$this->Mobile."'",$sql);
			$sql = str_replace("@Company","'".$this->Company."'",$sql);
			$sql = str_replace("@Address","'".$this->Address."'",$sql);
			$sql = str_replace("@Url","'".$this->Url."'",$sql);
			$sql = str_replace("@Title","'".$this->Title."'",$sql);
			$sql = str_replace("@Content","'".$this->Content."'",$sql);
			$sql = str_replace("@ReMail","'".$this->ReMail."'",$sql);
			$sql = str_replace("@ReTitle","'".$this->ReTitle."'",$sql);
			$sql = str_replace("@ReContent","'".$this->ReContent."'",$sql);
			$sql = str_replace("@IsView","'".$this->IsView."'",$sql);
			$sql = str_replace("@IsReply","'".$this->IsReply."'",$sql);
			$sql = str_replace("@CreatedTime","'".date("Y-m-d H:i:s")."'",$sql);
			$sql = str_replace("@CreatedIP","'".$this->CreatedIP."'",$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 updateRecord(){
			$sql = "UPDATE `contact` SET " .
				"`ReMail`=@ReMail" .
				",`ReTitle`=@ReTitle" .
				",`ReContent`=@ReContent" .
				",`IsView`=@IsView" .
				",`IsReply`=@IsReply" .
				",`ModifiedTime`=@ModifiedTime" .
				",`ModifiedUserID`=@ModifiedUserID" .
				" WHERE `ContactID`=@ContactID";
			$sql = str_replace("@ContactID","'".$this->ContactID."'",$sql);
			$sql = str_replace("@ReMail","'".$this->ReMail."'",$sql);
			$sql = str_replace("@ReTitle","'".$this->ReTitle."'",$sql);
			$sql = str_replace("@ReContent","'".$this->ReContent."'",$sql);
			$sql = str_replace("@IsView","'".$this->IsView."'",$sql);
			$sql = str_replace("@IsReply","'".$this->IsReply."'",$sql);
			$sql = str_replace("@ModifiedTime","'".$this->ModifiedTime."'",$sql);
			$sql = str_replace("@ModifiedUserID","'".$this->ModifiedUserID."'",$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 `contact` a";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			if(trim($orderExp) != "")
				$sql .= " ORDER BY ".$orderExp;
			else
				$sql .= " ORDER BY a.CreatedTime DESC";

			return $this->data->executeRecordset($sql,$start,$length);
		}
		public function countRecordset($filter=""){
			$sql = "SELECT COUNT(*) FROM `contact` a";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			return $this->data->executeScalar($sql);
		}
		public function deleteRecord($contactID){
			$sql = "DELETE FROM `contact` WHERE `ContactID`='".$contactID."'";
			$this->data->executeNonQuery($sql);

			if(!$this->data->error)
				return true;
			return false;
		}
		public function batDelete($contactIDs){
			$sql = "DELETE FROM `contact` WHERE `ContactID` IN (".$contactIDs.")";
			$this->data->executeNonQuery($sql);

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