<?php	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product_category extends CI_Controller {

	function __construct()	{
		parent::__construct();
		//$this->lang->load('masters', '');	
		$language = $this->session->userdata('language'); 
    		if ($language=='hindi') {
      	       $this->lang->load('hindi', '');
   			}else{
      		    $this->lang->load('masters', '');
            }
		//error_reporting(E_ALL);
	}

	function index()	{	
		$this->manage_product_category();
	}
	function list_product_category($query_id = 0, $sort_by = 'parent_category_id', $sort_order = 'desc', $offset = 0,$sort_type='') {
		
		admin_auth();
		//error_reporting(E_ALL);
		$submit=check_injections_filter($this->input->post('search')); 
		$limit = LIMIT_PER_PAGE;
		$this->page_data['page_title']="List of Product Category";
		$this->page_data['bredcrum']=array(
										'Masters'=>'#',
										'Product Category'=>site_url('gc/product_category/list_product_category'),
										'List Product Category'=>'#',
										);  
  



						$condition[] = array('join',TABLE_PRODUCT_CATEGORY.' as p','p.product_category_id = p1.parent_category_id','left');

						$this->page_data['main_category']='';
						$this->page_data['product_category_name']='';

						$this->load->library('NF_Query_search');
						$this->nf_query_search->load_query($query_id);

						$query_array = array(
 										'product_category_type'=>$this->input->get('product_category_type'),
 										'main_category'=>$this->input->get('main_category'),
 										'product_type' => $this->input->get('product_type'),

 										

										);
		$product_category_type=check_injections_filter($query_array['product_category_type']);
		$main_category=check_injections_filter($query_array['main_category']);
		$product_type=check_injections_filter($query_array['product_type']);

	// if(isset($submit) && !empty($submit))
	// 	{

         // $product_category_name=check_injections_filter($this->input->post('product_category_name'));
         // $main_category=check_injections_filter($this->input->post('main_category'));

        if ($product_category_type!='' && $product_category_type!='default') {
        	$condition[]=array('where','p1.product_category_id',$product_category_type);
        	$this->page_data['product_category_name']=$product_category_type;
        }else{
         	$this->page_data['product_category_name']='';
        }
       
        if ($main_category!='') {
        	$condition[]=array('like','p.product_category_name',$main_category);
        	$this->page_data['main_category']=$main_category;
        }else{
        	$this->page_data['main_category']='';
        }
        if ($product_type!='default') {
        	if ($product_type=='Dispenser') {
        		$condition[]=array('where','p1.category_type',$product_type);
        		$this->page_data['category_type']=$product_type;
        	}else if($product_type=='Refil'){
        	    $condition[]=array('where','p1.category_type',$product_type);
        		$this->page_data['category_type']=$product_type;
        	}else{
        		$this->page_data['category_type']='';
        	}
        	
        }else{
        	$this->page_data['category_type']='';
        
        }

         $this->page_data['fields'] = array(
         					'category_type'=>'Category Type',
							'product_category_name' => 'Product Category',
							'pn' => 'Main Category'

						
						);

	//}
						$sort_columns = array('pn','pi','product_category_name');
						$search_array=array(
							'limit' => $limit,
							'offset' => $offset,
							'sort_by' => $sort_by,
							'sort_type' => $sort_type,
							'sort_order' => $sort_order,
							'sort_columns'=> $sort_columns,
							'table_fields'=>'p.parent_category_id,p1.parent_category_id as pi,p.product_category_name as pn,p1.product_category_name,p1.product_category_name,p1.product_category_id,p1.is_active,p1.category_type',
							'table_name'=>TABLE_PRODUCT_CATEGORY.' as p1',
							'cond'=> $condition,
							);
						$results = $this->master_model->search($search_array);

						$search_full_array=array(
							'limit' => '',
							'offset' => '',
							'sort_by' => $sort_by,
							'sort_type' => $sort_type,
							'sort_order' => $sort_order,
							'sort_columns'=> $sort_columns,
							'table_fields'=>'p.parent_category_id,p1.parent_category_id as pi,p.product_category_name as pn,p1.product_category_name,p1.product_category_name,p1.product_category_id,p1.is_active,p1.category_type',
							'table_name'=>TABLE_PRODUCT_CATEGORY.' as p1',
							'condition'=> $condition,
							);
						$search_data=$this->master_model->select_data($search_full_array);
						//dump($this->db->last_query());
								$cnt_all_rows=count($search_data);
						if(!empty($results)&& is_array($results))
						{
									$this->page_data['product_category'] = $results['rows'];
									$this->page_data['num_results'] = $cnt_all_rows;
						}
						else
						{
							$results='';
						}
						$this->page_data['offset'] = $offset;
			/// pagination
			$this->load->library('pagination');
			$config = array();
			$config['base_url'] = site_url("gc/product_category/list_product_category/$query_id/$sort_by/$sort_order");
			$config['total_rows'] = $this->page_data['num_results'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 7;
			$config['full_tag_open'] = '<span class=\'paging\'>';
			$config['full_tag_close'] = '</span>';
			$config['first_link'] = 'First';
			$config['prev_link'] = '<<';
			$config['next_link'] = '>>';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			
			$this->page_data['pagination'] = $this->pagination->create_links();
			$this->page_data['sort_by'] = $sort_by;
			$this->page_data['sort_order'] = $sort_order;
			$this->page_data['sort_type'] = $sort_type;
			//$data['purpose'] = $purpose;
			$this->page_data['query_id'] = $query_id;
                         
                                           
  	        $this->load->view('gc/product_category_list',$this->page_data);



                
		       // $this->load->view('gc/product_category_list.php',$this->_page_data);
	}
	function search_category()
	{	
		

		$query_array = array(
						'product_category_type' =>check_injections_filter($this->input->post('product_category_type')),
						'main_category' => check_injections_filter($this->input->post('main_category')),
						'product_type' => check_injections_filter($this->input->post('product_type')),
						
					);
		
		$this->load->library('NF_Query_search');
		$query_id = $this->nf_query_search->save_query($query_array);	
		redirect('gc/product_category/list_product_category/'.$query_id);		
		
	}

	function view_product_category($product_category_id="") {
		admin_auth();
		$this->page_data['page_title']="View Product Category";
		//$id=$this->encrypt->decode($product_category_id);
		$id=encrypt_decrypt_parameters($product_category_id,'decrypt');
		$this->page_data['bredcrum']=array(
										'Masters'=>'#',
										'Product Category'=>site_url('gc/product_category/list_product_category'),
										'List Product Category'=>site_url('gc/product_category/list_product_category'),
										'View Product Category'=>'#',
										);

							$condition = array();
                            //$condition[] = array('where',TABLE_COMPANY.'.company_id',$company_id);

 							$condition[] = array('join',TABLE_PRODUCT_CATEGORY.' as p','p.product_category_id = p1.parent_category_id','left');
                                                    
                            $condition[] = array('where','p1.product_category_id',$id);

                             $selected_array=array(
                                'table_name'=>TABLE_PRODUCT_CATEGORY.' as p1',
                                'table_fields'=>'p.parent_category_id,p.product_category_name as pn,p1.product_category_name,p1.product_category_id,p1.category_type',
                                'condition'=>$condition,
                                'limit'=>'',
                                'offset'=>''
                            );



                            

		$product_category['product_category'] = $this->master_model->select_data($selected_array);
		if(!empty($product_category['product_category'] )&& is_array($product_category['product_category']))
			{
				 $this->page_data['product_category'] = $product_category['product_category'] ;
				 
  	  			 
  	        }



		$this->load->view('gc/product_category_view.php',$this->page_data);
	}
	
	function manage_product_category($product_category_id="") {
		admin_auth();
		
		//$id=$this->encrypt->decode($product_category_id);
		$id=encrypt_decrypt_parameters($product_category_id,'decrypt');
		$this->_page_data['category'] = $this->fetch_cateogory();
		if ($id) {
		$this->_page_data['page_title']=" Edit Product Category";
			$this->_page_data['bredcrum']=array(
										'Masters'=>'#',
										'Product Category'=>site_url('gc/product_category/list_product_category'),
										//'List Product Type'=>site_url('gc/product_category/list_gc_type'),
										' Edit Product Category'=>'#',
										);
		}else{$this->_page_data['page_title']=" Add Product Category";
			  $this->_page_data['bredcrum']=array(
										'Masters'=>'#',
										'Product Category'=>site_url('gc/product_category/list_product_category'),
										//'List Product Type'=>site_url('gc/product_category/list_gc_type'),
										' Add Product Category'=>'#',
										);
		}
		
		$this->_page_data['pc_Product_Category_Name']=$this->lang->line('pc_Product_Category_Name', FALSE);
		$this->_page_data['pc_Parent_Category_Name']=$this->lang->line('pc_Parent_Category_Name', FALSE);
		

       if (!$id) {
            
	       $this->load->view('gc/product_category_manage.php',$this->_page_data);

		}else{
                     
				 	$condition = array();
                            //$condition[] = array('where',TABLE_COMPANY.'.company_id',$company_id);
 					$condition[] = array('join',TABLE_PRODUCT_CATEGORY.' as p','p.product_category_id = p1.parent_category_id','left');
                                                    
                    $condition[] = array('where','p1.product_category_id',$id);

                            $selected_array=array(
                                'table_name'=>TABLE_PRODUCT_CATEGORY.' as p1',
                                'table_fields'=>'p.parent_category_id,p.product_category_name as pn,p1.product_category_name,p1.product_category_id,p.product_category_id as pi',
                                'condition'=>$condition,
                                'limit'=>'',
                                'offset'=>''
                            );


				
		$product_category['product_category'] = $this->master_model->select_data($selected_array);
		
		if(!empty($product_category['product_category'])&& is_array($product_category['product_category']))
			{
				 $this->_page_data['product_category'] = $product_category['product_category'] ;
				 
  	  			 
  	        }
		  $this->load->view('gc/product_category_manage.php',$this->_page_data); 
		
		}


		//$this->load->view('gc/product_category.php',$this->_page_data);
	}

	function add_product_category(){
		admin_auth();


 		$pc_Product_Category_created=$this->lang->line('pc_Product_Category_created', FALSE);
 		$pc_Product_Category_updated=$this->lang->line('pc_Product_Category_updated', FALSE);
 		$pc_Product_Category_present=$this->lang->line('pc_Product_Category_present', FALSE);
		
		$this->load->library('form_validation');
  	    
    	$product_category_name = check_injections_filter($this->input->post('product_category_name'));
    	$s_product_category_name=strtolower($product_category_name);
    	$parent_category_id =check_injections_filter($this->input->post('parent_category_name'));
    	$id = check_injections_filter($this->input->post('hidden')); 
    	$user_id = $this->session->userdata('user_id');
        $this->form_validation->set_rules('product_category_name', 'Product Category Name','required'); 
    	 if ($this->form_validation->run() == FALSE)
			{
                  

                   $this->_page_data['category'] = $this->fetch_cateogory();
		           $this->_page_data['page_title']="Add  Product Category";
				   $this->_page_data['bredcrum']=array(
										'Masters'=>'#',
										'Product Category'=>site_url('gc/product_category/list_product_category'),
										//'List Product Type'=>site_url('gc/product_category/list_gc_type'),
										' Add Product Category'=>'#',
										);
            
	       $this->load->view('gc/product_category_manage.php',$this->_page_data);



			}else{

			    // $condition[] = array('where','LOWER(product_category_name)',$s_product_category_name);
			    if ($id) {
			    	$condition[] = array('where','LOWER(product_category_name)',$s_product_category_name);
			    	$condition[] = array('where','parent_category_id',$parent_category_id);			    	
    	    		$condition[] = array('where_not_in','product_category_id',$id);
       			}else{
       				$condition[] = array('where','LOWER(product_category_name)',$s_product_category_name);
			    	$condition[] = array('where','parent_category_id',$parent_category_id);
       			}
    		    $selected_array=array(
								'table_name' =>TABLE_PRODUCT_CATEGORY,
								'table_fields'=>'product_category_name',
								'condition'=>$condition,
								'limit'=>'',
                                'offset'=>''						
					    );
    		   $product_category['product_category'] = $this->master_model->select_data($selected_array);
    		  // dump($this->db->last_query());
    		   // exit;
    			if(!empty($product_category['product_category']))
				{
					$this->session->set_flashdata('message',$pc_Product_Category_present);
				     redirect('gc/product_category/manage_product_category');
				}else{

                    if (!$id) {


                	$product_category_data=array(
								'product_category_name' => $product_category_name,
								'parent_category_id' =>$parent_category_id,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' => '',	
								'modified_on' => date("Y-m-d H:i:s")

								
					    );
				 	$this->session->set_flashdata('message',$pc_Product_Category_created);
				 	$this->master_model->insert_record(TABLE_PRODUCT_CATEGORY,$product_category_data);
    	         	redirect('gc/product_category/list_product_category');						
			
			        }else{

			   		    $product_category_data=array(
								'product_category_name' => $product_category_name,
								'parent_category_id' =>$parent_category_id,
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>$user_id,	
								'modified_on' => date("Y-m-d H:i:s")		
					    );
				        $condition=array('product_category_id'=>$id);
			            $this->master_model->update_record(TABLE_PRODUCT_CATEGORY,$product_category_data,$condition);
			            $this->session->set_flashdata('message',$pc_Product_Category_updated);
			            redirect('gc/product_category/list_product_category');
			        }  
       
			  
					
				}

		    }
	}

	 function fetch_cateogory(){
    	//admin_auth();
    	admin_auth();

    	$condition[] = array('where','parent_category_id','0');
    	$condition[] = array('where','is_active','1');
    	$selected_array = array( 

			                   'table_name' =>TABLE_PRODUCT_CATEGORY,
			                   'table_fields' =>'product_category_id,product_category_name,parent_category_id,is_active',
			                   'condition'=>$condition,
			                   'order_by_field'=>'product_category_name',
			                   'order'=>'ASC',
			                   'limit'=>'',
                               'offset'=>''
		);

		return $this->master_model->select_data($selected_array);
	
		
		       
    }
    function fetch_cateogory_types(){

		admin_auth();
       
		$cateogory_type = $this->input->post('cateogory_type'); 
		$product_category_id = $this->input->post('product_category_type'); 
		
		if ($cateogory_type=='Dispenser') {
			$condition[] = array('where','category_type','Dispenser');
		}else if($cateogory_type=='Refil'){
			$condition[] = array('where','category_type','Refil');
		}else{
			$condition[] = array('where','category_type','');
		}
		//$condition = array('where','product_type',$check);
		
		// /$condition[] = array('or_where','product_type','Dispenser-FTU');
		$condition[] = array('where','is_active','1');

    	$selected_array = array( 
			                   'table_name' =>TABLE_PRODUCT_CATEGORY,
			                   'table_fields' =>'product_category_id,product_category_name,is_active,category_type',
			                   'order_by_field'=>'product_category_name',
			                   'group_by_field'=>'product_category_name',
			                   'order'=>'ASC',
			                   'condition'=>$condition,
                               'limit'=>'',
                               'offset'=>''

		);
	
		$category_data['category_data'] = $this->master_model->select_data($selected_array);
		//dump($this->db->last_query());
		$output='<option value="default">Select Product Category</option>';
		foreach($category_data['category_data'] as $row)
		{
        
         	$selected='';

             if ($row['product_category_id']==$product_category_id) 

			 {

				$selected='selected="selected"';

			 }
		 		 $output .= '<option value='.$row['product_category_id'].' '.$selected.'>'.$row['product_category_name'].'</option>';
		 

		}

          echo $output;	
		
    }
    function disable_product_category($product_category_id){
		admin_auth();
		//$id=$this->encrypt->decode($product_category_id);
		$id=encrypt_decrypt_parameters($product_category_id,'decrypt');
		$user_id = $this->session->userdata('user_id');
		$pc_Product_Category_disable=$this->lang->line('pc_Product_Category_disable', FALSE);
		
     $product_category_data=array(
								'is_active' =>'0',								
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>$user_id,	
								'modified_on' => date("Y-m-d H:i:s")								
					    );
     	$condition=array('product_category_id'=>$id);
     	$this->session->set_flashdata('message',$pc_Product_Category_disable);
		$this->master_model->update_record(TABLE_PRODUCT_CATEGORY,$product_category_data,$condition);
		redirect('gc/product_category/list_product_category');
    }
    function enable_product_category($product_category_id){
      	admin_auth();
      	$pc_Product_Category_enable=$this->lang->line('pc_Product_Category_enable', FALSE);
      	//$id=$this->encrypt->decode($product_category_id);
		$id=encrypt_decrypt_parameters($product_category_id,'decrypt');
      	$user_id = $this->session->userdata('user_id');
      $product_category_data=array(
								'is_active' =>'1',								
								'created_on' => date("Y-m-d H:i:s"),
								'created_by' =>$user_id,
								'modified_by' =>$user_id,	
								'modified_on' => date("Y-m-d H:i:s")								
					    );
     	$condition=array('product_category_id'=>$id);
     	$this->session->set_flashdata('message',$pc_Product_Category_enable);
		$this->master_model->update_record(TABLE_PRODUCT_CATEGORY,$product_category_data,$condition);
		redirect('gc/product_category/list_product_category');
    }
  
}
?>