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