<?php 
class categories extends CI_Controller {
	function categories()	 
	{  
    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)
	{   
	    $this->load->model('categories_model');  /// model file name
	    	
	   //////pagination styl and config
	    $config['base_url'] = base_url().'index.php/categories/index/'.$type;
		$config['total_rows'] = $this->categories_model->get_num_cats($type);
		$config['per_page'] = 20;
		$config['num_links'] = 3; 
		$config['uri_segment'] = 4;	
		$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(4,0);
		$this->session->set_userdata('offset',$offset);
		$this->pagination->initialize($config); 
		
		$this->load->view('header');
		
		//// load left and send query to creat menu
		$status='all';
		
		$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->categories_model->get_categories($type,$config['per_page']);   ///////model class --->model func		
		$data['records']=$q;
		$data['type']=$type;
		$data['status']=$status;
		$this->load->view('categories',$data);
		
		$this->load->view('footer');
	}
	
	
	function add_category($type)
	{   $status='all';
	    $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;
		
		//// get admin lang
		$lang_q= $this->fun->get_lang();
		$data['lang']=$lang_q;
		
		
		
		
		
		$this->load->view('add_category',$data);
		$this->load->view('footer');

	}
	function save_add_category($type)
	{
	//// ********* upload_image
	    $file_name='';
		$this->load->library('upload');
		$config['upload_path'] = "../uploads/cats/";
		$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/cats/".$file_name;
			$config['new_image'] = "../uploads/cats/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(
	    'module'=>$type,
	    'name'=>$this->input->post('name'),
	    'short'=>$this->input->post('short'),
	    'pic'=>$file_name,
	    'name_ar'=>$this->input->post('name_ar'),
	    'short_ar'=>$this->input->post('short_ar')
	     	    );
	    
	    $this->load->model('categories_model');   ///model file name
	    $this->categories_model->add_new_category($data);  /////model class --->model func
	   echo "<script type=\"text/javascript\"> alert(\" Added successfully \") ;window.location.href= \"".base_url()."index.php/categories/index/".$type."\";</script>";

	    
	}




	function edit_category($type,$id)
	{ 	
	        $status='all';
	         $data['qry'] = $this->fun->getone("categories", $id); 
	         $lang_q= $this->fun->get_lang();			
			 $data['lang']=$lang_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;
		
				
	    $this->load->view('edit_category', $data);
	    $this->load->view('footer');
	
	}
	
	function save_edite_category($type,$id)
	{
	
	//// ********* upload_image
	    $file_name=$this->input->post('pic2');
		$this->load->library('upload');
		$config['upload_path'] = "../uploads/cats/";
		$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/cats/".$file_name;
			$config['new_image'] = "../uploads/cats/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(
	    'module'=>$type,
	    'name'=>$this->input->post('name'),
	    'short'=>$this->input->post('short'),
	    'pic'=>$file_name,
	    'name_ar'=>$this->input->post('name_ar'),
	    'short_ar'=>$this->input->post('short_ar'),
	    );
	    
	
	    
	    $this->load->model('categories_model');   ///model file name
	    $this->categories_model->update_category($data,$id);  /////model class --->model func
	    echo "<script type=\"text/javascript\"> alert(\" Edited successfully \") ;window.location.href= \"".base_url()."index.php/categories/edit_category/".$type."/".$id."\";</script>";
                		         
	}
	
	function delete_category($type,$id)
	{ 	
	    $q= $this->fun->delete_one("categories", $id); 
	    
	    if($q)
	    {
	     echo "<script type=\"text/javascript\"> alert(\" Deleted successfully \") ;window.location.href= \"".base_url()."index.php/categories/index/".$type."\";</script>";
	    }
	    else
	    {
	    echo "<script type=\"text/javascript\"> alert(\" Dont Deleted Please Try Again \") ;window.location.href= \"".base_url()."index.php/categories/index/".$type."\";</script>";
	    }
	   
	
	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */