Files
2026-04-27 10:31:17 +07:00

735 lines
22 KiB
PHP

<?php
class Usergroupv4 extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "USER GROUP API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
}
function lookupuser(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm['id'];
$sql = "select M_UserID as id,
M_UserM_UserGroupID as usergroupid,
M_UserEmail as username,
IF(M_StaffIsActive = 'N','',M_StaffName) as staffname,
M_UserM_StaffID as xstaff,
M_UserDefaultT_SampleStationID as xsamplestation,
'N' as iscoordinator,
M_UserR_ReportGroupID as xreport,
'xxx' as action
from m_user
join m_staff oN M_UserM_StaffID = M_StaffID
where
M_UserM_UserGroupID = {$id} AND M_UserIsActive = 'Y'";
// echo $sql;
$rows = $this->db_onedev->query($sql)->result();
$result = array ("total" => count($rows), "records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function lookup()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = $prm['search'];
$all = $prm['all'];
$limit = '';
if($all == 'N'){
$limit = ' LIMIT 10';
}
$sql = "select COUNT(*) as total
from m_usergroup
where
M_UserGroupIsActive = 'Y'";
$sql_param = array($search);
$total = $this->db_onedev->query($sql,$sql_param)->row()->total;
$sql = "select M_UserGroupID as id, M_UserGroupDashboard as dashboard,
M_UserGroupName as name, M_UserGroupIsClinic as clinic, M_UserGroupName as description , 'xxx' as usergrouptype
from m_usergroup
where
M_UserGroupName LIKE CONCAT('%','{$search}','%') AND
M_UserGroupIsActive = 'Y' $limit";
$sql_param = array($search);
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
if ($query) {
$rows = $query->result_array();
} else {
$this->sys_error_db("m_usergroup select",$this->db_onedev);
exit;
}
$result = array ("total" => $total, "total_filter"=>count($rows),"records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function getdashboards()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$group_id = $prm['group_id'];
$sql = "select menu.S_MenuID as id, CONCAT(menu.S_MenuName,' [ ',menuparent.S_MenuName,' ]') as name, CONCAT('one-ui/',menu.S_MenuUrl) as url, menuparent.S_MenuName as group_name
FROM s_menu menu
LEFT JOIN s_menu menuparent ON menu.S_MenuParentS_MenuID = menuparent.S_MenuID
WHERE
menu.S_MenuUrl <> '#' AND menu.S_MenuIsActive = 'Y'";
$sql_param = array($search);
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
if ($query) {
$rows = $query->result_array();
} else {
$this->sys_error_db("m_usergroup select",$this->db_onedev);
exit;
}
$result = array ("total" => $total, "total_filter"=>count($rows),"records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function addnewusergroup()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$name_usergroup = $prm['name'];
$dashboard_usergroup = $prm['dashboard'];
$clinic_usergroup = $prm['clinic'];
$query = "SELECT COUNT(*) as exist FROM m_usergroup WHERE M_UserGroupIsActive = 'Y' AND M_UserGroupName = '{$name_usergroup}'";
$exist_name = $this->db_onedev->query($query)->row()->exist;
//echo $exist_name;
if($exist_name == 0 ){
$sql = "insert into m_usergroup(
M_UserGroupName,
M_UserGroupDashboard,
M_UserGroupIsClinic,
M_UserGroupCreated,
M_UserGroupLastUpdated
)
values( ?, ?, ?,now(), now())";
$query = $this->db_onedev->query($sql,
array(
$name_usergroup,
$dashboard_usergroup,
$clinic_usergroup
)
);
//echo $this->db_onedev->last_query();
if (!$query) {
$this->sys_error_db("m_usergroup insert");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} else{
$errors = array();
if($exist_name != 0){
array_push($errors,array('field'=>'name','msg'=>'Nama sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function editusergroup()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$id_usergroup = $prm['id'];
$name_usergroup = $prm['name'];
$dashboard_usergroup = $prm['dashboard'];
$clinic_usergroup = $prm['clinic'];
$query = "SELECT COUNT(*) as exist FROM m_usergroup WHERE M_UserGroupIsActive = 'Y' AND M_UserGroupName = '{$name_usergroup}'
AND M_UserGroupID <> {$id_usergroup} ";
$exist_name = $this->db_onedev->query($query)->row()->exist;
if($exist_name == 0){
$sql = "update m_usergroup SET
M_UserGroupName = ?,
M_UserGroupDashboard = ?,
M_UserGroupIsClinic = ?,
M_UserGroupLastUpdated = now()
where
M_UserGroupID = ?
";
$query = $this->db_onedev->query($sql,
array(
$name_usergroup,
$dashboard_usergroup,
$clinic_usergroup,
$id_usergroup
)
);
//file_put_contents("/tmp/adi-update-user.sql",$this->db_onedev->last_query());
//echo $query;
if (!$query) {
$this->sys_error_db("m_usergroup update");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => $id_usergroup));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_name != 0){
array_push($errors,array('field'=>'name','msg'=>'Nama sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function getreportsample(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$rows = [];
$query =" SELECT *
FROM r_reportgroup
WHERE
R_ReportGroupIsActive = 'Y'
";
//echo $query;
$rows['reports'] = $this->db_onedev->query($query)->result_array();
$query =" SELECT *
FROM t_samplestation
WHERE
T_SampleStationIsActive = 'Y'
";
//echo $query;
$rows['samplestations'] = $this->db_onedev->query($query)->result_array();
$query =" SELECT *
FROM m_usergroup
WHERE
M_UserGroupIsActive = 'Y'
";
//echo $query;
$rows['usergroupnames'] = $this->db_onedev->query($query)->result_array();
$query =" SELECT *
FROM m_staff
WHERE
M_StaffIsActive = 'Y'
";
//echo $query;
$rows['staffs'] = $this->db_onedev->query($query)->result_array();
$result = array(
"total" => count($rows) ,
"records" => $rows,
);
$this->sys_ok($result);
exit;
}
public function edituser()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$userid = $prm['xid'];
$username = $prm['username'];
$xuserid = $this->sys_user["M_UserID"];
$password = $prm['password'];
$md5_password = md5($this->one_salt . $prm["password"] . $this->one_salt);
$xstaff = $prm['xstaff'];
$xsamplestation = $prm['xsamplestation'];
$xreport = $prm['xreport'];
$xusergroupname = $prm['xusergroupname'];
$iscoordinator = $prm['iscoordinator'];
$query = "SELECT COUNT(*) as exist FROM m_user WHERE M_UserIsActive = 'Y' AND M_UserEmail = '{$username}'
and M_UserID <> $userid ";
$exist_username = $this->db_onedev->query($query)->row()->exist;
if($exist_username == 0){
$sql = "SELECT * FROM m_user WHERE M_UserID = '{$prm['xid']}'";
$data_before = $this->db_onedev->query($sql)->row_array();
$json_before = json_encode($data_before);
$sql = "update m_user SET
M_UserEmail = ?,
M_UserM_StaffID = ?,
M_UserR_ReportGroupID = ?,
M_UserM_UserGroupID = ?,
M_UserLastUpdated = now(),
M_UserLastUpdatedUserID = ?
where M_UserID = ? ";
$query = $this->db_onedev->query($sql,
array(
$username,
$xstaff["M_StaffID"],
$xreport["R_ReportGroupID"],
$xusergroupname["M_UserGroupID"],
$xuserid,
$userid
));
if (!$query) {
$this->sys_error_db("m_user update",$this->db_onedev);
exit;
}
$sql = "SELECT * FROM m_user WHERE M_UserID = '{$prm['xid']}'";
$data_after = $this->db_onedev->query($sql)->row_array();
$json_after = json_encode($data_after);
$sql = "INSERT INTO cpone_log.log_user (
Log_UserM_UserID,
Log_UserDate,
Log_UserStatus,
Log_UserJSONBefore,
Log_UserJSONAfter,
Log_UserCreated,
Log_UserCreatedUserID
)
VALUES(
{$prm['xid']},
CURDATE(),
'UPDATE',
'{$json_before}',
'{$json_after}',
NOW(),
{$userid}
)";
$this->db_onedev->query($sql);
$result = array ("total" => 1, "records" => array("xid" => $userid));
$this->sys_ok($result);
} else {
$errors = array();
if($exist_name != 0){
array_push($errors,array('field'=>'username','msg'=>'Nama sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function addnewuser()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$usergroupid = $prm['usergroupid'];
$username = $prm['username'];
$xuserid = $this->sys_user["M_UserID"];
$password = $prm['password'];
$md5_password = md5($this->one_salt . $prm["password"] . $this->one_salt);
$xstaff = $prm['xstaff'];
$xsamplestation = $prm['xsamplestation'];
$xreport = $prm['xreport'];
$iscoordinator = $prm['iscoordinator'];
if($prm['xid'] == 0){
$query = "SELECT COUNT(*) as exist FROM m_user WHERE M_UserIsActive = 'Y' AND M_UserEmail = '{$username}'";
$exist_username = $this->db_onedev->query($query)->row()->exist;
//echo $this->db_onedev->last_query();
if($exist_username == 0){
$sql = "insert into m_user(
M_UserM_UserGroupID,
M_UserEmail,
M_UserPassword,
M_UserM_StaffID,
M_UserR_ReportGroupID,
M_UserCreated,
M_UserCreatedUserID,
M_UserLastUpdated
)
values( ?,?,?,?,?,now(),?,now())";
$query = $this->db_onedev->query($sql,
array(
$usergroupid,
$username,
$md5_password,
$xstaff["M_StaffID"],
$xreport["R_ReportGroupID"],
$xuserid
)
);
if (!$query) {
$this->sys_error_db("m_user insert",$this->db_onedev);
exit;
}
$last_id = $this->db_onedev->insert_id();
$sql = "SELECT * FROM m_user WHERE M_UserID = '{$last_id}'";
$data_after = $this->db_onedev->query($sql)->row_array();
$json_after = json_encode($data_after);
$sql = "INSERT INTO cpone_log.log_user (
Log_UserM_UserID,
Log_UserDate,
Log_UserStatus,
Log_UserJSONAfter,
Log_UserCreated,
Log_UserCreatedUserID
)
VALUES(
{$last_id},
CURDATE(),
'NEW',
'{$json_after}',
NOW(),
{$xuserid}
)";
$this->db_onedev->query($sql);
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_username != 0){
array_push($errors,array('field'=>'username','msg'=>'Nama User sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
}else{
$query = "SELECT COUNT(*) as exist FROM m_user WHERE M_UserIsActive = 'Y' AND M_UserUsername = '{$username}' AND M_UserID <> {$prm['xid']}";
$exist_username = $this->db_onedev->query($query)->row()->exist;
//echo $query;
//echo $query;
if($exist_username == 0 ){
$sql = "SELECT * FROM m_user WHERE M_UserID = '{$prm['xid']}'";
$data_before = $this->db_onedev->query($sql)->row_array();
$json_before = json_encode($data_before);
$sql = "UPDATE m_user SET M_UserUsername = '{$username}', M_UserPassword = '{$password}', M_UserM_StaffID = '{$xstaff}', M_UserR_ReportGroupID = '{$xreport}', M_UserDefaultT_SampleStationID = '{$xsamplestation}', M_UserIsCoordinator = '{$iscoordinator}', M_UserLastUpdatedUserID = '{$xuserid}', M_UserLastUpdated = now() WHERE M_UserID = '{$prm['xid']}'";
//echo $sql;
$query = $this->db_onedev->query($sql);
$sql = "SELECT * FROM m_user WHERE M_UserID = '{$prm['xid']}'";
$data_after = $this->db_onedev->query($sql)->row_array();
$json_after = json_encode($data_after);
$sql = "INSERT INTO cpone_log.log_user (
Log_UserM_UserID,
Log_UserDate,
Log_UserStatus,
Log_UserJSONBefore,
Log_UserJSONAfter,
Log_UserCreated,
Log_UserCreatedUserID
)
VALUES(
{$prm['xid']},
CURDATE(),
'UPDATE',
'{$json_before}',
'{$json_after}',
NOW(),
{$xuserid}
)";
$this->db_onedev->query($sql);
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_username != 0){
array_push($errors,array('field'=>'username','msg'=>'Username sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function generate_string($input, $strength = 4) {
$input_length = strlen($input);
$random_string = '';
for($i = 0; $i < $strength; $i++) {
$random_character = $input[mt_rand(0, $input_length - 1)];
$random_string .= $random_character;
}
return $random_string;
}
function reset_password(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm['id'];
$userid = $this->sys_user["M_UserID"];
$permitted_chars = 'wertyuioplkjhgfdsazxcvbnm0123456789';
$new_password = $this->generate_string($permitted_chars, 9);
$md5_password =md5($this->one_salt . $new_password . $this->one_salt);
$sql = "SELECT * FROM m_user WHERE M_UserID = {$id}";
$data_before = $this->db_onedev->query($sql)->row_array();
$json_before = json_encode($data_before);
//echo $new_password;
$sql = "UPDATE m_user SET M_UserPassword = '{$md5_password}', M_UserLastUpdated = NOW(), M_UserLastUpdatedUserID = {$userid} WHERE M_UserID = {$id}";
// echo $sql;
$rows = $this->db_onedev->query($sql);
$sql = "SELECT * FROM m_user WHERE M_UserID = {$id}";
$data_after = $this->db_onedev->query($sql)->row_array();
$json_after = json_encode($data_after);
$sql = "INSERT INTO cpone_log.log_user (
Log_UserM_UserID,
Log_UserDate,
Log_UserStatus,
Log_UserJSONBefore,
Log_UserJSONAfter,
Log_UserCreated,
Log_UserCreatedUserID
)
VALUES(
{$id},
CURDATE(),
'RESET_PWD',
'{$json_before}',
'{$json_after}',
NOW(),
{$userid}
)";
$this->db_onedev->query($sql);
$result = array ("password" => $new_password);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function deleteuser()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$sql = "update m_user SET
M_UserEmail = CONCAT(M_UserEmail,'-DELETED'),
M_UserIsActive = 'N',
M_UserPassword = '1234567890XD3L3T3US312',
M_UserActiveToken = NULL,
M_UserIsLoggedIn = 'N',
M_UserLastUpdated = now()
WHERE
M_UserID = ?
";
$query = $this->db_onedev->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("m_user delete");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function deleteusergroup()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$sql = "update m_usergroup SET
M_UserGroupIsActive = 'N',
M_UserGroupLastUpdated = now()
WHERE
M_UserGroupID = ?
";
$query = $this->db_onedev->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("m_usergroup delete");
exit;
}
$sql = "UPDATE m_user SET
M_UserIsActive = 'N',
M_UserLastUpdated = now()
WHERE
M_UserM_UserGroupID = ?
";
$query = $this->db_onedev->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("m_user delete");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}