<?php 
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/setting_ktm/business/AppSettingKtm.class.php';
class ProcessSettingKtm {

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

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

	function __construct($ret) {
		$this->Obj = new AppSettingKtm();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);

      if($ret === "json"){
         $this->pageInput = Dispatcher::Instance()->GetUrl('setting_ktm','inputSettingKtm','view','html',true);
         $this->pageView = Dispatcher::Instance()->GetUrl('setting_ktm','settingKtm','view','html',true);
      }else{
         $this->pageInput = Dispatcher::Instance()->GetUrl('setting_ktm','inputSettingKtm','view','html');
         $this->pageView = Dispatcher::Instance()->GetUrl('setting_ktm','settingKtm','view','html');
      }
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
			if(trim($this->_POST['jabatan']) == "" or trim($this->_POST['nama'])== "") {
				return "empty";
			} else {
				return true;
			}
		}
		return false;
	}
	
   function Add() { 
      $cek = $this->Check();		
		if($cek === true) {   
         $addSettingKtm = $this->Obj->DoAddSettingKtm($this->_POST['jabatan'],$this->_POST['nama'],$this->_POST['nip'],'');
			if ($addSettingKtm === true) {
				Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('setting_ktm', 'inputSettingKtm', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput;
		}
		return $this->pageView;
	}
	

	function Update() {
      $cek = $this->Check();
		if($cek === true) {
			$updateSettingKtm = $this->Obj->DoUpdateSettingKtm($this->_POST['jabatan'],$this->_POST['nama'],$this->_POST['nip'], $this->decId);
			if ($updateSettingKtm === true) {
				Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Perubahan Setting Ktm Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Perubahan Setting Ktm Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('setting_ktm', 'inputSettingKtm', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		}
		return $this->pageView;
	}
   
   function UpdateTtd($file,$idk){
   
      if (isset($_POST['btnbatal'])) {
         return $this->pageView;
      }
      if (isset($_POST['btnsimpan'])){
         $idk = $this->_POST['dataId'];
         $nama = $this->_POST['ktmNama'];
         $jabatan = $this->_POST['ktmJabatan'];
         
         $file_lama=$this->_POST['ktmTtd'];
         $files = explode(".", $_FILES['file']['name']);
         $ext = $files[count($files)-1];
         
         if ((strtolower($ext) == 'jpg') OR (strtolower($ext) == 'png') AND ($_FILES['file']['size']<='200999')){
            if ($_FILES["file"]["tmp_name"]!==''){
               $nama_file = 'ttd'.$jabatan.$nama.$_FILES["file"]["name"];
   		    }else{
   		 	   $nama_file = $file_lama;
   		 	}
		       if ($_FILES["file"]["name"]!==''){
  			       @unlink(GTFWConfiguration::GetValue( 'application', 'ttd_move_path').$this->POST['ktmTtd']);
  			       move_uploaded_file($_FILES['file']['tmp_name'], GTFWConfiguration::GetValue( 'application', 'ttd_move_path').$nama_file);
  	         }
            $addphoto = $this->Obj->DoUpdateTandaTangan($nama_file,$idk);
            if($addphoto == true){
               Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Penambahan file tanda tangan berhasil dilakukan', $this->cssDone),Messenger::NextRequest);	 
            }else{
               Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Penambahan file tanda tangan gagal dilakukan', $this->cssFail),Messenger::NextRequest);
            }
         }else {
            Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Foto harus berupa file .jpg atau .png', $this->cssFail),Messenger::NextRequest);
         }
         return $this->pageView;
     }
   }
   
	function AktifSettingKtm(){
      $this->Obj->StartTrans();
      
      $nonAktifSettingKtm = $this->Obj->DoNonAktifSettingKtm('');
      $aktifSettingKtm = $this->Obj->DoAktifSettingKtm($this->decId);
      
      $aktif = $nonAktifSettingKtm && $aktifSettingKtm;
      $this->Obj->EndTrans($aktif);
      
      if($aktif === true){
         Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Setting Ktm Berhasil Diaktifkan', $this->cssDone),Messenger::NextRequest);
      }else{
         Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Setting Ktm Gagal Diaktifkan', $this->cssFail),Messenger::NextRequest);
      }
      return $this->pageView;
   }

	function Delete() {
		$arrId = $this->_POST['idDelete'];
		$deleteDataById=$this->Obj->DoDeleteSettingKtmById($arrId);
		
		if($deleteDataById==false){
   		    if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
         $deleteArrData = $this->Obj->DoDeleteSettingKtmByArrayId($arr);
      }
		//print_r($this->_POST);
		if($deleteArrData === true) {
			Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
		} elseif($deleteDataById === true) {
			Messenger::Instance()->Send('setting_ktm', 'settingKtm', '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;
				$deleteData = $this->Obj->DoDeleteSettingKtmById($arr[$i]);
				if($deleteData === true) $sukses += 1;
				else $gagal += 1;
			}
			Messenger::Instance()->Send('setting_ktm', 'settingKtm', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
		}
		return $this->pageView;
	}
}
?>
