<?php
class M_client extends CI_Model{
		function tampil(){
			$this->db->select(' a.idlist, a.namalist, a.deskripsi, b.filename, b.idfile, a.status ');
			$this->db->from('list as a  ');
			$this->db->join('file as b ', 'b.nmtable ="list" ');
			$this->db->where('b.nmfield', "idlist");
			$this->db->where('b.nmvalue = a.idlist');
			$this->db->where('a.fungsi', 'client');	
			$query = $this->db->get();						   		
			return $query;
		}		
		function tampilhome(){
			$this->db->select(' a.idlist, a.namalist, a.deskripsi, b.filename, b.idfile, a.status ');
			$this->db->from('list as a  ');
			$this->db->join('file as b ', 'b.nmtable ="list" ');
			$this->db->where('b.nmfield', "idlist");
			$this->db->where('b.nmvalue = a.idlist');
			$this->db->where('a.fungsi', 'client');	
			$this->db->where('a.status', '1');	
			$query = $this->db->get();						   		
			return $query;
		}			
		function cekidlist($tipe){
			$this->db->select('idlist');
			$this->db->from('list');						
			$this->db->order_by('idlist DESC');
			$this->db->LIMIT('1');
			$query = $this->db->get();			   
			$row = $query->row();		
			return $row;		
		}	
	  
		function upload($tipe){		
		$config['upload_path'] = './images/';
		$config['allowed_types'] = 'jpg|png|jpeg|PNG|';
		$config['max_size']  = '10048';
		$config['remove_space'] = TRUE;
		//$config['encrypt_name'] = TRUE;
		//$search =array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',',' ','%','&','+');
		//$replace ='_'; 	
		//$new_name = date('Ymdhi').str_replace($search,$replace,$_FILES["input_gambar"]['name']);
	
		/*	$nama = strlen($_FILES["input_gambar"]);
		if($nama <=5 ){
		$array = explode('.', $_FILES['input_gambar']['name']);
		$extension = end($array);	
		$new_name = date('Ymdhi').'XXX'.$array;
		}else{
		}
		*/
		$new_name = date('Ymdhi').preg_replace("/[^a-zA-Z0-9.]/", "_", $_FILES["input_gambar"]['name']);	

		$config['file_name'] = $new_name;	
		//$filename = $data['file_name'];
		$this->load->library('upload', $config); // Load konfigurasi uploadnya	
		if($this->upload->do_upload('input_gambar')){ // Lakukan upload dan Cek jika proses upload berhasil
		
		$gbr = $this->upload->data();
		//Compress Image
		
		$config['image_library']='gd2';
		$config['source_image']='./images/'.$new_name;
		$config['create_thumb']= FALSE;
		$config['maintain_ratio']= FALSE;
		$config['quality']= '50%';
		$config['width']= 400;
		$config['height']= 173;
		$config['new_image']= './images/'.$gbr['file_name'];
	
		$this->load->library('image_lib', $config);
		$this->image_lib->resize();
		$this->image_lib->initialize($config);	
		if(	$config['height'] & $config['width']){	
		$return = array('result' => 'success', 'file' => $this->upload->data(), 'error' => '','namafilebaru' =>$gbr['file_name']);        
		return $return;
		}else{
		// Jika gagal :
		$return = array('result' => 'failed', 'file' => '', 'error' => $this->upload->display_errors());
		return $return;		
			
		}
		}else{
		// Jika gagal :
		$return = array('result' => 'failed', 'file' => '', 'error' => $this->upload->display_errors());
		return $return;
		}
	  }		
		function filelama($idfile){
			$this->db->select('filename');
			$this->db->from('file');						
			$this->db->where('idfile', $idfile);	
			$query = $this->db->get();			   
			$row = $query->row();		
			return $row;	
		}	  
		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);
			}				
}