<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

Class Search extends CI_Controller
{
    public function __construct()
    {
      parent::__construct();
      //load models
      $this->load->library('ion_auth');
      $this->load->model('search_model','',TRUE);
      //load array with categories from model
      $this->data['category_options'] = $this->search_model->category_options();
    }
    
    public function index()
    {
        if (!$this->ion_auth->logged_in())
		{
			//redirect them to the login page
			redirect('auth/login', 'refresh');
		}
		else
		{
		        $this->display();
		}
        
    }

    //show database entries that are part of the search
    public function display($query_id = 0, $sort_by = 'first_name', $sort_order = 'asc', $offset = 0)
    {        
        //Limit on items to show on table
        $limit = 15;
        
        //Values to Insert in Head of Table
	$this->data['fields'] = array(
			'first_name' => 'First Name',
			'last_name' => 'Last Name',
                        'company' => 'Company',
                        'position' => 'Position',
                        'email'    => 'Email',
                        'phone'    => 'Phone',
                        'city'      => 'City',
                        'state'       => 'State',
                        'country'  => 'Country',        
		);
                
         //$this->input->load_query($query_id);
         $this->load_query($query_id);
         
         //get value in category
        $category = $this->input->post('category');
      
        //set according to what is selected
        if($category == 0)
        {
            $category = 'first_name';
            $search_for = 'first_name';
        }
        else if($category == 1)
        {
            $category = 'last_name';
            $search_for = 'last_name';
        }
        else if($category == 2)
        {
            $category = 'company';
            $search_for = 'company';
        }
        else if($category == 3)
        {
            $category = 'position';
            $search_for = 'position';
        }
        else if($category == 4)
        {
            $category = 'city';
            $search_for = 'city';
        }
        else if($category == 5)
        {
            $category = 'state';
            $search_for = 'state';
        }
        else if($category == 6)
        {
            $category = 'country';
            $search_for = 'country';
        }
        
        //whats typed in
        $a = $this->input->post('searchinfo');
            
         $this->data['query_id'] = $query_id;
         $this->data['sort_by'] = $sort_by;
         $this->data['sort_order'] = $sort_order;
		
         //load the models
         $this->load->library('ion_auth');
	 $this->load->model('search_model');
		
         //set results to what is obtained in the search
         $results = $this->search_model->member_search($category, $a, $limit, $offset, $sort_by, $sort_order);
         
         //make into data to populate rows
         $this->data['membersforview'] = $results['rows'];
	 $this->data['num_results'] = $results['num_rows'];
         
         //pagenation
         $this->load->library('pagination');
         $config = array();
         $config['base_url'] = site_url("search/display/$query_id/$sort_by/$sort_order");
         $config['total_rows'] = $results['num_rows'];//$data['num_results'];
         $config['per_page'] = $limit;
         $config['uri_segment'] = 6;
         $this->pagination->initialize($config);
         $this->data['pagination'] = $this->pagination->create_links();
         
         
         //load views
         $this->data['subview'] = 'home';
         $this->data['subview'] = 'membership/user_search';
         
         //check if user is logged in 
         if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
         else if($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
         else
                {
                     //redirect them to the login page
		    redirect('auth/login', 'refresh');
                }  
    }
    public function search()
    {
        //get value in category
        $category = $this->input->post('category');
        
       //set according to what is selected
        if($category == 0)
        {
            $category = 'first_name';
            $search_for = 'first_name';
        }
        else if($category == 1)
        {
            $category = 'last_name';
            $search_for = 'last_name';
        }
        else if($category == 2)
        {
            $category = 'company';
            $search_for = 'company';
        }
        else if($category == 3)
        {
            $category = 'position';
            $search_for = 'position';
        }
         else if($category == 4)
        {
            $category = 'city';
            $search_for = 'city';
        }
        else if($category == 5)
        {
            $category = 'state';
            $search_for = 'state';
        }
        else if($category == 6)
        {
            $category = 'country';
            $search_for = 'country';
        }
        
        //needed
        //$searchinfo = $this->input->post('searchinfo');
         
       //get values in view
        /*
        $query_array = array(
            'searchinfo' => $this->input->post('searchinfo')
        );
         * */
        
        
        //save the query to rerun it
       //$query_id = $this->save_query($query_array);
           
        //reopen page with the query
        //redirect("search/display/$query_id");
        redirect("search/display/");
    }
    
    public function changeType($id, $type)
    {
        $this->search_model->changetype($id, $type);
    }
                
    //Outside Functions to use
    public function load_query($query_id)
    {
    $CI =& get_instance();

    $rows = $CI->db->get_where('users', array('id' => $query_id))->result();
    if (isset($rows[0])) 
    {
        parse_str($rows[0]->query_string, $_GET);       
    }

    }
    
    public function save_query($query_array)
{
    $CI =& get_instance();
    $CI->db->insert('e_queries', array('query_string' => http_build_query($query_array)));

    return $CI->db->insert_id();
}
    
}
?>