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'] ?? '');
|
$hire_date = trim($_POST['hire_date'] ?? '');
|
||||||
$status = trim($_POST['status'] ?? 'active');
|
$status = trim($_POST['status'] ?? 'active');
|
||||||
$auth_user_id = $_POST['auth_user_id'] !== '' ? (int)$_POST['auth_user_id'] : null;
|
$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 === '') {
|
if ($first_name === '' || $last_name === '') {
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
@ -55,6 +56,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
|
|||||||
'status' => $status
|
'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]);
|
echo json_encode(['success' => true]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
@ -70,6 +84,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
|
|||||||
$hire_date = trim($_POST['hire_date'] ?? '');
|
$hire_date = trim($_POST['hire_date'] ?? '');
|
||||||
$status = trim($_POST['status'] ?? 'active');
|
$status = trim($_POST['status'] ?? 'active');
|
||||||
$auth_user_id = $_POST['auth_user_id'] !== '' ? (int)$_POST['auth_user_id'] : null;
|
$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) {
|
if ($id <= 0) {
|
||||||
echo json_encode(['success' => false, 'message' => 'Invalid employee ID.']);
|
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
|
'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]);
|
echo json_encode(['success' => true]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
@ -196,10 +224,14 @@ $sql = "
|
|||||||
d.name AS department_name,
|
d.name AS department_name,
|
||||||
d.color AS department_color,
|
d.color AS department_color,
|
||||||
au.email AS user_email,
|
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
|
CONCAT(COALESCE(au.first_name, ''), ' ', COALESCE(au.last_name, '')) AS user_fullname
|
||||||
FROM employees e
|
FROM employees e
|
||||||
LEFT JOIN departments d ON e.department_id = d.id
|
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_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
|
ORDER BY e.id DESC
|
||||||
";
|
";
|
||||||
$stmtEmployees = $pdo->query($sql);
|
$stmtEmployees = $pdo->query($sql);
|
||||||
@ -208,6 +240,7 @@ $employees = $stmtEmployees->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
// Users list for select
|
// Users list for select
|
||||||
$sqlUsers = "
|
$sqlUsers = "
|
||||||
SELECT id,
|
SELECT id,
|
||||||
|
role_id,
|
||||||
CONCAT(
|
CONCAT(
|
||||||
COALESCE(first_name, ''),
|
COALESCE(first_name, ''),
|
||||||
' ',
|
' ',
|
||||||
@ -222,6 +255,15 @@ $sqlUsers = "
|
|||||||
$stmtUsers = $pdo->query($sqlUsers);
|
$stmtUsers = $pdo->query($sqlUsers);
|
||||||
$users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
|
$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
|
// Departments list for select
|
||||||
$sqlDepartments = "
|
$sqlDepartments = "
|
||||||
SELECT id, name, code, color
|
SELECT id, name, code, color
|
||||||
@ -471,7 +513,8 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
data-position="<?= htmlspecialchars($row['position'] ?? '', ENT_QUOTES) ?>"
|
data-position="<?= htmlspecialchars($row['position'] ?? '', ENT_QUOTES) ?>"
|
||||||
data-hire_date="<?= htmlspecialchars($row['hire_date'] ?? '', ENT_QUOTES) ?>"
|
data-hire_date="<?= htmlspecialchars($row['hire_date'] ?? '', ENT_QUOTES) ?>"
|
||||||
data-status="<?= htmlspecialchars($status, 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
|
✏️ Modifica
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
@ -571,13 +614,26 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
<select class="form-select" id="addAuthUserId" name="auth_user_id" style="width:100%;">
|
<select class="form-select" id="addAuthUserId" name="auth_user_id" style="width:100%;">
|
||||||
<option value="">-- None --</option>
|
<option value="">-- None --</option>
|
||||||
<?php foreach ($users as $u): ?>
|
<?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']) ?>
|
<?= htmlspecialchars($u['label']) ?>
|
||||||
</option>
|
</option>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</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">
|
<div class="text-center">
|
||||||
<button type="submit" class="btn btn-add">💾 Save</button>
|
<button type="submit" class="btn btn-add">💾 Save</button>
|
||||||
</div>
|
</div>
|
||||||
@ -656,13 +712,26 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
<select class="form-select" id="editAuthUserId" name="auth_user_id" style="width:100%;">
|
<select class="form-select" id="editAuthUserId" name="auth_user_id" style="width:100%;">
|
||||||
<option value="">-- None --</option>
|
<option value="">-- None --</option>
|
||||||
<?php foreach ($users as $u): ?>
|
<?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']) ?>
|
<?= htmlspecialchars($u['label']) ?>
|
||||||
</option>
|
</option>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</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">
|
<div class="text-center">
|
||||||
<button type="submit" class="btn btn-add">💾 Save Changes</button>
|
<button type="submit" class="btn btn-add">💾 Save Changes</button>
|
||||||
</div>
|
</div>
|
||||||
@ -715,11 +784,46 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
|
|
||||||
|
|
||||||
// Select2 on user selects
|
// Select2 on user selects
|
||||||
$('#addAuthUserId, #editAuthUserId, #addDepartmentId, #editDepartmentId').select2({
|
$('#addAuthUserId, #editAuthUserId, #addDepartmentId, #editDepartmentId, #addRoleId, #editRoleId').select2({
|
||||||
theme: 'bootstrap-5',
|
theme: 'bootstrap-5',
|
||||||
width: '100%'
|
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 -------- */
|
/* -------- ADD EMPLOYEE -------- */
|
||||||
$("#addEmployeeForm").on("submit", function(e) {
|
$("#addEmployeeForm").on("submit", function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@ -735,6 +839,7 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
payload.append('hire_date', $("#addHireDate").val());
|
payload.append('hire_date', $("#addHireDate").val());
|
||||||
payload.append('status', $("#addStatus").val());
|
payload.append('status', $("#addStatus").val());
|
||||||
payload.append('auth_user_id', $("#addAuthUserId").val() || '');
|
payload.append('auth_user_id', $("#addAuthUserId").val() || '');
|
||||||
|
payload.append('role_id', $("#addAuthUserId").val() ? ($("#addRoleId").val() || '') : '');
|
||||||
|
|
||||||
fetch("", {
|
fetch("", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@ -786,6 +891,17 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
const authUserId = btn.data("auth_user_id");
|
const authUserId = btn.data("auth_user_id");
|
||||||
$("#editAuthUserId").val(authUserId ? String(authUserId) : '').trigger('change');
|
$("#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");
|
$("#editEmployeeModal").modal("show");
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -805,6 +921,7 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
payload.append('hire_date', $("#editHireDate").val());
|
payload.append('hire_date', $("#editHireDate").val());
|
||||||
payload.append('status', $("#editStatus").val());
|
payload.append('status', $("#editStatus").val());
|
||||||
payload.append('auth_user_id', $("#editAuthUserId").val() || '');
|
payload.append('auth_user_id', $("#editAuthUserId").val() || '');
|
||||||
|
payload.append('role_id', $("#editAuthUserId").val() ? ($("#editRoleId").val() || '') : '');
|
||||||
|
|
||||||
fetch("", {
|
fetch("", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user