<?php
class user extends Controller
{

	function user()
	{

		parent::Controller();
	}


	function index()
	{
	    if($this->clogin->is_logged_in())
		{
			redirect("admin/cpanel", "Location");
		}
		else
		{
			redirect("login/index", "Location");
		}
	}

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


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Admin User List
 * @copyright 2011
 */

	function userList($errorMessage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$userCURD = $this->session->userdata('userCURD');
			if($userCURD != ALL_ACCESS)
			redirect("admin/cpanel/accessDenied", "Location");
			
			
			$data['login'] = "NULL";
			$data['fullName'] = "NULL";
			$data['dept'] = "NULL";
			$data['phone'] = "NULL";
			
			$data['startoffset'] = "0";
			$data['limit'] = LIMIT_PERPAGE;
			$data['total'] = $this->userModel->getTotalnumUser($login='NULL',$fullName='NULL',$dept='NULL',$phone = 'NULL');
			$data['userList'] = $this->userModel->userList($startoffset = 0,$limit = LIMIT_PERPAGE,$login='NULL',$fullName='NULL',$dept='NULL',$phone = 'NULL');
			$data['titleMessage'] = '';
			if($errorMessage == 2)
			$data['titleErrorMessage'] = FROMMESSAGE054;
			elseif($errorMessage == 3)
			$data['titleErrorMessage'] = FROMMESSAGE056;
			elseif($errorMessage == 4)
			$data['titleErrorMessage'] = FROMMESSAGE057;
			else
			$data['titleErrorMessage'] = "";
			$this->load->view('user/user_list',$data);
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Admin User List Pagination
 * @copyright 2011
 */

	function userListPagination($pageNumber,$limit,$login,$fullName,$dept,$phone,$myRandom=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$userCURD = $this->session->userdata('userCURD');
			
			$data['login'] = $login;
			$data['fullName'] = $fullName;
			$data['dept'] = $dept;
			$data['phone'] = $phone;
			
			$data['startoffset'] = $pageNumber;
			$data['limit'] = $limit;
			$data['total'] = $this->userModel->getTotalnumUser($login,$fullName,$dept,$phone);
			$data['userList'] = $this->userModel->userList($pageNumber,$limit,$login,$fullName,$dept,$phone);
			$data['titleMessage'] = '';
			$data['titleErrorMessage'] = "";
			$this->load->view('user/user_list_pagi',$data);
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	

/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Add Form of Administrator User
 * @copyright 2011
 */
	
	function addForm($errorMesssage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$userCURD = $this->session->userdata('userCURD');
			if($userCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{

				$data['id']		= '';
				$data['login']		= '';
				$data['password']		= '';
				$data['full_name']		= '';
				$data['display_name']  = '';
				$data['email']		= '';
				$data['phone']		= '';
				$data['dept']		= '';
				$data['reseller_section']		= '1';
				$data['wholesale_section']		= '0';
				$data['sip_section']		= '1';
				$data['ani_section']		= '1';
				$data['did_section']		= '0';
				$data['lots_section']		= '0';
				$data['rate_section']		= '0';
				$data['dialplan_section']		= '0';
				$data['user_section']		= '0';
				$data['report_section']		= '2';
				$data['status']		= '1';
				
				$data['action']		= 'Add';
				
				$data['titleAccountMessage'] = TITLE072;
				$data['titlePersonalMessage'] = TITLE073;
				
				if($errorMesssage == 0)
				$data['titleErrorMessage'] = FROMMESSAGE001;
				elseif($errorMesssage == -1)
				$data['titleErrorMessage'] = FROMMESSAGE001;
				elseif($errorMesssage == -2)
				$data['titleErrorMessage'] = FROMMESSAGE055;
				else
				$data['titleErrorMessage'] = FROMMESSAGE001;

				$this->load->view('user/add_form',$data);

			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Add/Edit Administrator
 * @copyright 2011
 */
 

	function addEdit()
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$userCURD = $this->session->userdata('userCURD');
			if($userCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
               $action = $this->input->post('action');
               $login = $this->input->post('login');
               $password = $this->input->post('password');
               $full_name = $this->input->post('full_name');
               $display_name = $this->input->post('display_name');
 
               if(empty($login) OR empty($password) OR empty($full_name) OR empty($display_name))
               redirect("user/addForm/-1", "Location");

               $addDscripttion = "Add New User,  Full Name: ".$full_name;
			   $editDescripttion = "Edit  User,  Full Name: ".$full_name;

			   if($action=="Add")
			   {
			      $checkLogin = $this->userModel->checkLoginName($login);
			      if($checkLogin == 1)
			      redirect("user/addForm/-2", "Location");
			      
				  $this->userModel->addUser($userID);
			      $this->generallib->addUserLog($userID,'-1','Insert',$addDscripttion);
			      redirect("user/userList/2", "Location");
			   }
			   else
			   {
			      $this->userModel->editUser($userID);
			      $this->generallib->addUserLog($userID,'-1','Edit',$editDescripttion);
			      redirect("user/userList/3", "Location");
			   }

			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Edit Form of Administrator
 * @copyright 2011
 */
 
	function editForm($id=0,$errorMesssage=0)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$userCURD = $this->session->userdata('userCURD');
			if($userCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
                $editInfo = $this->userModel->userEditInfo($id);
                if($editInfo==0)
                {
                   redirect("user/userList", "Location");
                }
                else
                {
                   foreach($editInfo as $row)
                   {
						$data['id']		= $row->id;
						$data['login']		= $row->login;
						$data['password']		= $row->password;
						$data['full_name']		= $row->full_name;
						$data['display_name']		= $row->display_name;
						$data['email']		= $row->email;
						$data['phone']		= $row->phone;
						$data['dept']		= $row->dept;
						$data['reseller_section']		= $row->reseller_section;
						$data['wholesale_section']		= $row->wholesale_section;
						$data['sip_section']		= $row->sip_section;
						$data['ani_section']		= $row->ani_section;
						$data['did_section']		= $row->did_section;
						$data['lots_section']		= $row->lots_section;
						$data['rate_section']		= $row->rate_section;
						$data['dialplan_section']		= $row->dialplan_section;
						$data['user_section']		= $row->user_section;
						$data['report_section']		= $row->report_section;
						$data['user_id']		= $row->user_id;
						$data['ipaddress']		= $row->ipaddress;
						$data['hidden']		= $row->hidden;
						$data['status']		= $row->status;
                   }
                   
	  				$data['action']		= 'Edit';
					$data['titleAccountMessage'] = TITLE072;
					$data['titlePersonalMessage'] = TITLE073;					
					if($errorMesssage == 0)
					$data['titleErrorMessage'] = FROMMESSAGE001;
					else
					$data['titleErrorMessage'] = FROMMESSAGE001;
	
					$this->load->view('user/edit_form',$data);
                }
			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Administrator Delete
 * @copyright 2011
 */
 

	function deleteUser($id)
	{
		if($this->clogin->is_logged_in())
		{
			$userID = $this->session->userdata('userID');
			$userCURD = $this->session->userdata('userCURD');
			if($userCURD != ALL_ACCESS)
			{
			  redirect("admin/cpanel/accessDenied", "Location");
			}
			else
			{
               $this->userModel->deleteUser($id);
               redirect("user/userList/4", "Location");
			}
		}
		else
		{
			redirect("login/index", "Location");
		}
	}
	
	
		

}
?>