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