<?php

class AppGolonganbidang extends Database {

	protected $mSqlFile= 'module/golongan_bidang/business/appgolonganbidang.sql.php';
	
	function __construct($connectionNumber=0) {
		parent::__construct($connectionNumber);
	}
		
	function GetData($offset, $limit, $gol='', $nama='') {
      if($gol == 'all' || $gol == "") {
         $str_gol = 0;
			$sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_data']);
      } else {
         $str_gol = $gol;
			$sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_data']);
      }
      
      $result = $this->Open($sql, array($str_gol, '%'.$nama.'%', '%'.$nama.'%', $offset, $limit));
		return $result;
	}

	function GetCountData($gol='', $nama='') {
      if($gol == 'all' || $gol == "") {
         $str_gol = 0;
			$sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_count_data']);
      } else {
         $str_gol = $gol;
			$sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_count_data']);
      }
      
      $result = $this->Open($sql, array($str_gol, '%'.$nama.'%', '%'.$nama.'%', $offset, $limit));
      if (!$result) {
			return 0;
		} else {
			return $result[0]['total'];
		}
	}

	function GetBidangById($bidangId) {
		$result = $this->Open($this->mSqlQueries['get_bidang_by_id'], array($bidangId));
		return $result;
	}

	function GetGolonganById($golonganId) {
		$result = $this->Open($this->mSqlQueries['get_golongan_by_id'], array($golonganId));
      return $result;
	}

	function GetComboGolongan() {
		$result = $this->Open($this->mSqlQueries['get_combo_golongan'], array());
		return $result;
	}
	
	function GetGolonganList(){
      $result = $this->Open($this->mSqlQueries['get_golongan_list'], array());
      return $result;
	}
   
   function GetKodeGolonganLast()
   {
      $result = $this->Open($this->mSqlQueries['get_kode_golongan_last'], array());
      return $result[0]['kode'];
   }
   
   // ngecek apakah kode golongan sudah digunakan, kalau sudah, return true
   function CekKodeGolongan ($id, $kode)
   {
      $result = $this->Open($this->mSqlQueries['cek_kode_golongan'], array((int)$kode));
      if (empty($result) or $result[0]['id'] == $id) return false;
      else return true;
   }
   
   function GetKodeBidangLast ()
   {
      $result = $this->Open($this->mSqlQueries['get_kode_bidang_last'], array());
      foreach ($result as $val) $return[$val['id']] = $val['kode'];
      return $return;
   }
   
   // ngecek apakah kode bidang sudah digunakan, kalau sudah, return true
   function CekKodeBidang ($id, $kode, $golongan)
   {
      $result = $this->Open($this->mSqlQueries['cek_kode_bidang'], array((int)$kode, (int)$golongan));
      if (empty($result) or $result[0]['id'] == $id) return false;
      else return true;
   }
//===DO==
	
	function DoAddGolongan($kode, $nama) {
		$result = $this->Execute($this->mSqlQueries['do_add_golongan'], array($kode, $nama));
		return $result;
	}
	
	function DoUpdateGolongan($id, $kode, $nama) {
		$result = $this->Execute($this->mSqlQueries['do_update_golongan'], array($kode, $nama, $id));
		return $result;
	}
	
	function DoAddBidang($kodegol, $kode, $nama) {
		$result = $this->Execute($this->mSqlQueries['do_add_bidang'], array($kodegol, $kode, $nama));
		return $result;
	}
	
	function DoUpdateBidang($id, $kodegol, $kode, $nama) {
		$result = $this->Execute($this->mSqlQueries['do_update_bidang'], array($kodegol, $kode, $nama, $id));
		return $result;
	}
	
	function DoDeleteBidang($bidangId) {
		$result = $this->Execute($this->mSqlQueries['do_delete_bidang'], array($bidangId));
		return $result;
	}

	function DoMassDeleteBidang($arrBidangId) {
		$string = array_fill(0, count($arrBidangId), '%d');
		$string = implode(',',$string);
		$newSql = sprintf($this->mSqlQueries['do_mass_delete_bidang'],$string);
		
		$result = $this->Execute($newSql, $arrBidangId);
		return $result;
	}
	
	function DoDeleteGolongan($golonganId) {
		$result = $this->Execute($this->mSqlQueries['do_delete_golongan'], array($golonganId));
		return $result;
	}

	function DoMassDeleteGolongan($arrGolonganId) {
		$string = array_fill(0, count($arrGolonganId), '%d');
		$string = implode(',',$string);
		$newSql = sprintf($this->mSqlQueries['do_mass_delete_golongan'],$string);
		
		$result = $this->Execute($newSql, $arrGolonganId);
		return $result;
	}
}
?>
