<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/propinsi/business/AppPropinsi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/propinsi/business/AppPropinsiToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/propinsi/business/AppPropinsiToUm.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'main/lib/dbf/dbf.class.php';
class ProcessPropinsi{
	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 AppPropinsi();
		$this->ObjToSia = new AppPropinsiToSia();
		$this->umObj = new AppPropinsiToUm();
      
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('propinsi', 'propinsi', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('propinsi', 'inputPropinsi', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}
   function Check() {
		if (isset($_POST['btnsimpan'])) {
         $dataPropinsi=$this->Obj->GetDataPropinsiById($this->_POST['propinsi_kode']);

			if((trim($this->_POST['propinsi_kode']) == "") or (trim($this->_POST['propinsi_nama']) == ""))  {
				return "empty";
			}elseif((empty($this->decId)) and($dataPropinsi[0]['propinsi_kode']===$this->_POST['propinsi_kode'])){
            return "use";
         }elseif((!empty($this->decId)) and ($dataPropinsi[0]['propinsi_kode']!=$this->decId) and ($dataPropinsi[0]['propinsi_kode']===$this->_POST['propinsi_kode'])){           
            return "use";           
         }else{
				return true;
			}
		}
		return false;
	}

   function Add(){
      $cek=$this->Check();
      if($cek===true){
      
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $addPropinsiToSireg=$this->Obj->DoAddPropinsi($this->_POST['propinsi_kode'],strtoupper($this->_POST['propinsi_nama']),strtoupper($this->_POST['propinsi_nama_singkat']));
         $addPropinsiToSia=$this->ObjToSia->DoAddPropinsi($this->_POST['propinsi_kode'],strtoupper($this->_POST['propinsi_nama']),strtoupper($this->_POST['propinsi_nama_singkat']));
        
         $addPropinsi=$addPropinsiToSireg && $addPropinsiToSia;
         if($this->useUm){
            $this->umObj->StartTrans();
            $addPropinsiToUm=$this->umObj->DoAddPropinsi($this->_POST['propinsi_kode'],strtoupper($this->_POST['propinsi_nama']));
            $addPropinsi =$addPropinsi && $addPropinsiToUm;
            $this->umObj->EndTrans($addPropinsi);
         }
         $this->Obj->EndTrans($addPropinsi);
         $this->ObjToSia->EndTrans($addPropinsi);         
         
         if($addPropinsi===true){
            Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="use"){
         Messenger::Instance()->Send('propinsi', 'inputPropinsi', 'view', 'html', array($this->_POST,'Kode Propinsi Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('propinsi', 'inputPropinsi', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput;
      }
      return $this->pageView;
   }
   
   function ImportFileDbfPropinsi($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('propinsi', 'importPropinsi', 'view', 'html', array($this->_POST,'File DBF Tidak Valid'),Messenger::NextRequest);
            if ($response == 'json')
               return Dispatcher::Instance()->GetUrl('propinsi', 'importPropinsi', 'view', 'html', true);
            else
               return Dispatcher::Instance()->GetUrl('propinsi', 'importPropinsi', '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)) {
            $dbfPropinsi = new dbf_class($file);
            $num_rec=$dbfPropinsi->dbf_num_rec;
            $field_num=$dbfPropinsi->dbf_num_field;
            $num_done=0;
            $num_fail=0;
            for($i=0; $i<$num_rec; $i++){
               if ($row = $dbfPropinsi->getRow($i)) {
                  //menentukan data PROPINSI yang akan diolah
                  if (($row[0] < 90) and (empty($row[2]))or ($row[0] == 99)){
                     
                     $this->Obj->StartTrans();
                     $this->ObjToSia->StartTrans();
                     
                     $importFileDbfPropinsToSireg=$this->Obj->DoImportFileDbfPropinsi($row[0],$row[1],'',$row[0],$row[1],'');
                     $importFileDbfPropinsiToSia=$this->ObjToSia->DoImportFileDbfPropinsi($row[0],$row[1],'',$row[0],$row[1],'');
                     
                     $importFileDbfPropinsi=$importFileDbfPropinsToSireg && $importFileDbfPropinsiToSia;
                     if($this->useUm){
                        $this->umObj->StartTrans();
                        $importFileDbfPropinsiToUm=$this->umObj->DoImportFileDbfPropinsi($row[0],$row[1],$row[0],$row[1]);
                        $importFileDbfPropinsi =$importFileDbfPropinsi && $importFileDbfPropinsiToUm;
                        $this->umObj->EndTrans($importFileDbfPropinsi);
                     }
                     $this->Obj->EndTrans($importFileDbfPropinsi);
                     $this->ObjToSia->EndTrans($importFileDbfPropinsi);                      
                     
                     if ($importFileDbfPropinsi===true){
                        $num_done++;
                     }else{
                        $num_fail++;
                     }
                  }
               }   
            }
         }
         # hapus file setelah proses import selesai
         unlink($file);
         
         if ($importFileDbfPropinsi===true){
            Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST,'Import Data Berhasil Dilakukan Sebanyak '.$num_done, $this->cssDone),Messenger::NextRequest);
         }else{
            //Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST,'Import Data Berhasil Dilakukan Sebanyak '.$num_done.'<br />'.'Import Data Gagal Dilakukan Sebanyak '.$num_fail,$this->cssDone),Messenger::NextRequest);
            Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST,'Import Data Gagal Dilakukan Sebanyak '.$num_fail, $this->cssFail),Messenger::NextRequest);
         }
      }

      if ($response == 'json')
         return Dispatcher::Instance()->GetUrl('propinsi', 'propinsi', 'view', 'html', true);
      else
         return Dispatcher::Instance()->GetUrl('propinsi', 'propinsi', 'view', 'html');
      #return $this->pageView;
   }
   
   function Update(){
      $cek=$this->Check();
      if($cek===true){
      
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();  
         
         $updatePropinsiToSireg=$this->Obj->DoUpdatePropinsi($this->_POST['propinsi_kode'],strtoupper($this->_POST['propinsi_nama']),strtoupper($this->_POST['propinsi_nama_singkat']),$this->decId);
         $updatePropinsiToSia=$this->ObjToSia->DoUpdatePropinsi($this->_POST['propinsi_kode'],strtoupper($this->_POST['propinsi_nama']),strtoupper($this->_POST['propinsi_nama_singkat']),$this->decId);

         $updatePropinsi=$updatePropinsiToSireg && $updatePropinsiToSia;
         if($this->useUm){
            $this->umObj->StartTrans();
            $updatePropinsiUm=$this->umObj->DoUpdatePropinsi($this->_POST['propinsi_kode'],strtoupper($this->_POST['propinsi_nama']),$this->decId);
            $updatePropinsi =$updatePropinsi && $updatePropinsiUm;
            $this->umObj->EndTrans($updatePropinsi);
         }
         $this->Obj->EndTrans($updatePropinsi);
         $this->ObjToSia->EndTrans($updatePropinsi);   
         
         if($updatePropinsi===true){
            Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST,'Perubahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="use"){
         Messenger::Instance()->Send('propinsi', 'inputPropinsi', 'view', 'html', array($this->_POST,'Kode Propinsi Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('propinsi', 'inputPropinsi', '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->umObj->addConn || $this->useUm==false)){
 
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();     
         
         $deleteDataByIdToSireg=$this->Obj->DoDeletePropinsiById($arrId); 
         $deleteDataByIdToSia=$this->ObjToSia->DoDeletePropinsiById($arrId); 
         
         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         if($this->useUm){
            $this->umObj->StartTrans();
            $deleteDataByIdUm=$this->umObj->DoDeletePropinsiById($this->_POST['propinsi_kode'],strtoupper($this->_POST['propinsi_nama']),$this->decId);
            $deleteDataById =$deleteDataById && $deleteDataByIdUm;
            $this->umObj->EndTrans($deleteDataById);
         }
         $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->DoDeletePropinsiByArrayId($arr);
            $deleteArrDataToSia = $this->ObjToSia->DoDeletePropinsiByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia;
            if($this->useUm){
               $this->umObj->StartTrans();
               $deleteArrDataUm=$this->umObj->DoDeletePropinsiByArrayId($arr);
               $deleteArrData =$deleteArrData && $deleteArrDataUm;
               $this->umObj->EndTrans($deleteArrData);
            }
            $this->Obj->EndTrans($deleteArrData);
            $this->ObjToSia->EndTrans($deleteArrData);
         }
         if($deleteArrData === true) {
            Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('propinsi', 'propinsi', '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($arr);$i++) {
            
               $deleteData = false;
               
               $this->Obj->StartTrans();
               $this->ObjToSia->StartTrans(); 
            
               $deleteDataToSireg = $this->Obj->DoDeletePropinsiById($arr[$i]);
               $deleteDataToSia = $this->ObjToSia->DoDeletePropinsiById($arr[$i]);
               
               $deleteData=$deleteDataToSireg && $deleteDataToSia;
               if($this->useUm){
                  $this->umObj->StartTrans();
                  $deleteDataUm=$this->umObj->DoDeletePropinsiByArrayId($arr[$i]);
                  $deleteData =$deleteData && $deleteDataUm;
                  $this->umObj->EndTrans($deleteData);
               }
               $this->Obj->EndTrans($deleteData);
               $this->ObjToSia->EndTrans($deleteData);        
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif(!$this->ObjToSia->addConn){
         Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }elseif(!$this->umObj->addConn){
         Messenger::Instance()->Send('propinsi', 'propinsi', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Admisi ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;

   }
}
?>
