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

	function __construct()	{
		parent::__construct();
		
	}

	function index()	{	
		$this->manage_product_category();
	}
	function list_product_category() {
		
		admin_auth();
		$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');

                          // $condition[] = array('where','p.parent_category_id','product_category_id');

                            $select_array=array(
                                'table_name'=>TABLE_PRODUCT_CATEGORY.' as p1',
                                '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',
                                'condition'=>$condition,
                                'limit'=>'',
                                'offset'=>''
                            );

                           $product_category['product_category']=$this->master_model->select_data($select_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_list',$this->page_data);



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

	function view_product_category($product_category_id="") {
		admin_auth();
		//$this->_page_data['page_title']="View Product Category";
		$id=$this->encrypt->decode($product_category_id);
		$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',
                                '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();
	//if($purpose=='')
			//$purpose='Add';
		
		//$this->_page_data['purpose']=$purpose;
		$id=$this->encrypt->decode($product_category_id);
		$this->_page_data['category'] = $this->fetch_cateogory();
		//$this->_page_data['page_title']=$purpose." 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'=>'#',
										);

       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();
		
		 $this->load->library('form_validation');
  	    
    	$product_category_name = check_injections_filter($this->input->post('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','product_category_name',$product_category_name);
			    if ($id) {

    	    		$condition[] = array('where_not_in','product_category_id',$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);
    			if(!empty($product_category['product_category']))
				{
					$this->session->set_flashdata('message','Product Category already present');
				     redirect('http://jade.nityam.in/index.php/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','Product Category inserted successfully');
				 	$this->master_model->insert_record(TABLE_PRODUCT_CATEGORY,$product_category_data);
    	         	redirect('http://jade.nityam.in/index.php/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','Product Category updated  successfully');
			            redirect('http://jade.nityam.in/index.php/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'=>'Asc',
			                   'limit'=>'',
                               'offset'=>''
		);

		return $this->master_model->select_data($selected_array);
		
		       
    }
    function disable_product_category($product_category_id){
		admin_auth();
		$id=$this->encrypt->decode($product_category_id);
		$user_id = $this->session->userdata('user_id');
		
     $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','Product Category is disable');
		$this->master_model->update_record(TABLE_PRODUCT_CATEGORY,$product_category_data,$condition);
		redirect('http://jade.nityam.in/index.php/gc/product_category/list_product_category');
    }
      function enable_product_category($product_category_id){
      	admin_auth();
      	$id=$this->encrypt->decode($product_category_id);
      	$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','Product Category  is enable');
		$this->master_model->update_record(TABLE_PRODUCT_CATEGORY,$product_category_data,$condition);
		redirect('http://jade.nityam.in/index.php/gc/product_category/list_product_category');
    }

    

	
  
}