<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class templateDAL extends dalBase{
		public $TemplateID;
		public $CategoryID;
		public $TemplateName;
		public $PathName;
		public $Image;
		public $File;
		public $Website;
		public $Description;
		public $Visible;
		public $OrderNO;
		//外部欄位
		public $CategoryName;

		public function loadRecord(){
			$sql = "SELECT a.*,b.`CategoryName` FROM `template` a LEFT JOIN `template_category` b ON a.`CategoryID`=b.`CategoryID` WHERE a.`TemplateID`='".$this->TemplateID."'";
			$reader = $this->data->executeReader($sql);

			$this->TemplateID = $reader["TemplateID"];
			$this->CategoryID = $reader["CategoryID"];
			$this->TemplateName = $reader["TemplateName"];
			$this->PathName = $reader["PathName"];
			$this->Image = $reader["Image"];
			$this->File = $reader["File"];
			$this->Website = $reader["Website"];
			$this->Description = $reader["Description"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];
			$this->CategoryName = $reader["CategoryName"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `template` (`CategoryID`, `TemplateName`, `PathName`, `Image`, `File`, `Website`, `Description`, `Visible`, `OrderNO`)" .
				" VALUES (@CategoryID,@TemplateName,@PathName,@Image,@File,@Website,@Description,@Visible,@OrderNO)";
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@TemplateName","'".$this->TemplateName."'",$sql);
			$sql = str_replace("@PathName","'".$this->PathName."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@File","'".$this->File."'",$sql);
			$sql = str_replace("@Website","'".$this->Website."'",$sql);
			$sql = str_replace("@Description","'".$this->Description."'",$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` SET " .
				"`CategoryID`=@CategoryID" .
				",`TemplateName`=@TemplateName" .
				",`PathName`=@PathName" .
				",`Image`=@Image" .
				",`File`=@File" .
				",`Website`=@Website" .
				",`Description`=@Description" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				" WHERE `TemplateID`=@TemplateID";
			$sql = str_replace("@TemplateID","'".$this->TemplateID."'",$sql);
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@TemplateName","'".$this->TemplateName."'",$sql);
			$sql = str_replace("@PathName","'".$this->PathName."'",$sql);
			$sql = str_replace("@Image","'".$this->Image."'",$sql);
			$sql = str_replace("@File","'".$this->File."'",$sql);
			$sql = str_replace("@Website","'".$this->Website."'",$sql);
			$sql = str_replace("@Description","'".$this->Description."'",$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.*,b.`CategoryName` FROM `template` a LEFT JOIN `template_category` b ON a.`CategoryID`=b.`CategoryID`";
			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 `template` a LEFT JOIN `template_category` b ON a.`CategoryID`=b.`CategoryID`";
			if($filter != "")
				$sql .= " WHERE ".$filter;

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

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

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

		//檢查路徑是否已存在
		public function checkPathNameExists($pathName){
			$sql = "SELECT COUNT(*) FROM `template` WHERE `PathName`='".$pathName."'";
			$num = $this->data->executeScalar($sql);

			if($num > 0)
				return true;
			return false;
		}
	}
?>