<?php 
class Model_pengumuman extends CI_model{

//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_(){
        return $this->db->query("SELECT * from pengumuman ORDER BY tgl_posting DESC");
    }
	
    function tambah_baru(){
      
			$config['upload_path'] = 'asset/foto_pengumuman/';
            $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG';
            $config['max_size'] = '3000'; // kb
            $this->load->library('upload', $config);
            $this->upload->do_upload('c');
            $hasil=$this->upload->data();
            if ($hasil['file_name']==''){
                    $datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
                                    'judul_seo'=>seo_title($this->input->post('a')),
                                    'isi_pengumuman'=>$this->input->post('b'),
                                    'tgl_posting'=>date('Y-m-d'),
                                    'username'=>$this->session->username,
                                    'dibaca'=>'0',
                                    'jam'=>date('H:i:s'),
                                    'hari'=>hari_ini(date('w')));
            }else{
            		$datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
                                    'judul_seo'=>seo_title($this->input->post('a')),
                                    'isi_pengumuman'=>$this->input->post('b'),
                                    'tgl_posting'=>date('Y-m-d'),
                                    'gambar'=>$hasil['file_name'],
                                    'username'=>$this->session->username,
                                    'dibaca'=>'0',
                                    'jam'=>date('H:i:s'),
                                    'hari'=>hari_ini(date('w')));
            }
        $this->db->insert('pengumuman',$datadb);
		$title = "Tambah Data Pengumuman <br />dengan Judul [id=".$this->input->post('a')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'pengumuman');
	}
	function simpan_update(){
		$config['upload_path'] = 'asset/foto_pengumuman/';
            $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG';
            $config['max_size'] = '3000'; // kb
            $this->load->library('upload', $config);
            $this->upload->do_upload('c');
            $hasil=$this->upload->data();
            if ($hasil['file_name']==''){
                    $datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
                                    'judul_seo'=>seo_title($this->input->post('a')),
                                    'isi_pengumuman'=>$this->input->post('b'),
                                    'tgl_posting'=>date('Y-m-d'),
                                    'username'=>$this->session->username,
                                    'dibaca'=>'0',
                                    'jam'=>date('H:i:s'),
                                    'hari'=>hari_ini(date('w')));
            }else{
            		$datadb = array('judul'=>$this->db->escape_str($this->input->post('a')),
                                    'judul_seo'=>seo_title($this->input->post('a')),
                                    'isi_pengumuman'=>$this->input->post('b'),
                                    'tgl_posting'=>date('Y-m-d'),
                                    'gambar'=>$hasil['file_name'],
                                    'username'=>$this->session->username,
                                    'dibaca'=>'0',
                                    'jam'=>date('H:i:s'),
                                    'hari'=>hari_ini(date('w')));
            }
		$this->db->where('id_pengumuman',$this->input->post('id'));
        $this->db->update('pengumuman',$datadb);
		$title = "Update Data pengumuman <br />dengan Judul [id=".$this->input->post('a')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'pengumuman');
    }
	function list_edit($id){
        return $this->db->query("SELECT * from pengumuman WHERE id_pengumuman='$id'");
    }
	 function hapus_data($id){
        return $this->db->query("DELETE FROM tb_gelar_pendidikan where gelar_pendidikan_id='$id'");
		$title = "Hapus Data Gelar <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("DELETE",$title,$descrip,'tb_gelar_pendidikan');
    }
	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) {
		}
	}

}