diff --git a/public/userarea/analysisModal.js b/public/userarea/analysisModal.js new file mode 100644 index 0000000..5364046 --- /dev/null +++ b/public/userarea/analysisModal.js @@ -0,0 +1,253 @@ +(function () { + "use strict"; + let analysisMatriciMap = {}; + + function loadAnalysisMatrixNames() { + return $.ajax({ + url: "get_matrici_db.php", + method: "GET", + dataType: "json", + }) + .done(function (data) { + analysisMatriciMap = {}; + + (data.value || []).forEach(function (matrice) { + analysisMatriciMap[String(matrice.IdMatrice)] = + matrice.NomeMatrice || "#" + matrice.IdMatrice; + }); + + applyAnalysisMatrixNames(); + }) + .fail(function () { + analysisMatriciMap = {}; + applyAnalysisMatrixNames(); + }); + } + + function applyAnalysisMatrixNames() { + const modal = document.getElementById("analysisModal"); + if (!modal) return; + + modal.querySelectorAll(".analysis-matrix-item").forEach((item) => { + const matrixId = item.getAttribute("data-matrix-id"); + const nameEl = item.querySelector(".analysis-matrix-name"); + + let matrixName = "No Matrix"; + if (matrixId && matrixId !== "NO_MATRIX") { + matrixName = + analysisMatriciMap[String(matrixId)] || "#" + matrixId; + } + + item.setAttribute("data-matrix-name", matrixName); + + if (nameEl) { + nameEl.textContent = matrixName; + } + }); + + modal.querySelectorAll(".analysis-part-matrix-name").forEach((el) => { + const matrixId = el.getAttribute("data-matrix-id"); + + if (!matrixId || matrixId === "NO_MATRIX") { + el.textContent = "No Matrix"; + return; + } + + el.textContent = + analysisMatriciMap[String(matrixId)] || "#" + matrixId; + }); + } + + function updateSelectedPartsInfo() { + const modal = document.getElementById("analysisModal"); + if (!modal) return; + + const checked = modal.querySelectorAll( + ".analysis-part-checkbox:checked", + ); + const ids = Array.from(checked).map((el) => el.value); + + const countEl = modal.querySelector("#analysisSelectedPartsCount"); + const idsEl = modal.querySelector("#analysisSelectedPartsIds"); + + if (countEl) { + countEl.textContent = `${ids.length} selected`; + } + + if (idsEl) { + idsEl.textContent = ids.length ? ids.join(", ") : "-"; + } + + modal.querySelectorAll(".analysis-part-item").forEach((item) => { + const checkbox = item.querySelector(".analysis-part-checkbox"); + if (checkbox && checkbox.checked) { + item.classList.add("part-checked"); + } else { + item.classList.remove("part-checked"); + } + }); + } + + function selectPartsByMatrix(matrixId, matrixLabel) { + const modal = document.getElementById("analysisModal"); + if (!modal) return; + + const partItems = modal.querySelectorAll(".analysis-part-item"); + const matrixLabelEl = modal.querySelector("#analysisCurrentMatrix"); + + partItems.forEach((item) => { + const itemMatrixId = item.getAttribute("data-matrix-id"); + const checkbox = item.querySelector(".analysis-part-checkbox"); + + item.classList.remove("matrix-active"); + + if (itemMatrixId === String(matrixId)) { + item.classList.add("matrix-active"); + if (checkbox) checkbox.checked = true; + } else { + if (checkbox) checkbox.checked = false; + } + }); + + if (matrixLabelEl) { + matrixLabelEl.textContent = matrixLabel || "-"; + } + + updateSelectedPartsInfo(); + } + + function clearAnalysisSelection() { + const modal = document.getElementById("analysisModal"); + if (!modal) return; + + modal.querySelectorAll(".analysis-matrix-item").forEach((item) => { + item.classList.remove("active"); + }); + + modal.querySelectorAll(".analysis-part-item").forEach((item) => { + item.classList.remove("matrix-active", "part-checked"); + }); + + modal.querySelectorAll(".analysis-part-checkbox").forEach((cb) => { + cb.checked = false; + }); + + const matrixLabelEl = modal.querySelector("#analysisCurrentMatrix"); + if (matrixLabelEl) matrixLabelEl.textContent = "-"; + + updateSelectedPartsInfo(); + } + + function initAnalysisModal() { + const modal = document.getElementById("analysisModal"); + if (!modal) return; + + modal.querySelectorAll(".analysis-matrix-item").forEach((btn) => { + btn.addEventListener("click", function () { + modal + .querySelectorAll(".analysis-matrix-item") + .forEach((x) => x.classList.remove("active")); + this.classList.add("active"); + + const matrixId = this.getAttribute("data-matrix-id"); + const matrixLabel = + this.getAttribute("data-matrix-name") || + this.textContent.trim(); + + selectPartsByMatrix(matrixId, matrixLabel); + }); + }); + + modal.querySelectorAll(".analysis-part-checkbox").forEach((cb) => { + cb.addEventListener("change", function () { + updateSelectedPartsInfo(); + }); + }); + + const clearBtn = modal.querySelector("#analysisClearSelectionBtn"); + if (clearBtn) { + clearBtn.addEventListener("click", function () { + clearAnalysisSelection(); + }); + } + + const firstActiveMatrix = modal.querySelector( + ".analysis-matrix-item.active", + ); + if (firstActiveMatrix) { + const matrixId = firstActiveMatrix.getAttribute("data-matrix-id"); + const matrixLabel = + firstActiveMatrix.getAttribute("data-matrix-name") || + firstActiveMatrix.textContent.trim(); + + selectPartsByMatrix(matrixId, matrixLabel); + } else { + updateSelectedPartsInfo(); + } + } + + // OPEN ANALYSIS MODAL FROM PARTS MODAL BUTTON + $(document).on("click", ".open-analysis-modal-btn", function () { + const partsModal = document.getElementById("partsModal"); + const iddatadb = $("#partsModal").data("iddatadb"); + + if (!iddatadb) { + console.error("iddatadb not found on #partsModal"); + alert("iddatadb not found"); + return; + } + + $.ajax({ + url: "modal_analysis.php", + method: "GET", + data: { iddatadb: iddatadb }, + success: function (response) { + $("#analysisModalContainer").html(response); + + const modalElement = document.getElementById("analysisModal"); + if (!modalElement) { + console.error("Analysis modal not found: #analysisModal"); + return; + } + + let modal = bootstrap.Modal.getInstance(modalElement); + if (!modal) { + modal = new bootstrap.Modal(modalElement, { + backdrop: true, + keyboard: true, + focus: true, + }); + } + + loadAnalysisMatrixNames().always(function () { + initAnalysisModal(); + modal.show(); + }); + }, + error: function (xhr, status, error) { + console.error("Error loading analysis modal:", error); + alert("Error loading analysis modal: " + error); + }, + }); + }); + + // CLEANUP ON CLOSE + $(document).on("hidden.bs.modal", "#analysisModal", function () { + const modalElement = document.getElementById("analysisModal"); + if (modalElement) { + const modal = bootstrap.Modal.getInstance(modalElement); + if (modal) modal.dispose(); + } + + $("#analysisModalContainer").empty(); + + // keep parts modal alive, but remove extra backdrop leftovers + $(".modal-backdrop").last().remove(); + + if ($("#partsModal").hasClass("show")) { + $("body").addClass("modal-open"); + } else { + $("body").removeClass("modal-open").css("padding-right", ""); + } + }); +})(); diff --git a/public/userarea/get_analisi_by_matrice.php b/public/userarea/get_analisi_by_matrice.php new file mode 100644 index 0000000..fe33eb0 --- /dev/null +++ b/public/userarea/get_analisi_by_matrice.php @@ -0,0 +1,56 @@ + 'Missing or invalid id_matrice']); + exit; + } + + $api = VisualLimsApiClient::getInstance(); + + /** + * OData hypothesis: + * - Expand enabled matrices + * - Return only selectable analyses + * - Include generic analyses OR analyses enabled for the given matrix + * + * This endpoint must be verified against the real VisualLims API metadata. + */ + $filter = rawurlencode("SelezionabileSuWeb eq true and (IsGenerico eq true or MatriciAbilitate/any(m:m/IdMatrice eq $idMatrice))"); + $expand = rawurlencode("MatriciAbilitate"); + $endpoint = "Analisi?\$top=10"; + + // Debug URL + $base_url = 'https://93.43.5.102/limsapi/api/odata/'; + $full_url = $base_url . $endpoint; + file_put_contents(__DIR__ . '/last_url_analisi.txt', $full_url . PHP_EOL, FILE_APPEND); + + $data = $api->get($endpoint, []); + + file_put_contents( + __DIR__ . '/analisi_by_matrice_response.json', + json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) + ); + + $analisi = $data['value'] ?? []; + + echo json_encode(['value' => $analisi], JSON_UNESCAPED_UNICODE); +} catch (Exception $e) { + file_put_contents( + __DIR__ . '/error_log_analisi.txt', + date('Y-m-d H:i:s') . ' - ' . $e->getMessage() . PHP_EOL, + FILE_APPEND + ); + + http_response_code(500); + echo json_encode(['error' => $e->getMessage()]); +} diff --git a/public/userarea/import_edit2.php b/public/userarea/import_edit2.php index 133bba8..a139914 100644 --- a/public/userarea/import_edit2.php +++ b/public/userarea/import_edit2.php @@ -1426,6 +1426,7 @@ function fixedDefaultValue(array $f): string
+ @@ -1444,6 +1445,7 @@ function fixedDefaultValue(array $f): string +