matrix skills
This commit is contained in:
@@ -8,7 +8,7 @@ $db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
/* ==========================================
|
||||
AJAX HANDLERS (ADD / EDIT / DELETE)
|
||||
AJAX HANDLERS (ADD / EDIT / DELETE / SKILLS)
|
||||
========================================== */
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['ajax'] == '1') {
|
||||
header('Content-Type: application/json');
|
||||
@@ -17,6 +17,7 @@ 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'] ?? '');
|
||||
@@ -59,6 +60,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
|
||||
}
|
||||
|
||||
if ($action === 'edit') {
|
||||
// Codice originale per edit
|
||||
$id = (int)($_POST['id'] ?? 0);
|
||||
$employee_code = trim($_POST['employee_code'] ?? '');
|
||||
$first_name = trim($_POST['first_name'] ?? '');
|
||||
@@ -115,6 +117,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
|
||||
}
|
||||
|
||||
if ($action === 'delete') {
|
||||
// Codice originale per delete
|
||||
$id = (int)($_POST['id'] ?? 0);
|
||||
|
||||
if ($id <= 0) {
|
||||
@@ -129,9 +132,52 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
|
||||
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()
|
||||
@@ -141,7 +187,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
|
||||
}
|
||||
|
||||
/* ==========================================
|
||||
PAGE DATA (LIST + USERS LIST)
|
||||
PAGE DATA (LIST + USERS LIST + SKILLS LIST)
|
||||
========================================== */
|
||||
|
||||
// Employees list
|
||||
@@ -172,6 +218,16 @@ $sqlUsers = "
|
||||
";
|
||||
$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);
|
||||
?>
|
||||
|
||||
<!doctype html>
|
||||
@@ -276,6 +332,21 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
|
||||
background-color: #fee2e2;
|
||||
color: #b91c1c;
|
||||
}
|
||||
|
||||
.btn-matrix {
|
||||
background-color: #198754;
|
||||
color: #fff;
|
||||
border-radius: 8px;
|
||||
padding: 10px 20px;
|
||||
font-weight: 500;
|
||||
transition: all 0.2s ease-in-out;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.btn-matrix:hover {
|
||||
background-color: #157347;
|
||||
transform: scale(1.02);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
@@ -297,9 +368,14 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
|
||||
<div class="card-body">
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h6 class="fw-semibold mb-0">Elenco Completo</h6>
|
||||
<button class="btn btn-add" data-bs-toggle="modal" data-bs-target="#addEmployeeModal">
|
||||
➕ Aggiungi Dipendente
|
||||
</button>
|
||||
<div>
|
||||
<button class="btn btn-matrix" onclick="location.href='skill_matrix.php'">
|
||||
📊 Matrice Skills
|
||||
</button>
|
||||
<button class="btn btn-add" data-bs-toggle="modal" data-bs-target="#addEmployeeModal">
|
||||
➕ Aggiungi Dipendente
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- TABELLA -->
|
||||
@@ -369,6 +445,13 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
|
||||
✏️ Modifica
|
||||
</button>
|
||||
|
||||
<button
|
||||
class="btn btn-sm btn-outline-info manage-skills"
|
||||
data-id="<?= (int)$row['id'] ?>"
|
||||
data-name="<?= htmlspecialchars($fullName, ENT_QUOTES) ?>">
|
||||
🛠️ Skills
|
||||
</button>
|
||||
|
||||
<button
|
||||
class="btn btn-sm btn-outline-danger delete-employee"
|
||||
data-id="<?= (int)$row['id'] ?>"
|
||||
@@ -544,9 +627,33 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- MODALE GESTIONE SKILLS -->
|
||||
<div class="modal fade" id="manageSkillsModal" tabindex="-1">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header" style="background-color:#cfe3ff;">
|
||||
<h5 class="modal-title">Gestione Skills per <span id="skillsEmployeeName"></span></h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
|
||||
<div class="modal-body">
|
||||
<form id="manageSkillsForm">
|
||||
<input type="hidden" id="skillsEmployeeId">
|
||||
<div id="skillsContainer"></div>
|
||||
<div class="text-center">
|
||||
<button type="submit" class="btn btn-add">💾 Salva Skills</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php include('jsinclude.php'); ?>
|
||||
|
||||
<script>
|
||||
const allSkills = <?= json_encode($allSkills) ?>;
|
||||
|
||||
$(document).ready(function() {
|
||||
// DataTable
|
||||
$('#tabellaDipendenti').DataTable({
|
||||
@@ -743,6 +850,139 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/* -------- OPEN SKILLS MODAL -------- */
|
||||
$(document).on('click', '.manage-skills', function() {
|
||||
const id = $(this).data('id');
|
||||
const name = $(this).data('name');
|
||||
|
||||
$('#skillsEmployeeId').val(id);
|
||||
$('#skillsEmployeeName').text(name);
|
||||
|
||||
const payload = new URLSearchParams();
|
||||
payload.append('ajax', '1');
|
||||
payload.append('action', 'get_employee_skills');
|
||||
payload.append('id', id);
|
||||
|
||||
fetch('', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: payload.toString()
|
||||
})
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
const currentSkills = data.skills || {};
|
||||
let html = '';
|
||||
let currentLine = '';
|
||||
|
||||
allSkills.forEach(skill => {
|
||||
let line = skill.line_name || 'Generali';
|
||||
if (line !== currentLine) {
|
||||
if (currentLine) html += '</div>';
|
||||
html += `<h6 class="mt-3">${line}</h6><div class="row">`;
|
||||
currentLine = line;
|
||||
}
|
||||
|
||||
let level = currentSkills[skill.id] || 'NON RICH.';
|
||||
let options = '';
|
||||
if (skill.name === 'TURNO NOTTURNO') {
|
||||
options = `
|
||||
<option value="si" ${level === 'si' ? 'selected' : ''}>Si</option>
|
||||
<option value="no" ${level === 'no' ? 'selected' : ''}>No</option>
|
||||
`;
|
||||
} else {
|
||||
options = `
|
||||
<option value="DF" ${level === 'DF' ? 'selected' : ''}>Da formare</option>
|
||||
<option value="C" ${level === 'C' ? 'selected' : ''}>Conosce l'attività</option>
|
||||
<option value="CQ" ${level === 'CQ' ? 'selected' : ''}>Conoscenza media</option>
|
||||
<option value="Q" ${level === 'Q' ? 'selected' : ''}>Qualificato</option>
|
||||
<option value="NON RICH." ${level === 'NON RICH.' ? 'selected' : ''}>Non richiesto</option>
|
||||
`;
|
||||
}
|
||||
|
||||
html += `
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label">${skill.name}</label>
|
||||
<select name="skills[${skill.id}]" class="form-select">
|
||||
${options}
|
||||
</select>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
if (currentLine) html += '</div>';
|
||||
|
||||
$('#skillsContainer').html(html);
|
||||
$('#manageSkillsModal').modal('show');
|
||||
} else {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Error',
|
||||
text: data.message || 'Unable to load skills.'
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Error',
|
||||
text: 'Communication error.'
|
||||
});
|
||||
console.error(err);
|
||||
});
|
||||
});
|
||||
|
||||
/* -------- SAVE SKILLS -------- */
|
||||
$('#manageSkillsForm').on('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
const id = $('#skillsEmployeeId').val();
|
||||
const skills = {};
|
||||
$('select[name^="skills["]').each(function() {
|
||||
const skillId = $(this).attr('name').match(/\[(\d+)\]/)[1];
|
||||
skills[skillId] = $(this).val();
|
||||
});
|
||||
|
||||
const payload = new URLSearchParams();
|
||||
payload.append('ajax', '1');
|
||||
payload.append('action', 'save_employee_skills');
|
||||
payload.append('id', id);
|
||||
payload.append('skills', JSON.stringify(skills));
|
||||
|
||||
fetch('', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: payload.toString()
|
||||
})
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
Swal.fire({
|
||||
icon: 'success',
|
||||
title: 'Skills salvate!'
|
||||
});
|
||||
$('#manageSkillsModal').modal('hide');
|
||||
} else {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Error',
|
||||
text: data.message || 'Unable to save skills.'
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Error',
|
||||
text: 'Communication error.'
|
||||
});
|
||||
console.error(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user