<?php
class User_Model extends CI_Model {
	function __construct(){

	}

	public function getUsers($status=null,$limit=20,$start=0){
		$this->db->select('*');
		$this->db->from('users');
		if(!empty($status)){
			$this->db->where('verified',$status);
		}
		$this->db->limit($limit,$start);
		$this->db->order_by('id','desc');
		$query = $this->db->get();
		return $query->result();
		
	}
	public function getUser($id){
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
		
	}
	public function checkUser($email){
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where('email',$email);
		$query = $this->db->get();
		$rowcount = $query->num_rows();
		if($rowcount==0){
			return false;
		}else{
			return $query->row()->id;
		}
		
		
	}
        public function checkPassword($user_id,$password){
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where('id',$user_id);
                $this->db->where('password',md5($password));
		$query = $this->db->get();
		$rowcount = $query->num_rows();
		if($rowcount==0){
			return false;
		}else{
			return true;
		}
		
		
	}
	public function createUser($name,$email,$password=null,$type=1,$contact=null){
		if(empty($password)){
			$password=generatePassword();
		}
		$data = array(
			'name'=>$name,
			'email'=>$email,
			'password'=>md5($password),
			'type'=>$type,
                        'phone'=>$contact,
			'vcode'=>null,
			'verified'=>0,
			'dt_created' => date('Y:m:d h:i:s'),
			'dt_modified'=> date('Y:m:d h:i:s')	
		);
		$result=$this->db->insert('users', $data); 
		
		if($result){
			$userId=$this->checkUser($email);
			$user=$this->getUser($userId);
			//send mail 
			$userid=$user->id;
			$verificationCode=generateVerificationCode(25);
			$data=array('vcode'=>$verificationCode);
			$this->db->where('id', $userid);
			$this->db->update('users', $data); 
			$fromname="Dewas.co.in Support";
			$fromemail="dewas.co.in@gmail.com";
			$toname=$user->name;
			$toemail=$user->email;
			$subject='Welcome to Dewas.co.in';
			$content="Dear ".$toname.",<br> Your account has been created. Please click on the link below to verify your account.<p><a href='".base_url()."account/verify/".$user->id."/".$verificationCode."'>".base_url()."account/verify/".$user->id."/".$verificationCode."</a></p><p>Thanks,<br> Dewas.co.in Support</p>";
			$this->load->library('email');
			$config['protocol'] = 'sendmail';
			$config['mailtype'] = 'html';
			$this->email->initialize($config);
			$this->email->from($fromemail, $fromname);
			$this->email->to($toemail,$toname);
			$this->email->subject($subject);
			$this->email->message($content);
			$this->email->send();
			return $user;
		}else{
			return false;
		}
	}
	public function getUserPostings($user_id){
		$this->db->select('*');
		$this->db->from('postings');
		$this->db->where('user_id',$user_id);
                $this->db->order_by('dt_created','desc');
		$query = $this->db->get();
		return $query->result();
		
	}
public function getUserCount(){
		return $this->db->count_all("users");
	}
	
	public function getDomainData($subdomain){
		$this->db->select('id,subdomain');
		$this->db->from('users');
		$this->db->where('subdomain',$subdomain);
        $query = $this->db->get();
		return $query->row();
	}
} 
?>
