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

class Laporan extends CI_Controller {
	
	public function __construct()
    {
        parent::__construct();
		$this->load->model('login');
		$this->load->model('laporan_m', 'laporan');
    }

	
	public function index()
	{
		$a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
		$d['i_judul'] = "Laporan ".$this->config->item('nama_aplikasi_full');
        
        
        $config['total_rows'] = $this->laporan->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/laporan/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->laporan->laporan_semua("", $this->session->userdata('hak'), "limit $hal, $config[per_page]");
        
        
		$this->load->view("theme/header", $d);
		$this->load->view('laporan/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 Berita";
            $d['judul_ind']=balik2("at_berita", "judul_ind", "id", $id, "tipe", $this->session->userdata('hak'));
            $d['judul_ing']=balik2("at_berita", "judul_ing", "id", $id, "tipe", $this->session->userdata('hak'));
            $d['isi_ind']=balik2("at_berita", "isi_ind", "id", $id, "tipe", $this->session->userdata('hak'));
            $d['isi_ing']=balik2("at_berita", "isi_ing", "id", $id, "tipe", $this->session->userdata('hak'));
            $d['tanggal']=balik2("at_berita", "tanggal", "id", $id, "tipe", $this->session->userdata('hak'));
            $d['foto']=balik2("at_berita", "foto", "id", $id, "tipe", $this->session->userdata('hak'));
			$d['oleh']=balik2("at_berita", "oleh", "id", $id, "tipe", $this->session->userdata('hak'));
			$d['depan']=balik2("at_berita", "urut", "id", $id, "tipe", $this->session->userdata('hak'));
        }
        elseif($aksi=="" and $id==""){
			$d['i_judul'] = "Tambah Berita";
            $d['judul_ind']="";
            $d['judul_ing']="";
            $d['isi_ind']="";
            $d['isi_ing']="";
            $d['tanggal']="";
            $d['foto']="";
			$d['oleh']="";
			$d['depan']="";
            
        }
        
		$this->load->view("theme/header", $d);
		$this->load->view('berita/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_ing = $this->input->post('judul_ing');
       $judul_ind = $this->input->post('judul_ind');
       $isi_ind = $this->input->post('isi_ind');
       $isi_ing = $this->input->post('isi_ing');
       $tanggal = $this->input->post('tanggal');
	   $oleh = $this->input->post('oleh');
	   $depan = $this->input->post('depan');
       $tipe = $this->session->userdata('hak');
       
       $config['upload_path'] = lokasi_berita_14;
		$config['allowed_types'] = 'jpg';
        $config['max_width']  = '600';
		$config['max_height']  = '397';
        $config['file_name']  = "jogdas_".date('YmdHis');
        
		$gambar="";
        if($_FILES["userfile"]["name"]!=""){
        $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="";
               
            }else{
                //array_push($data,array('error' => 'success'));
                $tGambar    = $this->upload->data();
                $d['error']="Berhasil di Upload ".$_FILES["userfile"]["name"];
				$d['f']=$config['file_name'].".jpg";
				$s="1";
            }
        }else{
			
            $d['error']="";
			$d['f']="";
        }
		$gambar=$d['f'];
		}
       if($gambar==""){
        $gambar=balik("at_berita", "foto", "id", $id);
       }
       
       //echo $isi_ind;
	   //exit;
       
       if($id==""){
        $this->berita->simpan($judul_ind, $judul_ing, $isi_ind, $isi_ing, $tanggal, $gambar, $tipe, $oleh, $depan);
        echo"<html><head><meta http-equiv='refresh' content='0;url=".base_url()."index.php/berita'></head><body></body></html>";
       }
       if($id!=""){
        if($_FILES["userfile"]["name"]!=""){
            $gambar2=balik("at_berita", "foto", "id", $id);
        	unlink(lokasi_berita_14."$gambar2");
       }
        $this->berita->editsimpan($id, $judul_ind, $judul_ing, $isi_ind, $isi_ing, $tanggal, $gambar, $tipe, $oleh, $depan);
        echo"<html><head><meta http-equiv='refresh' content='0;url=".base_url()."index.php/berita'></head><body></body></html>";
       }
       
       
       
    }
    
    public function hapus($id=null)
	{
		$a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
        $tipe=balik("at_berita", "tipe", "id", $id);
        $gambar2=balik("at_berita", "foto", "id", $id);
        unlink(lokasi_berita_14."$gambar2");
		$this->berita->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/berita'></head><body></body></html>";
	}
	
	
	
	
	
	
	
}
