<?php
class M_pegawai extends CI_Model{


		function tampil(){
		$query = $this->db->query("SELECT a.*, b.*, a.file_name as file_name,b.file_name as file_name_jenis
													FROM pegawai AS a
													LEFT JOIN jenis AS b ON b.idjenis = a.idjenis
													ORDER BY a.namapegawai ASC");		
		return $query;
		}
		
		function edit($id){
		 $id;
		$query = $this->db->query("SELECT a.*, b.*, a.file_name as file_name,b.file_name as file_name_jenis
												FROM pegawai AS a
												LEFT JOIN jenis AS b ON b.idjenis = a.idjenis
												WHERE a.idpegawai = '$id'
												ORDER BY a.namapegawai ASC													
												");		
		return $query;
		}

		function filelama($id){
		 $id;
		$query = $this->db->query("SELECT * FROM `pegawai`
													WHERE idpegawai = '$id' ");		
		$row = $query->row();     		
		return $row;
		}
		
		function upload(){ 
		$id_admin = '1';
		$b = base_url().'/admin/pegawai/add';
		$r =array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',','.pdf',' ','.','%','&','+','-','.xls');
		$r2 ='_'; 				
		$filename= $_FILES["input_gambar"]["name"];
		$filename_new= substr($filename, 0, -4);
		$file_ext = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
		//$string = '.xls';

		if(strtolower(str_replace(array('mp3','wav','mp4'),'', $file_ext)) == $file_ext){
		$exok = 'false';
		$error = 'error';
		echo '<script language="javascript">alert("Ada kesalahan, jika membuat archive, buat file berformat ZIP '.$error.'"); document.location="'.$b.'";</script>';   	
		//$return = array('result' => 'failed', 'file' => '', 'error' => $this->upload->display_errors());        
		//return $return; 	
		}else{
		$exok = 'true';
		$file_size= $_FILES["input_gambar"]["size"];
		$config['upload_path'] = './images/pegawai/';   
		//$config['allowed_types'] = 'jpg|png|jpeg|PNG|pdf|pptx|docx|ppt|zip|mp4|mpeg|mpg|mpe|avi|mp3|xlsx|csv|xls'; //type yang dapat diakses bisa anda sesuaikan
		$config['allowed_types'] = '*';
		//$config['max_size']  = '20000';          
		$config['remove_space'] = TRUE;   
		$new_name = str_replace($r, $r2, $id_admin.'-'.date('ymds').'-'.$filename_new).'.'.$file_ext;
		$config['file_name'] = $new_name;		 
		$this->load->library('upload', $config); 
		// Load konfigurasi uploadnya      
		//Chek apakah di dalam file mengandung kata php atau script yg dilarang
		$file = $_FILES['input_gambar']['tmp_name'];
		$searchfor	= array('<?php','script');			
		// get the file contents, assuming the file to be readable (and exist)
		$contents = file_get_contents($file);

		$berbahaya = false;
		foreach ($searchfor as $searchfor) {
		  if (preg_match("/\b$searchfor\b/", $contents)) {
			$berbahaya = true;
			break;
		  }
		}	
				
		if($this->upload->do_upload('input_gambar') AND $berbahaya == false AND $exok=='true'){ 
		// Lakukan upload dan Cek jika proses upload berhasil          
		// Jika berhasil :    
		$return = array('result' => 'success', 'file' => $this->upload->data(), 'error' => '','namafilebaru' =>$new_name, 'file_ext' => $file_ext, 'file_size' => $file_size);       
		return $return;   
		}else{             
		// Jika gagal :
		$return = array('result' => 'failed', 'file' => '', 'error' => $this->upload->display_errors());        
		return $return;         
		}       
		}
		}
		
		function in($data_save,$table){
					$this->db->insert($table,$data_save);
		} 	
		function update($where,$data,$table){
				$this->db->where($where);
				$this->db->update($table,$data);
		}	
			function hapus($where,$table){
				$this->db->where($where);
				$this->db->delete($table);
		}			
}