<?php

class MAdmins extends Model{

	function MAdmins(){
		parent::Model();
	}


	function verifyUser($u,$pw){
		$this->db->select('id,username');
		$this->db->where('username',$u);
		$this->db->where('password', md5($pw));
		$this->db->where('status', 'active');
		$this->db->limit(1);
		$Q = $this->db->get('users');
		$this->session->set_userdata('lastquery', $this->db->last_query());
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return array();
		}		
	}

	function getUser($id){
      $data = array();
      $options = array('id' => $id);
      $Q = $this->db->getwhere('users',$options,1);
      if ($Q->num_rows() > 0){
        $data = $Q->row_array();
      }

      $Q->free_result();    
      return $data;  		

	}
	
	function getAllUsers(){
     $data = array();
	 $this->db->where('username !=','admin');
	 $this->db->where('status =','active');
     $Q = $this->db->get('users');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
     }
     $Q->free_result();    
     return $data; 	
	}
	
	function addUser(){
      $data = array('username' => $this->input->post('username'),
                    'email' => $this->input->post('email'),
                    'status' => $this->input->post('status'),
                    'password' => md5($this->input->post('password'))
                    );
	
	  $this->db->insert('users',$data);
	
	}
	
	function updateUser(){
      $data = array('username' => $this->input->post('username'),
                    'email' => $this->input->post('email'),
                    'status' => $this->input->post('status'),
                    'password' => md5($this->input->post('password'))
                    );
	  $this->db->where('id',$this->input->post('id'));
	  $this->db->update('users',$data);	
	
	}
	
	
	function deleteUser($id){
 	 $data = array('status' => 'inactive');
 	 $this->db->where('id', $id);
	 $this->db->update('users', $data);
	
	}
	
	

	
}


?>