<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/negara/business/AppNegara.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/negara/business/AppNegaraToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'main/lib/dbf/dbf.class.php';
class ProcessNegara{
	var $_POST;
	var $Obj;
	var $pageView;
	var $pageInput;
	var $pageImport;
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";

	var $return;
	var $decId;
	var $encId;

   function __construct() {
		$this->Obj = new AppNegara();
		$this->ObjToSia = new AppNegaraToSia();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('negara', 'negara', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('negara', 'inputNegara', 'view', 'html');
	}
   
   function Check() {
		if (isset($_POST['btnsimpan'])) {
         $dataNegara=$this->Obj->GetDataNegaraById($this->_POST['negara_kode']);

			if((trim($this->_POST['negara_kode']) == "") or (trim($this->_POST['negara_nama']) == ""))  {
				return "empty";
			}elseif((empty($this->decId)) and($dataNegara[0]['negara_kode']===$this->_POST['negara_kode'])){
            return "use";
         }elseif((!empty($this->decId)) and ($dataNegara[0]['negara_kode']!=$this->decId) and ($dataNegara[0]['negara_kode']===$this->_POST['negara_kode'])){           
            return "use";           
         }else
				return true;
		}
		return false;
	}
	
   function ImportFileDbfNegara($response=''){
      if (isset($_POST['btnimport'])){
         $_FILES['file']['name'] = preg_replace(
            '/[^a-zA-Z0-9\.\$\%\'\`\-\@\{\}\~\!\#\(\)\&\_\^]/'
            ,'',str_replace(array(' ','%20'),array('_','_'),$_FILES['file']['name']));
   
         $files = explode(".", $_FILES['file']['name']);
         if (strtolower($files[count($files) - 1]) != "dbf") {
            Messenger::Instance()->Send('negara', 'importNegara', 'view', 'html', array($this->_POST,'File DBF Tidak Valid'),Messenger::NextRequest);
            if ($response == 'json')
               return Dispatcher::Instance()->GetUrl('negara', 'importNegara', 'view', 'html', true);
            else
               return Dispatcher::Instance()->GetUrl('negara', 'importNegara', 'view', 'html');
			   #return $this->pageImport;
         }else {
            $dir = dirname(realpath($_SERVER['SCRIPT_FILENAME']));
            $file = $dir . "/file/tmp_import/" . date('Ymd') . '_' . $_FILES['file']['name'];
            move_uploaded_file($_FILES['file']['tmp_name'], $file);
         }
   
         if (file_exists($file)) {
            $dbfNegara = new dbf_class($file);
            $num_rec=$dbfNegara->dbf_num_rec;
            $field_num=$dbfNegara->dbf_num_field;
            $num_done=0;
            $num_fail=0;
            for($i=0; $i<$num_rec; $i++){
               if ($row = $dbfNegara->getRow($i)) {
                  //menentukan data negara yng akan diolah
                  if (($row[0]>=94)and($row[0]!=99)){
                  
                     $this->Obj->StartTrans();
                     $this->ObjToSia->StartTrans();
                     
                     $importFileDbfNegaraToSireg=$this->Obj->DoImportFileDbfNegara($row[0].$row[2],$row[3],'',$row[0].$row[2],$row[3],'');
                     $importFileDbfNegaraToSia=$this->ObjToSia->DoImportFileDbfNegara($row[0].$row[2],$row[3],'',$row[0].$row[2],$row[3],'');
                  
                     $importFileDbfNegara=$importFileDbfNegaraToSireg && $importFileDbfNegaraToSia;
                     
                     $this->Obj->EndTrans($importFileDbfNegara);
                     $this->ObjToSia->EndTrans($importFileDbfNegara);
                     
                  if ($importFileDbfNegara===true){
                        $num_done++;
                     }else{
                        $num_fail++;
                     }
                  }
               }   
            }
         }
         # hapus file setelah proses import selesai
         unlink($file);
         
         if ($importFileDbfNegara===true){
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST,'Import Data Berhasil Dilakukan Sebanyak '.$num_done, $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST,'Import Data Gagal Dilakukan Sebanyak '.$num_fail, $this->cssFail),Messenger::NextRequest);
         }
      }
      
      if ($response == 'json')
         return Dispatcher::Instance()->GetUrl('negara', 'negara', 'view', 'html', true);
      else
         return Dispatcher::Instance()->GetUrl('negara', 'negara', 'view', 'html');
      #return $this->pageView;
   }
   
   function Add(){
      $cek=$this->Check();
      if($cek===true){
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $addNegaraToSireg=$this->Obj->DoAddNegara($this->_POST['negara_kode'],strtoupper($this->_POST['negara_nama']),strtoupper($this->_POST['negara_nama_singkat']));
         $addNegaraToSia=$this->ObjToSia->DoAddNegara($this->_POST['negara_kode'],strtoupper($this->_POST['negara_nama']),strtoupper($this->_POST['negara_nama_singkat']));
         
         $addNegara=$addNegaraToSireg && $addNegaraToSia;
         
         $this->Obj->EndTrans($addNegara);
         $this->ObjToSia->EndTrans($addNegara);        
         
         if($addNegara===true){
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="use"){
         Messenger::Instance()->Send('negara', 'inputNegara', 'view', 'html', array($this->_POST,'Kode Negara Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('negara', 'inputNegara', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput;
      }
      return $this->pageView;
   }
   
   function Update(){
      $cek=$this->Check();
      if($cek===true){
      
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $updateNegaraToSireg=$this->Obj->DoUpdateNegara($this->_POST['negara_kode'],strtoupper($this->_POST['negara_nama']),strtoupper($this->_POST['negara_nama_singkat']),$this->decId);
         $updateNegaraToSia=$this->ObjToSia->DoUpdateNegara($this->_POST['negara_kode'],strtoupper($this->_POST['negara_nama']),strtoupper($this->_POST['negara_nama_singkat']),$this->decId);
         
         $updateNegara=$updateNegaraToSireg && $updateNegaraToSia;
         
         $this->Obj->EndTrans($updateNegara);
         $this->ObjToSia->EndTrans($updateNegara);            
         
         if($updateNegara===true){
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST,'Perubahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="use"){
         Messenger::Instance()->Send('negara', 'inputNegara', 'view', 'html', array($this->_POST,'Kode Negara Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('negara', 'inputNegara', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
      }
      return $this->pageView;
   }
 
   function Delete(){

      $arrId = $this->_POST['idDelete'];
      
      if($this->ObjToSia->addConn){


         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $deleteDataByIdToSireg=$this->Obj->DoDeleteNegaraById($arrId); 
         $deleteDataByIdToSia=$this->ObjToSia->DoDeleteNegaraById($arrId); 
         
         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         

         $this->Obj->EndTrans($deleteDataById);
         $this->ObjToSia->EndTrans($deleteDataById);      

       
   
         if($deleteDataById==false){
   		    if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;

            $this->Obj->StartTrans();
            $this->ObjToSia->StartTrans(); 
         
            $deleteArrDataToSireg = $this->Obj->DoDeleteNegaraByArrayId($arr);
            $deleteArrDataToSia = $this->Obj->DoDeleteNegaraByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia;
            
            $this->Obj->EndTrans($deleteArrData);
            $this->ObjToSia->EndTrans($deleteArrData);
            

         }
         if($deleteArrData === true) {
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            //jika masuk disini, berarti PASTI ada salah satu atau lebih data yang gagal dihapus
            for($i=0;$i<sizeof($arrId);$i++) {
               $deleteData = false;
               
               $this->Obj->StartTrans();
               $this->ObjToSia->StartTrans();
               
               $deleteDataToSireg = $this->Obj->DoDeleteNegaraById($arr[$i]);
               $deleteDataToSia = $this->ObjToSia->DoDeleteNegaraById($arr[$i]);
               
               $deleteData=$deleteDataToSireg && $deleteDataToSia;
               
               $this->Obj->EndTrans($deleteArrData);
               $this->ObjToSia->EndTrans($deleteArrData);      
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif(!$this->ObjToSia->addConn){
         Messenger::Instance()->Send('negara', 'negara', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;

   }
}
?>