<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Jenis extends CI_Controller {
	function __construct(){
		error_reporting(0);
		parent::__construct();	
			$this->load->library('session');
			$this->load->library('form_validation');
			//$this->load->model('m_jenis');
			$this->load->model('m_jenis');
			$this->load->model('m_bidang');
			if($this->session->userdata('token') !=md5(md5(md5(date('Ymd').'tamu')))){
				redirect(base_url("home"));
			}			
	}
	public function index()
	{
	$data['jenis'] = $this->m_jenis->tampil();
	$this->load->view('admin/jenis', $data);			
	}
	public function add()
	{
	$data['cek']='';
	$data['jenis'] = $this->m_jenis->tampil();
	$data['bidang'] = $this->m_bidang->tampil();
	$this->load->view('admin/jenis-add', $data);				
	}	
	public function save()
	{
		if(isset($_POST['button'])){
		$this->form_validation->set_rules('namajenis','namajenis','required|max_length[100]');			
		$this->form_validation->set_rules('act','act','required');
		$act = $this->security->xss_clean($this->input->post('act'));	
		$a = array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',');		
		$idjenis = str_replace($a,"",$this->security->xss_clean($this->input->post('idjenis')));					
		$namajenis = str_replace($a,"",$this->security->xss_clean($this->input->post('namajenis')));	
		if($idjenis ==''){	
		if($_FILES['input_gambar']['name'] !=''){				
		$upload = $this->m_jenis->upload();
		if($upload['result'] == "success"){
		
		$namafilebaru = $upload['namafilebaru'];	
		$data_save = array(
								'namajenis' => ucwords($namajenis),
								'file_name' => $namafilebaru,
						);	
		$this->m_jenis->in($data_save,'jenis');
		}
		}else{
		$data_save = array(
								'namajenis' => ucwords($namajenis),
								'idbidang' => $idbidang,
						);	
		$this->m_jenis->in($data_save,'jenis');			
			
		}
		
		$data['jenis'] = $this->m_jenis->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Berhasil disimpan';		
		$this->load->view('admin/jenis', $data);				
		}else if($idjenis !=''){
		if($_FILES['input_gambar']['name'] !=''){		
			$upload = $this->m_jenis->upload();
			if($upload['result'] == "success"){	
			$namafilebaru = $upload['namafilebaru'];				
				$data_save = array(
									'namajenis' => ucwords($namajenis),
									'file_name' => $namafilebaru,
								);			
				$where = array(
								'idjenis' => $idjenis
						);
				$this->m_jenis->update($where,$data_save,'jenis');	
			}
		}else{
			$data_save = array(
									'namajenis' => ucwords($namajenis),
									'idbidang' => $idbidang,
							);			
			$where = array(
							'idjenis' => $idjenis
					);
			$this->m_jenis->update($where,$data_save,'jenis');					
		}		
		$data['jenis'] = $this->m_jenis->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Berhasil diperbarui';		
		$this->load->view('admin/jenis', $data);		
		}
		}
		if(isset($_POST['kembali'])){
		$data['alert'] = '';
		$data['pesan'] = '';				
		$data['jenis'] = $this->m_jenis->tampil();
		$this->load->view('admin/jenis', $data);			
		}		
	}	
	
	public function edit()
	{
		$a = array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',');	
		$id = str_replace($a,"",$this->uri->segment(4));
		$data['cek'] ='edit';
		$data['jenis'] = $this->m_jenis->tampil();		
		$data['edit'] = $this->m_jenis->edit($id);
		$this->load->view('admin/jenis-add', $data);							
	}
	
	public function hapus()
	{
		$a = array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',');	
		$id = str_replace($a,"",$this->uri->segment(4));
		//delete file lama 
		$cek= $this->m_jenis->filelama($id);
		$namafilelama = $cek->file_name;
		if(file_exists('./images/jenis/'.$namafilelama)){		
		unlink('./images/jenis/'.$namafilelama); 		
		}
			
		$where = array(
						'idjenis' => $id
				);
		$this->m_jenis->hapus($where,'jenis');
		$data['jenis'] = $this->m_jenis->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Data berhasil dihapus';			
		$this->load->view('admin/jenis', $data);				
		
	}		
}