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

		public function loadRecord(){
			$sql = "SELECT a.* FROM `template_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"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `template_category` (`CategoryName`, `Visible`, `OrderNO`)" .
				" VALUES (@CategoryName,@Visible,@OrderNO)";
			$sql = str_replace("@CategoryName","'".$this->CategoryName."'",$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 `template_category` SET " .
				" `CategoryName`=@CategoryName" .
				",Visible=@Visible" .
				",OrderNO=@OrderNO" .
				" 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);

			$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 `template_category` a";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			if(trim($orderExp) != "")
				$sql .= " ORDER BY ".$orderExp;
			else
				$sql .= " ORDER BY a.`OrderNO`";

			return $this->data->executeRecordset($sql,$start,$length);
		}
		//查詢計數
		public function countRecordset($filter){
			$sql = "SELECT COUNT(*) FROM `template_category` a";
			if($filter != "")
				$sql .= " WHERE ".$filter;

			return $this->data->executeScalar($sql);
		}
		//刪除
		public function deleteRecord($categoryID){
			$sql = "DELETE FROM `template_category` WHERE `CategoryID`='".$CategoryID."'";
			$this->data->executeNonQuery($sql);

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