<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class htaccessDAL extends dalBase {
		public $ID;
		public $ActionName;
		public $HtmlName;
		public $Visible;
		public $OrderNO;

		public function loadRecord(): bool {
			$intoAry = [
				parent::addArray(":id", $this->ID, "int")
			];
			$sql = "SELECT a.* FROM `htaccess` a WHERE a.`ID`=:id";
			$reader = $this->data->executeReader($sql, $intoAry);
			
			if (!empty($reader)) {
				$this->ID = $reader["ID"];
				$this->ActionName = $reader["ActionName"];
				$this->HtmlName = $reader["HtmlName"];
				$this->Visible = $reader["Visible"];
				$this->OrderNO = $reader["OrderNO"];
				return true;
			} else {
				return false;
			}
		}
		public function insertRecord(): bool|int {
			$intoAry = [
				parent::addArray(":actionName", $this->ActionName, "str", 128),
				parent::addArray(":htmlName", $this->HtmlName, "str", 128),
				parent::addArray(":visible", $this->Visible, "int"),
				parent::addArray(":orderNO", $this->OrderNO, "int")
			];
			$sql = "INSERT INTO `htaccess` (`ActionName`, `HtmlName`, `Visible`, `OrderNO`) VALUES (:actionName, :htmlName, :visible, :orderNO)";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? $this->data->getInsertID() : false;
		}
		public function updateRecord(): bool {
			$intoAry = [
				parent::addArray(":id", $this->ID, "int"),
				parent::addArray(":htmlName", $this->HtmlName, "str", 128),
				parent::addArray(":visible", $this->Visible, "int"),
				parent::addArray(":orderNO", $this->OrderNO, "int")
			];
			$sql = "UPDATE `htaccess` SET `HtmlName`=:htmlName, `Visible`=:visible, `OrderNO`=:orderNO WHERE `ID`=:id";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
		//查詢
		public function executeRecordset(array $intoAry = [], string $filter = "", string $order = "", int $start = 0, int $length = -1): array {
			$sql = "SELECT a.* FROM `htaccess` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			$sql .= (!isset($order) || $order === "") ? " ORDER BY a.`OrderNO`" : " ORDER BY $order";
			$length = is_int($length) ? $length : -1;
			return $this->data->executeRecordset($sql, $intoAry, $start, $length);
		}
		//查詢計數
		public function countRecordset(array $intoAry = [], string $filter = ""): int {
			$sql = "SELECT COUNT(*) FROM `htaccess` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			return $this->data->executeScalar($sql, $intoAry);
		}
	}
?>