<?php
class M_fasilitas extends CI_Model{
		function tampil(){
			$this->db->select('a.idfasilitas, a.namafasilitas, a.harga, a.status, a.idkategori, b.namakategori, a.waktu, a.umum');
			$this->db->from('fasilitas as a ');
			$this->db->join('kategori as b', ' b.idkategori = a.idkategori ', 'left');
			$this->db->where('a.aktif', 'Y');
			$this->db->order_by('a.waktu ASC');			
			$query = $this->db->get();				
			return $query ;
		}	
		function fasilitas(){
			$query = $this->db->get_where('fasilitas',array('status'=>'1'));							
			return $query ;
		}	
		function idfasilitasdesc(){
			$this->db->select('idfasilitas');
			$this->db->from('fasilitas');	
			$this->db->where('aktif', 'Y');				
			$this->db->order_by('idfasilitas DESC');		
			$query = $this->db->get();			   
			$row = $query->row();		
			return $row;
		}			
		function tampilwaktu($waktu){
			$this->db->select('a.idfasilitas, a.namafasilitas, a.harga, a.status, a.idkategori, b.namakategori, a.waktu, a.umum');
			$this->db->from('fasilitas as a ');
			$this->db->join('kategori as b', ' b.idkategori = a.idkategori ', 'left');
			$this->db->where('a.waktu', $waktu);				
			$this->db->where('a.aktif', 'Y');				
			$this->db->order_by('a.namafasilitas ASC');			
			$query = $this->db->get();				
			return $query ;						
		}			
		function edit($idfasilitas){
			$query = $this->db->get_where('fasilitas',array('idfasilitas'=>$idfasilitas));							
			return $query ;
		}
		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 fileupload($idfasilitas){
			$this->db->select('a.namafasilitas, b.filename, b.idfile, b.deskripsi');
			$this->db->from('fasilitas as a ');
			$this->db->join('file as b', ' b.nmvalue = a.idfasilitas');
			$this->db->where(' b.nmtable ', 'fasilitas');
			$this->db->where(' b.nmfield ', 'idfasilitas');
			$this->db->where(' b.nmvalue ',$idfasilitas);
			$this->db->where('a.aktif', 'Y');
			$this->db->order_by('b.idfile DESC');
			//$this->db->limit('10');
			$query = $this->db->get();						   	
			return $query;
		}
		/*
		function upload($tipe){
		$config['upload_path'] = './images/';
		$config['allowed_types'] = 'jpg|png|jpeg|PNG|';
		$config['max_size']  = '3048';
		$config['remove_space'] = TRUE;
		$config['create_thumb']= FALSE;
		$config['maintain_ratio']= FALSE;
		$config['quality']= '50%';
		$config['width']= 800;
		$config['height']= 529;	  
		$this->load->library('upload', $config); // Load konfigurasi uploadnya	
		if($this->upload->do_upload('input_gambar')){ // Lakukan upload dan Cek jika proses upload berhasil
		  // Jika berhasil :
			$file_gambar	=str_replace(" ","_",$_FILES["input_gambar"]['name']);
			$file_name	=str_replace(" ","_",$_FILES["input_gambar"]['name']);
			//KONVERSI KE WEBP				
			$search =array('.jpg','.jpeg','.png','.JPG','.PNG','\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',','.pdf',' ','.','%','&','+');
			$replace ='_'; 		 
			//$im = imagecreatefromstring(file_get_contents('path/to/image.jpg')); 
			// Create image identifier		
			$im = imagecreatefromstring(file_get_contents('images/'.$file_gambar));
			// Create image identifier	
			$ubahnama = $tipe.date('ymdhi').str_replace($search,$replace,$file_name).'.webp';	
			
			imagewebp($im, 'images/webp/'.$ubahnama); // Generate webp image and save to location	
			imagedestroy($im); // Free up image identifier		
			if(file_exists('images/'.$file_gambar)){		
			unlink('images/'.$file_gambar); 		
			}			
			//UPDATE GAMBAR	
			$return = array('result' => 'success', 'file' => $this->upload->data(), 'error' => '','namafilebaru' =>$ubahnama);        
			return $return;
		
		}else{
		  // Jika gagal :
		  $return = array('result' => 'failed', 'file' => '', 'error' => $this->upload->display_errors());
		  return $return;
		}
	  }
	  */
	  
		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']= 700;
		$config['height']= 500;
		$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 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);
		}			
}