<?php 
class Model_kecamatan extends CI_model{
//private $db2;

 public function __construct()
 {
  parent::__construct();
       //  $this->db2 = $this->load->database('dbperkara', TRUE);
 }
//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_($awal, $akhir,$ceknamasatker){
        return $this->db->query("SELECT * from tb_kelurahan_komdanas WHERE satker_name ='$ceknamasatker' ORDER BY id DESC LIMIT $awal, $akhir");
    }
	function tambah_baru(){
	   $datadb = array('nama_kec'=>$this->db->escape_str($this->input->post('a')),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->username);
		
        $this->db->insert('kecamatan',$datadb);
		$title = "Tambah Data Kecamatan <br />dengan ID [id=".$this->input->post('a')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'kecamatan');
			
    }
	function list_detail($id){
        return $this->db->query("SELECT * FROM kecamatan
								WHERE id_kec ='$id'");
    }
	function list_update(){
	 $datadb = array('nama_kec'=>$this->db->escape_str($this->input->post('a')),
						'tanggal_perubahan'=>date('Y-m-d H:i:s'),
					'username_perubahan'=>$this->session->username);
	$this->db->where('id_kec',$this->input->post('id'));
	$this->db->update('kecamatan',$datadb);
	$title = "Update Data Kecamatan <br />dengan ID [id=".$this->input->post('id')."]";
		$descrip = $this->fetch_description($title,$datadb);
		$this->add_audittrail("UPDATE",$title,$descrip,'kecamatan');
			
    }
	function list_cari($cari){
        return $this->db->query("SELECT * FROM kecamatan 
								 WHERE nama_kec LIKE '%$cari%'
								 ORDER BY id_kec DESC");
    }
//xxxxxxxxxxxxxxxxxxxxxxxxx

	function fetch_description($title,$data){
		$descrip = '<br><table style="vertical-align:top" cellspacing="0" cellpadding="1" border="1">';
		$descrip .= '<tr><th>Nama Kolom</th><th>Nilai</th></tr>';
		foreach ($data as $key => $value) {
			$descrip .= '<tr>';
			$descrip .= '<td>'.$key.'</td>';
			$descrip .= '<td>'.$value.'</td>';
			$descrip .= '</tr>';
		}
		$descrip .= '</table>';
		return $descrip;
	}

	function add_audittrail($action,$title,$descrip,$tablename){
		try {
			$data = array(
				'datetime' => date("Y-m-d H:i:s"),
				'ipaddress' => $this->input->ip_address(),
				'username' => $this->session->userdata('username'),
				'tablename' => $tablename,
				'formname' => 'by komar',
				'action' => $action,
				'title' => $title,
				'description' => $descrip
			);
			$this->db->insert('sys_audittrail', $data);
		} catch (Exception $e) {
		}
	}

}