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