<?php

require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
         'module/unitkerja_tree/business/AppUnitkerja.class.php';
         
class ProcessUnitkerja 
{
	protected $_POST;
	protected $Obj;
	protected $pageView;
	protected $pageInput;
    protected $pageListCari;
    protected $p;
	//css hanya dipake di view
	protected $cssDone = "notebox-done";
	protected $cssFail = "notebox-warning";

	protected $return;
	protected $decId;
	protected $encId;

	public function __construct() 
    {
		$this->Obj = new AppUnitkerja();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->decJenis = Dispatcher::Instance()->Decrypt($_GET['jenis']);
		$this->encJenis = Dispatcher::Instance()->Encrypt($this->decJenis);
        $parentId = Dispatcher::Instance()->Decrypt($_GET['parentUnitId']);
        $this->p = Dispatcher::Instance()->Decrypt($_GET['p']);
        if($parentId != ''){
            $url_parent_id = '&parentUnitId='.Dispatcher::Instance()->Encrypt($parentId);
        } 
        if($this->p != ''){
            $url_p = '&p='.Dispatcher::Instance()->Encrypt($this->p);
        }
        if($this->decJenis == 'unit') {
            $this->satker = '';   
        } else {
            $this->satker = $this->_POST['satker'];
        }
        $this->pageListCari = Dispatcher::Instance()->GetUrl('unitkerja_tree', 'unitkerjaCari', 'view', 'html');
		$this->pageView = Dispatcher::Instance()->GetUrl(
                                                            'unitkerja_tree', 
                                                            'unitkerja', 
                                                            'view', 
                                                            'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl(
                                                            'unitkerja_tree', 
                                                            'inputUnitkerja', 
                                                            'view', 
                                                            'html') . 
                                                            '&jenis=' . 
                                                            $this->encJenis . 
                                                            $url_parent_id.
                                                            $url_p;
	}

	public function Check() 
    {
		if (isset($_POST['btnsimpan'])) {
			if(trim($this->_POST['satker']) == "" && $this->decJenis == 'subunit') {
				return "empty";
			} elseif(trim($this->_POST['unitkerja_kode']) == "") {
				return "empty";
			} elseif(trim($this->_POST['unitkerja_nama']) == "") {
				return "empty";
			} elseif(trim($this->_POST['tipeunit']) == "") {
				return "empty";
			} elseif(trim($this->_POST['unitkerja_pimpinan']) == "") {
				return "empty";
			} else {
				return true;
			}
		}
		return false;
	}

	public function Add() 
    {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         
			$addUnitkerja = $this->Obj->DoAddUnitkerja(
                                                    $this->_POST['unitkerja_pimpinan'], 
                                                    $this->_POST['unitkerja_kode'], 
                                                    $this->_POST['unitkerja_nama'], 
                                                    $this->_POST['tipeunit'], 
                                                    $this->_POST['statusunit'],
                                                    $this->satker);
			
			$addGtfwUnit = $this->Obj->DoAddGtfwUnit($this->_POST['unitkerja_nama']);
			
			if(GTFWConfiguration::GetValue( 'application', 'aset')){
				$Obj2 = new AppUnitkerja(1);
				$Obj2->StartTrans();
				$addUnitkerja2 = $Obj2->DoAddUnitkerja(
                                                $this->_POST['unitkerja_pimpinan'], 
                                                $this->_POST['unitkerja_kode'], 
                                                $this->_POST['unitkerja_nama'], 
                                                $this->_POST['tipeunit'], 
                                                $this->_POST['statusunit'],
                                                $this->satker);
				$addData = $addUnitkerja && $addUnitkerja2 && $Obj2->addConn;
				$Obj2->EndTrans($addData);
			}else
				$addData = $addUnitkerja&& $addGtfwUnit;
			
			$this->Obj->EndTrans($addData);
			if ($addData === true) {
			     $message = 'Penambahan data Berhasil Dilakukan';
                 $status ='done';
			} else {
			     $message = 'Gagal Menambah Data';
                 $status = 'fail';
			}
		} elseif($cek == "empty") {
		    $this->ShowMessage('inputUnitkerja','Lengkapi Isian Data','fail');
			//Messenger::Instance()->Send('unitkerja_tree', 
            //'inputUnitkerja', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),
            //Messenger::NextRequest);
			return $this->pageInput;
		}
        
        if($this->p == 'list'){
            $this->ShowMessage('UnitkerjaCari',$message,$status);
            return $this->pageListCari;    
        } else {
            $this->ShowMessage('Unitkerja',$message,$status);
            return $this->pageView;
        }
		
	}

	public function Update() 
    {
		$cek = $this->Check();
		if($cek === true) {
		   $this->Obj->StartTrans();
			$updateUnitkerja = $this->Obj->DoUpdateUnitkerja(
                                                 $this->_POST['unitkerja_pimpinan'], 
                                                 $this->_POST['unitkerja_kode'], 
                                                 $this->_POST['unitkerja_nama'], 
                                                 $this->_POST['tipeunit'],
                                                 $this->_POST['statusunit'], 
                                                 $this->satker, 
                                                 $this->decId);
			$updateGtfwUnit = $this->Obj->DoUpdateGtfwUnit($this->_POST['unitkerja_nama'],$this->decId);
			if(GTFWConfiguration::GetValue( 'application', 'aset')){
				 $Obj2 = new AppUnitkerja(1);
                 $Obj2->StartTrans();
			     $updateUnitkerja2 = $Obj2->DoUpdateUnitkerja(
                                                        $this->_POST['unitkerja_pimpinan'], 
                                                        $this->_POST['unitkerja_kode'], 
                                                        $this->_POST['unitkerja_nama'], 
                                                        $this->_POST['tipeunit'], 
                                                        $this->_POST['statusunit'], 
                                                        $this->satker, 
                                                        $this->decId);
			   
        		$updateData = $updateUnitkerja && $updateUnitkerja2 && $Obj2->addConn;
        		$Obj2->EndTrans($updateData);
			}else
				$updateData = $updateUnitkerja && $updateGtfwUnit;
			
			$this->Obj->EndTrans($updateData);
         
			   
			if ($updateData === true) {			   
				$message ='Perubahan Data Berhasil Dilakukan';
                $status = 'done';
			} else {
				$message ='Perubahan Data Gagal Dilakukan';
                $status = 'fail';
			}
		} elseif($cek == "empty") {
		      $this->ShowMessage('inputUnitkerja','Lengkapi Isian Data','fail');
              return $this->pageInput . "&dataId=" . $this->encId;
		}
        
       if($this->p == 'list'){
            $this->ShowMessage('UnitkerjaCari',$message,$status);
            return $this->pageListCari;    
        } else {
            $this->ShowMessage('Unitkerja',$message,$status);
            return $this->pageView;
        }
	}

	public function Delete() 
    {
		$arrId = $this->_POST['idDelete'];
		
		$this->Obj->StartTrans();
		$deleteArrData = $this->Obj->DoDeleteUnitkerjaByArrayId($arrId);
		
		if(GTFWConfiguration::GetValue( 'application', 'aset')){
			$Obj2 = new AppUnitkerja(1);
			$deleteArrData2 = $Obj2->DoDeleteUnitkerjaByArrayId($arrId);
			
			$deleteData = $deleteArrData && $deleteArrData2 && $Obj2->addConn;
			$Obj2->EndTrans($deleteData);
		}
		else
			$deleteData = $deleteArrData;
			
        $this->Obj->EndTrans($deleteData);

		
		if($deleteData === true) {
            $this->ShowMessage('UnitkerjaCari','Penghapusan Data Berhasil Dilakukan');
			//Messenger::Instance()->Send(
            //'unitkerja_tree', 'unitkerja', '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;
				$deleteData = $this->Obj->DoDeleteUnitkerjaById($arrId[$i]);
				if($deleteData === true) 
				{$sukses += 1;
               
               $return = $Obj->DoDeleteUnitkerjaById($arrId[$i]);
            }
				else {
					$gagal += 1;
					$sebab = $this->Obj->GetError();
				}
			}
            $this->ShowMessage('UnitkerjaCari',$gagal . ' Data Tidak Dapat Dihapus.<br />' . $sebab,'fail');
			//Messenger::Instance()->Send(
            //'unitkerja_tree', 'unitkerja', 'view', 'html', 
            //array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.<br />' . 
            //$sebab, $this->cssFail),Messenger::NextRequest);
		}
		//return $this->pageView;
        return $this->pageListCari;
	}
    
    protected function ShowMessage($subModule,$msg,$tipe='done')
    {
        if($tipe == 'done'){
            $status = $this->cssDone;
        }
        if($tipe == 'fail'){
            $status = $this->cssFail;
        }
        Messenger::Instance()->Send('unitkerja_tree', 
                                    $subModule, 
                                    'view', 
                                    'html', 
                                    array(
                                         $this->_POST,
                                         $msg, 
                                         $status),
                                    Messenger::NextRequest);
    }
}
