<?php
	class User_model extends CI_Model{

		public function register($enc_password){
			$data = array(
				'name' => $this->input->post('name'),
				'email' => $this->input->post('email'),
                'password' => $enc_password,
			);

			return $this->db->insert('users', $data);
		}


		public function login($email, $password){

			$this->db->where('email', $email);
			$this->db->where('password', $password);

			$result = $this->db->get('users');

			if($result->num_rows() == 1){
				return $result->row(0)->id;
			} else {
				return false;
			}
		}

        public function get() {
            $this->db->where('id', 1);
            $val = $this->db->get('users', 1, 0);
            return $val->num_rows()>0?$val->row():'';
        }

		public function update($name, $email) {
            if(!empty($name) && !empty($email)){
                $this->db->set('name', $name);
                $this->db->set('email', $email);
                $this->db->where('id', 1);
                return $this->db->update('users');
            }
            return false;
		}

        public function changePassword($password) {
            if(!empty($password)){
                $this->db->set('password', md5($password));
                $this->db->where('id', 1);
                return $this->db->update('users');
            }
            return false;
		}

		public function emailExists($email){
			$query = $this->db->get_where('users', array('email' => $email));
			if(empty($query->row_array())){
				return true;
			} else {
				return false;
			}
		}
	}