<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_keluarga/business/AppStatusKeluarga.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_keluarga/business/AppStatusKeluargaToFinansi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'main/lib/gtfw/validasi/validation.class.php';

class ProcessStatusKeluarga {
   var $_POST;
	var $Obj;
	var $finansiObj;
	var $pageView;
	var $pageInput;
	//css hanya dipake di view
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";

	var $return;
	var $decId;
	var $encId;
   
   function __construct() {
		$this->Obj = new AppStatusKeluarga();
        $this->finansiObj = new AppStatusKeluargaToFinansi();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('status_keluarga', 'statusKeluarga', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('status_keluarga', 'inputStatusKeluarga', 'view', 'html');      
	}
   
   function validasi(){
      $valObj = new ValidationData();
      $valObj->required($this->_POST['sts_klrga_nama'],GTFWConfiguration::GetValue('language','status_keluarga'));      
      $pesanErr = $valObj->validate();
      
      return $pesanErr;
   }
   
   function Add(){
      //cek apakah tombol batal yang di pencet
      if(isset($this->_POST['btnbalik'])){         
         return $this->pageView;
      }
      
      //validasi data
      $validasi = $this->validasi();      
      if($validasi != ""){         
         Messenger::Instance()->Send('status_keluarga', 'inputStatusKeluarga', 'view', 'html', array($validasi,$this->cssFail),Messenger::NextRequest);
			return $this->pageInput;
      }
      
      $this->Obj->StartTrans();            
      $this->finansiObj->StartTrans();
      $addStatusKeluarga = $this->Obj->AddStatusKeluarga($this->_POST['sts_klrga_nama']);
      $getData = $this->Obj->GetDataStatusKeluargaByNama($this->_POST['sts_klrga_nama']);
      $status_keluarga_id = $getData[0]['ID'];
      $addStatusKeluargaFinansi = $this->finansiObj->AddStatusKeluarga($status_keluarga_id, $this->_POST['sts_klrga_nama']);
      $addData = $addStatusKeluarga && $addStatusKeluargaFinansi;
      $this->Obj->EndTrans($addData);
      $this->finansiObj->EndTrans($addData);
      
      if($addData === true){
         Messenger::Instance()->Send('status_keluarga', 'statusKeluarga', 'view', 'html', array('Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }else{
         Messenger::Instance()->Send('status_keluarga', 'statusKeluarga', 'view', 'html', array('Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
      }
      
      return $this->pageView;
   }
   
   function Update(){
      //cek apakah tombol batal yang di pencet
      if(isset($this->_POST['btnbalik'])){         
         return $this->pageView;
      }
      
      //validasi data
      $validasi = $this->validasi();      
      if($validasi != ""){         
         Messenger::Instance()->Send('status_keluarga', 'inputStatusKeluarga', 'view', 'html', array($validasi,$this->cssFail),Messenger::NextRequest);
			return $this->pageInput."&dataId=".$this->encId;
      }
      
      $this->Obj->StartTrans();            
      $this->finansiObj->StartTrans();
      $updateStatusKeluarga = $this->Obj->updateStatusKeluarga($this->_POST['sts_klrga_nama'],$this->decId);
      $updateStatusKeluargaFinansi = $this->finansiObj->updateStatusKeluarga($this->_POST['sts_klrga_nama'],$this->decId);
      $updateData = $updateStatusKeluarga && $updateStatusKeluargaFinansi;
      $this->Obj->EndTrans($updateData);
      $this->finansiObj->EndTrans($updateData);
      
      if($updateData === true){
         Messenger::Instance()->Send('status_keluarga', 'statusKeluarga', 'view', 'html', array('Edit Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }else{
         Messenger::Instance()->Send('status_keluarga', 'statusKeluarga', 'view', 'html', array('Gagal Mengedit Data', $this->cssFail),Messenger::NextRequest);
      }
      
      return $this->pageView;
   }
   
   function Delete(){
      //delete 1 id
      $arrId = $this->_POST['idDelete'];      
      $this->Obj->StartTrans();
      $this->finansiObj->StartTrans();
      $deleteStatusKeluargaById = $this->Obj->deleteStatusKeluargaById($arrId);
      $deleteStatusKeluargaByIdFinansi = $this->finansiObj->deleteStatusKeluargaById($arrId);
      $deleteDataById = $deleteStatusKeluargaById && $deleteStatusKeluargaByIdFinansi;
      $this->Obj->EndTrans($deleteDataById);
      $this->finansiObj->EndTrans($deleteDataById);
      
      if($deleteDataById == false){
         //delete banyak id dalam array         
         if(is_array($arrId)==false)
            $arr[0]=$arrId;
         else
            $arr=$arrId;
         
         $this->Obj->StartTrans();
         $this->finansiObj->StartTrans();
         $deleteArrStatusKeluarga = $this->Obj->deleteArrStatusKeluarga($arr);
         $deleteArrStatusKeluargaFinansi = $this->finansiObj->deleteArrStatusKeluarga($arr);
         $deleteArrData = $deleteArrStatusKeluarga && $deleteArrStatusKeluargaFinansi;
         $this->Obj->EndTrans($deleteArrData);
         $this->finansiObj->EndTrans($deleteArrData);
      }
      
      if($deleteArrData === true) {
         Messenger::Instance()->Send('status_keluarga', 'statusKeluarga', 'view', 'html', array('Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      } elseif($deleteDataById === true) {
         Messenger::Instance()->Send('status_keluarga', 'statusKeluarga', 'view', 'html', array('Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }else{
         //proses delete gagal
         for($i=0;$i<sizeof($arr);$i++) {
            $this->Obj->StartTrans();
            $this->finansiObj->StartTrans();
            
            $deleteData = false;
            $deleteStatusKeluargaById = $this->Obj->deleteStatusKeluargaById($arr[$i]);            
            $deleteStatusKeluargaByIdFinansi = $this->finansiObj->deleteStatusKeluargaById($arr[$i]);
            $deleteData = $deleteStatusKeluargaById && $deleteStatusKeluargaByIdFinansi;
            
            $this->Obj->EndTrans($deleteData);
            $this->finansiObj->EndTrans($deleteData);
            if($deleteData === true) $sukses += 1;
            else $gagal += 1;
         }
         Messenger::Instance()->Send('status_keluarga','statusKeluarga', 'view', 'html', array($gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
      }      
      return $this->pageView;
   }
}
?>
