<?php
class User_model extends CI_Model {

	public function get_user_by_credentials($email, $password) {
	
		$this->db->where('email', $email);
		$this->db->where('password', md5($password));
		return $this->db->get('user');
	
	}
	
	public function get_user($email) {
	
		$this->db->where('email', $email);
		return $this->db->get('user');
	
	}
	
	public function get_user_by_id($user_id) {
	
		$this->db->where('id', $user_id);
		return $this->db->get('user');
	
	}
	
	public function check_existing_user($emailAddress) {
		
		$this->db->where('email', $emailAddress);
		$query = $this->db->get('user');
		
		return $query->num_rows == 1;
	}
	
	public function create_user_account($emailAddress) {
		
		$this->load->helper('date');
		
		$new_user_account_insert_data = array(
				'email' => $emailAddress,
				'password' => md5($this->input->post('password')),
				'creationDate' => mdate("%Y-%m-%d", now()),
				'state' => User_State::NORMAL,
                                'is_backoffice' => false
				);

		return $this->db->insert('user', $new_user_account_insert_data);
	}
	
	public function change_password_user($user_id, $new_password) {
		
		if ($user_id == null || $new_password == null || $new_password == '') {
			return false;
		}
		
		$userData = array(
				'password' => md5($new_password)
		);
		
		$this->db->where('id', $user_id);
		return $this->db->update('user', $userData);
	}
	
	public function update_user_data($user_id, $user_data) {
		
		if ($user_id == null) {
			return false;
		}
		
		$this->db->where('id', $user_id);
		$this->db->update('user', $user_data);
		
	}
	
		
}
