<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class menuDAL extends dalBase{
		public $MenuID;
		public $ParentID;
		public $MenuName;
		public $MenuIcon;
		public $MenuLink;
		public $LinkTarget;
		public $Visible;
		public $OrderNO;
		//
		public $ParentName;

		public function loadRecord(){
			$sql = "SELECT a.*,b.`MenuName` AS `ParentName` FROM `menu` a LEFT JOIN `menu` b ON a.`ParentID`=b.`MenuID`";
			if($this->MenuID != ""){
				$sql .= " WHERE a.`MenuID`='".$this->MenuID."'";
			}else{
				$sql .= " WHERE a.`MenuName`='".$this->MenuName."'";
			}
			$reader = $this->data->executeReader($sql);

			$this->MenuID = $reader["MenuID"];
			$this->ParentID = $reader["ParentID"];
			$this->MenuName = $reader["MenuName"];
			$this->MenuIcon = $reader["MenuIcon"];
			$this->MenuLink = $reader["MenuLink"];
			$this->LinkTarget = $reader["LinkTarget"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];
			$this->ParentName = $reader["ParentName"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `menu` (`ParentID`,`MenuName`,`MenuIcon`,`MenuLink`,`LinkTarget`,`Visible`,`OrderNO`)" .
				" VALUES" .
				" (@ParentID,@MenuName,@MenuIcon,@MenuLink,@LinkTarget,@Visible,@OrderNO)";
			$sql = str_replace("@ParentID","'".$this->ParentID."'",$sql);
			$sql = str_replace("@MenuName","'".$this->MenuName."'",$sql);
			$sql = str_replace("@MenuIcon","'".$this->MenuIcon."'",$sql);
			$sql = str_replace("@MenuLink","'".$this->MenuLink."'",$sql);
			$sql = str_replace("@LinkTarget","'".$this->LinkTarget."'",$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 `menu` SET " .
				"`ParentID`=@ParentID" .
				",`MenuName`=@MenuName" .
				",`MenuIcon`=@MenuIcon" .
				",`MenuLink`=@MenuLink" .
				",`LinkTarget`=@LinkTarget" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				" WHERE `MenuID`=@MenuID";
			$sql = str_replace("@MenuID","'".$this->MenuID."'",$sql);
			$sql = str_replace("@ParentID","'".$this->ParentID."'",$sql);
			$sql = str_replace("@MenuName","'".$this->MenuName."'",$sql);
			$sql = str_replace("@MenuIcon","'".$this->MenuIcon."'",$sql);
			$sql = str_replace("@MenuLink","'".$this->MenuLink."'",$sql);
			$sql = str_replace("@LinkTarget","'".$this->LinkTarget."'",$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.*,b.`MenuName` AS `ParentName` FROM `menu` a LEFT JOIN `menu` b ON a.`ParentID`=b.`MenuID`";
			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 `menu` a LEFT JOIN `menu` b ON a.`ParentID`=b.`MenuID`";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			return $this->data->executeScalar($sql);
		}

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

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