<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/kota/business/AppKota.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/kota/business/AppKotaToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/kota/business/AppKotaToUm.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'main/lib/dbf/dbf.class.php';

class ProcessKota{
	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 AppKota();
		$this->siaObj = new AppKotaToSia();
		$this->umObj = new AppKotaToUm();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('kota', 'kota', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('kota', 'inputKota', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}
   function Check() {
		if (isset($_POST['btnsimpan'])) {
         $dataKota=$this->Obj->GetDataKotaById($this->_POST['kota_kode']);

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

	function ImportFileDbfKota($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('kota', 'importKota', 'view', 'html', array($this->_POST,'File DBF Tidak Valid'),Messenger::NextRequest);
            if ($response == 'json')
               return Dispatcher::Instance()->GetUrl('kota', 'importKota', 'view', 'html', true);
            else
               return Dispatcher::Instance()->GetUrl('kota', 'importKota', '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)) {
            
            $dbfKota= new dbf_class($file);
            $num_rec=$dbfKota->dbf_num_rec;
            $field_num=$dbfKota->dbf_num_field;
            $num_done=0;
            $num_fail=0;
            for($i=0; $i<$num_rec; $i++){
               if ($row = $dbfKota->getRow($i)) {
                  //menentukan data Kota yang akan diolah
                  if (($row[0] < 90) and (!empty($row[3]))) { 
                     //echo $row[0];echo $row[1];echo "--------";echo $row[3];echo '<br />';
                     $this->Obj->StartTrans();
                     $this->siaObj->StartTrans();
                     
                     $importFileDbfKota=$this->Obj->DoImportFileDbfKota($row[0].$row[2],$row[3],$row[0],$row[0].$row[2],$row[3],$row[0]);
                     $importFileDbfKotaToSia=$this->siaObj->DoImportFileDbfKotaToSia($row[0].$row[2],$row[3],$row[0],$row[0].$row[2],$row[3],$row[0]);
                     $importData = $importFileDbfKota && $importFileDbfKotaToSia;
                     if($this->useUm){
                        $this->umObj->StartTrans();
                        $importDataToUm=$this->umObj->DoImportFileDbfKotaToUm($row[0].$row[2],$row[3],$row[0],$row[0].$row[2],$row[3],$row[0]);
                        $importData =$importData && $importDataToUm;
                        $this->umObj->EndTrans($importData);
                     }
                     $this->Obj->EndTrans($importData);
                     $this->siaObj->EndTrans($importData);
                     if ($importData===true){
                        $num_done++;
                     }else{
                        $num_fail++;
                     }
                  }
               }   
            }
         }
         # hapus file setelah proses import selesai
         unlink($file);
         
         if ($importData === true){
            Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Import Data Berhasil Dilakukan sebanyak '.$num_done, $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Import Data Gagal Dilakukan '.$num_fail, $this->cssFail),Messenger::NextRequest);
         }
      }
      
      if ($response == 'json')
         return Dispatcher::Instance()->GetUrl('kota', 'kota', 'view', 'html', true);
      else
         return Dispatcher::Instance()->GetUrl('kota', 'kota', 'view', 'html');
      #return $this->pageView;
   }
   function Add(){
      $cek=$this->Check();
      if($cek===true){
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
         $addKota = $this->Obj->DoAddKota($this->_POST['kota_kode'],strtoupper($this->_POST['kota_nama']),$this->_POST['propinsi']);
         $addKotaToSia = $this->siaObj->DoAddKotaToSia($this->_POST['kota_kode'],strtoupper($this->_POST['kota_nama']),$this->_POST['propinsi']);
         $addData = $addKota && $addKotaToSia;
         if($this->useUm){
            $this->umObj->StartTrans();
            $addKotaToUm = $this->umObj->DoAddKotaToUm($this->_POST['kota_kode'],strtoupper($this->_POST['kota_nama']),$this->_POST['propinsi']);    
            $addData =$addData && $addKotaToUm;
            $this->umObj->EndTrans($addData);
         }
         $this->Obj->EndTrans($addData);
         $this->siaObj->EndTrans($addData);
         if($addData === true){
            Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="use"){
         Messenger::Instance()->Send('kota', 'inputKota', 'view', 'html', array($this->_POST,'Kode Kota Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('kota', 'inputKota', '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->siaObj->StartTrans();
         
         $updateKota = $this->Obj->DoUpdateKota($this->_POST['kota_kode'],strtoupper($this->_POST['kota_nama']),$this->_POST['propinsi'],$this->decId);
         $updateKotaSia = $this->siaObj->DoUpdateKotaSia($this->_POST['kota_kode'],strtoupper($this->_POST['kota_nama']),$this->_POST['propinsi'],$this->decId);
         $updateData = $updateKota && $updateKotaSia;
         if($this->useUm){
            $this->umObj->StartTrans();
            $updateDataUm = $this->umObj->DoUpdateKotaUm($this->_POST['kota_kode'],strtoupper($this->_POST['kota_nama']),$this->_POST['propinsi'],$this->decId);    
            $updateData =$updateData && $updateDataUm;
            $this->umObj->EndTrans($updateData);
         }
         $this->Obj->EndTrans($updateData);
         $this->siaObj->EndTrans($updateData);
         if($updateData===true){
            Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Perubahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="use"){
         Messenger::Instance()->Send('kota', 'inputKota', 'view', 'html', array($this->_POST,'Kode Kota Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('kota', 'inputKota', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
      }
      return $this->pageView;
   }

   function Delete(){
      if(!$this->siaObj->addConn){
         Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }elseif(!$this->umObj->addConn && $this->useUm==true){
         Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Admisi', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }
      if(!$this->siaObj->addConn){
         Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }else{
         $arrId = $this->_POST['idDelete'];
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
         $deleteKotaById=$this->Obj->DoDeleteKotaById($arrId);
         $deleteKotaSiaById=$this->siaObj->DoDeleteKotaSiaById($arrId);
         $deleteDataById = $deleteKotaById && $deleteKotaSiaById;
         if($this->useUm){
            $this->umObj->StartTrans();
            $deleteDataByIdUm = $this->umObj->DoDeleteKotaUmById($arrId);    
            $deleteDataById =$deleteDataById && $deleteDataByIdUm;
            $this->umObj->EndTrans($deleteDataById);
         }
         $this->Obj->EndTrans($deleteDataById);
         $this->siaObj->EndTrans($deleteDataById);
         if($deleteDataById==false){
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            //print_r($arr);  
            
            $this->Obj->StartTrans();
            $this->siaObj->StartTrans();
            
            $deleteArrKota = $this->Obj->DoDeleteKotaByArrayId($arr);
            $deleteArrKotaSia = $this->siaObj->DoDeleteKotaSiaByArrayId($arr);
            $deleteArrData = $deleteArrKota && $deleteArrKotaSia;
           #
            if($this->useUm){
               $this->umObj->StartTrans();
               $deleteArrKotaUm = $this->umObj->DoDeleteKotaUmByArrayId($arr);    
               $deleteArrData =$deleteArrData && $deleteArrKotaUm;
               $this->umObj->EndTrans($deleteArrData);
            }
             #var_dump($deleteArrData);exit;
            $this->Obj->EndTrans($deleteArrData);
            $this->siaObj->EndTrans($deleteArrData);
         }
   		if($deleteArrData === true) {
   			Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   	   }elseif($deleteDataById===true){
            Messenger::Instance()->Send('kota', 'kota', '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++) {
   			   $this->Obj->StartTrans();
   			   $this->siaObj->StartTrans();
   			   
   				$deleteData = false;
   				$deleteKota = $this->Obj->DoDeleteKotaById($arr[$i]);
   				$deleteKotaSia = $this->siaObj->DoDeleteKotaSiaById($arr[$i]);
   				$deleteData = $deleteKota && $deleteKotaSia;
               if($this->useUm){
                  $this->umObj->StartTrans();
                  $deleteKotaSiaUm = $this->umObj->DoDeleteKotaSiaById($arr[$i]);    
                  $deleteData =$deleteData && $deleteKotaSiaUm;
                  $this->umObj->EndTrans($deleteData);
               }
   				$this->Obj->EndTrans($deleteData);
   				$this->siaObj->EndTrans($deleteData);
               if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('kota', 'kota', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
   		return $this->pageView;
      }
   }
}
?>
