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

 public function __construct()
 {
  parent::__construct();
       //  $this->db2 = $this->load->database('dbperkara', TRUE);
 }
//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_($awal, $akhir){
        return $this->db->query("SELECT * from download WHERE kategori='GUGATAN' ORDER BY id DESC LIMIT $awal, $akhir");
    }
	 function list_bas($awal, $akhir){
        return $this->db->query("SELECT A.*,B.nama_pegawai from download A 
								LEFT JOIN pegawai B ON A.pengirim=B.nip 
								WHERE A.kategori='BAS' ORDER BY A.id DESC LIMIT $awal, $akhir");
    }
	function list_putusan($awal, $akhir){
        return $this->db->query("SELECT A.*,B.nama_pegawai from download A 
								LEFT JOIN pegawai B ON A.pengirim=B.nip 
								WHERE A.kategori='PUTUSAN' ORDER BY A.id DESC LIMIT $awal, $akhir");
    }
	function list_putusan_anonim($awal, $akhir){
        return $this->db->query("SELECT A.*,B.nama_pegawai from download A 
								LEFT JOIN pegawai B ON A.pengirim=B.nip 
								WHERE A.kategori='ANONIM' ORDER BY A.id DESC LIMIT $awal, $akhir");
    }
	function list_arsip_lain($awal, $akhir){
        return $this->db->query("SELECT A.*,B.nama_pegawai from download A 
								LEFT JOIN pegawai B ON A.pengirim=B.nip 
								WHERE A.kategori='LAIN' ORDER BY A.id DESC LIMIT $awal, $akhir");
    }
    function tambah_(){
		$allowed_ext	= array('doc', 'docx','rtf', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'rar', 'zip');
				$file_name		= $_FILES['file']['name'];
				$file_ext		= strtolower(end(explode('.', $file_name)));
				$file_size		= $_FILES['file']['size'];
				$file_tmp		= $_FILES['file']['tmp_name'];
								
				$namaawal			= $this->input->post('a');
				$ubahnama		=  explode("/",$namaawal);
				$gabungx		=  implode("-",$ubahnama);
				$ubahnamax		=  explode(".",$gabungx);
				$nama		=  implode("-",$ubahnamax);
				
				
				$tgl			= date('Y-m-d');
				$kpemohon		= $this->input->post('a');
				$ktermohon		= $this->input->post('b');
				if(in_array($file_ext, $allowed_ext) === true){
					if($file_size = 9000000){
						$lokasi = 'arsip_posbakum/'.$nama.'.'.$file_ext;
						move_uploaded_file($file_tmp, $lokasi);
						$in = $this->db->query("INSERT INTO download (id,tanggal_upload,nama_file,tipe_file,ukuran_file,file,pengirim,kategori,namap,namat) VALUES(NULL, '$tgl', '$nama', '$file_ext', '$file_size', '$lokasi', 'POSBAKUM', 'GUGATAN','$kpemohon','$ktermohon' )");
						
						if($in){
							echo '<div class="red">SUCCESS: File berhasil di Upload!</div>';
						}else{
							echo '<div class="error">ERROR: Gagal upload file!</div>';
						}
					}else{
						echo '<div class="error">ERROR: Besar ukuran file (file size) maksimal 3 Mb!</div>';
					}
				}else{
					echo '<div class="error">ERROR: Ekstensi file tidak di izinkan!</div>';
				}
		/* $config['upload_path'] = 'asset/arsip_posbakum/';
        $config['allowed_types'] = 'rtf|docx|doc|pdf|xlsx';
        $config['max_size'] = '10024'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('fileku');
        $hasil=$this->upload->data();
            
            if ($hasil['file_name']==''){
                    $datadb = array('namat'=>$this->db->escape_str($this->input->post('a')),
									'namap'=>$this->input->post('b'),
									'pengirim'=>'Posbakum',
									'kategori'=>'GUGATAN',
									'tanggal_upload'=>date('Y-m-d'),
                                    'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            }else{
                     $datadb = array('namat'=>$this->db->escape_str($this->input->post('a')),
									'namap'=>$this->input->post('b'),
									'pengirim'=>'Posbakum',
									'kategori'=>'GUGATAN',
									'nama_file'=>$hasil['file_name'],
									'tanggal_upload'=>date('Y-m-d'),
                                    'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
					 
            }
        $this->db->insert('download',$datadb);
			$title = "Tambah Data POSBAKUM <br />dengan ID [id=".$this->input->post('c')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'download'); */
    }
	function list_edit($id){
        return $this->db->query("SELECT * from tb_konsentrasi_mata_kuliah WHERE kelompok_id='$id'");
    }
	
	function list_cari($cari){
        return $this->db->query("SELECT * FROM download 
								 WHERE namap LIKE '%$cari%' OR namat LIKE '%$cari%' OR nama_file LIKE '%$cari%'
								 ORDER BY id DESC");
    }
	function list_cari_bas($cari){
        return $this->db->query("SELECT A.*,B.nama_pegawai FROM download A
									LEFT JOIN pegawai B ON A.pengirim=B.nip 
								 WHERE B.nama_pegawai LIKE '%$cari%' OR A.nama_file LIKE '%$cari%'
								 ORDER BY A.id DESC");
    }
	function list_cari_putusan($cari){
        return $this->db->query("SELECT A.*,B.nama_pegawai FROM download A
									LEFT JOIN pegawai B ON A.pengirim=B.nip 
								 WHERE B.nama_pegawai LIKE '%$cari%' OR A.nama_file LIKE '%$cari%'
								 ORDER BY A.id DESC");
    }
	function list_cari_lainnya($cari){
        return $this->db->query("SELECT A.*,B.nama_pegawai FROM download A
									LEFT JOIN pegawai B ON A.pengirim=B.nip 
								 WHERE B.nama_pegawai LIKE '%$cari%' OR A.nama_file LIKE '%$cari%'
								 ORDER BY A.id DESC");
    }
	function getCari($cari){
		try {
			$this->db->where('kode_kelompok',$cari);
			$this->db->where('nama',$cari);
			$this->db->select('*');
			return $this->db->get('tb_konsentrasi_mata_kuliah');
		} catch (Exception $e) {
			return '';
		}
	}
	 public function edit($table, $data){
        return $this->db->get_where($table, $data);
    }
	 function hapus_data($id){
        return $this->db->query("DELETE FROM tb_master_akun where akun_id='$id'");
		
    }
	function deleteData($table,$data){
		$this->db->delete($table,$data);
	}
	function list_update(){
		
		$datadb = array('kode_kelompok'=>$this->db->escape_str($this->input->post('a')),
						'nama'=>$this->db->escape_str($this->input->post('b')),
						'diperbaharui_tanggal'=>date('Y-m-d H:i:s'),
						'diperbaharui_oleh'=>$this->session->admin_user);
		
        $this->db->where('kelompok_id',$this->input->post('id'));
        $this->db->update('tb_konsentrasi_mata_kuliah',$datadb);
			$title = "Update Data Master Kelompok Mata Kuliah <br />dengan ID [id=".$this->input->post('id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'tb_konsentrasi_mata_kuliah');
    }
	function list_upload_file(){
        $config['upload_path'] = 'asset/arsip_posbakum/';
        $config['allowed_types'] = 'doc|rtf|docx|pdf|rar';
        $config['max_size'] = '3072'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('file');
        $hasil=$this->upload->data();
		$hasilupload='asset/arsip_posbakum/'.$this->input->post('file');
            $datadb = array('nama_file'=>$hasil['file_name'],
									'file'=>$hasilupload,
									'namap'=>$this->input->post('a'),
									'namat'=>$this->input->post('b'),
									'pengirim'=>'Posbakum',
									'kategori'=>'GUGATAN',
									'tanggal_upload'=>date('Y-m-d'),
                                    'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            
        $this->db->insert('download', $datadb);
       
		
    }
	function list_upload_filebas(){
        $config['upload_path'] = 'asset/arsip_bas/';
        $config['allowed_types'] = 'doc|rtf|docx|pdf|rar';
        $config['max_size'] = '3072'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('file');
        $hasil=$this->upload->data();
		$hasilupload='asset/arsip_bas/'.$this->input->post('file');
            $datadb = array('nama_file'=>$hasil['file_name'],
									'file'=>$hasilupload,
									'noperk'=>$this->input->post('nomor_perkara'),
									'namap'=>$this->input->post('pihak1_text'),
									'namat'=>$this->input->post('pihak2_text'),
									'pengirim'=>$this->session->pengguna_id,
									'kategori'=>'BAS',
									'tanggal_upload'=>date('Y-m-d'),
                                    'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            
        $this->db->insert('download', $datadb);
       
		
    }
	function list_upload_putusan(){
        $config['upload_path'] = 'asset/arsip_putusan/';
        $config['allowed_types'] = 'doc|rtf|docx|pdf|rar';
        $config['max_size'] = '3072'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('file');
        $hasil=$this->upload->data();
		$hasilupload='asset/arsip_putusan/'.$this->input->post('file');
            $datadb = array('nama_file'=>$hasil['file_name'],
									'file'=>$hasilupload,
									'noperk'=>$this->input->post('nomor_perkara'),
									'namap'=>$this->input->post('pihak1_text'),
									'namat'=>$this->input->post('pihak2_text'),
									'pengirim'=>$this->session->pengguna_id,
									'kategori'=>'PUTUSAN',
									'tanggal_upload'=>date('Y-m-d'),
                                    'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            
        $this->db->insert('download', $datadb);
       
		
    }
	function list_upload_anonim(){
        $config['upload_path'] = 'asset/arsip_anonim/';
        $config['allowed_types'] = 'doc|rtf|docx|pdf|rar';
        $config['max_size'] = '3072'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('file');
        $hasil=$this->upload->data();
		$namafile = $hasil['file_name'];
		$hasilupload='asset/arsip_anonim/'.$namafile;
            $datadb = array('nama_file'=>$hasil['file_name'],
									'file'=>$hasilupload,
									'noperk'=>$this->input->post('nomor_perkara'),
									'namap'=>$this->input->post('pihak1_text'),
									'namat'=>$this->input->post('pihak2_text'),
									'pengirim'=>$this->session->pengguna_id,
									'kategori'=>'ANONIM',
									'tanggal_upload'=>date('Y-m-d'),
                                    'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            
        $this->db->insert('download', $datadb);
       
		
    }
	function list_upload_lain(){
        $config['upload_path'] = 'asset/arsip_lain/';
        $config['allowed_types'] = 'doc|rtf|docx|pdf|rar|zip';
        $config['max_size'] = '3072'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('file');
        $hasil=$this->upload->data();
		$namafile = $hasil['file_name'];
		$hasilupload='asset/arsip_lain/'.$namafile;
            $datadb = array('nama_file'=>$hasil['file_name'],
									'file'=>$hasilupload,
									'pengirim'=>$this->session->pengguna_id,
									'kategori'=>'LAIN',
									'tanggal_upload'=>date('Y-m-d'),
                                    'diinput_tanggal'=>date('Y-m-d H:i:s'),
                                    'diinput_oleh'=>$this->session->username);
            
        $this->db->insert('download', $datadb);
       
		
    }
	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) {
		}
	}

}