From fa2f293835e9e1125b46bd61bf547abf40003697 Mon Sep 17 00:00:00 2001 From: solocla Date: Thu, 7 May 2026 14:39:50 +0200 Subject: [PATCH] added roles edit into employees --- public/userarea/employees.php | 125 ++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 4 deletions(-) diff --git a/public/userarea/employees.php b/public/userarea/employees.php index ce02399..2b25e30 100644 --- a/public/userarea/employees.php +++ b/public/userarea/employees.php @@ -26,6 +26,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $hire_date = trim($_POST['hire_date'] ?? ''); $status = trim($_POST['status'] ?? 'active'); $auth_user_id = $_POST['auth_user_id'] !== '' ? (int)$_POST['auth_user_id'] : null; + $role_id = $_POST['role_id'] !== '' ? (int)$_POST['role_id'] : null; if ($first_name === '' || $last_name === '') { echo json_encode([ @@ -55,6 +56,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj 'status' => $status ]); + if ($auth_user_id !== null && $role_id !== null) { + $checkRole = $pdo->prepare("SELECT COUNT(*) FROM auth_roles WHERE id = ?"); + $checkRole->execute([$role_id]); + + if ((int)$checkRole->fetchColumn() > 0) { + $stmtRole = $pdo->prepare("UPDATE auth_users SET role_id = :role_id, updated_at = NOW() WHERE id = :auth_user_id"); + $stmtRole->execute([ + 'role_id' => $role_id, + 'auth_user_id' => $auth_user_id + ]); + } + } + echo json_encode(['success' => true]); exit; } @@ -70,6 +84,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj $hire_date = trim($_POST['hire_date'] ?? ''); $status = trim($_POST['status'] ?? 'active'); $auth_user_id = $_POST['auth_user_id'] !== '' ? (int)$_POST['auth_user_id'] : null; + $role_id = $_POST['role_id'] !== '' ? (int)$_POST['role_id'] : null; if ($id <= 0) { echo json_encode(['success' => false, 'message' => 'Invalid employee ID.']); @@ -112,6 +127,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj 'id' => $id ]); + if ($auth_user_id !== null && $role_id !== null) { + $checkRole = $pdo->prepare("SELECT COUNT(*) FROM auth_roles WHERE id = ?"); + $checkRole->execute([$role_id]); + + if ((int)$checkRole->fetchColumn() > 0) { + $stmtRole = $pdo->prepare("UPDATE auth_users SET role_id = :role_id, updated_at = NOW() WHERE id = :auth_user_id"); + $stmtRole->execute([ + 'role_id' => $role_id, + 'auth_user_id' => $auth_user_id + ]); + } + } + echo json_encode(['success' => true]); exit; } @@ -196,10 +224,14 @@ $sql = " d.name AS department_name, d.color AS department_color, au.email AS user_email, + au.role_id AS user_role_id, + ar.display_name AS role_display_name, + ar.name AS role_name, CONCAT(COALESCE(au.first_name, ''), ' ', COALESCE(au.last_name, '')) AS user_fullname FROM employees e LEFT JOIN departments d ON e.department_id = d.id LEFT JOIN auth_users au ON e.auth_user_id = au.id + LEFT JOIN auth_roles ar ON ar.id = au.role_id ORDER BY e.id DESC "; $stmtEmployees = $pdo->query($sql); @@ -208,6 +240,7 @@ $employees = $stmtEmployees->fetchAll(PDO::FETCH_ASSOC); // Users list for select $sqlUsers = " SELECT id, + role_id, CONCAT( COALESCE(first_name, ''), ' ', @@ -222,6 +255,15 @@ $sqlUsers = " $stmtUsers = $pdo->query($sqlUsers); $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC); +// Roles list for select +$sqlRoles = " + SELECT id, name, display_name + FROM auth_roles + ORDER BY display_name, name +"; +$stmtRoles = $pdo->query($sqlRoles); +$roles = $stmtRoles->fetchAll(PDO::FETCH_ASSOC); + // Departments list for select $sqlDepartments = " SELECT id, name, code, color @@ -471,7 +513,8 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); data-position="" data-hire_date="" data-status="" - data-auth_user_id=""> + data-auth_user_id="" + data-role_id=""> ✏️ Modifica @@ -571,13 +614,26 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); +
+ + + Visible only when an auth user is linked. +
+
@@ -656,13 +712,26 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); +
+ + + Visible only when an auth user is linked. +
+
@@ -715,11 +784,46 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); // Select2 on user selects - $('#addAuthUserId, #editAuthUserId, #addDepartmentId, #editDepartmentId').select2({ + $('#addAuthUserId, #editAuthUserId, #addDepartmentId, #editDepartmentId, #addRoleId, #editRoleId').select2({ theme: 'bootstrap-5', width: '100%' }); + function syncAddRoleVisibility() { + const authUserId = $('#addAuthUserId').val(); + + if (authUserId) { + $('#addRoleWrapper').removeClass('d-none'); + + const selectedRoleId = $('#addAuthUserId option:selected').data('role_id'); + if (selectedRoleId) { + $('#addRoleId').val(String(selectedRoleId)).trigger('change'); + } + } else { + $('#addRoleWrapper').addClass('d-none'); + $('#addRoleId').val('').trigger('change'); + } + } + + function syncEditRoleVisibility() { + const authUserId = $('#editAuthUserId').val(); + + if (authUserId) { + $('#editRoleWrapper').removeClass('d-none'); + + const selectedRoleId = $('#editAuthUserId option:selected').data('role_id'); + if (selectedRoleId) { + $('#editRoleId').val(String(selectedRoleId)).trigger('change'); + } + } else { + $('#editRoleWrapper').addClass('d-none'); + $('#editRoleId').val('').trigger('change'); + } + } + + $('#addAuthUserId').on('change', syncAddRoleVisibility); + $('#editAuthUserId').on('change', syncEditRoleVisibility); + /* -------- ADD EMPLOYEE -------- */ $("#addEmployeeForm").on("submit", function(e) { e.preventDefault(); @@ -735,6 +839,7 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); payload.append('hire_date', $("#addHireDate").val()); payload.append('status', $("#addStatus").val()); payload.append('auth_user_id', $("#addAuthUserId").val() || ''); + payload.append('role_id', $("#addAuthUserId").val() ? ($("#addRoleId").val() || '') : ''); fetch("", { method: "POST", @@ -786,6 +891,17 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); const authUserId = btn.data("auth_user_id"); $("#editAuthUserId").val(authUserId ? String(authUserId) : '').trigger('change'); + const roleId = btn.data("role_id"); + if (authUserId && roleId) { + $("#editRoleWrapper").removeClass('d-none'); + $("#editRoleId").val(String(roleId)).trigger('change'); + } else { + $("#editRoleWrapper").addClass('d-none'); + $("#editRoleId").val('').trigger('change'); + } + + $("#editEmployeeModal").modal("show"); + $("#editEmployeeModal").modal("show"); }); @@ -805,6 +921,7 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); payload.append('hire_date', $("#editHireDate").val()); payload.append('status', $("#editStatus").val()); payload.append('auth_user_id', $("#editAuthUserId").val() || ''); + payload.append('role_id', $("#editAuthUserId").val() ? ($("#editRoleId").val() || '') : ''); fetch("", { method: "POST",