visa_app/application/controllers/User_Controller.php
venbatechnologies@gmail.com 769edb667b profile
2018-08-09 10:27:59 +05:30

427 lines
15 KiB
PHP
Executable File

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/libraries/BaseController.php';
/**
* Class : User_Controller
* User_Controller Class to control all user related operations.
* @author : Kishor Mali
* @version : 1.1
* @since : 15 November 2016
*/
class User_Controller extends BaseController
{
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->load->model('User_Model');
$this->isLoggedIn();
}
/**
* This function used to load the first screen of the user
*/
public function index()
{
$this->global['pageTitle'] = 'VISA : Dashboard';
$this->loadViews("dashboard", $this->global, NULL , NULL);
}
/**
* This function used to load the User Details page
*/
function user_details_master(){
$this->global['pageTitle'] = 'Visa : User Details';
$data['getuserdetails']= $this->User_Model->Get_UserDetails();
$this->loadViews('user_details', $this->global,$data , NULL);
}
/**
* This function used to load the Profile Details page
*/
function Profile(){
$this->global['pageTitle'] = 'Visa : Profile Details';
//$userId=$this->global['userid'];
// echo $userId;
$data['getprofile']= $this->User_Model->Get_Profile($this->global['userid']);
$this->loadViews('profile', $this->global,$data , NULL);
}
/**
* To Store Register Details
*/
function Update_Profile(){
$RegNo = $this->input->post('HideRegNo'); // value getting from hidden field
$userId = $this->input->post('HideuserId'); // value getting from hidden field
$Registerdata =
array( 'Name'=>$this->input->post('Name'),
'PhoneNo'=>$this->input->post('PhoneNo'),
'Email'=>$this->input->post('Email'),
'AlternateEmail'=>$this->input->post('AlternateEmail'),
'Designation'=>$this->input->post('Designation'),
'CompanyName'=>$this->input->post('CompanyName'),
'CityName'=>$this->input->post('CityName'),
'CountryName'=>$this->input->post('CountryName'),
'Address'=>$this->input->post('Address'));
$Userdata =
array( 'email'=>$this->input->post('Email'),
'password'=>getHashedPassword($this->input->post('Password')),
'mobile'=>$this->input->post('PhoneNo'),
'roleId'=>REGISTER_USERS,
'name'=>$this->input->post('Name'));
$register= $this->User_Model->Update_Register($Registerdata,$RegNo);
$user= $this->User_Model->Update_User($Userdata,$userId);
if($register == True && $user == True)
{
echo"<script>alert('Updated Successfully');window.location.href='profile';</script>";
}
else{
echo"<script>alert('Not Saved Try Again');window.location.href='profile';</script>";
}
}
/**
* This function is used to load the user list
*/
function userListing()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$searchText = $this->security->xss_clean($this->input->post('searchText'));
$data['searchText'] = $searchText;
$this->load->library('pagination');
$count = $this->User_Model->userListingCount($searchText);
$returns = $this->paginationCompress ( "userListing/", $count, 10 );
$data['userRecords'] = $this->User_Model->userListing($searchText, $returns["page"], $returns["segment"]);
$this->global['pageTitle'] = 'CodeInsect : User Listing';
$this->loadViews("users", $this->global, $data, NULL);
}
}
/**
* This function is used to load the add new form
*/
function addNew()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$this->load->model('User_Model');
$data['roles'] = $this->User_Model->getUserRoles();
$this->global['pageTitle'] = 'CodeInsect : Add New User';
$this->loadViews("addNew", $this->global, $data, NULL);
}
}
/**
* This function is used to check whether email already exist or not
*/
function checkEmailExists()
{
$userId = $this->input->post("userId");
$email = $this->input->post("email");
if(empty($userId)){
$result = $this->User_Model->checkEmailExists($email);
} else {
$result = $this->User_Model->checkEmailExists($email, $userId);
}
if(empty($result)){ echo("true"); }
else { echo("false"); }
}
/**
* This function is used to add new user to the system
*/
function addNewUser()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$this->load->library('form_validation');
$this->form_validation->set_rules('fname','Full Name','trim|required|max_length[128]');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|max_length[128]');
$this->form_validation->set_rules('password','Password','required|max_length[20]');
$this->form_validation->set_rules('cpassword','Confirm Password','trim|required|matches[password]|max_length[20]');
$this->form_validation->set_rules('role','Role','trim|required|numeric');
$this->form_validation->set_rules('mobile','Mobile Number','required|min_length[10]');
if($this->form_validation->run() == FALSE)
{
$this->addNew();
}
else
{
$name = ucwords(strtolower($this->security->xss_clean($this->input->post('fname'))));
$email = $this->security->xss_clean($this->input->post('email'));
$password = $this->input->post('password');
$roleId = $this->input->post('role');
$mobile = $this->security->xss_clean($this->input->post('mobile'));
$userInfo = array('email'=>$email, 'password'=>getHashedPassword($password), 'roleId'=>$roleId, 'name'=> $name,
'mobile'=>$mobile, 'createdBy'=>$this->vendorId, 'createdDtm'=>date('Y-m-d H:i:s'));
$this->load->model('User_Model');
$result = $this->User_Model->addNewUser($userInfo);
if($result > 0)
{
$this->session->set_flashdata('success', 'New User created successfully');
}
else
{
$this->session->set_flashdata('error', 'User creation failed');
}
redirect('addNew');
}
}
}
/**
* This function is used load user edit information
* @param number $userId : Optional : This is user id
*/
function editOld($userId = NULL)
{
if($this->isAdmin() == TRUE || $userId == 1)
{
$this->loadThis();
}
else
{
if($userId == null)
{
redirect('userListing');
}
$data['roles'] = $this->User_Model->getUserRoles();
$data['userInfo'] = $this->User_Model->getUserInfo($userId);
$this->global['pageTitle'] = 'CodeInsect : Edit User';
$this->loadViews("editOld", $this->global, $data, NULL);
}
}
/**
* This function is used to edit the user information
*/
function editUser()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$this->load->library('form_validation');
$userId = $this->input->post('userId');
$this->form_validation->set_rules('fname','Full Name','trim|required|max_length[128]');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|max_length[128]');
$this->form_validation->set_rules('password','Password','matches[cpassword]|max_length[20]');
$this->form_validation->set_rules('cpassword','Confirm Password','matches[password]|max_length[20]');
$this->form_validation->set_rules('role','Role','trim|required|numeric');
$this->form_validation->set_rules('mobile','Mobile Number','required|min_length[10]');
if($this->form_validation->run() == FALSE)
{
$this->editOld($userId);
}
else
{
$name = ucwords(strtolower($this->security->xss_clean($this->input->post('fname'))));
$email = $this->security->xss_clean($this->input->post('email'));
$password = $this->input->post('password');
$roleId = $this->input->post('role');
$mobile = $this->security->xss_clean($this->input->post('mobile'));
$userInfo = array();
if(empty($password))
{
$userInfo = array('email'=>$email, 'roleId'=>$roleId, 'name'=>$name,
'mobile'=>$mobile, 'updatedBy'=>$this->vendorId, 'updatedDtm'=>date('Y-m-d H:i:s'));
}
else
{
$userInfo = array('email'=>$email, 'password'=>getHashedPassword($password), 'roleId'=>$roleId,
'name'=>ucwords($name), 'mobile'=>$mobile, 'updatedBy'=>$this->vendorId,
'updatedDtm'=>date('Y-m-d H:i:s'));
}
$result = $this->User_Model->editUser($userInfo, $userId);
if($result == true)
{
$this->session->set_flashdata('success', 'User updated successfully');
}
else
{
$this->session->set_flashdata('error', 'User updation failed');
}
redirect('userListing');
}
}
}
/**
* This function is used to delete the user using userId
* @return boolean $result : TRUE / FALSE
*/
function deleteUser()
{
if($this->isAdmin() == TRUE)
{
echo(json_encode(array('status'=>'access')));
}
else
{
$userId = $this->input->post('userId');
$userInfo = array('isDeleted'=>1,'updatedBy'=>$this->vendorId, 'updatedDtm'=>date('Y-m-d H:i:s'));
$result = $this->User_Model->deleteUser($userId, $userInfo);
if ($result > 0) { echo(json_encode(array('status'=>TRUE))); }
else { echo(json_encode(array('status'=>FALSE))); }
}
}
/**
* This function is used to load the change password screen
*/
function loadChangePass()
{
$this->global['pageTitle'] = 'CodeInsect : Change Password';
$this->loadViews("changePassword", $this->global, NULL, NULL);
}
/**
* This function is used to change the password of the user
*/
function changePassword()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('oldPassword','Old password','required|max_length[20]');
$this->form_validation->set_rules('newPassword','New password','required|max_length[20]');
$this->form_validation->set_rules('cNewPassword','Confirm new password','required|matches[newPassword]|max_length[20]');
if($this->form_validation->run() == FALSE)
{
$this->loadChangePass();
}
else
{
$oldPassword = $this->input->post('oldPassword');
$newPassword = $this->input->post('newPassword');
$resultPas = $this->User_Model->matchOldPassword($this->vendorId, $oldPassword);
if(empty($resultPas))
{
$this->session->set_flashdata('nomatch', 'Your old password not correct');
redirect('loadChangePass');
}
else
{
$usersData = array('password'=>getHashedPassword($newPassword), 'updatedBy'=>$this->vendorId,
'updatedDtm'=>date('Y-m-d H:i:s'));
$result = $this->User_Model->changePassword($this->vendorId, $usersData);
if($result > 0) { $this->session->set_flashdata('success', 'Password updation successful'); }
else { $this->session->set_flashdata('error', 'Password updation failed'); }
redirect('loadChangePass');
}
}
}
/**
* Page not found : error 404
*/
function pageNotFound()
{
$this->global['pageTitle'] = 'CodeInsect : 404 - Page Not Found';
$this->loadViews("404", $this->global, NULL, NULL);
}
/**
* This function used to show login history
* @param number $userId : This is user id
*/
function loginHistoy($userId = NULL)
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$userId = ($userId == NULL ? $this->session->userdata("userId") : $userId);
$searchText = $this->input->post('searchText');
$fromDate = $this->input->post('fromDate');
$toDate = $this->input->post('toDate');
$data["userInfo"] = $this->User_Model->getUserInfoById($userId);
$data['searchText'] = $searchText;
$data['fromDate'] = $fromDate;
$data['toDate'] = $toDate;
$this->load->library('pagination');
$count = $this->User_Model->loginHistoryCount($userId, $searchText, $fromDate, $toDate);
$returns = $this->paginationCompress ( "login-history/".$userId."/", $count, 5, 3);
$data['userRecords'] = $this->User_Model->loginHistory($userId, $searchText, $fromDate, $toDate, $returns["page"], $returns["segment"]);
$this->global['pageTitle'] = 'CodeInsect : User Login History';
$this->loadViews("loginHistory", $this->global, $data, NULL);
}
}
}
?>