<?
class userModel extends Model{

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


/**
 ***********************************************************************
 * @Start Global Section of Reseller ***********************************
 ***********************************************************************
*/

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}



/**
 ***********************************************************************
 * @Start Administrator User  Section **********************************
 ***********************************************************************
*/



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of User
 * @copyright 2011
 */
 
 	function getTotalnumUser($login,$fullName,$dept,$phone)
 	{
		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND description like '$login%'";

		if($fullName=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND full_name like '$fullName%'";

		if($phone=="NULL")
		$phoneStr = "";
		else
		$phoneStr = "AND phone like '$phone%'";

		if($dept=="NULL")
		$deptStr = "";
		else
		$deptStr = "AND dept like '$dept%'";


	   	$sql="SELECT
	   	count(id) as totalrows
	   	FROM admin Where id != '-1' $loginStr $fullNameStr $phoneStr $deptStr";
		$row = 0;
		$query = $this->db->query($sql);
		foreach($query->result() as $resultrow)
		{
			$row = $resultrow->totalrows;
		}
		return $row;

	}
	

/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Rate Name List
 * @copyright 2011
 */
	function userList($startoffset,$limit,$login,$fullName,$dept,$phone)
	{

		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
	    $limit_string = "LIMIT $startoffset1,$limit";

		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND description like '$login%'";

		if($fullName=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND full_name like '$fullName%'";

		if($phone=="NULL")
		$phoneStr = "";
		else
		$phoneStr = "AND phone like '$phone%'";

		if($dept=="NULL")
		$deptStr = "";
		else
		$deptStr = "AND dept like '$dept%'";


        $sql = "SELECT *FROM admin
				where id !=-1 $loginStr $fullNameStr $phoneStr $deptStr order by full_name ASC $limit_string";

			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Rate Name
 * @copyright 2011
 */

 function addUser($userID)
 {

		//$data['id']		= $this->input->post('id');
		$data['login']		= $this->input->post('login');
		$data['password']		= MD5($this->input->post('password'));
		$data['full_name']		= $this->input->post('full_name');
		$data['display_name']		= $this->input->post('display_name');
		$data['email']		= $this->input->post('email');
		$data['phone']		= $this->input->post('phone');
		$data['dept']		= $this->input->post('dept');
		$data['reseller_section']		= $this->input->post('reseller_section');
		$data['wholesale_section']		= $this->input->post('wholesale_section');
		$data['sip_section']		= $this->input->post('sip_section');
		$data['ani_section']		= $this->input->post('ani_section');
		$data['did_section']		= $this->input->post('did_section');
		$data['lots_section']		= $this->input->post('lots_section');
		$data['rate_section']		= $this->input->post('rate_section');
		$data['dialplan_section']		= $this->input->post('dialplan_section');
		$data['user_section']		= $this->input->post('user_section');
		$data['report_section']		= $this->input->post('report_section');
		$data['user_id']		= $userID;
		$data['ipaddress']		= $this->getRealIpAddr();
		$data['hidden']		= $this->input->post('password');
		$data['status']		= $this->input->post('status');

		$sql = $this->db->insert_string('admin', $data);
		$query = $this->db->query($sql);
		$idInsert = $this->db->insert_id();
 }
 
 
 
 
  /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Return unique login name
 * @copyright 2011
 */

	function checkLoginName($login)
	{
    	$query = $this->db->query("select *from admin where login = '$login'");
    	if($query->num_rows()== 1)
		{
			return $query->result();
		}
    	else
		return 0;
	}
	


 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Return Edit Infomation of Administrator
 * @copyright 2011
 */

	function userEditInfo($id)
	{
    	$query = $this->db->query("select *from admin where id = '$id'");
    	if($query->num_rows()== 1)
		{
			return $query->result();
		}
    	else
		return 0;
	}
		
 
 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Edit Administrator Information
 * @copyright 2011
 */

 function editUser($userID)
 {

		$keyvalue		= $this->input->post('id');
		//$data['login']		= $this->input->post('login');
		$data['password']		= MD5($this->input->post('password'));
		$data['full_name']		= $this->input->post('full_name');
		$data['display_name']		= $this->input->post('display_name');
		$data['email']		= $this->input->post('email');
		$data['phone']		= $this->input->post('phone');
		$data['dept']		= $this->input->post('dept');
		$data['reseller_section']		= $this->input->post('reseller_section');
		$data['wholesale_section']		= $this->input->post('wholesale_section');
		$data['sip_section']		= $this->input->post('sip_section');
		$data['ani_section']		= $this->input->post('ani_section');
		$data['did_section']		= $this->input->post('did_section');
		$data['lots_section']		= $this->input->post('lots_section');
		$data['rate_section']		= $this->input->post('rate_section');
		$data['dialplan_section']		= $this->input->post('dialplan_section');
		$data['user_section']		= $this->input->post('user_section');
		$data['report_section']		= $this->input->post('report_section');
		$data['user_id']		= $userID;
		$data['ipaddress']		= $this->getRealIpAddr();
		$data['hidden']		= $this->input->post('password');
		$data['status']		= $this->input->post('status');
		
        $where = "id = $keyvalue";
		$sql = $this->db->update_string('admin', $data, $where);
		$query = $this->db->query($sql);
		
 }


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information of Rate Name
 * @copyright 2011
 */


function deleteUser($id)
{
    $query = $this->db->query("delete from admin where id = '$id'");
}



}

?>