<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class webmenuDAL extends dalBase{
		public $MenuID;
		public $MenuType;
		public $MenuTitle;
		public $SubTitle;
		public $MenuLink;
		public $SubMenu;
		public $Visible;
		public $OrderNO;

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

			$this->MenuID = $reader["MenuID"];
			$this->MenuType = $reader["MenuType"];
			$this->MenuTitle = $reader["MenuTitle"];
			$this->SubTitle = $reader["SubTitle"];
			$this->MenuLink = $reader["MenuLink"];
			$this->SubMenu = $reader["SubMenu"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `webmenu` (`MenuType`, `MenuTitle`, `SubTitle`, `MenuLink`, `SubMenu`, `Visible`, `OrderNO`)" .
				" VALUES (@MenuType,@MenuTitle,@SubTitle,@MenuLink,@SubMenu,@Visible,@OrderNO)";
			$sql = str_replace("@MenuType","'".$this->MenuType."'",$sql);
			$sql = str_replace("@MenuTitle","'".$this->MenuTitle."'",$sql);
			$sql = str_replace("@SubTitle","'".$this->SubTitle."'",$sql);
			$sql = str_replace("@MenuLink","'".$this->MenuLink."'",$sql);
			$sql = str_replace("@SubMenu","'".$this->SubMenu."'",$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 `webmenu` SET " .
				"`MenuType`=@MenuType" .
				",`MenuTitle`=@MenuTitle" .
				",`SubTitle`=@SubTitle" .
				",`MenuLink`=@MenuLink" .
				",`SubMenu`=@SubMenu" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				" WHERE `MenuID`=@MenuID";
			$sql = str_replace("@MenuID","'".$this->MenuID."'",$sql);
			$sql = str_replace("@MenuType","'".$this->MenuType."'",$sql);
			$sql = str_replace("@MenuTitle","'".$this->MenuTitle."'",$sql);
			$sql = str_replace("@SubTitle","'".$this->SubTitle."'",$sql);
			$sql = str_replace("@MenuLink","'".$this->MenuLink."'",$sql);
			$sql = str_replace("@SubMenu","'".$this->SubMenu."'",$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 replaceRecord(){
			$sql = "REPLACE INTO `webmenu` (`MenuID`, `MenuType`, `MenuTitle`, `SubTitle`, `MenuLink`, `SubMenu`, `Visible`, `OrderNO`)" .
				" VALUES (@MenuID,@MenuType,@MenuTitle,@SubTitle,@MenuLink,@SubMenu,@Visible,@OrderNO)";
			$sql = str_replace("@MenuID","'".$this->MenuID."'",$sql);
			$sql = str_replace("@MenuType","'".$this->MenuType."'",$sql);
			$sql = str_replace("@MenuTitle","'".$this->MenuTitle."'",$sql);
			$sql = str_replace("@SubTitle","'".$this->SubTitle."'",$sql);
			$sql = str_replace("@MenuLink","'".$this->MenuLink."'",$sql);
			$sql = str_replace("@SubMenu","'".$this->SubMenu."'",$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="",$order="",$start=0,$length=-1){
			$sql = "SELECT a.* FROM `webmenu` 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 `webmenu` a";
			if($filter != "")
				$sql .= " WHERE ".$filter;
			return $this->data->executeScalar($sql);
		}

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

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