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="= htmlspecialchars($row['position'] ?? '', ENT_QUOTES) ?>" data-hire_date="= htmlspecialchars($row['hire_date'] ?? '', ENT_QUOTES) ?>" data-status="= htmlspecialchars($status, ENT_QUOTES) ?>" - data-auth_user_id="= $row['auth_user_id'] !== null ? (int)$row['auth_user_id'] : '' ?>"> + data-auth_user_id="= $row['auth_user_id'] !== null ? (int)$row['auth_user_id'] : '' ?>" + data-role_id="= $row['user_role_id'] !== null ? (int)$row['user_role_id'] : '' ?>"> ✏️ Modifica @@ -571,13 +614,26 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); +