<?php 
class Model_identitas extends CI_model{

//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_(){
        return $this->db->query("SELECT * from identitas LIMIT 1");
    }
	function list_update(){
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG';
        $config['max_size'] = '3000'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('file_name');
        $hasil=$this->upload->data();
            if ($hasil['file_name']==''){
                    $datadb = array('nama_website'=>$this->input->post('nama_app'),
									'alamat'=>$this->input->post('alamat'),
									'email'=>$this->input->post('email'),
									'nama_kota'=>$this->input->post('nama_kota'),
									'pemilik_rekening'=>$this->input->post('pemilik_rekening'),
									'nama_bank'=>$this->input->post('nama_bank'),
									'nama_kasir'=>$this->input->post('nama_kasir'),
									'nip_kasir'=>$this->input->post('nip_kasir'),
									'biaya_pos'=>$this->input->post('biaya_pos'),
									'nomor_rekening'=>$this->input->post('nomor_rekening'),
									'sambutan'=>$this->input->post('sambutan'),
									'no_telp'=>$this->input->post('telp'),
									'nama_pimpinan'=>$this->input->post('nama_pimpinan'),
									'nama_app'=>$this->input->post('nama_app'),
									'nama_satker'=>$this->input->post('nama_yayasan'));
            }else{
                    $datadb = array('nama_website'=>$this->input->post('nama_app'),
									'nama_satker'=>$this->input->post('nama_yayasan'),
									'alamat'=>$this->input->post('alamat'),
									'nama_kota'=>$this->input->post('nama_kota'),
									'pemilik_rekening'=>$this->input->post('pemilik_rekening'),
									'nama_bank'=>$this->input->post('nama_bank'),
									'biaya_pos'=>$this->input->post('biaya_pos'),
									'nama_kasir'=>$this->input->post('nama_kasir'),
									'nip_kasir'=>$this->input->post('nip_kasir'),
									'nomor_rekening'=>$this->input->post('nomor_rekening'),
									'sambutan'=>$this->input->post('sambutan'),
									'email'=>$this->input->post('email'),
									'no_telp'=>$this->input->post('telp'),
									'nama_pimpinan'=>$this->input->post('nama_pimpinan'),
									'nama_app'=>$this->input->post('nama_app'),
									'logo'=>$hasil['file_name']);
				}
        $this->db->where('id_identitas',$this->input->post('idp'));
        $this->db->update('identitas',$datadb);
		$title = "Update Data Mahasiswa <br />dengan ID [id=".$this->input->post('idp')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'tb_mahasiswa');
			
    }
    function tambah_baru(){
      
		$datadb = array('nama_gedung'=>$this->db->escape_str($this->input->post('a')),
						'nama_ruangan'=>$this->db->escape_str($this->input->post('b')),
						'kapasitas'=>$this->input->post('c'),
						'keterangan'=>$this->db->escape_str($this->input->post('d')),
						'kondisi'=>$this->input->post('e'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->username);
		$this->db->insert('tb_ruangan',$datadb);
    }
	function list_edit($id){
		$title = "Tambah Data Skripsi Mahasiswa <br />dengan ID [id=".$this->input->post('id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'tb_skripsi');
        return $this->db->query("SELECT * from tb_ruangan WHERE ruangan_id='$id'");
    }
	 function hapus_data($id){
        return $this->db->query("DELETE FROM tb_ruangan where ruangan_id='$id'");
		
    }
	function list_detail($id){
        return $this->db->query("SELECT A.*,B.keterangan AS agama FROM tb_mahasiswa A LEFT JOIN tb_agama B ON A.agama_id=B.agama_id
								where A.mahasiswa_id='$id'")->result_array();
    }
	
	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) {
		}
	}

}