<?php


require_once(APPPATH . 'third_party/php_utils/abstract_dao' . EXT);

class User_details_dao  extends Abstract_DAO{
    
    public function __construct($db_conn) {
        parent::__construct($db_conn);
    }
    
    public function save($table_params){ 
        $query  = $this->_get_insert_query('users_details', $table_params);
        $mode = 'insert';
        $this->_dbExec($query, $mode, $table_params);
        return($this->_return_result());
    }
     public function get_role($id) {
        $query = "select role_name from roles where id ='$id'";
        $this->_dbExec($query, $mode = 'assoc');
        return ($this->_return_result());
    }
    public function update($table_params){ 
        $query  = $this->_get_update_query('users_details', $table_params, 'id = :id');
        $mode = 'update';
        $this->_dbExec($query, $mode, $table_params);
        return($this->_return_result());
    }
    
    public function update_active($id, $record_status) {

        $bind_params = array();
        $query = "";
        $query = "Update users_details SET active_status = " . $record_status .
                " WHERE id = '" . $id . "'";

        $statement = $this->DBO->prepare($query);

        $results = $statement->execute();
        return $results;
    }
    public function get($table_obj){
        $bind_params = array();
        if($table_obj->id != ''){
            $query = $this->_get_select_query('users_details', 'id = :id');
            $bind_params['id'] = $table_obj->id;
        }else{
            $query = $this->_get_select_query('users_details', 'contact = :contact');
            $bind_params['contact'] = $table_obj->contact;
        }
        $this->_dbExec($query, $mode = 'assoc',$bind_params);
        return($this->_return_result());
    }
    
    
    public function get_user_metadata($table_obj,$filters){
       
        $sqlstr = "select users_details.id as id,users_details.first_name,"
                . "users_details.last_name,users_details.contact,users_details.email,users_details.password,"
                . "users_details.native_place,users_details.address,users_details.image,users_details.active_status,"
                . "users_details.pass_out_year,users_details.gender,users_details.blood_group,users_details.marital_status,business.id as business_id,business.business_name,"
                . "business.website as business_site,business_type.business_type_name,business.business_address,business.website as business_site,business.location as business_location,"
                . "business_nature.business_nature_name,salaried_employee.id as salaried_id,designations.designation,salaried_employee.employer,"
                . "salaried_employee.website as employer_site,sectors.sector,salaried_employee.skills,profession.id as profession_id,profession.firm_name,profession_type.profession_type_name,"
                . "profession.area_of_expertise,profession.firm_address,profession.website as firm_site "
                . "from users_details "
                . "left join business ON (business.user_id=users_details.id)"
                . "left join business_type ON (business.business_type=business_type.id)"
                . "left join business_nature ON(business.business_nature=business_nature.id)"
                . "left join salaried_employee ON (salaried_employee.user_id=users_details.id)"
                . "left join designations ON (salaried_employee.designation=designations.id) "
                . "left join sectors ON (salaried_employee.sector=sectors.id) "
                . "left join profession ON (profession.user_id= users_details.id)"
                . "left join profession_type ON(profession_type.id=profession.profession_type)";
       
        $size = count((array)$filters);
       
        if($filters && $size>=1) {
            
            if(isset($filters->user_id)){
                 
                $sqlstr.=" where users_details.id = "."'$filters->user_id'";
                 
            }
            elseif(isset($filters->business_id)) {
                
                $sqlstr.=" where business.id = "."'$filters->business_id'";
            }
            else {
                $sqlstr .= "where";
                $count=1;
                foreach ($filters as $key=>$value){
                    if($count==1){ 
                            $sqlstr.=" $key ilike "."'$value%'";

                    }else {
                            $sqlstr.=" and $key ilike "."'$value%'";

                    }
                        $count++;
                }
            
            }
        }  
      
        $prepStmt = $this->DBO->prepare($sqlstr);
        $prepStmt->execute();
        $resultSet = array();
        while (($row = $prepStmt->fetch(PDO::FETCH_ASSOC)) != false){
            $resultSet[] = $row;
        }
        return $resultSet;
    }
    public function get_user_list($table_obj,$filters){
       
        $sqlstr = "select users_details.id as user_id,users_details.first_name,"
                . "users_details.last_name,business.id as business_id,"
                . "users_details.native_place,users_details.image,salaried_employee.id as salaried_id ,salaried_employee.employer ,"
                . "users_details.pass_out_year,users_details.gender,"
                . "designations.designation,business.business_name,profession.id as profession_id,"
                . "profession_type.profession_type_name,profession.firm_name "
                . "from users_details "
                . "left join salaried_employee ON (salaried_employee.user_id=users_details.id)"
                . "left join designations ON (salaried_employee.designation=designations.id) "
                . "left join business ON (business.user_id=users_details.id) "
                . "left join profession ON (profession.user_id= users_details.id) "
                . "left join profession_type ON(profession_type.id=profession.profession_type)";
       
        $size = count((array)$filters);
       
        if($filters && $size>=1) {
            
            if(isset($filters->user_id)){
                 
                $sqlstr.=" where users_details.id = "."'$filters->user_id' and users_details.active_status = 1 ";
                 
            }
            elseif(isset($filters->business_id)) {
                
                $sqlstr.=" where business.id = "."'$filters->business_id' and users_details.active_status = 1 ";
            }
            else {
                $sqlstr .= " where";
                $count=1;
                foreach ($filters as $key=>$value){
                    if($count==1){ 
                            $sqlstr.=" $key ilike "."'$value%'";

                    }else {
                            $sqlstr.=" and $key ilike "."'$value%'";

                    }
                        $count++;
                }
                $sqlstr.=" and users_details.active_status = 1 ";
            }
        }  
        else
        {
            $sqlstr.=" where users_details.active_status = 1 and users_details.role = 1";
        }

        $prepStmt = $this->DBO->prepare($sqlstr);
        $prepStmt->execute();
        $resultSet = array();
        while (($row = $prepStmt->fetch(PDO::FETCH_ASSOC)) != false){
            $resultSet[] = $row;
        }
        return $resultSet;
    }
    
    public function get_non_active_users($count) {
        
        if($count){
            $sqlstr = "select count(*) from users_details where active_status!=1 ";
        }
        else
        {
            $sqlstr = "select * from users_details where active_status!=1 ";
        }
      
        $prepStmt = $this->DBO->prepare($sqlstr);
        $prepStmt->execute();
        $resultSet = array();
        while (($row = $prepStmt->fetch(PDO::FETCH_ASSOC)) != false){
            $resultSet[] = $row;
        }
        return $resultSet;
    }
    
    public function get_old_password($table_obj) {

        $query = "select * from users_details
                   where id = '$table_obj->id'";
        $this->_dbExec($query, $mode = 'assoc');
        return ($this->_return_result());
    }

    public function update_password($table_obj) {

        $bind_params = array();
        $query = "";
        $query = "Update users_details SET password = '$table_obj->password' WHERE id = '$table_obj->id'";

        $statement = $this->DBO->prepare($query);

        $results = $statement->execute();
        return $results;
    }

}
