<?php 
class gallery extends CI_Controller {
	function gallery()	 
	{  
    parent::__construct();	
    $this->load->library('session');	
    $this->load->Model('functions', 'fun');  //function in model 
    $this->load->library('pagination');
    $this->load->library('DX_Auth');	
	$this->dx_auth->check_uri_permissions();

    } 
    
	
		
	function index($type,$status,$post_id)
	{   
	    $this->load->model('gallery_model');  /// model file name
	    	
	   //////pagination styl and config
	    $config['base_url'] = base_url().'index.php/gallery/index/'.$type.'/'.$status.'/'.$post_id;
		$config['total_rows'] = $this->gallery_model->get_num_imgs($type,$post_id);
		$config['per_page'] = 10;
		$config['num_links'] = 3; 
		$config['uri_segment'] = 6;	
		$config['full_tag_open']='<div class="pagination">';
		$config['full_tag_close'] = '</div>';
		$config['next_link'] = 'Next';
		$config['prev_link'] = 'Prev';
		$config['cur_tag_open'] = '<span class="current">';
		$config['cur_tag_close'] = '</span>';		
		$offset = $this->uri->segment(6,0);
		$this->session->set_userdata('offset',$offset);
		$this->pagination->initialize($config); 
		
		$this->load->view('header');
		
		//// load left and send query to creat menu
		$menu=array();
		$menu_q= $this->fun->get_modules();
		$menu['mods']=$menu_q;$menu['status']=$status;
		$this->load->view('left',$menu);
		
		////// load content send query to get post 
		$data=array();
		$q=$this->gallery_model->get_imgs($type,$post_id,$config['per_page']);   ///////model class --->model func		
		$data['records']=$q;
		$data['type']=$type;
		$data['status']=$status;
		$data['post_id']=$post_id;
		

		$this->load->view('gallery',$data);
		
		$this->load->view('footer');
	}
	
	
	function add_img($type,$status,$post_id)
	{ 
	    $data=array();
	    $this->load->view('header');
	    
	    $menu=array();
		$menu_q= $this->fun->get_modules();
		$menu['mods']=$menu_q;
		$menu['status']=$status;
		$this->load->view('left',$menu);
		$data['type']=$type;
		$data['status']=$status;
		$data['post_id']=$post_id;

		//// get admin lang
		$lang_q= $this->fun->get_lang();
		$data['lang']=$lang_q;
				
		$this->load->view('add_img',$data);
		$this->load->view('footer');

	}
	function save_add_img($type,$status,$post_id)
	{
	//// ********* upload_image
	     $file_name='';
		$this->load->library('upload');
		$config['upload_path'] = "../uploads/gallery/";
		$config['allowed_types'] = 'gif|jpg|png';
		$config['overwrite'] = false;
		$config['encrypt_name'] =true;	
		$this->upload->set_max_filesize(201);
		$this->upload->initialize($config);
		$url = basename($_FILES['pic']['name']);
		if(!empty($url)) // select img //
		{
			if (!$this->upload->do_upload('pic'))
			{
			$error = $this->upload->display_errors();
			echo $error;
			echo "<script type=\"text/javascript\">
			alert(\" Image not uploaded please try again \");
			//window.location = \"addnew\";
			</script>"; 
			} 
			else 
			{
			$upname = $this->upload->data();
			$file_name = $upname['file_name'];
			}
        }
	////////******* make thumb
			$config['image_library'] = 'gd2';
			$config['source_image'] = "../uploads/gallery/".$file_name;
			$config['new_image'] = "../uploads/gallery/small/".$file_name;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = "";
			$config['width'] = 250;
			$config['height'] = 150;
			$this->load->library('image_lib',$config);
			$this->image_lib->resize();
			
		

	
	
	$data=array(
		'post_type'=>$type,
		'post_id'=>$post_id,
	    'name'=>$this->input->post('name'),
		'name_ar'=>$this->input->post('name_ar'),
		'pic'=>$file_name
	    );
	    
	    $this->load->model('gallery_model');   ///model file name
	    $this->gallery_model->add_new_img($data);  /////model class --->model func
	   echo "<script type=\"text/javascript\"> alert(\" Added successfully \") ;window.location.href= \"".base_url()."index.php/gallery/index/".$type."/".$status."/".$post_id."\";</script>";    
	}




	function edit_img($type,$status,$post_id,$id)
	{ 	
	        $data['qry'] = $this->fun->getone("gallery", $id); 
	    
	         $lang_q= $this->fun->get_lang();
			
			$data['lang']=$lang_q;
			$st_q= $this->fun->get_type_settings($type);
			$data['sett']=$st_q;

	    
	    $this->load->view('header');
	    
	    $menu=array();
		$menu_q= $this->fun->get_modules();
		$menu['mods']=$menu_q;
		$menu['status']=$status;
		$this->load->view('left',$menu);
		
		$data['type']=$type;
		$data['status']=$status;
		$data['post_id']=$post_id;
		
	
		
	    $this->load->view('edit_img', $data);
	    $this->load->view('footer');
	
	}
	
	function save_edite_img($type,$status,$post_id,$id)
	{
	//// ********* upload_image
	     $file_name=$this->input->post('pic2');
		$this->load->library('upload');
		$config['upload_path'] = "../uploads/gallery/";
		$config['allowed_types'] = 'gif|jpg|png';
		$config['overwrite'] = false;
		$config['encrypt_name'] =true;	
		$this->upload->set_max_filesize(201);
		$this->upload->initialize($config);
		$url = basename($_FILES['pic']['name']);
		if(!empty($url)) // select img //
		{
			if (!$this->upload->do_upload('pic'))
			{
			$error = $this->upload->display_errors();
			echo $error;
			echo "<script type=\"text/javascript\">
			alert(\" Image not uploaded please try again \");
			//window.location = \"addnew\";
			</script>"; 
			} 
			else 
			{
			$upname = $this->upload->data();
			$file_name = $upname['file_name'];
				////////******* make thumb
			$config['image_library'] = 'gd2';
			$config['source_image'] = "../uploads/gallery/".$file_name;
			$config['new_image'] = "../uploads/gallery/small/".$file_name;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = "";
			$config['width'] = 250;
			$config['height'] = 150;
			$this->load->library('image_lib',$config);
			$this->image_lib->resize();

			}
        }
        


			

	$data=array(
	   
	    'name'=>$this->input->post('name'),
	    'name_ar'=>$this->input->post('name_ar'),
	    'pic'=>$file_name
	    );
	    
	    $this->load->model('gallery_model');   ///model file name
	    $this->gallery_model->update_img($data,$id);  /////model class --->model func
	    echo "<script type=\"text/javascript\"> alert(\" Edited successfully \") ;window.location.href= \"../../../../index/".$type."/".$status."/".$post_id."\";</script>";
                		         
	}
	
	function delete_img($type,$status,$post_id,$id)
	{ 	
	    $q= $this->fun->delete_one("gallery", $id); 
	    
	    if($q)
	    {
	    echo "<script type=\"text/javascript\"> alert(\" Deleted successfully \") ;window.location.href= \"../../../../index/".$type."/".$status."/".$post_id."\";</script>";
	    }
	    else
	    {
	    echo "<script type=\"text/javascript\"> alert(\" Don't Deleted Pls Try Again ! \") ;window.location.href= \"../../../../index/".$type."/".$status."/".$post_id."\";</script>";
	    }
	   
	
	}
	
	function change_status($type,$status,$post_id)
	{
	   $actb=$this->input->post('act_all');
       $delb=$this->input->post('del_all');
       $notactb=$this->input->post('notact_all');
		      /* if($actb!='')
		       {
		           if(sizeof($this->input->post('list'))>0)
		           {
		           $selectd_id=$this->input->post('list');
		            $data=array('active'=>'yes');
		           foreach($selectd_id as $pid):
		             $this->fun->update_status('posts',$pid,$data);
		           endforeach;
		            $this->index($type,$status);
		           }
		           else
		           {
		            $this->index($type,$status);
		           }
		       }
		       
		       
		       if($notactb!='')
		       {
		           if(sizeof($this->input->post('list'))>0)
		           {
		           $selectd_id=$this->input->post('list');
		            $data=array('active'=>'no');
			           foreach($selectd_id as $pid):
			             $this->fun->update_status('posts',$pid,$data);
			           endforeach;
		             $this->index($type,$status);
		           }
		           else
		           {
		            $this->index($type,$status);
		           }
		       }
		       */
		       
		       if($delb!='')
		       {
		           if(sizeof($this->input->post('list'))>0)
		           {
		           $selectd_id=$this->input->post('list');
		               foreach($selectd_id as $pid):
	                        $this->fun->delete_one("gallery", $pid); 
			           endforeach;
		             $this->index($type,$status,$post_id);
		           }
		           else
		           {
		            $this->index($type,$status,$post_id);
		           }
		       }



	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */