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

class Download extends CI_Controller {
	
	public function __construct()
    {
        parent::__construct();
		$this->load->model('login');
		$this->load->model('download_m', 'download');
    }

	
	public function index()
	{
		$a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
		$d['i_judul'] = "Download ".$this->config->item('nama_aplikasi_full');
        
        
        $config['total_rows'] = $this->download->hal($this->session->userdata('hak'));
		$config['per_page'] = 10;
		$config['uri_segment'] = 3;
		$config['full_tag_open'] = '<table id="simple-table" class="table-striped table-bordered table-hover"><thead><tr>';
		$config['full_tag_close'] = '</tr></thead></table>';
		$config['prev_link'] = '<th width="20" align="center">&lt;</th>';
		$config['prev_tag_open'] = '<th width="20" align="center">';
		$config['prev_tag_close'] = '</th>';
		$config['next_link'] = '<th width="20" align="center">&gt;</th>';
		$config['next_tag_open'] = '<th width="20" align="center">';
		$config['next_tag_close'] = '</th>';
		$config['cur_tag_open'] = '<th width=20" align="center"><a href="#" class="pi-active">';
		$config['cur_tag_close'] = '</a></th>';
		$config['num_tag_open'] = '<th width="20" align="center">';
		$config['num_tag_close'] = '</th>';
		 
		$config['first_tag_open'] = '<td width="20" align="center">';
		$config['first_tag_close'] = '</td>';
		$config['last_tag_open'] = '<td width="20" align="center">';
		$config['last_tag_close'] = '</td>';
		 
		$config['first_link'] = '<td width="20" align="center">&lt;&lt;</td>';
		$config['last_link'] = '<td width="20" align="center">&gt;&gt;</td>';
		$config['base_url'] = base_url()."index.php/download/index/";
		$this->pagination->initialize($config);
		
		
		
		$hal = $this->uri->segment(3);
		 if($hal==""){
			 $hal=0;
		}
           
        
		$d['page'] = $this->pagination->create_links();
		$d['no'] = $hal;
        
        $d['blog'] = $this->download->download_semua("", $this->session->userdata('hak'), "limit $hal, $config[per_page]");
        
        
		$this->load->view("theme/header", $d);
		$this->load->view('download/at_tampil',$d);
		$this->load->view("theme/footer");
	}
	
	
	 public function tambah($aksi=null, $id=null)
	{
	   $a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
		
		
        
        
       
        
        $d['id'] = $id;
        
        if($aksi=="edit" and $id!=""){
			$d['i_judul'] = "Edit download";
            $d['judul']=balik2("at_download", "judul", "id", $id, "tenan_id", $this->session->userdata('hak'));
            $d['isi']=balik2("at_download", "isi", "id", $id, "tenan_id", $this->session->userdata('hak'));
            
        }
        elseif($aksi=="" and $id==""){
			$d['i_judul'] = "Tambah Download";
            $d['judul']="";
            $d['isi']="";
            
        }
        
		$this->load->view("theme/header", $d);
		$this->load->view('download/at_tambah',$d);
		$this->load->view("theme/footer");
	}
    
    public function simpan()
	{
	   $a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
		
	   $id = $this->input->post('id');
       $judul = $this->input->post('judul');
       $tipe = $this->session->userdata('hak');
       
       $config['upload_path'] = lokasi_down_14;
	   $config['allowed_types'] = '*';
        $config['file_name']  = "jogdas_".date('YmdHis');
		
		
		
		
		
		
        
		$gambar="";
        if($_FILES["userfile"]["name"]!=""){
		$file_extension = strtolower(substr(strrchr($_FILES["userfile"]["name"],"."),1));

	switch($file_extension){
	  case "pdf": $ctype="application/pdf"; break;
	  case "exe": $ctype="application/octet-stream"; break;
	  case "zip": $ctype="application/zip"; break;
	  case "rar": $ctype="application/rar"; break;
	  case "doc": $ctype="application/msword"; break;
	  case "xls": $ctype="application/vnd.ms-excel"; break;
	  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
	  case "gif": $ctype="image/gif"; break;
	  case "png": $ctype="image/png"; break;
	  case "jpeg":
	  case "jpg": $ctype="image/jpg"; break;
	  default: $ctype="application/proses";
	}	
			
			
			
			
			
			
			
			
			
        $this->load->library('upload', $config);
		if (isset($_FILES["userfile"]["name"])){
            if ( ! $this->upload->do_upload()){
				$d['error']=$this->upload->display_errors().$_FILES["userfile"]["name"];
				$d['f']="";
				$s="";
				
				echo $d['error'];exit;
               
            }else{
                //array_push($data,array('error' => 'success'));
                $tGambar    = $this->upload->data();
                $d['error']="Berhasil di Upload ".$_FILES["userfile"]["name"];
				$d['f']=$config['file_name'].".$file_extension";
				$s="1";
            }
        }else{
			
            $d['error']="";
			$d['f']="";
        }
		$gambar=$d['f'];
		}
       if($gambar==""){
        $gambar=balik("at_download", "isi", "id", $id);
       }
       
       //echo $isi_ind;
	   //exit;
       
       if($id==""){
        $this->download->simpan($judul, $gambar, $tipe);
        echo"<html><head><meta http-equiv='refresh' content='0;url=".base_url()."index.php/download'></head><body></body></html>";
       }
       if($id!=""){
        if($_FILES["userfile"]["name"]!=""){
            $gambar2=balik("at_download", "isi", "id", $id);
        	unlink(lokasi_down_14."$gambar2");
       }
        $this->download->editsimpan($id, $judul, $gambar, $tipe);
        echo"<html><head><meta http-equiv='refresh' content='0;url=".base_url()."index.php/download'></head><body></body></html>";
       }
       
       
       
    }
    
    public function hapus($id=null)
	{
		$a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
        $tipe=balik("at_download", "tenan_id", "id", $id);
        $gambar2=balik("at_download", "isi", "id", $id);
        unlink(lokasi_down_14."$gambar2");
		$this->download->hapus($id, $this->session->userdata('hak'));
        
		echo "<script>alert('Data berhasil dihapus')</script>";
		echo"<html><head><meta http-equiv='refresh' content='0;url=".base_url()."index.php/download'></head><body></body></html>";
	}
	
	
	
	
	
	
	
}
