<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class autokeywordDAL extends dalBase{
		public $KeywordID;
		public $KeywordTitle;
		public $Visible = 1;
		public $OrderNO = 0;
		public $Important = 0;

		public function loadRecord(){
			$sql = "SELECT a.* FROM `autokeyword` a WHERE a.`KeywordID`='".$this->KeywordID."'";

			$reader = $this->data->executeReader($sql);
			$this->KeywordID = $reader["KeywordID"];
			$this->KeywordTitle = $reader["KeywordTitle"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];
			$this->Important = $reader["Important"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `autokeyword` (`KeywordTitle`,`Visible`,`OrderNO`,`Important`) VALUES (@KeywordTitle,@Visible,@OrderNO,@Important)";
			$sql = str_replace("@KeywordTitle","'".$this->KeywordTitle."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@OrderNO","'".$this->OrderNO."'",$sql);
			$sql = str_replace("@Important","'".$this->Important."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return $this->data->getInsertID();
			return false;
		}
		public function updateRecord(){
			$sql = "UPDATE `autokeyword` SET " .
				"`KeywordTitle`=@KeywordTitle" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				",`Important`=@Important" .
				" WHERE `KeywordID`=@KeywordID";
			$sql = str_replace("@KeywordID","'".$this->KeywordID."'",$sql);
			$sql = str_replace("@KeywordTitle","'".$this->KeywordTitle."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@OrderNO","'".$this->OrderNO."'",$sql);
			$sql = str_replace("@Important","'".$this->Important."'",$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.* FROM `autokeyword` a";
			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 `autokeyword` a";
			if($filter != "")
				$sql .= " WHERE ".$filter;

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

		//刪除
		public function deleteRecord($keywordID){
			$sql = "DELETE a,b FROM `autokeyword` a LEFT JOIN `autokeywordlink` b ON a.`KeywordID`=b.`KeywordID` WHERE a.`KeywordID`='".$keywordID."'";
			$this->data->executeNonQuery($sql);

			if(!$this->data->error)
				return true;
			return false;
		}
		
		//變更重要關鍵字
		public function changeRecord($id=0){
			$sql = "UPDATE `autokeyword` SET `Important`='0'";
			$this->data->executeNonQuery($sql);
			
			if($id > 0){
				$sql = "UPDATE `autokeyword` SET `Important`='1' WHERE `KeywordID`='".$id."'";
				$this->data->executeNonQuery($sql);
			}
			
			if(!$this->data->error)
				return true;
			return false;
		}
	}
?>