nhance/app/Controllers/UserController.php
2025-07-15 11:30:07 +05:30

284 lines
9.8 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\UserModel;
use App\Models\RoleModel;
use App\Models\TeamModel;
use App\Models\UserTeamsModel;
use App\Helpers\BookStackUserHelper;
use App\Helpers\AuthHistoryModel;
use App\Models\UserActivityHistoryModel;
class UserController extends AdminController
{
protected $myLogger;
protected $userModel;
protected $roleModel;
protected $teamModel;
protected $userTeamsModel;
protected $bookStack;
public function __construct()
{
set_session_context('User');
$this->myLogger = \Config\Services::mylogger();
$this->userModel = new UserModel();
$this->roleModel = new RoleModel();
$this->teamModel = new TeamModel();
$this->userTeamsModel = new UserTeamsModel();
$this->bookStack = new BookStackUserHelper();
$this->authHistoryModel = new AuthHistoryModel();
$this->userActivityHistoryModel = new UserActivityHistoryModel();
}
public function list()
{
$data['page_name'] = 'User';
$this->myLogger->logme('error','User list function called');
$data['UserList'] = $this->userModel->getUserList();
// echo '<pre>';
// print_r($data); die;
$data['roleData'] = $this->roleModel->select('id, role')->findAll();
$data['teamData'] = $this->teamModel->select('id, name')->findAll();
$this->loadLayout('UserList', $data);
}
public function create()
{
$this->myLogger->logme('error', 'User create function called');
$teams = $this->request->getPost('team');
//if this is get method return to user creation page
if (!$this->request->getPost()) {
return redirect()->to(base_url('/user/list'));
} else {
$userData = $this->request->getPost();
$userData['created_by'] = get_session_userid();
$temp_team = $userData['team'];
unset($userData['team']);
$insert = $this->userModel->insert($userData);
$bookStackData = [
'name' => $userData['first_name'],
'email' => $userData['email'],
];
$this->bookStack->createEditUser($bookStackData);
if ($insert) {
$teamData['user_id'] = $insert;
foreach ($teams as $value) {
$teamData['team_id'] = $value;
$teamData['created_by'] = get_session_userid();
$this->userTeamsModel->insert($teamData);
}
$db = \Config\Database::connect();
$tableName = 'hdz_staff';
// Set default values
$admin = 0;
$acm = 0;
$acm_id = null;
if ($userData['role'] == 3) {
$admin = 0;
$acm = 1;
$acm_id = $insert;
} else if (in_array($userData['role'], [1, 5])) {
$admin = 1;
$acm = 0;
$acm_id = null;
}
$password = '12345678'; // Default password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$hdz_staff = [
'emp_code' => $userData['emp_code'],
'fullname' => $userData['first_name'],
'username' => strtolower($userData['first_name']),
'email' => $userData['email'],
'admin' => $admin,
'acm' => $acm,
'acm_id' => $acm_id,
'registration' => time(),
'password' => $hashedPassword,
'active' => 1,
'department' => 'a:7:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"5";i:3;s:1:"3";i:4;s:1:"9";i:5;s:1:"4";i:6;s:2:"10";}',
];
$db->table($tableName)->insert($hdz_staff);
}
}
$this->myLogger->logme('error', 'User create Successfully created by id {data}', ['data' => get_session_userid()]);
return redirect()->to(base_url('/user/list'));
}
public function getuser($id = null)
{
$userTeamData = $this->userTeamsModel->where('user_id', $id)->findAll();
$data = $this->userModel->getUserById($id);
if($data){
echo json_encode(array("status" => true , 'data' => $data, 'userTeamData' => $userTeamData));
}else{
echo json_encode(array("status" => false));
}
}
public function edit()
{
if (!$this->request->getPost()) {
return redirect()->to(base_url('/user/list'));
} else {
$id = $this->request->getPost('PrimaryKey');
$teams = $this->request->getPost('team');
$userData = $this->request->getPost();
unset($userData['csrf_test_name']);
unset($userData['PrimaryKey']);
$existingData = $this->userModel->where('id',$id)->first();;
// Update data in the 'users' table based on the $id
$userData['updated_by'] = get_session_userid();
$update = $this->userModel->where('id', $id)->set($userData)->update();
$bookStackData = [
'name' => $userData['first_name'],
'email' => $userData['email'],
];
$this->bookStack->createEditUser($bookStackData,$existingData);
if ($update) {
if ($teams) {
$this->userTeamsModel->where('user_id', $id)->delete();
$teamData['user_id'] = $id;
foreach ($teams as $value) {
$teamData['team_id'] = $value;
$teamData['created_by'] = get_session_userid();
$this->userTeamsModel->insert($teamData);
}
}
$db = \Config\Database::connect();
$tableName = 'hdz_staff';
$hdz_staff = [
'emp_code' => $userData['emp_code'],
'fullname' => $userData['first_name'],
'username' => strtolower($userData['first_name']),
'email' => $userData['email'],
'registration' => time(),
'active' => 1,
'department' => 'a:7:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"5";i:3;s:1:"3";i:4;s:1:"9";i:5;s:1:"4";i:6;s:2:"10";}',
];
if ($userData['role'] == 3) {
$hdz_staff['admin'] = 0;
$hdz_staff['acm'] = 1;
$hdz_staff['acm_id'] = $id;
} elseif (in_array($userData['role'], [1, 5])) {
$hdz_staff['admin'] = 1;
$hdz_staff['acm'] = 0;
$hdz_staff['acm_id'] = null;
} else {
return redirect()->to(base_url('/user/list'));
}
// Check if staff data exists
$staffData = $db->table($tableName)
// ->where('emp_code', $userData['emp_code'])
->where('email', $userData['email'])
->get()->getResult();
if (!empty($staffData)) {
// Update existing record
$db->table($tableName)
// ->where('emp_code', $userData['emp_code'])
->where('email', $userData['email'])
->set($hdz_staff)->update();
} else {
$password = '12345678'; // Default password
$hdz_staff['password'] = password_hash($password, PASSWORD_DEFAULT);
// Insert new record
$db->table($tableName)->insert($hdz_staff);
}
}
return redirect()->to(base_url('/user/list'));
}
}
public function deactive($id = null)
{
$model = new UserModel();
$emailToDelete = $model->where('id', $id)->first();
$deactive = $model->where('id', $id)->set(['is_active' => 0])->update();
$this->bookStack->deleteUser($emailToDelete);
if($deactive)
{
// $db = \Config\Database::connect();
// $tableName = 'hdz_staff';
// $db->table($tableName)->insert($hdz_staff);
echo json_encode(array("status" => true));
}else{
echo json_encode(array("status" => false));
}
}
public function getRolesAndTeams()
{
$roleData = $this->roleModel->select('id, role')->findAll();
$teamData = $this->teamModel->select('id, name')->findAll();
echo json_encode(array("status" => true , 'roleData' => $roleData, 'teamData' => $teamData,));
}
public function getUserActivityHistory()
{
$user_id = $this->request->getVar('user_id');
$pre_hr_id = $this->request->getVar('pre_hr_id');
// echo $user_id.' - '.$pre_hr_id;
$field_for_where_condition = 'user_id';
if($user_id == 0 || $user_id == NULL)
{
$field_for_where_condition = 'pre_hr_id';
$user_id = $pre_hr_id;
}
//get login history
$auth_data = $this->authHistoryModel->where($field_for_where_condition,$user_id);
//get activity history
$activity_data = $this->userActivityHistoryModel->where();
}
}