<?php

class AppUser extends Database {

   protected $mSqlFile= 'module/user/business/appuser.sql.php';
   
   function __construct($connectionNumber=0) {
      parent::__construct($connectionNumber);
      #$this->SetDebugOn();
   }
   
   function GetUnitKodeSistemByUserId($userId){
   	$result = $this->Open($this->mSqlQueries['get_unit_kodesistem_by_user_id'],array($userId));
   	return $result[0];
   }
      
   # multiunit
   function GetDataUser ($offset, $limit, $userName='', $realName='') {		   
      if(($userName!='') and ($realName!=''))                      
         $str = ' OR ';
      else
         $str = ' AND ';
      $user = $this->GetUnitKodeSistemByUserId(Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId());      
      $sql = sprintf($this->mSqlQueries['get_data_user'], '%s','%s','%s',$str,'%s','%d','%d');      
      $result = $this->Open($sql, array($user['unitkerjaKodeSistem'],$user['unitkerjaKodeSistem'].'.%','%'.$userName.'%', '%'.$realName.'%', $offset, $limit));
      return $result;
   }
   
   function GetDataUserByInstansi ($realName, $instansiId, $offset, $limit) {
      $result = $this->Open($this->mSqlQueries['get_data_user_by_instansi'], array($realName, $instansiId, $offset, $limit));
      return $result;
   }
   
   function GetDataUserById($id) {
      $result = $this->Open($this->mSqlQueries['get_data_user_by_id'], array($id));
      return $result;
   }
   
   function GetCountDuplicateUsername($newUserName, $userId) {
      $result = $this->Open($this->mSqlQueries['get_count_duplicate_username'], array($newUserName, $userId));
      //echo $this->GetLastError();
      return $result[0]['COUNT'];
   }

   function GetCountDataUser ($userName='', $realName='') {
      $result = $this->Open($this->mSqlQueries['get_count_data_user'], array('%'.$userName.'%', '%'.$realName.'%'));
      if (!$result) {
         return 0;
      } else {
         return $result[0]['total'];
      }
   }
   
   function GetCount(){
   	$result = $this->Open($this->mSqlQueries['get_count'],array());
   	return $result[0]['total'];
   }
   
   function GetCountDataUserByInstansi ($realName, $instansiId) {
      $result = $this->Open($this->mSqlQueries['get_count_data_user_by_instansi'], array($realName, $instansiId));
      if (!$result) {
         return 0;
      } else {
         return $result[0]['total'];
      }
   }
    
   /*function GetDataUserById($userId) {
      $result = $this->Open($this->mSqlQueries['get_data_user_by_id'], array($userId));  
      return $result;
   }*/

   function GetNoPegawai($id){
      return $this->Open($this->mSqlQueries['get_no_pegawai'], array($id));
   }

   function GetMaxId(){
      $rs = $this->Open($this->mSqlQueries['get_max_id'], array());
      return $rs[0]['id'];
   }

   # multiunit
   function GetComboUnitKerja(){
   	  $user = $this->GetUnitKodeSistemByUserId(Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId());
      return $this->Open($this->mSqlQueries['get_combo_unit_kerja'],array($user['unitkerjaKodeSistem'],$user['unitkerjaKodeSistem'].'.%'));
   }

   function GetDataGroup($groupName, $withPrivilege= false) {

      if ($withPrivilege){
         $result = $this->Open($this->mSqlQueries['get_data_group_with_privilege'], array('%'.$groupName.'%'));
      } else {
         $result = $this->Open($this->mSqlQueries['get_data_group'], array('%'.$groupName.'%'));
      }
      return $result;
   }

   function GetDataGroupByUnitId($groupName, $unitId) {
      $result = $this->Open($this->mSqlQueries['get_data_group_by_unit_id'], array('%'.$groupName.'%', $unitId));
      //echo $this->getLastError();
      return $result;
   }

//===DO==

   function DoAddUserDefGroup($UserId, $GroupId, $ApplicationId) {
      $result = $this->ExecuteInsertQuery($this->mSqlQueries['do_add_user_def_group'], array($UserId, $GroupId, $ApplicationId));
      return $result;
   }

   function DoAddUserGroup($UserId, $GroupId) {
      $result = $this->ExecuteInsertQuery($this->mSqlQueries['do_add_user_group'], array($UserId, $GroupId));
      return $result;
   }
   
   function DoAddUserUnitKerja($UserId, $GtfwUnitId){
   	  $result = $this->Execute($this->mSqlQueries['do_add_user_unit_kerja'],array($UserId, $GtfwUnitId));
   	  return $result;
   }
   
   function DoUpdateUserUnitKerja($GtfwUnitId,$UserId){
   	  $result = $this->Execute($this->mSqlQueries['do_update_user_unit_kerja'],array($GtfwUnitId,$UserId));
   	  return $result;
   }

   function DoUpdateUserDefGroup($GroupId, $ApplicationId, $UserId) {
      $result = $this->Execute($this->mSqlQueries['do_update_user_def_group'], array($GroupId, $ApplicationId, $UserId));
      return $result;
   }

   function DoUpdateUserGroup($GroupId, $UserId) {
      $result = $this->ExecuteInsertQuery($this->mSqlQueries['do_update_user_group'], array($GroupId, $UserId));
      return $result;
   }
   
   function DoAddUser($userName, $password, $realName, $description, $active, $groupId) {
      $result = $this->Execute($this->mSqlQueries['do_add_user'], array($userName, $password, $realName, $description, $active, $groupId));
      return $result;
   }
   
   function DoUpdateUser($userName, $realName, $active, $decription, $groupId, $userId) {
      $result = $this->Execute($this->mSqlQueries['do_update_user'], array($userName, $realName, $active, $decription, $groupId, $userId));
      return $result;
   }
   
   function DoUpdateProfile($realName,$description,$userId){
      return $this->Execute($this->mSqlQueries['do_update_profile'], array($realName, $description, $userId));
   }
   
   function DoDeleteUserById($userId) {
      $result=$this->Execute($this->mSqlQueries['do_delete_user_by_id'], array($userId));
      //echo $this->GetLastError();
      return $result;
   }
   
	function DoDeleteUserByArrayId($arrUserId) {
		$userId = implode("', '", $arrUserId);
		$result=$this->Execute($this->mSqlQueries['do_delete_user_by_array_id'], array($userId));
		return $result;
	}
   
   //tambahan 
   function DoUpdatePasswordUser($password, $userId) {
      $result = $this->Execute($this->mSqlQueries['do_update_password_user'], array($password, $userId));
      return $result;
   }

   function DoAddNoPeg($noPeg){
      return $this->Execute($this->mSqlQueries['do_add_no_peg'], array($noPeg));
   }

   function DoUpdateAddNoPeg($id,$noPeg){
      return $this->Execute($this->mSqlQueries['do_update_add_no_peg'], array($id,$noPeg));
   }

   function DoUpdateNoPeg($id,$noPeg){
      return $this->Execute($this->mSqlQueries['do_update_no_peg'], array($noPeg,$id));
   }

   /*function DoAddUserGroup($group){
      return $this->Execute($this->mSqlQueries['add_user_group'], array($group));
   }*/

   /*function DoUpdateUserGroup($group,$id){
      return $this->Execute($this->mSqlQueries['update_user_group'],array($group,$id));
   }*/

}
?>
