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

class Admin_gallery extends CI_Controller {
	
	public function __construct()
    {
        parent::__construct();
		$this->load->model('login');
        $this->load->model('gallery_m', 'data');
    }

	public function index($id=null)
	{
	   $a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
		
		$d['i_judul'] = balik("at_galeri_id", "galery", "id", $id);
        
        $config['total_rows'] = $this->data->jum_hal($id);
		$config['per_page'] = 10;
		$config['uri_segment'] = 4;
		$config['full_tag_open'] = '<div class="pagenavi">';
		$config['full_tag_close'] = '</div>';
		 
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<span>';
		$config['first_tag_close'] = '</span>';
		 
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<span>';
		$config['last_tag_close'] = '</span>';
		 
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<span>';
		$config['next_tag_close'] = '</span>';
		 
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<span>';
		$config['prev_tag_close'] = '</span>';
		 
		$config['cur_tag_open'] = '<span class="current">';
		$config['cur_tag_close'] = '</span>';
		 
		$config['num_tag_open'] = '<span>';
		$config['num_tag_close'] = '</span>';
		$config['base_url'] = base_url()."admin_gallery/index/$id";
		$this->pagination->initialize($config);
		
		
		
		$hal = $this->uri->segment(4);
		 if($hal==""){
			 $hal=0;
		}
           
        
		$d['page'] = $this->pagination->create_links();
		$d['no'] = $hal;
        $d['tipe'] = $id;
        
        $d['data'] = $this->data->semua($id, "limit $hal, $config[per_page]");
        
        
		$this->load->view('theme/header.php',$d);
        $this->load->view('admin_gallery/at_tampil.php',$d);
        $this->load->view('theme/footer.php',$d);
	}
    
    public function tambah($tipe=null,$aksi=null, $id=null)
	{
	   $a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
		
		$d['i_judul'] = balik("at_galeri_id", "galery", "id", $tipe);
        
        
        $d['id'] = $id;
        
        if($aksi=="edit" and $id!=""){
            $d['judul_ind']=balik("at_galeri", "judul_ind", "id", $id);
            $d['judul_ing']=balik("at_galeri", "judul_ing", "id", $id);
            $d['gambar']=balik("at_galeri", "gambar", "id", $id);
        }
        elseif($aksi=="" and $id==""){
            $d['judul_ind']="";
            $d['judul_ing']="";
            $d['gambar']="";
            
        }
        $d['tipe']=$tipe;
        
		$this->load->view('theme/header.php',$d);
        $this->load->view('admin_gallery/tambah.php',$d);
        $this->load->view('theme/footer.php',$d);
	}
    
    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');
       $tipe = $this->input->post('tipe');
	   
	   
	   $lokasi_gal= lokasi_gal($this->session->userdata('hak'));
       
       $config['upload_path'] = $lokasi_gal;
		$config['allowed_types'] = 'jpg';
        $config['max_width']  = '500';
		$config['max_height']  = '500';
        $config['file_name']  = date('YmdHis');
        
        
        $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'].".jpg";
				$s="1";
            }
        }else{
			
            $d['error']="";
			$d['f']="";
        }
		$gambar=$d['f'];
       if($gambar==""){
        $gambar=balik("at_galeri", "gambar", "id", $id);
       }
       
       //echo $isi_ind;
	   //exit;
       
       if($id==""){
        $this->data->simpan($judul_ind, $judul_ing, $gambar, $tipe);
        echo"<html><head><meta http-equiv='refresh' content='0;url=".base_url()."index.php/admin_gallery/index/$tipe'></head><body></body></html>";
       }
       if($id!=""){
        if($_FILES["userfile"]["name"]!=""){
            $gambar2=balik("at_galeri", "gambar", "id", $id);
       		unlink($lokasi_gal."$gambar2");
       }
        $this->data->editsimpan($id, $judul_ind, $judul_ing, $gambar, $tipe);
        echo"<html><head><meta http-equiv='refresh' content='0;url=".base_url()."index.php/admin_gallery/index/$tipe'></head><body></body></html>";
       }
       
       
       
    }
    
    public function hapus($id=null)
	{
		 $a=$this->session->userdata('username');
		$this->login->cek_login_siswa($a);
        $tipe=balik("at_galeri", "galeri_id", "id", $id);
        $gambar2=balik("at_galeri", "gambar", "id", $id);
        unlink(lokasi_gal_14."$gambar2");
		$this->data->hapus($id);
        
		echo "<script>alert('Data berhasil dihapus')</script>";
		echo"<html><head><meta http-equiv='refresh' content='0;url=".base_url()."index.php/admin_gallery/index/$tipe'></head><body></body></html>";
	}
	

	
}
