From 0b470f290e6e5ffca47260456208f2a043e27666 Mon Sep 17 00:00:00 2001 From: "r.mubarakzyanov" Date: Sat, 23 May 2026 23:46:33 +0300 Subject: [PATCH] fix auto-open --- public/userarea/scadenzario/detail.php | 22 +- .../include/deadline_form_data.php | 32 ++ .../scadenzario/include/deadline_modal.php | 145 +++++ .../include/deadline_modal_css.php | 99 ++++ .../scadenzario/include/deadline_modal_js.php | 277 ++++++++++ public/userarea/scadenzario/index.php | 510 +----------------- 6 files changed, 582 insertions(+), 503 deletions(-) create mode 100644 public/userarea/scadenzario/include/deadline_form_data.php create mode 100644 public/userarea/scadenzario/include/deadline_modal.php create mode 100644 public/userarea/scadenzario/include/deadline_modal_css.php create mode 100644 public/userarea/scadenzario/include/deadline_modal_js.php diff --git a/public/userarea/scadenzario/detail.php b/public/userarea/scadenzario/detail.php index 78c55ea..c365288 100644 --- a/public/userarea/scadenzario/detail.php +++ b/public/userarea/scadenzario/detail.php @@ -85,6 +85,14 @@ if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { + + + + + + + + <?= $deadline ? htmlspecialchars($deadline['topic'], ENT_QUOTES, 'UTF-8') . ' — ' : '' ?>Scadenzario + diff --git a/public/userarea/scadenzario/include/deadline_form_data.php b/public/userarea/scadenzario/include/deadline_form_data.php new file mode 100644 index 0000000..56bd8c6 --- /dev/null +++ b/public/userarea/scadenzario/include/deadline_form_data.php @@ -0,0 +1,32 @@ +getConnection(); +} + +if (!isset($employees)) { + $employees = $pdo->query(" + SELECT e.id, e.first_name, e.last_name, e.department_id, dep.name AS department_name + FROM employees e + LEFT JOIN departments dep ON dep.id = e.department_id + WHERE e.status = 'active' + ORDER BY e.first_name, e.last_name + ")->fetchAll(PDO::FETCH_ASSOC); +} + +if (!isset($departments)) { + $departments = $pdo->query(" + SELECT id, name, code, color + FROM departments + WHERE is_active = 1 + ORDER BY sort_order ASC, name ASC + ")->fetchAll(PDO::FETCH_ASSOC); +} + +if (!isset($subjects)) { + $subjects = $pdo->query("SELECT id, name, color FROM scad_subjects ORDER BY name")->fetchAll(PDO::FETCH_ASSOC); +} diff --git a/public/userarea/scadenzario/include/deadline_modal.php b/public/userarea/scadenzario/include/deadline_modal.php new file mode 100644 index 0000000..e0a4b58 --- /dev/null +++ b/public/userarea/scadenzario/include/deadline_modal.php @@ -0,0 +1,145 @@ + + + diff --git a/public/userarea/scadenzario/include/deadline_modal_css.php b/public/userarea/scadenzario/include/deadline_modal_css.php new file mode 100644 index 0000000..093126f --- /dev/null +++ b/public/userarea/scadenzario/include/deadline_modal_css.php @@ -0,0 +1,99 @@ + + diff --git a/public/userarea/scadenzario/include/deadline_modal_js.php b/public/userarea/scadenzario/include/deadline_modal_js.php new file mode 100644 index 0000000..d8356cb --- /dev/null +++ b/public/userarea/scadenzario/include/deadline_modal_js.php @@ -0,0 +1,277 @@ + → opens edit for that id + * #edit → opens edit for window.SCAD_DETAIL_ID (used by detail.php) + */ +?> + diff --git a/public/userarea/scadenzario/index.php b/public/userarea/scadenzario/index.php index 8fe672f..99be297 100644 --- a/public/userarea/scadenzario/index.php +++ b/public/userarea/scadenzario/index.php @@ -69,27 +69,7 @@ $stmt = $pdo->prepare($sql); $stmt->execute($params); $deadlines = $stmt->fetchAll(PDO::FETCH_ASSOC); -$employees = $pdo->query(" - SELECT - e.id, - e.first_name, - e.last_name, - e.department_id, - dep.name AS department_name - FROM employees e - LEFT JOIN departments dep ON dep.id = e.department_id - WHERE e.status = 'active' - ORDER BY e.first_name, e.last_name -")->fetchAll(PDO::FETCH_ASSOC); - -$departments = $pdo->query(" - SELECT id, name, code, color - FROM departments - WHERE is_active = 1 - ORDER BY sort_order ASC, name ASC -")->fetchAll(PDO::FETCH_ASSOC); - -$subjects = $pdo->query("SELECT id, name, color FROM scad_subjects ORDER BY name")->fetchAll(PDO::FETCH_ASSOC); +require __DIR__ . '/include/deadline_form_data.php'; $today = date('Y-m-d'); @@ -1059,143 +1039,7 @@ function getContrastTextColor($hexColor) - - + @@ -1225,111 +1069,6 @@ function getContrastTextColor($hexColor) var fpDue = flatpickr('#filterDueRange', fpOpts); var fpCheck = flatpickr('#filterCheckRange', fpOpts); - // --- Flatpickr Italian date fields in deadline modal --- - // Visible format: dd/mm/yyyy - // Submitted format: yyyy-mm-dd, compatible with MySQL DATE - var fpDocDate = flatpickr('#dlDocDate', { - dateFormat: 'Y-m-d', - altInput: true, - altFormat: 'd/m/Y', - locale: 'it', - allowInput: true - }); - - var fpDueDate = flatpickr('#dlDueDate', { - dateFormat: 'Y-m-d', - altInput: true, - altFormat: 'd/m/Y', - locale: 'it', - allowInput: true - }); - - var fpCheckDate = flatpickr('#dlCheckDate', { - dateFormat: 'Y-m-d', - altInput: true, - altFormat: 'd/m/Y', - locale: 'it', - allowInput: true - }); - - // --- Select2 --- - $('#dlSubject').select2({ - theme: 'bootstrap-5', - placeholder: 'Seleziona argomento...', - allowClear: true, - dropdownParent: $('#deadlineModal .modal-body'), - language: 'it', - width: '100%' - }); - - $('#dlDepartments').select2({ - theme: 'bootstrap-5', - placeholder: 'Seleziona reparti...', - allowClear: true, - dropdownParent: $('#deadlineModal .modal-body'), - language: 'it', - width: '100%' - }); - - $('#dlEmployees').select2({ - theme: 'bootstrap-5', - placeholder: 'Seleziona persone...', - allowClear: true, - dropdownParent: $('#deadlineModal .modal-body'), - language: 'it', - width: '100%' - }); - - // --- Auto-calc due_date from document_date + recurrence --- - var RECURRENCE_OFFSETS = { - monthly: { - months: 1 - }, - quarterly: { - months: 3 - }, - semiannual: { - months: 6 - }, - annual: { - years: 1 - }, - biennial: { - years: 2 - }, - triennial: { - years: 3 - }, - quadriennial: { - years: 4 - }, - quinquennial: { - years: 5 - }, - decennial: { - years: 10 - }, - quindecennial: { - years: 15 - } - }; - - function computeDueDate() { - var docVal = document.getElementById('dlDocDate').value; - var recurrence = document.getElementById('dlRecurrence').value; - var offset = RECURRENCE_OFFSETS[recurrence]; - if (!docVal || !offset) return; - var d = new Date(docVal + 'T00:00:00'); - if (isNaN(d.getTime())) return; - if (offset.months) d.setMonth(d.getMonth() + offset.months); - if (offset.years) d.setFullYear(d.getFullYear() + offset.years); - var iso = d.getFullYear() + '-' + - String(d.getMonth() + 1).padStart(2, '0') + '-' + - String(d.getDate()).padStart(2, '0'); - fpDueDate.setDate(iso, true, 'Y-m-d'); - } - $('#dlDocDate, #dlRecurrence').on('change', computeDueDate); - // --- DataTables custom filters --- $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) { if (settings.nTable.id !== 'deadlinesTable') return true; @@ -1491,165 +1230,8 @@ function getContrastTextColor($hexColor) // Apply default filter on load applyFiltersRefresh(); - // --- Modal --- - var modal = new bootstrap.Modal(document.getElementById('deadlineModal')); - var form = document.getElementById('deadlineForm'); - - // Add - // Add document.getElementById('btnAddDeadline').addEventListener('click', function() { - form.reset(); - - document.getElementById('dlId').value = ''; - document.getElementById('dlNotifDays').value = '7'; - document.getElementById('modalTitle').textContent = 'Nuova Scadenza'; - document.getElementById('dlFiles').value = ''; - - fpDocDate.clear(); - fpDueDate.clear(); - fpCheckDate.clear(); - - $('#dlSubject').val('').trigger('change'); - $('#dlDepartments').val(null).trigger('change'); - $('#dlEmployees').val(null).trigger('change'); - - renderAttachments([]); - modal.show(); - }); - - // Save - var isSaving = false; - form.addEventListener('submit', function(e) { - e.preventDefault(); - if (isSaving) return; - isSaving = true; - var saveBtn = form.querySelector('[type="submit"]'); - saveBtn.disabled = true; - saveBtn.innerHTML = ' Salvataggio...'; - var formData = new FormData(form); - - fetch('scadenzario/ajax/save_deadline.php', { - method: 'POST', - body: formData - }) - .then(function(r) { - return r.json(); - }) - .then(function(data) { - if (data.success) { - var deadlineId = data.id; - var fileInput = document.getElementById('dlFiles'); - if (fileInput.files.length > 0) { - // Upload files - var fileData = new FormData(); - fileData.append('deadline_id', deadlineId); - for (var i = 0; i < fileInput.files.length; i++) { - fileData.append('files[]', fileInput.files[i]); - } - return fetch('scadenzario/ajax/upload_attachment.php', { - method: 'POST', - body: fileData - }) - .then(function(r) { - return r.json(); - }) - .then(function(upData) { - modal.hide(); - Swal.fire({ - icon: 'success', - title: 'Salvato', - text: data.message + ' ' + upData.message, - timer: 2000, - showConfirmButton: false - }) - .then(function() { - location.reload(); - }); - }); - } else { - modal.hide(); - Swal.fire({ - icon: 'success', - title: 'Salvato', - text: data.message, - timer: 1500, - showConfirmButton: false - }) - .then(function() { - location.reload(); - }); - } - } else { - Swal.fire('Errore', data.message, 'error'); - isSaving = false; - saveBtn.disabled = false; - saveBtn.innerHTML = ' Salva'; - } - }) - .catch(function() { - Swal.fire('Errore', 'Errore di connessione.', 'error'); - isSaving = false; - saveBtn.disabled = false; - saveBtn.innerHTML = ' Salva'; - }); - }); - - // Render attachments list - function renderAttachments(attachments) { - var container = document.getElementById('attachmentsList'); - if (!attachments || attachments.length === 0) { - container.innerHTML = '
Nessun allegato
'; - return; - } - container.innerHTML = attachments.map(function(a) { - return '
' + - '' + $('').text(a.original_name).html() + '' + - '' + - '' + - '' + - '
'; - }).join(''); - } - - // Delete attachment - $(document).on('click', '.att-remove', function(e) { - e.preventDefault(); - var btn = $(this); - var attId = btn.data('att-id'); - Swal.fire({ - title: 'Rimuovere l\'allegato?', - text: 'Il collegamento verrà rimosso da questa scadenza. Il file resta disponibile se è usato da altre scadenze.', - icon: 'warning', - showCancelButton: true, - confirmButtonColor: '#dc3545', - cancelButtonText: 'Annulla', - confirmButtonText: 'Rimuovi', - reverseButtons: true - }).then(function(result) { - if (result.isConfirmed) { - fetch('scadenzario/ajax/delete_attachment.php?id=' + attId) - .then(function(r) { - return r.json(); - }) - .then(function(data) { - if (data.success) { - btn.closest('.att-item').remove(); - if ($('#attachmentsList .att-item').length === 0) { - renderAttachments([]); - } - Swal.fire({ - icon: 'success', - title: 'Fatto', - text: data.message, - timer: 1800, - showConfirmButton: false - }); - } else { - Swal.fire('Errore', data.message, 'error'); - } - }); - } - }); + if (window.openDeadlineCreate) window.openDeadlineCreate(); }); // Edit with confirmation @@ -1666,52 +1248,9 @@ function getContrastTextColor($hexColor) confirmButtonText: 'Sì, modifica', reverseButtons: true }).then(function(result) { - if (!result.isConfirmed) { - return; + if (result.isConfirmed && window.openDeadlineEdit) { + window.openDeadlineEdit(id); } - - fetch('scadenzario/ajax/get_deadline.php?id=' + id) - .then(function(r) { - return r.json(); - }) - .then(function(data) { - if (!data.success) { - Swal.fire('Errore', data.message, 'error'); - return; - } - - var d = data.data; - - document.getElementById('dlId').value = d.id; - $('#dlSubject').val(d.subject_id || '').trigger('change'); - document.getElementById('dlTopic').value = d.topic || ''; - document.getElementById('dlLaw').value = d.law_regulation || ''; - document.getElementById('dlRecurrence').value = d.recurrence_type || 'once'; - fpDocDate.setDate(d.document_date || null, false, 'Y-m-d'); - fpDueDate.setDate(d.due_date || null, false, 'Y-m-d'); - fpCheckDate.setDate(d.check_date || null, false, 'Y-m-d'); - document.getElementById('dlNotifDays').value = d.notification_days || 7; - document.getElementById('dlStorage').value = d.storage_location || ''; - document.getElementById('dlNotes').value = d.notes || ''; - document.getElementById('dlFiles').value = ''; - - document.getElementById('modalTitle').textContent = 'Modifica Scadenza'; - - $('#dlDepartments').val(d.department_names || []).trigger('change'); - - if (Array.isArray(d.employee_ids)) { - $('#dlEmployees').val(d.employee_ids.map(String)).trigger('change'); - } else { - $('#dlEmployees').val(null).trigger('change'); - } - - renderAttachments(d.attachments || []); - - modal.show(); - }) - .catch(function() { - Swal.fire('Errore', 'Errore di connessione.', 'error'); - }); }); }); @@ -1739,9 +1278,9 @@ function getContrastTextColor($hexColor) showConfirmButton: false }) .then(function() { - // Auto-open the newly created deadline; otherwise just refresh the list + // Open the new deadline's detail page with the edit modal auto-opening if (data.new_id) { - window.location = 'scadenzario/index.php?edit=' + data.new_id; + window.location = 'scadenzario/detail.php?id=' + data.new_id + '#edit'; } else { location.reload(); } @@ -1851,38 +1390,6 @@ function getContrastTextColor($hexColor) }); }); - // Auto-open edit modal from ?edit=ID - var urlParams = new URLSearchParams(window.location.search); - var editId = urlParams.get('edit'); - if (editId) { - history.replaceState(null, '', 'scadenzario/index.php'); - fetch('scadenzario/ajax/get_deadline.php?id=' + editId) - .then(function(r) { - return r.json(); - }) - .then(function(data) { - if (!data.success) return; - var d = data.data; - document.getElementById('dlId').value = d.id; - $('#dlSubject').val(d.subject_id || '').trigger('change'); - document.getElementById('dlTopic').value = d.topic || ''; - document.getElementById('dlLaw').value = d.law_regulation || ''; - document.getElementById('dlRecurrence').value = d.recurrence_type || 'once'; - fpDocDate.setDate(d.document_date || null, false, 'Y-m-d'); - fpDueDate.setDate(d.due_date || null, false, 'Y-m-d'); - fpCheckDate.setDate(d.check_date || null, false, 'Y-m-d'); - document.getElementById('dlNotifDays').value = d.notification_days || 7; - document.getElementById('dlStorage').value = d.storage_location || ''; - document.getElementById('dlNotes').value = d.notes || ''; - document.getElementById('dlFiles').value = ''; - document.getElementById('modalTitle').textContent = 'Modifica Scadenza'; - $('#dlDepartments').val(d.department_names || []).trigger('change'); - $('#dlEmployees').val(d.employee_ids.map(String)).trigger('change'); - renderAttachments(d.attachments || []); - modal.show(); - }); - } - // Stampa function doStampa() { var params = []; @@ -1905,6 +1412,7 @@ function getContrastTextColor($hexColor) if (btnStampaMobile) btnStampaMobile.addEventListener('click', doStampa); }); + - \ No newline at end of file +