<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class news_categoryDAL extends dalBase{
		public $CategoryID;
		public $CategoryName;
		public $Visible;
		public $OrderNO;
		public $WebTitle;
		public $WebKeyword;
		public $WebDescription;

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

			$this->CategoryID = $reader["CategoryID"];
			$this->CategoryName = $reader["CategoryName"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];
			$this->WebTitle = $reader["WebTitle"];
			$this->WebKeyword = $reader["WebKeyword"];
			$this->WebDescription = $reader["WebDescription"];

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

		public function insertRecord(){
			$sql = "INSERT INTO `news_category` (`CategoryName`, `Visible`, `OrderNO`, `WebTitle`, `WebKeyword`, `WebDescription`) " .
				" VALUES (@CategoryName,@Visible,@OrderNO,@WebTitle,@WebKeyword,@WebDescription)";
			$sql = str_replace("@CategoryName","'".$this->CategoryName."'",$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 `news_category` SET " .
				"`CategoryName`=@CategoryName" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				",`WebTitle`=@WebTitle" .
				",`WebKeyword`=@WebKeyword" .
				",`WebDescription`=@WebDescription" .
				" WHERE `CategoryID`=@CategoryID";
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@CategoryName","'".$this->CategoryName."'",$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.* FROM `news_category` 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 deleteRecord($categoryID){
			$sql = "DELETE FROM `news_category` WHERE `CategoryID`='".$categoryID."'";
			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return true;
			return false;
		}
	}
?>