<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
'module/mitra_kerja/business/MitraKerja.class.php';

class ProcessMitraKerja
{
	var $_POST;
	var $Obj;
	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 MitraKerja;
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['mkId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('mitra_kerja', 'mitraKerja', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('mitra_kerja', 'inputMitraKerja', 'view', 'html');
		$this->_POST['mkDataPrepared'] = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
	}
   
   function Check ()
   {
      if (isset($this->_POST['btnbalik'])) return $this->pageView;
      if (($this->_POST['mkNama']) == '' OR trim($this->_POST['mkAlamat']) == '')
      {
         $msg = array($this->_POST, 'Lengkapi Isian Data!', '');
         Messenger::Instance()->Send('mitra_kerja', 'inputMitraKerja', 'view', 'html', $msg, Messenger::NextRequest);
         
         $return = $this->pageInput;
         if (isset($_GET['mkId'])) $return .= "&mkId=".$_GET['mkId'];
         return $return;
      }
         
      return true;
   }
   
   function Update()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
	  
	  // upload file Doc profile mitra kerja
	  $files = $_FILES['mkDocProfile'];
	  if(trim($files['name']) != "")
		$file = $this->UploadFile($files);
	  else{
		$file = $this->_POST['hidden_file'];
	  }
      $this->_POST['mkDocProfile']=$file;
     
	  //upload file foto contactperson
	  $fileCp = $_FILES['fileCp'];
	  if(!empty($fileCp['name'][1])){
		$file = $this->UploadFileCp($fileCp);
		for($i=1;$i<=sizeof($file);$i++) 
		{
		$this->_POST['cp'][$i]['foto']=$file[$i];
		} 
		
	 }else{
		for($i=1;$i<=sizeof($this->_POST['cp']);$i++) 
		{
		$this->_POST['cp'][$i]['foto']=$this->_POST['cp'][$i]['fotoHidden'];
		}
	 }

      $result = $this->Obj->UpdateMitraKerja($this->_POST);
      if ($result === 0)
         $msg = array(1=>'Perubahan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>'Tidak Berhasil Mengubah Data!', $this->cssFail);
      Messenger::Instance()->Send('mitra_kerja', 'mitraKerja', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Add()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
      
	  // upload file Doc profile mitra kerja
	  $files = $_FILES['mkDocProfile'];
	  if(trim($files['name']) != ""){
	  $file =  $this->UploadFile($files);}
	  @$this->_POST['mkDocProfile']=$file; 
	  
	  //upload file foto contactperson
	  @$fileCp = $_FILES['fileCp'];
	  if(!empty($fileCp)){
		$file =  $this->UploadFileCp($fileCp);
		for($i=1;$i<=sizeof($fileCp['name']);$i++) 
		{
		$this->_POST['cp'][$i]['foto']=$file[$i];
		}
	  }
	  
      $result = $this->Obj->AddMitraKerja($this->_POST);
      if ($result === 0)
         $msg = array(1=>'Penambahan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>'Proses Penambahan Data Gagal!', $this->cssFail);
      Messenger::Instance()->Send('mitra_kerja', 'mitraKerja', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Delete()
   {
      $result = $this->Obj->DeleteMitra($this->_POST['idDelete']);
      if ($result === 0)
         $msg = array(1=>'Penghapusan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>'Proses Penghapusan Data Gagal!', $this->cssFail);
      Messenger::Instance()->Send('mitra_kerja', 'mitraKerja', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
    function UploadFile($files){
			if 	($files!='') {
					$ext = end(explode(".", $files['name']));
					$file = date("Y-m-d_H-i-s_") . substr(md5(microtime()), 0, 3).'.'.$ext;
					@move_uploaded_file($files['tmp_name'], GTFWConfiguration::GetValue( 'application', 
				'docroot')."document/mitrakerja/".$file);
			}
			else {$file='';}
			
			return $file;
	}
	
	function UploadFileCp($files){
				for($i=1;$i<=sizeof($files['name']);$i++) {
					$ext = end(explode(".", $files['name'][$i]));
                    $namafile[$i] = date("Y-m-d_H-i-s_") . substr(md5(microtime()), 0, 3) . "." . $ext;
					@move_uploaded_file($files['tmp_name'][$i], GTFWConfiguration::GetValue( 'application', 
					'docroot')."document/contactperson/".$namafile[$i]);
				}
			return $namafile;
	}
}
?>