user profile

This commit is contained in:
2026-05-14 16:09:39 +03:00
parent fa2f293835
commit d155d1cbab
55 changed files with 5691 additions and 144 deletions
+166 -67
View File
@@ -17,16 +17,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
try {
if ($action === 'add') {
// Codice originale per add
$employee_code = trim($_POST['employee_code'] ?? '');
$first_name = trim($_POST['first_name'] ?? '');
$last_name = trim($_POST['last_name'] ?? '');
$address = trim($_POST['address'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$email = trim($_POST['email'] ?? '');
$department_id = $_POST['department_id'] !== '' ? (int)$_POST['department_id'] : null;
$position = trim($_POST['position'] ?? '');
$job_role_id = ($_POST['job_role_id'] ?? '') !== '' ? (int)$_POST['job_role_id'] : null;
$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;
$role_id = $_POST['role_id'] !== '' ? (int)$_POST['role_id'] : null;
if ($first_name === '' || $last_name === '') {
echo json_encode([
@@ -35,23 +37,31 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
]);
exit;
}
if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'message' => 'Email non valida.']);
exit;
}
if (!in_array($status, ['active', 'inactive', 'suspended'], true)) {
$status = 'active';
}
$sql = "INSERT INTO employees
(auth_user_id, employee_code, first_name, last_name, department_id, position, hire_date, status, created_at, updated_at)
(auth_user_id, employee_code, first_name, last_name, address, phone, email,
department_id, job_role_id, hire_date, status, created_at, updated_at)
VALUES
(:auth_user_id, :employee_code, :first_name, :last_name, :department_id, :position, :hire_date, :status, NOW(), NOW())";
(:auth_user_id, :employee_code, :first_name, :last_name, :address, :phone, :email,
:department_id, :job_role_id, :hire_date, :status, NOW(), NOW())";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'auth_user_id' => $auth_user_id,
'employee_code' => $employee_code !== '' ? $employee_code : null,
'first_name' => $first_name,
'last_name' => $last_name,
'address' => $address !== '' ? $address : null,
'phone' => $phone !== '' ? $phone : null,
'email' => $email !== '' ? $email : null,
'department_id' => $department_id,
'position' => $position !== '' ? $position : null,
'job_role_id' => $job_role_id,
'hire_date' => $hire_date !== '' ? $hire_date : null,
'status' => $status
]);
@@ -74,17 +84,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
}
if ($action === 'edit') {
// Codice originale per edit
$id = (int)($_POST['id'] ?? 0);
$id = (int)($_POST['id'] ?? 0);
$employee_code = trim($_POST['employee_code'] ?? '');
$first_name = trim($_POST['first_name'] ?? '');
$last_name = trim($_POST['last_name'] ?? '');
$address = trim($_POST['address'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$email = trim($_POST['email'] ?? '');
$department_id = $_POST['department_id'] !== '' ? (int)$_POST['department_id'] : null;
$position = trim($_POST['position'] ?? '');
$job_role_id = ($_POST['job_role_id'] ?? '') !== '' ? (int)$_POST['job_role_id'] : null;
$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;
$role_id = $_POST['role_id'] !== '' ? (int)$_POST['role_id'] : null;
if ($id <= 0) {
echo json_encode(['success' => false, 'message' => 'Invalid employee ID.']);
@@ -98,7 +110,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
]);
exit;
}
if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'message' => 'Email non valida.']);
exit;
}
if (!in_array($status, ['active', 'inactive', 'suspended'], true)) {
$status = 'active';
}
@@ -108,8 +123,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
employee_code = :employee_code,
first_name = :first_name,
last_name = :last_name,
address = :address,
phone = :phone,
email = :email,
department_id = :department_id,
position = :position,
job_role_id = :job_role_id,
hire_date = :hire_date,
status = :status,
updated_at = NOW()
@@ -120,8 +138,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
'employee_code' => $employee_code !== '' ? $employee_code : null,
'first_name' => $first_name,
'last_name' => $last_name,
'address' => $address !== '' ? $address : null,
'phone' => $phone !== '' ? $phone : null,
'email' => $email !== '' ? $email : null,
'department_id' => $department_id,
'position' => $position !== '' ? $position : null,
'job_role_id' => $job_role_id,
'hire_date' => $hire_date !== '' ? $hire_date : null,
'status' => $status,
'id' => $id
@@ -223,6 +244,7 @@ $sql = "
SELECT e.*,
d.name AS department_name,
d.color AS department_color,
jr.name AS job_role_name,
au.email AS user_email,
au.role_id AS user_role_id,
ar.display_name AS role_display_name,
@@ -230,6 +252,7 @@ $sql = "
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 job_roles jr ON jr.id = e.job_role_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
@@ -237,6 +260,11 @@ $sql = "
$stmtEmployees = $pdo->query($sql);
$employees = $stmtEmployees->fetchAll(PDO::FETCH_ASSOC);
// Job roles for the dropdown
$jobRoles = $pdo->query("
SELECT id, name FROM job_roles WHERE is_active = 1 ORDER BY sort_order, name
")->fetchAll(PDO::FETCH_ASSOC);
// Users list for select
$sqlUsers = "
SELECT id,
@@ -297,7 +325,6 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
<!-- jQuery e Bootstrap -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<!-- DataTables -->
@@ -415,7 +442,7 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
</head>
<body>
<div class="wrapper toggled">
<div class="wrapper" id="appWrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
@@ -448,14 +475,15 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
<thead>
<tr>
<th>ID</th>
<th>Code</th>
<th>Name</th>
<th>Department</th>
<th>Position</th>
<th>Hire Date</th>
<th>Status</th>
<th>Linked User</th>
<th>Actions</th>
<th>Codice</th>
<th>Nome</th>
<th>Contatti</th>
<th>Reparto</th>
<th>Mansione</th>
<th>Data Assunzione</th>
<th>Stato</th>
<th>Utente collegato</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
@@ -484,7 +512,24 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
<tr>
<td><?= (int)$row['id'] ?></td>
<td><?= htmlspecialchars($row['employee_code'] ?? '') ?></td>
<td><?= htmlspecialchars($fullName) ?></td>
<td>
<a href="employee-profile.php?id=<?= (int)$row['id'] ?>" class="fw-semibold text-decoration-none">
<?= htmlspecialchars($fullName) ?>
</a>
</td>
<td class="text-start">
<?php if (!empty($row['email'])): ?>
<a href="mailto:<?= htmlspecialchars($row['email'], ENT_QUOTES) ?>" class="text-decoration-none small">
✉️ <?= htmlspecialchars($row['email']) ?>
</a><br>
<?php endif; ?>
<?php if (!empty($row['phone'])): ?>
<a href="tel:<?= htmlspecialchars($row['phone'], ENT_QUOTES) ?>" class="text-decoration-none small">
📞 <?= htmlspecialchars($row['phone']) ?>
</a>
<?php endif; ?>
<?php if (empty($row['email']) && empty($row['phone'])): ?>-<?php endif; ?>
</td>
<td>
<?php if (!empty($row['department_name'])): ?>
<span class="department-badge" style="background-color: <?= htmlspecialchars($row['department_color'] ?? '#6c757d', ENT_QUOTES) ?>;">
@@ -494,7 +539,7 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
-
<?php endif; ?>
</td>
<td><?= htmlspecialchars($row['position'] ?? '') ?></td>
<td><?= !empty($row['job_role_name']) ? htmlspecialchars($row['job_role_name']) : '-' ?></td>
<td><?= $hireDate ?></td>
<td>
<span class="badge-status <?= $statusClass ?>">
@@ -510,7 +555,10 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
data-first_name="<?= htmlspecialchars($row['first_name'] ?? '', ENT_QUOTES) ?>"
data-last_name="<?= htmlspecialchars($row['last_name'] ?? '', ENT_QUOTES) ?>"
data-department_id="<?= $row['department_id'] !== null ? (int)$row['department_id'] : '' ?>"
data-position="<?= htmlspecialchars($row['position'] ?? '', ENT_QUOTES) ?>"
data-job_role_id="<?= $row['job_role_id'] !== null ? (int)$row['job_role_id'] : '' ?>"
data-address="<?= htmlspecialchars($row['address'] ?? '', ENT_QUOTES) ?>"
data-phone="<?= htmlspecialchars($row['phone'] ?? '', ENT_QUOTES) ?>"
data-email="<?= htmlspecialchars($row['email'] ?? '', 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'] : '' ?>"
@@ -560,26 +608,42 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
<div class="modal-body">
<form id="addEmployeeForm">
<div class="mb-3">
<label class="form-label fw-semibold">Employee Code</label>
<input type="text" class="form-control" id="addEmployeeCode" name="employee_code" placeholder="Optional">
<label class="form-label fw-semibold">Codice Dipendente</label>
<input type="text" class="form-control" id="addEmployeeCode" name="employee_code" placeholder="Opzionale">
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">First Name</label>
<label class="form-label fw-semibold">Nome</label>
<input type="text" class="form-control" id="addFirstName" name="first_name" required>
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Last Name</label>
<label class="form-label fw-semibold">Cognome</label>
<input type="text" class="form-control" id="addLastName" name="last_name" required>
</div>
</div>
<div class="mb-3">
<label class="form-label fw-semibold">Indirizzo</label>
<input type="text" class="form-control" id="addAddress" name="address" placeholder="Via, città, CAP">
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Department</label>
<label class="form-label fw-semibold">Telefono</label>
<input type="tel" class="form-control" id="addPhone" name="phone">
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Email</label>
<input type="email" class="form-control" id="addEmail" name="email">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Reparto</label>
<select class="form-select" id="addDepartmentId" name="department_id" style="width:100%;">
<option value="">-- Select Department --</option>
<option value="">-- Nessuno --</option>
<?php foreach ($departments as $d): ?>
<option value="<?= (int)$d['id'] ?>">
<?= htmlspecialchars($d['name']) ?>
@@ -589,30 +653,35 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
</select>
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Position</label>
<input type="text" class="form-control" id="addPosition" name="position" placeholder="e.g. Line Operator">
<label class="form-label fw-semibold">Mansione</label>
<select class="form-select" id="addJobRoleId" name="job_role_id" style="width:100%;">
<option value="">-- Nessuna --</option>
<?php foreach ($jobRoles as $jr): ?>
<option value="<?= (int)$jr['id'] ?>"><?= htmlspecialchars($jr['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Hire Date</label>
<label class="form-label fw-semibold">Data Assunzione</label>
<input type="date" class="form-control" id="addHireDate" name="hire_date">
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Status</label>
<label class="form-label fw-semibold">Stato</label>
<select class="form-select" id="addStatus" name="status">
<option value="active" selected>Active</option>
<option value="inactive">Inactive</option>
<option value="suspended">Suspended</option>
<option value="active" selected>Attivo</option>
<option value="inactive">Cessato</option>
<option value="suspended">Sospeso</option>
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label fw-semibold">Linked User (auth_users)</label>
<label class="form-label fw-semibold">Utente collegato (account login)</label>
<select class="form-select" id="addAuthUserId" name="auth_user_id" style="width:100%;">
<option value="">-- None --</option>
<option value="">-- Nessuno --</option>
<?php foreach ($users as $u): ?>
<option value="<?= (int)$u['id'] ?>" data-role_id="<?= (int)$u['role_id'] ?>">
<?= htmlspecialchars($u['label']) ?>
@@ -622,16 +691,16 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
</div>
<div class="mb-3 d-none" id="addRoleWrapper">
<label class="form-label fw-semibold">User Role</label>
<label class="form-label fw-semibold">Ruolo di accesso</label>
<select class="form-select" id="addRoleId" name="role_id" style="width:100%;">
<option value="">-- Select Role --</option>
<option value="">-- Seleziona ruolo --</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>
<small class="text-muted">Visibile solo quando è collegato un utente di sistema.</small>
</div>
<div class="text-center">
@@ -658,26 +727,42 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
<input type="hidden" id="editEmployeeId">
<div class="mb-3">
<label class="form-label fw-semibold">Employee Code</label>
<input type="text" class="form-control" id="editEmployeeCode" name="employee_code" placeholder="Optional">
<label class="form-label fw-semibold">Codice Dipendente</label>
<input type="text" class="form-control" id="editEmployeeCode" name="employee_code" placeholder="Opzionale">
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">First Name</label>
<label class="form-label fw-semibold">Nome</label>
<input type="text" class="form-control" id="editFirstName" name="first_name" required>
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Last Name</label>
<label class="form-label fw-semibold">Cognome</label>
<input type="text" class="form-control" id="editLastName" name="last_name" required>
</div>
</div>
<div class="mb-3">
<label class="form-label fw-semibold">Indirizzo</label>
<input type="text" class="form-control" id="editAddress" name="address">
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Department</label>
<label class="form-label fw-semibold">Telefono</label>
<input type="tel" class="form-control" id="editPhone" name="phone">
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Email</label>
<input type="email" class="form-control" id="editEmail" name="email">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Reparto</label>
<select class="form-select" id="editDepartmentId" name="department_id" style="width:100%;">
<option value="">-- Select Department --</option>
<option value="">-- Nessuno --</option>
<?php foreach ($departments as $d): ?>
<option value="<?= (int)$d['id'] ?>">
<?= htmlspecialchars($d['name']) ?>
@@ -687,30 +772,35 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
</select>
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Position</label>
<input type="text" class="form-control" id="editPosition" name="position">
<label class="form-label fw-semibold">Mansione</label>
<select class="form-select" id="editJobRoleId" name="job_role_id" style="width:100%;">
<option value="">-- Nessuna --</option>
<?php foreach ($jobRoles as $jr): ?>
<option value="<?= (int)$jr['id'] ?>"><?= htmlspecialchars($jr['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Hire Date</label>
<label class="form-label fw-semibold">Data Assunzione</label>
<input type="date" class="form-control" id="editHireDate" name="hire_date">
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-semibold">Status</label>
<label class="form-label fw-semibold">Stato</label>
<select class="form-select" id="editStatus" name="status">
<option value="active">Active</option>
<option value="inactive">Inactive</option>
<option value="suspended">Suspended</option>
<option value="active">Attivo</option>
<option value="inactive">Cessato</option>
<option value="suspended">Sospeso</option>
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label fw-semibold">Linked User (auth_users)</label>
<label class="form-label fw-semibold">Utente collegato (account login)</label>
<select class="form-select" id="editAuthUserId" name="auth_user_id" style="width:100%;">
<option value="">-- None --</option>
<option value="">-- Nessuno --</option>
<?php foreach ($users as $u): ?>
<option value="<?= (int)$u['id'] ?>" data-role_id="<?= (int)$u['role_id'] ?>">
<?= htmlspecialchars($u['label']) ?>
@@ -720,16 +810,16 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
</div>
<div class="mb-3 d-none" id="editRoleWrapper">
<label class="form-label fw-semibold">User Role</label>
<label class="form-label fw-semibold">Ruolo di accesso</label>
<select class="form-select" id="editRoleId" name="role_id" style="width:100%;">
<option value="">-- Select Role --</option>
<option value="">-- Seleziona ruolo --</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>
<small class="text-muted">Visibile solo quando è collegato un utente di sistema.</small>
</div>
<div class="text-center">
@@ -784,7 +874,7 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
// Select2 on user selects
$('#addAuthUserId, #editAuthUserId, #addDepartmentId, #editDepartmentId, #addRoleId, #editRoleId').select2({
$('#addAuthUserId, #editAuthUserId, #addDepartmentId, #editDepartmentId, #addRoleId, #editRoleId, #addJobRoleId, #editJobRoleId').select2({
theme: 'bootstrap-5',
width: '100%'
});
@@ -834,8 +924,11 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
payload.append('employee_code', $("#addEmployeeCode").val().trim());
payload.append('first_name', $("#addFirstName").val().trim());
payload.append('last_name', $("#addLastName").val().trim());
payload.append('address', $("#addAddress").val().trim());
payload.append('phone', $("#addPhone").val().trim());
payload.append('email', $("#addEmail").val().trim());
payload.append('department_id', $("#addDepartmentId").val() || '');
payload.append('position', $("#addPosition").val().trim());
payload.append('job_role_id', $("#addJobRoleId").val() || '');
payload.append('hire_date', $("#addHireDate").val());
payload.append('status', $("#addStatus").val());
payload.append('auth_user_id', $("#addAuthUserId").val() || '');
@@ -884,7 +977,10 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
$("#editFirstName").val(btn.data("first_name"));
$("#editLastName").val(btn.data("last_name"));
$("#editDepartmentId").val(btn.data("department_id") ? String(btn.data("department_id")) : '').trigger('change');
$("#editPosition").val(btn.data("position"));
$("#editJobRoleId").val(btn.data("job_role_id") ? String(btn.data("job_role_id")) : '').trigger('change');
$("#editAddress").val(btn.data("address"));
$("#editPhone").val(btn.data("phone"));
$("#editEmail").val(btn.data("email"));
$("#editHireDate").val(btn.data("hire_date"));
$("#editStatus").val(btn.data("status"));
@@ -916,8 +1012,11 @@ $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
payload.append('employee_code', $("#editEmployeeCode").val().trim());
payload.append('first_name', $("#editFirstName").val().trim());
payload.append('last_name', $("#editLastName").val().trim());
payload.append('address', $("#editAddress").val().trim());
payload.append('phone', $("#editPhone").val().trim());
payload.append('email', $("#editEmail").val().trim());
payload.append('department_id', $("#editDepartmentId").val() || '');
payload.append('position', $("#editPosition").val().trim());
payload.append('job_role_id', $("#editJobRoleId").val() || '');
payload.append('hire_date', $("#editHireDate").val());
payload.append('status', $("#editStatus").val());
payload.append('auth_user_id', $("#editAuthUserId").val() || '');