service); //CALL TO API FOR User Role Assignments $api_url = '/v2/user_role_assignments/user_id='.$user_ID; $role_assignments = ioServer($api_url,''); //Decode Payload if (!empty($role_assignments)){$role_assignments = json_decode($role_assignments);}else{$role_assignments = null;} //CALL TO API FOR All Available Roles $api_url = '/v2/user_roles/status=1&p=1'; $all_roles_response = ioServer($api_url,''); //Decode Payload if (!empty($all_roles_response)){ $all_roles = json_decode($all_roles_response); if (!is_array($all_roles)){ $all_roles = []; } } else { $all_roles = []; } //------------------------------ // Handle POST for inline edit (user AND roles) //------------------------------ if (isset($_POST['save_user']) && $update_allowed_edit === 1) { // Build user data using existing field names $user_data = [ 'id' => $user_ID, 'userkey' => $_POST['userkey'] ?? 1, 'username' => $_POST['username'] ?? '', 'email' => $_POST['email'] ?? '', 'view' => $_POST['view'] ?? 3, 'settings' => $_POST['settings'] ?? '', 'service' => $_POST['service'] ?? 0, 'language' => $_POST['language'] ?? '', 'login_count' => $_POST['login_count'] ?? 0, 'salesid' => $_POST['salesid'] ?? '', 'soldto' => $_POST['soldto'] ?? '', 'shipto' => $_POST['shipto'] ?? '', 'location' => $_POST['location'] ?? '' ]; $data = json_encode($user_data, JSON_UNESCAPED_UNICODE); ioServer('/v2/users', $data); // Also save role assignments $role_data = [ 'batch_update' => true, 'user_id' => (int)$user_ID, 'roles' => isset($_POST['roles']) ? array_map('intval', $_POST['roles']) : [] ]; $data = json_encode($role_data, JSON_UNESCAPED_UNICODE); ioServer('/v2/user_role_assignments', $data); // Redirect to refresh header('Location: index.php?page=user&id='.$user_ID.'&success_msg=2'); exit; } // Handle password reset if (isset($_POST['reset']) && $update_allowed_edit === 1) { $data = json_encode(['id' => $user_ID, 'reset' => 'reset'], JSON_UNESCAPED_UNICODE); ioServer('/v2/users', $data); header('Location: index.php?page=user&id='.$user_ID.'&success_msg=4'); exit; } // Handle unblock if (isset($_POST['unblock']) && $update_allowed_edit === 1) { $data = json_encode(['id' => $user_ID, 'login_count' => '0'], JSON_UNESCAPED_UNICODE); ioServer('/v2/users', $data); header('Location: index.php?page=user&id='.$user_ID.'&success_msg=5'); exit; } // Handle delete if (isset($_POST['delete']) && $delete_allowed === 1) { $data = json_encode(['id' => $user_ID, 'delete' => 'delete'], JSON_UNESCAPED_UNICODE); ioServer('/v2/users', $data); header('Location: index.php?page=users&success_msg=3'); exit; } //------------------------------ //Variables //------------------------------ $is_blocked = ($user->login_count > 4); $is_active = ($user->userkey && $user->userkey != ''); if ($is_blocked) { $status_text = ($User_block ?? 'Blocked'); $status_class = 'id0'; } elseif ($is_active) { $status_text = ($enabled ?? 'Active'); $status_class = 'id1'; } else { $status_text = ($disabled ?? 'Inactive'); $status_class = 'id0'; } // Handle success messages if (isset($_GET['success_msg'])) { if ($_GET['success_msg'] == 1) { $success_msg = ($message_user_1 ?? 'User created successfully'); } if ($_GET['success_msg'] == 2) { $success_msg = ($message_user_2 ?? 'User updated successfully'); } if ($_GET['success_msg'] == 3) { $success_msg = ($message_user_3 ?? 'User deleted successfully'); } if ($_GET['success_msg'] == 4) { $success_msg = ($message_user_4 ?? 'Password reset successfully'); } if ($_GET['success_msg'] == 5) { $success_msg = ($message_user_5 ?? 'User unblocked successfully'); } if ($_GET['success_msg'] == 6) { $success_msg = ($message_user_6 ?? 'Roles updated successfully'); } } template_header(($user_title ?? 'User'), 'user', 'view'); $view = '

'.($user_h2 ?? 'User').' - '.$user->username.'

'; if ($update_allowed_edit === 1){ $view .= '✏️'; $view .= ''; } $view .= '
'; if (isset($success_msg)){ $view .= '

'.$success_msg.'

'; } // Start form wrapper for edit mode $view .= '
'; $view .= '
'; // User Information Block $view .= '
'.($view_user_information ?? 'User Information').'

'.($general_status ?? 'Status').'

'.$status_text.'

'.($User_username ?? 'Username').'

'.$user->username.'

'.($User_email ?? 'Email').'

'.$user->email.'

'.($User_language ?? 'Language').'

'.($user->language ?? '-').'

'; // Role Assignments Block $view .='
'.($view_user_roles ?? 'Assigned Roles').'
'; // Get list of already assigned role IDs $assigned_role_ids = []; if (!empty($role_assignments)){ foreach ($role_assignments as $assignment){ if ($assignment->is_active == 1){ $assigned_role_ids[] = $assignment->role_id; } } } // VIEW MODE - Show only assigned roles if (!empty($role_assignments)){ $has_active_roles = false; foreach ($role_assignments as $assignment){ if ($assignment->is_active == 1){ $has_active_roles = true; $view .= '

'.$assignment->role_name.'

'; if (!empty($assignment->role_description)){ $view .= '

'.$assignment->role_description.'

'; } $view .= '
'; } } if (!$has_active_roles){ $view .= '

-

'.($no_roles_assigned ?? 'No roles assigned to this user').'

'; } } else { $view .= '

-

'.($no_roles_assigned ?? 'No roles assigned to this user').'

'; } $view .= '
'; // Close view-mode-roles // EDIT MODE - Show all roles with checkboxes (only if user has edit permission) if ($update_allowed_edit === 1 && !empty($all_roles)){ $view .= ''; // Close edit-mode-roles } $view .= '
'; // Close content-block $view .= '
'; // Close content-block-wrapper // Permissions Block $view .= '
'.($view_user_permissions ?? 'Permissions').'
'.($User_permission ?? 'Permission Level').' '; // Display permission level text switch($user->view){ case 1: $view .= ($permission1 ?? 'View'); break; case 2: $view .= ($permission2 ?? 'Edit'); break; case 3: $view .= ($permission3 ?? 'Admin'); break; case 4: $view .= ($permission4 ?? 'Super Admin'); break; case 5: $view .= ($permission5 ?? 'System'); break; default: $view .= '-'; } $view .= '
'.($User_profile ?? 'Profile').' '.($user->settings ?? '-').''; if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){ $view .= ''; } else { $view .= ''; } $view .= '
'.($User_service ?? 'Service Access').' '.(($service_active == 1) ? ($enabled ?? 'Enabled') : ($disabled ?? 'Disabled')).'
'; // Partner Hierarchy Block $partner_data = json_decode($user->partnerhierarchy) ?? json_decode($_SESSION['partnerhierarchy']); $view .= '
'.($view_user_partners ?? 'Partner Hierarchy').'
'; if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){ $salesid_dropdown = listPartner('salesid', $_SESSION['permission'], $partner_data->salesid ?? '', ''); $soldto_dropdown = listPartner('soldto', $_SESSION['permission'], $partner_data->soldto ?? '', ''); $view .= ''; } $shipto_dropdown = listPartner('shipto', $_SESSION['permission'], $partner_data->shipto ?? '', ''); $location_dropdown = listPartner('location', $_SESSION['permission'], $partner_data->location ?? '', ''); $view .= '
'.($general_salesid ?? 'Sales ID').' '.($partner_data->salesid ?? '-').'
'.($general_soldto ?? 'Sold To').' '.($partner_data->soldto ?? '-').'
'.($general_shipto ?? 'Ship To').' '.($partner_data->shipto ?? '-').'
'.($general_location ?? 'Location').' '.($partner_data->location ?? '-').'
'; // Metadata Block $view .= '
'.($tab3 ?? 'Details').'
'.($general_created ?? 'Created').' '.getRelativeTime($user->created).'
'.($User_lastlogin ?? 'Last Login').' '.($user->lastlogin ? getRelativeTime($user->lastlogin) : '-').'
'.($general_updated ?? 'Updated').' '.($user->updated ? getRelativeTime($user->updated) : '-').'
'.($general_updatedby ?? 'Updated By').' '.($user->updatedby ?? '-').'
'.($User_pw_login_count ?? 'Login Attempts').' '.$user->login_count.''; if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){ $view .= ''; } else { $view .= ''; } $view .= '
'; // Actions Block (outside form for separate actions) if ($update_allowed_edit === 1){ $view .= '
'.($general_actions ?? 'Actions').'
'; if ($is_blocked){ $view .= ' '; } if ($delete_allowed === 1){ $view .= ' '; } $view .= '
'.($User_pw_reset ?? 'Reset Password').'
'.($User_unblock ?? 'Unblock User').'
'.($general_delete ?? 'Delete User').'
'; } //OUTPUT echo $view; $js = 'var userEditMode = false; function toggleUserEdit() { userEditMode = !userEditMode; var editBtn = document.getElementById("editBtn"); var saveBtn = document.getElementById("saveBtn"); var viewElements = document.querySelectorAll(".view-mode"); var editElements = document.querySelectorAll(".edit-mode"); var viewRolesElements = document.querySelectorAll(".view-mode-roles"); var editRolesElements = document.querySelectorAll(".edit-mode-roles"); var i; if (userEditMode) { // Enter edit mode for user info AND roles for (i = 0; i < viewElements.length; i++) { viewElements[i].style.display = "none"; } for (i = 0; i < editElements.length; i++) { editElements[i].style.display = "inline"; } for (i = 0; i < viewRolesElements.length; i++) { viewRolesElements[i].style.display = "none"; } for (i = 0; i < editRolesElements.length; i++) { editRolesElements[i].style.display = "block"; } editBtn.style.display = "none"; saveBtn.style.display = "inline-block"; } else { // Exit edit mode for (i = 0; i < viewElements.length; i++) { viewElements[i].style.display = "inline"; } for (i = 0; i < editElements.length; i++) { editElements[i].style.display = "none"; } for (i = 0; i < viewRolesElements.length; i++) { viewRolesElements[i].style.display = "block"; } for (i = 0; i < editRolesElements.length; i++) { editRolesElements[i].style.display = "none"; } editBtn.style.display = "inline-block"; saveBtn.style.display = "none"; } }'; template_footer($js);