<?php

class AppUnitkerja extends Database {

	protected $mSqlFile= 'module/unitkerja/business/appunitkerja.sql.php';
	
	function __construct($connectionNumber=0) {
		parent::__construct($connectionNumber);
	}


	function GetUnitKerja($offset, $limit,$kode,$nama,$type){
		$strplus ='';
		if($type!='')
			$strplus = " AND unitkerjaTipeunitId = '%s'";
		
		$newSql = sprintf($this->mSqlQueries['get_unit_kerja'],'%s','%s',$strplus,'%s','%s',$strplus,'%s','%s');
		//echo $newSql;
		if($type!='')
			$ret = $this->Open($newSql,array('%'.$kode.'%','%'.$nama.'%',$type,'%'.$kode.'%','%'.$nama.'%',$type,$offset, $limit));
		else
			$ret = $this->Open($newSql,array('%'.$kode.'%','%'.$nama.'%','%'.$kode.'%','%'.$nama.'%',$offset, $limit));
      //print_r($ret);
      return $ret;
	}
	//old function

  function GetQueryKeren($sql,$params) {
		foreach ($params as $k => $v) {
			if (is_array($v)) {
				$params[$k] = '~~' . join("~~,~~", $v) . '~~';
				$params[$k] = str_replace('~~', '\'', addslashes($params[$k]));
			} else {
				$params[$k] = addslashes($params[$k]);
			}
		}
		$param_serialized = '~~' . join("~~,~~", $params) . '~~';
		$param_serialized = str_replace('~~', '\'', addslashes($param_serialized));
		eval('$sql_parsed = sprintf("' . $sql . '", ' . $param_serialized . ');');
		//echo $sql_parsed;
		return $sql_parsed;
	}
		
	function GetDataUnitkerja ($offset, $limit, $unitkerja='', $kode='', $tipeunit='') {
		if($tipeunit != "") 
			$str_tipeunit = " AND unitkerjaTipeUnitId = " . $tipeunit;
		else 
			$str_tipeunit = "";

		$sql = $this->GetQueryKeren($this->mSqlQueries['get_data_unitkerja'], array('%'.$kode.'%', '%'.$kode.'%', '%'.$unitkerja.'%', '%'.$unitkerja.'%', $str_tipeunit, $offset, $limit));
		//echo "<pre>" . $sql . "</pre>";
		return $this->Open($sql, array());
	}
/*
	function GetDataUnitkerja ($offset, $limit, $satker='', $kode='', $unitkerja='', $tipeunit='') {
		if($satker != "") $str_satker = " AND ukr.unitkerjaParentId = $satker ";
		else $str_satker = "";
		if($tipeunit != "") $str_tipeunit = " AND tukr.tipeunitId = $tipeunit ";
		else $str_tipeunit = "";

		//$sql = sprintf($this->mSqlQueries['get_data_unitkerja'], '%s', '%s', '%s', '%s', '%d', '%d');
		$result = $this->GetQueryKeren($this->mSqlQueries['get_data_unitkerja'], array('%'.$kode.'%', '%'.$unitkerja.'%', $str_satker, $str_tipeunit, $offset, $limit));
		//echo $result;

	  
	  //$debug = sprintf($sql, '%'.$kode.'%', '%'.$unitkerja.'%', $str_satker, $str_tipeunit, $offset, $limit);
	 // echo $debug;
	  //print_r($result);

		return $this->Open($result, array());
	}
*/
	function GetCountDataUnitkerja ($unitkerja='', $kode='', $tipeunit='') {
		if($tipeunit != "") 
			$str_tipeunit = " AND unitkerjaTipeUnitId = " . $tipeunit;
		else 
			$str_tipeunit = "";
		/*
      if($satker != "") $str_satker = " AND ukr.unitkerjaParentId = $satker ";
		else $str_satker = "";
		if($tipeunit != "") $str_tipeunit = " AND tukr.tipeunitId = $tipeunit ";
		else $str_tipeunit = "";
      */
		$sql = $this->GetQueryKeren($this->mSqlQueries['get_count_data_unitkerja'], array('%'.$kode.'%', '%'.$kode.'%', '%'.$unitkerja.'%', '%'.$unitkerja.'%', $str_tipeunit));
		$result = $this->Open($sql, array());
	 // $debug = sprintf($this->mSqlQueries['get_count_data_unitkerja'], '%'.$kode.'%', '%'.$unitkerja.'%', $str_satker, $str_tipeunit);
	 // echo $debug;
	  //print_r($result);

		if (!$result) {
			return 0;
		} else {
			return $result[0]['total'];
		}
	}
	 
	function GetDataUnitkerjaById($unitkerjaId) {
		$result = $this->Open($this->mSqlQueries['get_data_unitkerja_by_id'], array($unitkerjaId));
	  //$debug = sprintf($this->mSqlQueries['get_data_unitkerja_by_id'], $unitkerjaId);
	  //echo $debug;
		return $result;
	}

	function GetDataUnitkerjaByArrayId($arrUnitkerjaId) {
		$unitkerjaId = implode("', '", $arrUnitkerjaId);
		$result = $this->Open($this->mSqlQueries['get_data_unitkerja_by_array_id'], array($unitkerjaId));
	  //$debug = sprintf($this->mSqlQueries['get_data_unitkerja_by_id'], $unitkerjaId);
	  //echo $debug;
		return $result;
	}

	//untuk combo box
	function GetDataSatker($unitkerjaId = NULL) {
		$result = $this->Open($this->mSqlQueries['get_data_satker'], array());
		return $result;
	}

	function GetDataTipeUnit($unitkerjaId = NULL) {
		$result = $this->Open($this->mSqlQueries['get_data_tipe_unit'], array());
		return $result;
	}
	
	function GetStatusUnitKerja(){
		return $this->Open($this->mSqlQueries['get_status_unit_kerja'],array());
	}

//===DO==
	
	function DoAddUnitkerja($pimpinan, $unitkerjaKode, $unitkerjaNama, $tipeunit, $statusunit, $satker) {
      if($satker == "-") $satker = "0";
		$result = $this->Execute($this->mSqlQueries['do_add_unitkerja'], array($unitkerjaKode, $unitkerjaNama, $tipeunit, $statusunit, $satker, $pimpinan));
	 //$debug = sprintf($this->mSqlQueries['do_add_unitkerja'], $unitkerjaKode, $unitkerjaNama, $tipeunit, $satker, $pimpinan);
	  //echo $debug;
		return $result;
	}
	
	function DoUpdateUnitkerja($pimpinan, $unitkerjaKode, $unitkerjaNama, $tipeunit, $statusunit, $satker, $unitkerjaId) {
      if($satker == "-") $satker = "0";
		$result = $this->Execute($this->mSqlQueries['do_update_unitkerja'], array($unitkerjaKode, $unitkerjaNama, $tipeunit, $statusunit, $satker, $pimpinan, $unitkerjaId));
	  //$debug = sprintf($this->mSqlQueries['do_update_unitkerja'], $unitkerjaKode, $unitkerjaNama, $tipeunit, $statusunit, $satker, $pimpinan, $unitkerjaId);
	  //echo $debug;
		return $result;
	}
	
	function DoDeleteUnitkerjaById($unitkerjaId) {
		$result=$this->Execute($this->mSqlQueries['do_delete_unitkerja_by_id'], array($unitkerjaId,$unitkerjaId));
		return $result;
	}
	function DoDeleteUnitkerjaByArrayId($arrUnitkerjaId) {
		$unitkerjaId = implode("', '", $arrUnitkerjaId);
		$result=$this->Execute($this->mSqlQueries['do_delete_unitkerja_by_array_id'], array($unitkerjaId,$unitkerjaId));
		return $result;
	}

   function GetComboUnitKerja(){
      return $this->Open($this->mSqlQueries['get_combo_unit_kerja'],array());
   }
}
?>
