<?php
require_once (PATH_MODELS . "admin/OrganizationClass.php") ;
require_once (PATH_MODELS . "admin/OrganizationOptions.php") ;
abstract class MasterBase {
	protected $db ;
	protected $tbl = "" ;
	protected $fldid = "";
	protected $flddesc = "" ;
	protected $fldorg = "" ;
	protected $orgId = -1;
	
	function __construct() {
	}
	function __destruct() {
	}

	function getRecordApi() {
		$sql = "select ".$this->fldid.",".$this->flddesc." from " . $this->tbl;
		$rows = $this->db->getTable($sql) ;
		if (is_null($rows) || count($rows) == 0)
			return null ;
		else 
			return $rows ; 
	}
	
	function addRecord($datas) {
		$id = 0 ;
		$sql = "insert into " . $this->tbl  ;
		$fld = "" ;
		$val = "" ;
		$fs = "" ;
		$params = array() ;
		if (is_array($datas) && count($datas) > 0) {
			foreach ($datas as $data) {
				$fld .= $fs . $data['field'] ;
				$val .= $fs . $this->db->formatValueParam($data['field']) ;
				$params[] = $this->db->valueParam($data['field'],$data['value']) ;
				$fs = ", " ;
			}
			
			$sql .= " (" . $fld . ") values (" . $val . ")";
			
			$id = $this->db->insertRowGetId($sql,$params) ;
		}
		
		return $id ;
	}
	function updateRecord($id,$datas) {
		$sql = "update " . $this->tbl . " set " ;
		$fs = "" ;
		$params = array() ;
		if (is_array($datas) && count($datas) > 0) {
			foreach ($datas as $data) {
				$sql .= $fs . $this->db->fieldParam($data['field']) ;
				$fs = ", " ;
				$params[] = $this->db->valueParam($data['field'],$data['value']) ;
			}
			$sql .= " where " . $this->db->fieldParam($this->fldid) ;
			
			$params[] = $this->db->valueParam($this->fldid,$id) ;
			return $this->db->updateRow($sql,$params) ;
		} else  {
			return false ;
		}
	}
	function deleteRecord($id) {
		$sql = "delete from " . $this->tbl 
			. " where " . $this->db->fieldParam($this->fldid) ;

		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$id) ;
		return $this->db->deleteRows($sql,$params) ;
	}
	function getRecord($id) {
		$sql = "select * from " . $this->tbl
			. " where " . $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$id) ;
		$rows = $this->db->getRow($sql,$params) ;
		if (is_null($rows) || count($rows) == 0)
			return null ;
		else 
			return $rows[0] ;
	}
	function getValueList($orgid) {
		$sql = "select " . $this->fldid . " as code, " . $this->flddesc . " as [desc] "
			. " from " . $this->tbl 
			. " where " . $this->db->fieldParam($this->fldorg) 
			. " order by " . $this->flddesc ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldorg,$orgid) ;
		return $this->db->getTable($sql,$params) ;
	}
	function isFound($id) {
		$filter = $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$id) ;
		if ($this->db->getRowsCount($this->tbl,$filter,$params) > 0 )
			return true ;
		else 
			return false ;
	}
	function initTable($condition) {
		return $this->db->initTable($this->tbl,$condition) ;
	}
	function getTable($filter="",$orderby="",$params=null) {
		$sql = "select * from " . $this->tbl ;
		if (!empty($filter))
			$sql .= " where " . $filter ;
		if (!empty($orderby))
			$sql .= " order by " . $orderby ;

		return $this->db->getTable($sql,$params) ;
	}
	function getDescription($id) {
		$sql = "select " . $this->flddesc . " from " . $this->tbl
			. " where " . $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$id) ;
		$rows = $this->db->getRow($sql,$params) ;
		if (is_null($rows) || count($rows) == 0)
			return "" ;
		else 
			return $rows[0][$this->flddesc] ;
	}
    function isUsed($id){
        return "";
    }
    function isAllowDeleteMaster(){
		$cls = new OrganizationClass($this->db) ;
		if($this->orgId == -1)
			$row = $cls->getRecord($_SESSION[SE_ORGID]) ;
		else
			$row = $cls->getRecord($this->orgId) ;
		if (is_null($row))
			return false;
        
		$opt = new OrganizationOptions() ;
		$opt->loadXml($row[OrganizationTable::C_OPTIONS]) ;
		$op = $opt->getOption() ;
        
        return $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_ALLOW_DELETE_MASTER] == '1';
    }
    function prepareStringValues($str){
        return str_replace("'", "''", $str);
    }

    function getMaxFieldByJobcentreId($jobCentreId, $field) {
		$sql = "select MAX(".$field.") from " . $this->tbl
		. " where " . $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$jobCentreId) ;
		
		$rows = $this->db->getRow($sql,$params) ;
		
		if (is_null($rows) || count($rows) == 0)
			return null ;
		else
			return $rows[0][0] ;
	}

	function getMaxFieldByJobCentreResourceId($jobCentreResourceId, $field) {
		$sql = "select MAX(".$field.") from " . $this->tbl
		. " where " . $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$jobCentreResourceId) ;
		
		$rows = $this->db->getRow($sql,$params) ;
		
		if (is_null($rows) || count($rows) == 0)
			return null ;
		else
			return $rows[0][0] ;
	}

	function getMaxFieldByJobAssignId($jobAssignId, $field) {
		$sql = "select MAX(".$field.") from " . $this->tbl
		. " where " . $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$jobAssignId) ;
		
		$rows = $this->db->getRow($sql,$params) ;
		if (is_null($rows) || count($rows) == 0)
			return null ;
		else
			return $rows[0][0] ;
	}

	function getMaxFieldBySalesQuotationAppendixId($salesQuotationId, $field) {
		$sql = "select MAX(".$field.") from " . $this->tbl
		. " where " . $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$salesQuotationId) ;
		
		$rows = $this->db->getRow($sql,$params) ;
		
		if (is_null($rows) || count($rows) == 0)
			return null ;
		else
			return $rows[0][0] ;
	}

	function getMaxFieldBySalesQuotationProjectPlanId($salesQuotationId, $field) {
		$sql = "select MAX(".$field.") from " . $this->tbl
		. " where " . $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$salesQuotationId) ;
		
		$rows = $this->db->getRow($sql,$params) ;
		
		if (is_null($rows) || count($rows) == 0)
			return null ;
		else
			return $rows[0][0] ;
	}

	function getMaxFieldBySalesQuotationMaintenanceId($salesQuotationId, $field) {
		$sql = "select MAX(".$field.") from " . $this->tbl
		. " where " . $this->db->fieldParam($this->fldid) ;
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid,$salesQuotationId) ;
		
		$rows = $this->db->getRow($sql,$params) ;
		
		if (is_null($rows) || count($rows) == 0)
			return null ;
		else
			return $rows[0][0] ;
	}
} 