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

	function __construct()	{
		parent::__construct();
		//error_reporting(E_ALL);
	}

	function index()	{	
		$this->list_user_type();
	}

	function list_user_type() {
		admin_auth();
		$this->page_data['page_title']="List of User Type";
		$this->page_data['bredcrum']=array(
										'User Management'=>'#',
										'User Types'=>'#',
										'List of User Type'=>'#'
										);

		$selected_array = array( 
			                   'table_name' =>TABLE_USER_TYPE,
			                   'table_fields' => 'is_active,user_type_id,user_type,module_ids',
			                   'order_by_field'=>'Asc'
		);

		
		$user_type['user_type'] = $this->master_model->get_table_data($selected_array); 

		 if(!empty($user_type['user_type'])&& is_array($user_type['user_type'])){
							
				 $this->page_data['user_type'] =   $user_type['user_type'];
		 	

  	    }
             
  	        $this->load->view('gc/user_type_list',$this->page_data);
		
	}
	
	function view_user_type($user_type_id) {
		admin_auth();
		$this->data['page_title']="View User Type";
		//$id=$this->encrypt->decode($user_type_id);
		$id=encrypt_decrypt_parameters($user_type_id,'decrypt');
		$this->data['bredcrum']=array(
										'User Management'=>'#',
										'User Types'=>'#',
										'List of User Type'=>site_url('gc/user_type/list_user_type'),
										'View User Type'=>'#'
										);

		$condition=array();
		$condition[]=array('where','user_type_id',$id);

		$selected_array = array(

							    'table_name' =>TABLE_USER_TYPE,
							    'table_fields'=>'user_type_id,user_type,module_ids',
							    'condition'=>$condition
		);
		$user_type['user_type'] = $this->master_model->select_data($selected_array);
		// echo "<pre>";
		// print_r($user_type);
	    // $module_name=module_view($module_id);
		// echo "<pre>";
		// print_r($module_name);
		// exit;
		
		if(!empty($user_type['user_type'])&& is_array($user_type['user_type']))
			{
				 $this->data['user_type'] = $user_type['user_type'] ;
  	  			 
  	        }
		$this->load->view('gc/user_type_view.php',$this->data);
		
	}
	
	function manage_user_type($user_type_id="") {
		admin_auth();
		//$id=$this->encrypt->decode($user_type_id);
		$id=encrypt_decrypt_parameters($user_type_id,'decrypt');
		$this->_page_data['module']=$this->Module();
		$this->_page_data['page_title']="Add  User Type";
		$this->_page_data['bredcrum']=array(
										'User Management'=>'#',
										'User Types'=>'#',
										//'List of User Type'=>site_url('gc/user_type/list_gc_type'),
										'Add  User Type'=>'#'
										);

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

		}else{
                     
			$condition=array();
		    $condition[]=array('where','user_type_id',$id);

		    $selected_array = array(

							    'table_name' =>TABLE_USER_TYPE,
							    'table_fields'=>'user_type_id,user_type,module_ids',
							    'limit'=>'',
							    'offset'=>'',
							    'condition'=>$condition
		);
		$user_type['user_type'] = $this->master_model->select_data($selected_array);
		
		if(!empty($user_type['user_type'] )&& is_array($user_type['user_type']))
			{
				 $this->_page_data['user_type'] = $user_type['user_type'] ;
				 
  	  			 
  	        }
		  $this->load->view('gc/user_type_manage.php',$this->_page_data); 
		
		}
		
	}
	 function add_user_type(){
             admin_auth();
			$id = $this->input->post('hidden');
			$user_type = check_injections_filter($this->input->post('user_type'));
		    $module = $this->input->post('form-field-checkbox');
		    $modules=implode(",", $module);
		    $user_id=$this->session->userdata('user_id');



		     if ($id) {
				$this->session->set_flashdata('message','Company Branch updated  successfully');
				$user_type_data=array(
								'user_type' => $user_type,								
								'module_ids' =>$modules,
								'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")
								
					    );
				$this->session->set_flashdata('message','User Type updated  successfully');
						 $condition=array('user_type_id'=>$id);
						 $this->master_model->update_record(TABLE_USER_TYPE,$user_type_data,$condition);
					     redirect('gc/user_type/list_user_type');	
			}
		 
 

            $condition[] = array('where','user_type',$user_type);

    	    $selected_array=array(
								'table_name' =>TABLE_USER_TYPE,								
								'table_fields'=>'user_type',
								'condition'=>$condition,
								'limit'=>'',
								'offset'=>''							
					         );

    			$user_type['user_type'] = $this->master_model->select_data($selected_array);
    			
    			if(!empty($user_type['user_type'])&& is_array($user_type['user_type']))
				{
					 $this->session->set_flashdata('message','User Type already present');
				     redirect('gc/user_type/manage_user_type');
				}else{
			     
				   $user_type_data=array(
								'user_type' => $user_type,								
								'module_ids' =>$modules,
								'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','User Type inserted successfully');
				   $this->master_model->insert_record(TABLE_USER_TYPE,$user_type_data);
			   	   redirect('gc/user_type/list_user_type/');
		        }
				
    }
     function disable_user_type($user_type_id){
     	admin_auth();
     	$id=$this->encrypt->decode($user_type_id);
		$id=encrypt_decrypt_parameters($user_type_id,'decrypt');
     	$user_id=$this->session->userdata('user_id');
        $user_type_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('user_type_id'=>$id);
     	$this->session->set_flashdata('message','User Type is disable');
		$this->master_model->update_record(TABLE_USER_TYPE,$user_type_data,$condition);
		redirect('gc/user_type/list_user_type');
    }
      function enable_user_type($user_type_id){
      	admin_auth();
      	$id=$this->encrypt->decode($user_type_id);
		$id=encrypt_decrypt_parameters($user_type_id,'decrypt');
      	$user_id=$this->session->userdata('user_id');
      $user_type_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('user_type_id'=>$id);
        $this->session->set_flashdata('message','User Type  is enable');
		$this->master_model->update_record(TABLE_USER_TYPE,$user_type_data,$condition);
		redirect('gc/user_type/list_user_type');
    }
     function Module(){
     	admin_auth();
        $condition[] = array('where','is_visible','1');
    	$selected_array = array( 
			                   'table_name' =>MODULES,
			                   'table_fields' => 'module_id,module_name,module_parent_id,is_visible',
			                   'order_by_field'=>'Asc',
			                   'condition'=>$condition,
			                   'limit'=>'',
			                   'offset'=>'',
		);
		
		return $this->master_model->select_data($selected_array);
		//return $this->master_model->get_table_data($selected_array);		         
    }
}