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