<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pesan extends CI_Controller {
	function __construct() {
		parent::__construct();
		if($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == ""){
          redirect("error");
       }
	   $this->load->model('app_model');
	   $this->load->model('model_utama');
	   $this->load->model('model_arsip_posbakum');
	}
	
	public function list_data_() {
		if($this->session->userdata('admin_valid')==FALSE){
			redirect("error");
			return;
		}
		$ta = $this->session->userdata('admin_ta');
		   
		$total_row		= $this->db->query("SELECT * FROM download WHERE kategori='LAIN'")->num_rows();
		$per_page		= 10;
		
		$awal	= $this->uri->segment(4); 
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;
		
		$akhir	= $per_page;
		
		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."arsip_lain/list_data_/p");
		
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);
		
		$cari					= addslashes($this->input->post('q'));

	
		if ($mau_ke == "cari") {
			$a['record']		= $this->model_arsip_posbakum->list_cari_lainnya($cari);
			$a['page']		= "arsip_lain/list_";
		} else if ($mau_ke == "add") {
			$a['page']		= "arsip_lain/form_";
		
		} else {
			$a['record'] = $this->model_arsip_posbakum->list_arsip_lain($awal,$akhir);
	
			$a['page']		= "arsip_lain/list_";
		}
		
		$this->load->view('admin/home_menu', $a);
	}
	
	function file_download(){
		$cekid = $this->db->query("SELECT * FROM download WHERE id ='".$this->uri->segment(3)."'");
		foreach ($cekid->result_array() as $row){
			$tifefile = $row['tipe_file'];
			$name = $row['nama_file'];
			//$namedownload = $name.".".$tifefile;
			//$namedownload = $name;
			if(empty($tifefile)){
				$namedownload = $name; 
			}else { 
				
				$namedownload = $name.".".$tifefile;
			}
		 }
		
		if ($cekid->num_rows()<=0){
			redirect('arsip_anonim/list_data_');
		}else{
			$data = file_get_contents("asset/arsip_anonim/".$namedownload);
			force_download($namedownload, $data);
		}
	}
	function upload_file(){
	  $this->load->helper('file');
		$this->load->library('form_validation');
		if (isset($_POST['submit'])){
			 $this->form_validation->set_rules('file', '', 'callback_cek_file');
			 if($this->form_validation->run() == true){
				$this->model_arsip_posbakum->list_upload_lain();
				$this->session->set_flashdata("k", "<div class=\"alert alert-info\">Data Arsip berhasil diupload</div>");
				redirect('arsip_lain/list_data_');
			 }
		}else{
			$a['page']		= "arsip_lain/form_";
			$this->load->view('admin/home_menu', $a);
		}
		redirect('arsip_lain/list_data_');
		$this->load->view('admin/home_menu');
	}

    public function cek_file($str){
		$allowed_mime_type_arr = array('application/pdf','text/rtf','application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip', 'application/msword', 'application/x-zip','application/vnd.ms-office','image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');
        $mime = get_mime_by_extension($_FILES['file']['name']);
        if(isset($_FILES['file']['name']) && $_FILES['file']['name']!=""){
            if(in_array($mime, $allowed_mime_type_arr)){
                return true;
            }else{
				$this->session->set_flashdata("k", "<div class=\"alert alert-warning\" id=\"alert\">ERROR: Ekstensi file tidak di izinkan!</div>");
					
               // $this->form_validation->set_message('cek_file', 'Silahkan pilih hanya file pdf/gif/jpg/png.');
                return false;
            }
        }else{
			$this->session->set_flashdata("k", "<div class=\"alert alert-warning\" id=\"alert\">ERROR: Silakan pilih file untuk diupload</div>");
		
            return false;
        }
    }
	function delete_arsip_(){
		$id = $this->uri->segment(3);
		$ect_id = $this->uri->segment(3);
		$id_enc_ = $this->encrypt->decode(base64_decode($ect_id));
		//echo "ID : ".$id_enc_;exit;
		$cekid = $this->db->query("SELECT * FROM download WHERE id ='".$id_enc_."'");
		foreach ($cekid->result_array() as $row){
			$tifefile = $row['tipe_file'];
			$name = $row['nama_file'];
			if(empty($tifefile)){
				$namedownload = $name; 
			}else { 
				
				$namedownload = $name.".".$tifefile;
			}
		 }
		$path 		= './asset/arsip_lain/'.$namedownload;
			@unlink($path);
			//hapus database
		$this->model_arsip_posbakum->deleteData("download",array('id' => $id_enc_));
		$this->session->set_flashdata("k", "<div class='alert alert-success'>Dokumen Arsip Lain-lain Sukses dihapus..</div>");
		redirect('arsip_lain/list_data_');
		
	}
	
}
