<?php
class Galeri extends CI_Controller{
	function __construct(){
		parent::__construct();
		if($this->session->userdata('masuk') !=TRUE){
            $url=base_url('administrator');
            redirect($url);
        };
		$this->load->model('m_album');
		$this->load->model('m_galeri');
		$this->load->model('m_pengguna');
		$this->load->library('upload');
	}


	function index(){
		
		$x['data']=$this->m_galeri->get_all_galeri();
		$x['alb']=$this->m_album->get_all_album();
		$this->load->view('admin/v_galeri',$x);
	}
	
	function simpan_galeri(){
		$config['upload_path'] = './assets/images/'; //path folder
        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
        $config['encrypt_name'] = TRUE; //nama yang terupload nantinya

        $judul = strip_tags($this->input->post('xjudul'));
        $album = strip_tags($this->input->post('xalbum'));
        $kode = $this->session->userdata('idadmin');
        $user = $this->m_pengguna->get_pengguna_login($kode);
        $p = $user->row_array();
        $user_id = $p['pengguna_id'];
        $user_nama = $p['pengguna_nama'];

        // Handle multiple file uploads
        if(!empty($_FILES['filefoto']['name'][0]))
        {
            $files = $_FILES['filefoto'];
            $uploaded_count = 0;
            $errors = array();

            // Loop through each file
            for($i = 0; $i < count($files['name']); $i++)
            {
                if($files['error'][$i] == 0)
                {
                    $_FILES['filefoto']['name'] = $files['name'][$i];
                    $_FILES['filefoto']['type'] = $files['type'][$i];
                    $_FILES['filefoto']['tmp_name'] = $files['tmp_name'][$i];
                    $_FILES['filefoto']['error'] = $files['error'][$i];
                    $_FILES['filefoto']['size'] = $files['size'][$i];

                    $this->upload->initialize($config);
                    
                    if ($this->upload->do_upload('filefoto'))
                    {
                        $gbr = $this->upload->data();
                        
                        //Compress Image
                        $config_img['image_library'] = 'gd2';
                        $config_img['source_image'] = './assets/images/'.$gbr['file_name'];
                        $config_img['create_thumb'] = FALSE;
                        $config_img['maintain_ratio'] = FALSE;
                        $config_img['quality'] = '60%';
                        $config_img['width'] = 500;
                        $config_img['height'] = 400;
                        $config_img['new_image'] = './assets/images/'.$gbr['file_name'];
                        $this->load->library('image_lib', $config_img);
                        $this->image_lib->resize();
                        $this->image_lib->clear();

                        $gambar = $gbr['file_name'];
                        
                        // Simpan setiap gambar dengan judul yang sama
                        $this->m_galeri->simpan_galeri($judul, $album, $user_id, $user_nama, $gambar);
                        $uploaded_count++;
                    }
                    else
                    {
                        $errors[] = $files['name'][$i] . ': ' . $this->upload->display_errors('', '');
                    }
                }
            }

            if($uploaded_count > 0)
            {
                echo $this->session->set_flashdata('msg','success');
                if(count($errors) > 0)
                {
                    echo $this->session->set_flashdata('msg_warning', implode('<br>', $errors));
                }
            }
            else
            {
                echo $this->session->set_flashdata('msg','warning');
            }
        }
        else
        {
            echo $this->session->set_flashdata('msg','warning');
        }
        
        redirect('admin/galeri');
	}
	
	function update_galeri(){
				
	            $config['upload_path'] = './assets/images/'; //path folder
	            $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
	            $config['encrypt_name'] = TRUE; //nama yang terupload nantinya

	            $this->upload->initialize($config);
	            if(!empty($_FILES['filefoto']['name']))
	            {
	                if ($this->upload->do_upload('filefoto'))
	                {
	                        $gbr = $this->upload->data();
	                        //Compress Image
	                        $config['image_library']='gd2';
	                        $config['source_image']='./assets/images/'.$gbr['file_name'];
	                        $config['create_thumb']= FALSE;
	                        $config['maintain_ratio']= FALSE;
	                        $config['quality']= '60%';
	                        $config['width']= 500;
	                        $config['height']= 400;
	                        $config['new_image']='./assets/images/'.$gbr['file_name'];
	                        $this->load->library('image_lib', $config);
	                        $this->image_lib->resize();

	                        $gambar=$gbr['file_name'];
	                        $galeri_id=$this->input->post('kode');
	                        $judul=strip_tags($this->input->post('xjudul'));
						$album=strip_tags($this->input->post('xalbum'));
						$images=$this->input->post('gambar');
						$path='./assets/images/'.$images;
						unlink($path);
						$kode=$this->session->userdata('idadmin');
						$user=$this->m_pengguna->get_pengguna_login($kode);
						$p=$user->row_array();
						$user_id=$p['pengguna_id'];
						$user_nama=$p['pengguna_nama'];
						$this->m_galeri->update_galeri($galeri_id,$judul,$album,$user_id,$user_nama,$gambar);
						echo $this->session->set_flashdata('msg','info');
						redirect('admin/galeri');
	                    
	                }else{
	                    echo $this->session->set_flashdata('msg','warning');
	                    redirect('admin/galeri');
	                }
	                
	            }else{
						$galeri_id=$this->input->post('kode');
	                        $judul=strip_tags($this->input->post('xjudul'));
						$album=strip_tags($this->input->post('xalbum'));
						$kode=$this->session->userdata('idadmin');
						$user=$this->m_pengguna->get_pengguna_login($kode);
						$p=$user->row_array();
						$user_id=$p['pengguna_id'];
						$user_nama=$p['pengguna_nama'];
						$this->m_galeri->update_galeri_tanpa_img($galeri_id,$judul,$album,$user_id,$user_nama);
						echo $this->session->set_flashdata('msg','info');
						redirect('admin/galeri');
	            } 

	}

	function hapus_galeri(){
		$kode=$this->input->post('kode');
		$album=$this->input->post('album');
		$gambar=$this->input->post('gambar');
		$path='./assets/images/'.$gambar;
		unlink($path);
		$this->m_galeri->hapus_galeri($kode,$album);
		echo $this->session->set_flashdata('msg','success-hapus');
		redirect('admin/galeri');
	}

}
