added roles edit into employees
This commit is contained in:
parent
fc35adc7f9
commit
fa2f293835
@ -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
|
||||
</button>
|
||||
|
||||
@ -571,13 +614,26 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
||||
<select class="form-select" id="addAuthUserId" name="auth_user_id" style="width:100%;">
|
||||
<option value="">-- None --</option>
|
||||
<?php foreach ($users as $u): ?>
|
||||
<option value="<?= (int)$u['id'] ?>">
|
||||
<option value="<?= (int)$u['id'] ?>" data-role_id="<?= (int)$u['role_id'] ?>">
|
||||
<?= htmlspecialchars($u['label']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="mb-3 d-none" id="addRoleWrapper">
|
||||
<label class="form-label fw-semibold">User Role</label>
|
||||
<select class="form-select" id="addRoleId" name="role_id" style="width:100%;">
|
||||
<option value="">-- Select Role --</option>
|
||||
<?php foreach ($roles as $r): ?>
|
||||
<option value="<?= (int)$r['id'] ?>">
|
||||
<?= htmlspecialchars($r['display_name'] ?: $r['name']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<small class="text-muted">Visible only when an auth user is linked.</small>
|
||||
</div>
|
||||
|
||||
<div class="text-center">
|
||||
<button type="submit" class="btn btn-add">💾 Save</button>
|
||||
</div>
|
||||
@ -656,13 +712,26 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
||||
<select class="form-select" id="editAuthUserId" name="auth_user_id" style="width:100%;">
|
||||
<option value="">-- None --</option>
|
||||
<?php foreach ($users as $u): ?>
|
||||
<option value="<?= (int)$u['id'] ?>">
|
||||
<option value="<?= (int)$u['id'] ?>" data-role_id="<?= (int)$u['role_id'] ?>">
|
||||
<?= htmlspecialchars($u['label']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="mb-3 d-none" id="editRoleWrapper">
|
||||
<label class="form-label fw-semibold">User Role</label>
|
||||
<select class="form-select" id="editRoleId" name="role_id" style="width:100%;">
|
||||
<option value="">-- Select Role --</option>
|
||||
<?php foreach ($roles as $r): ?>
|
||||
<option value="<?= (int)$r['id'] ?>">
|
||||
<?= htmlspecialchars($r['display_name'] ?: $r['name']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<small class="text-muted">Visible only when an auth user is linked.</small>
|
||||
</div>
|
||||
|
||||
<div class="text-center">
|
||||
<button type="submit" class="btn btn-add">💾 Save Changes</button>
|
||||
</div>
|
||||
@ -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",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user