<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ruang_sidang extends CI_Model {
	function parseOrderby($col){
		if($col==1){
			return "kode"; 
		}else if($col==2){
			return "nama"; 
		}else if($col==3){
			return "aktif"; 
		}else{
			return "nama";
		}
	}

	function get_ruang_list($col=2,$asc_desc='ASC',$begin=0,$key=''){
		$orderby = $this->parseOrderby($col);
		$where = '';
		if(!empty($key)){
			$where .= ' WHERE (nama LIKE "%'.$key.'%" OR keterangan LIKE "%'.$key.'%") ';
		}
		try {
			return $this->db->query("SELECT SQL_CALC_FOUND_ROWS id, kode,
				nama, keterangan,IF(aktif='T','Tidak','Aktif') AS aktif_text, aktif
				FROM ruangan_sidang ".$where." ORDER BY ".$orderby." ".$asc_desc." LIMIT ".$begin.",50;");
		} catch (Exception $e) {
			
		}
	}

	function fecth_data_num(){
		$query = $this->db->query("SELECT FOUND_ROWS() as cnt;");
		if($query->num_rows>0){
			foreach ($query->result() as $row) {
				return $row->cnt;
			}
    		return $query->num_rows;
    	}else{
    		return 0;
    	}
	}


	function get_ruang_info($id){
		if(empty($id)){
			return "";
		}
		try {
			return $this->db->query("SELECT id,	kode, nama,
				IF(aktif='T',0,1) AS aktif, keterangan FROM ruangan_sidang where id=".$id);
		} catch (Exception $e) {
			
		}
	}

	

	function get_last_id(){
		try {
			$this->db->select_max('id');
			$query = $this->db->get('ruangan_sidang');
			if ($query->num_rows() > 0){
				return $query->row();
			}else{
				return 0;
			}		
		} catch (Exception $e) {
			
		}
	}

	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){
		try {
			$data = array(
				'datetime' => date("Y-m-d H:i:s"),
				'ipaddress' => $this->input->ip_address(),
				'username' => $this->session->userdata('username'),
				'tablename' => 'ruangan_sidang',
				'formname' => 'Data Ruang Sidang',
				'action' => $action,
				'title' => $title,
				'description' => $descrip
			);
			$this->db->insert('sys_audittrail', $data);
		} catch (Exception $e) {
			
		}
	}

	function add_data($data){
		try {
			$this->db->insert('ruangan_sidang', $data);
			$title = "Tambah ruangan_sidang [Ruangan=<b>".$data['nama']."</b>]<br />Tambah table <b>ruangan_sidang</b> dari halaman <b>ruangan_sidang</b> dengan Primary Key [id=".$data['id']."]";
			$descrip = $this->fetch_description($title,$data);
			$this->add_audittrail("INSERT",$title,$descrip);
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function update_data($data,$id){
		try {
			$this->db->where('id', $id);
			$this->db->update('ruangan_sidang', $data); 
			$title = "Edit Data ruangan_sidang [Ruangan=<b>".$data['nama']."</b>]<br />Edit table <b>ruangan_sidang</b> dari halaman <b>ruangan_sidang</b> dengan Primary Key [ID Hakim=".$id."]";
			$descrip = $this->fetch_description($title,$data);
			$this->add_audittrail("UPDATE",$title,$descrip);
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}


}
?>