getConnection(); /* ========================================== AJAX HANDLERS (ADD / EDIT / DELETE / SKILLS) ========================================== */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['ajax'] == '1') { header('Content-Type: application/json'); $action = $_POST['action'] ?? ''; 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'] ?? ''); $department = trim($_POST['department'] ?? ''); $position = trim($_POST['position'] ?? ''); $hire_date = trim($_POST['hire_date'] ?? ''); $status = trim($_POST['status'] ?? 'active'); $auth_user_id = $_POST['auth_user_id'] !== '' ? (int)$_POST['auth_user_id'] : null; if ($first_name === '' || $last_name === '') { echo json_encode([ 'success' => false, 'message' => 'First name and Last name are required.' ]); 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, position, hire_date, status, created_at, updated_at) VALUES (:auth_user_id, :employee_code, :first_name, :last_name, :department, :position, :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, 'department' => $department !== '' ? $department : null, 'position' => $position !== '' ? $position : null, 'hire_date' => $hire_date !== '' ? $hire_date : null, 'status' => $status ]); echo json_encode(['success' => true]); exit; } if ($action === 'edit') { // Codice originale per edit $id = (int)($_POST['id'] ?? 0); $employee_code = trim($_POST['employee_code'] ?? ''); $first_name = trim($_POST['first_name'] ?? ''); $last_name = trim($_POST['last_name'] ?? ''); $department = trim($_POST['department'] ?? ''); $position = trim($_POST['position'] ?? ''); $hire_date = trim($_POST['hire_date'] ?? ''); $status = trim($_POST['status'] ?? 'active'); $auth_user_id = $_POST['auth_user_id'] !== '' ? (int)$_POST['auth_user_id'] : null; if ($id <= 0) { echo json_encode(['success' => false, 'message' => 'Invalid employee ID.']); exit; } if ($first_name === '' || $last_name === '') { echo json_encode([ 'success' => false, 'message' => 'First name and Last name are required.' ]); exit; } if (!in_array($status, ['active', 'inactive', 'suspended'], true)) { $status = 'active'; } $sql = "UPDATE employees SET auth_user_id = :auth_user_id, employee_code = :employee_code, first_name = :first_name, last_name = :last_name, department = :department, position = :position, hire_date = :hire_date, status = :status, updated_at = NOW() WHERE id = :id"; $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, 'department' => $department !== '' ? $department : null, 'position' => $position !== '' ? $position : null, 'hire_date' => $hire_date !== '' ? $hire_date : null, 'status' => $status, 'id' => $id ]); echo json_encode(['success' => true]); exit; } if ($action === 'delete') { // Codice originale per delete $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { echo json_encode(['success' => false, 'message' => 'Invalid employee ID.']); exit; } $stmt = $pdo->prepare("DELETE FROM employees WHERE id = :id"); $stmt->execute(['id' => $id]); echo json_encode(['success' => true]); exit; } if ($action === 'get_employee_skills') { $id = (int)$_POST['id']; if ($id <= 0) { echo json_encode(['success' => false, 'message' => 'Invalid ID']); exit; } $stmt = $pdo->prepare("SELECT skill_id, level FROM employee_skills WHERE employee_id = ?"); $stmt->execute([$id]); $skills = []; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $skills[$row['skill_id']] = $row['level']; } echo json_encode(['success' => true, 'skills' => $skills]); exit; } if ($action === 'save_employee_skills') { $id = (int)$_POST['id']; $skills_json = $_POST['skills'] ?? ''; $skills = json_decode($skills_json, true); if ($id <= 0 || !is_array($skills)) { echo json_encode(['success' => false, 'message' => 'Invalid data']); exit; } $pdo->beginTransaction(); $stmtDelete = $pdo->prepare("DELETE FROM employee_skills WHERE employee_id = ?"); $stmtDelete->execute([$id]); $stmtInsert = $pdo->prepare("INSERT INTO employee_skills (employee_id, skill_id, level) VALUES (?, ?, ?)"); foreach ($skills as $skill_id => $level) { $stmtInsert->execute([$id, (int)$skill_id, $level]); } $pdo->commit(); echo json_encode(['success' => true]); exit; } echo json_encode(['success' => false, 'message' => 'Unknown action.']); exit; } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); exit; } } /* ========================================== PAGE DATA (LIST + USERS LIST + SKILLS LIST) ========================================== */ // Employees list $sql = " SELECT e.*, au.email AS user_email, CONCAT(COALESCE(au.first_name, ''), ' ', COALESCE(au.last_name, '')) AS user_fullname FROM employees e LEFT JOIN auth_users au ON e.auth_user_id = au.id ORDER BY e.id DESC "; $stmtEmployees = $pdo->query($sql); $employees = $stmtEmployees->fetchAll(PDO::FETCH_ASSOC); // Users list for select $sqlUsers = " SELECT id, CONCAT( COALESCE(first_name, ''), ' ', COALESCE(last_name, ''), ' (', email, ')' ) AS label FROM auth_users ORDER BY first_name, last_name, email "; $stmtUsers = $pdo->query($sqlUsers); $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC); // Skills list for JS $sqlSkills = " SELECT s.id, s.name, pl.name as line_name, pl.line_number FROM skills s LEFT JOIN production_lines pl ON s.production_line_id = pl.id ORDER BY IFNULL(pl.line_number, 999), s.name "; $stmtSkills = $pdo->query($sqlSkills); $allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC); ?> Gestione Dipendenti - <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?>
Gestione Dipendenti
Elenco Completo
ID Code Name Department Position Hire Date Status Linked User Actions