<?php 

class User_model extends CI_Model {
	
	public function _consruct(){
		parent::_construct();
 	}
		
 	function getUserData(){
 		$user_id = $this->session->userdata('id');
 		$user_type = $this->session->userdata('user_type');
 		if($user_type == 1){
 			$result = $this->db->get_where('admin_users',array('status'=>'1','id'=>$user_id));
 		} else {
 			$sql = "SELECT AU.*, CMP.* 
 					FROM admin_users AS AU 
 					INNER JOIN company AS CMP ON (CMP.company_id = AU.id)
 					WHERE AU.status = '1' AND AU.id = '".$user_id."'";
 			$result = $this->db->query($sql);
 		}
 		if(empty($result)){
 			return 0;
 		}
		return $result->row();
 	}

 	function updateUser($user_id = '',$user_type = '',$user_data = array()){
 		if(empty($user_id) || empty($user_type) || empty($user_data)){
 			return 0;
 		}
 		$emailChk = $this->db->get_where('admin_users',array('username'=>$user_data['email_id'],'id !='=>$user_id,'status !='=>'2'));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$admUpArr = array('username'=>$user_data['email_id'],'display_name'=>$user_data['company_name']);
 		if(!empty($user_data['profile_image'])){
 			$admUpArr['profile_image'] = $user_data['profile_image'];
 		}
 		if(!empty($user_data['password'])){
 			$admUpArr['password'] = $user_data['password'];
 		}
 		$status = $this->db->update('admin_users',$admUpArr,array('id'=>$user_id));
 		if($status && $user_type == 2){
 			$company_federal_id = (isset($user_data['company_federal_id']) && !empty($user_data['company_federal_id']))?$user_data['company_federal_id']:'';
 			$status = $this->db->update('company',array('company_name'=>$user_data['company_name'],'address'=>$user_data['address'],'phone'=>$user_data['phone'],'fax'=>$user_data['fax'],'email_id'=>$user_data['email_id'],'company_contact'=>$user_data['company_contact'],'company_info'=>$user_data['company_info'],'company_federal_id'=>$company_federal_id), array('company_id'=>$user_id));
 		}
 		return $status;
 	}
}
?>