Thesaurus/js/script.js

331 lines
12 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* script.js BIBB-Thesaurus Normdatenverwaltung
* Erstellt: 2021/07/01 (Roland)
* Überarbeitet: 2026/02 Bugfixes, Bereinigung, Fehlertoleranz
*/
// ============================================================
// HILFSFUNKTIONEN
// ============================================================
/**
* Zeigt eine Fehlermeldung in einem Alert-Element an.
* @param {string} elementId ID des Alert-Divs
* @param {string} msg Anzuzeigende Nachricht
* @param {string} level Bootstrap-Level: 'danger' | 'warning' | 'success'
*/
function showAlert(elementId, msg, level) {
const a = document.getElementById(elementId);
if (!a) return;
a.className = 'alert alert-' + (level || 'danger');
a.textContent = msg;
a.style.display = 'block';
}
function hideAlert(elementId) {
const a = document.getElementById(elementId);
if (!a) return;
a.className = 'alert';
a.textContent = '';
a.style.display = 'none';
}
function setFieldsDisabled(fields, disabled) {
fields.forEach(id => {
const el = document.getElementById(id);
if (el) el.disabled = disabled;
});
}
// ============================================================
// SUCHE
// ============================================================
function Search() {
const search_string = $("#search_text").val().trim();
if (search_string === "") {
alert('Bitte Suchkriterien eingeben!');
return;
}
$.post("ajax/search.php", { search_string }, function(data) {
$(".records_content").html(data);
$("#search_text").val("");
}).fail(function() {
alert('Fehler bei der Suche. Bitte erneut versuchen.');
});
}
// ============================================================
// NEUES SCHLAGWORT
// ============================================================
function newSubjectShow() {
const editableFields = ["new_term", "new_type", "new_detailtype", "new_classification", "new_scopenote"];
document.getElementById("newSubjectSave").style.display = 'inline';
document.getElementById("newSubjectDismiss").style.display = 'inline';
document.getElementById("SubjectModifySave").style.display = 'none';
document.getElementById("new_relation_label").style.display = 'none';
document.getElementById("newRelationSubjectDismiss").style.display = 'none';
document.getElementById("relation_table").style.display = 'none';
setFieldsDisabled(editableFields, false);
hideAlert("errorNewSubject");
$("#new_term").val("");
$("#new_classification").val("");
$("#new_scopenote").val("");
showRelations(0, 0, 0);
$("#NewSubjectModal").modal("show");
}
function CreateNewEntry() {
const term = $("#new_term").val().trim();
const type = $("#new_type").val().trim();
const detailtype = $("#new_detailtype").val().trim();
const classification = $("#new_classification").val().trim();
const scopenote = $("#new_scopenote").val().trim();
if (term.length === 0) {
showAlert("errorNewSubject", "Bitte Schlagwort eingeben!", "danger");
return;
}
hideAlert("errorNewSubject");
$.post("ajax/newSubject.php", { term, type, detailtype, classification, scopenote },
function(data) {
const dt = JSON.parse(data);
if (dt.status === 200) {
const editableFields = ["new_term", "new_type", "new_detailtype", "new_classification", "new_scopenote"];
setFieldsDisabled(editableFields, true);
document.getElementById("newSubjectSave").style.display = 'none';
document.getElementById("newSubjectDismiss").style.display = 'none';
document.getElementById("new_relation_label").style.display = 'block';
document.getElementById("newRelationSubjectDismiss").style.display = 'inline';
showAlert("errorNewSubject", "Neues Schlagwort aufgenommen", "success");
showRelations(dt.Anchor, dt.Entry, dt.Linking);
} else {
showAlert("errorNewSubject", dt.message || "Unbekannter Fehler", "danger");
}
}
).fail(function() {
showAlert("errorNewSubject", "Serverfehler beim Anlegen des Eintrags.", "danger");
});
}
// ============================================================
// RELATIONEN
// ============================================================
function showRelations(Anchor, Entry, Linking) {
$.get("ajax/getRelations.php", { anchorID: Anchor },
function(data, status) {
if (status === "success") {
$(".new_modal_content").html(JSON.parse(data));
document.getElementById("relation_table").style.display = 'block';
$("#NewSubjectModal").modal("show");
}
}
).fail(function() {
console.error("Fehler beim Laden der Relationen für AnchorID:", Anchor);
});
}
function CreateNewRelation(AnchorID) {
const relationType = document.getElementById("new_relationtype").value;
const relationText = document.getElementById("search_relation").value;
const relationID = relationText.substring(
relationText.lastIndexOf(":") + 2,
relationText.lastIndexOf(")")
).trim();
if (!relationID) {
showAlert("errorNewSubject", "Kein gültiger Relationseintrag ausgewählt.", "warning");
return;
}
$.post("ajax/writeNewRelation.php", { AnchorID, relationType, relationID },
function(data, status) {
if (status === "success") {
showAlert("errorNewSubject", "Neue Relation aufgenommen", "success");
showRelations(AnchorID, 0, 0);
}
}
).fail(function() {
showAlert("errorNewSubject", "Fehler beim Speichern der Relation.", "danger");
});
}
function DeleteRelation(AnchorID, LinkingID) {
$.post("ajax/deleteRelation.php", { AnchorID, LinkingID },
function(data, status) {
if (status === "success") {
showAlert("errorNewSubject", "Relation gelöscht", "success");
showRelations(AnchorID, 0, 0);
}
}
).fail(function() {
showAlert("errorNewSubject", "Fehler beim Löschen der Relation.", "danger");
});
}
// ============================================================
// EINTRAG BEARBEITEN / LÖSCHEN
// ============================================================
function DeleteTerm(AnchorID) {
$.post("ajax/deleteTerm.php", { AnchorID: AnchorID[0] },
function(data, status) {
if (status !== "success") {
alert(JSON.parse(data));
}
}
).fail(function() {
alert("Fehler beim Löschen des Eintrags.");
});
}
function ModifyTerm(AnchorID) {
$("#ID").val(AnchorID[0]);
$.get("ajax/getAuthorityDataRaw.php", { id: AnchorID[0] },
function(data, status) {
if (status === "success") {
const dt = JSON.parse(data).rows[0];
if (!dt) return;
$("#new_term").val(dt.Text);
$("#new_id").val(dt.ID);
$("#new_detailtype").val(dt.DetailType);
$("#new_classification").val(dt.Classification);
$("#new_descriptor").val(dt.Descriptor);
$("#new_type").val(dt.Type);
$("#new_scopenote").val(dt.Scopenote);
}
}
);
$.get("ajax/getRelations.php", { anchorID: AnchorID[0] },
function(data, status) {
if (status === "success") {
$(".new_modal_content").html(JSON.parse(data));
document.getElementById("relation_table").style.display = 'block';
document.getElementById("newSubjectSave").style.display = 'none';
document.getElementById("newSubjectDismiss").style.display = 'inline';
document.getElementById("SubjectModifySave").style.display = 'inline';
document.getElementById("newRelationSubjectDismiss").style.display = 'none';
$("#NewSubjectModalHeadline").val("Schlagwort ändern");
$("#NewSubjectModal").modal("show");
}
}
);
}
function UpdateEntry(authType) {
const AnchorID = $("#ID").val();
const term = $("#new_term").val().trim();
const type = $("#new_type").val().trim();
const detailtype = $("#new_detailtype").val().trim();
const classification = $("#new_classification").val().trim();
const scopenote = $("#new_scopenote").val().trim();
if (term.length === 0) {
showAlert("errorNewSubject", "Bitte Schlagwort eingeben!", "danger");
return;
}
hideAlert("errorNewSubject");
$.post("ajax/UpdateTerm.php", { AnchorID, authType, term, type, detailtype, classification, scopenote },
function(data, status) {
if (status === "success") {
document.getElementById("relation_table").style.display = 'block';
document.getElementById("newSubjectSave").style.display = 'none';
document.getElementById("newSubjectDismiss").style.display = 'inline';
document.getElementById("SubjectModifySave").style.display = 'none';
document.getElementById("newRelationSubjectDismiss").style.display = 'none';
showAlert("errorNewSubject", "Schlagwort geändert", "success");
$("#NewSubjectModalHeadline").val("Schlagwort ändern");
$("#NewSubjectModal").modal("show");
}
}
).fail(function() {
showAlert("errorNewSubject", "Fehler beim Speichern des Eintrags.", "danger");
});
}
// ============================================================
// STATISTIK
// ============================================================
function generateStatistics() {
const startVal = document.getElementById("startPeriod").value;
const endVal = document.getElementById("endPeriod").value;
if (!startVal || !endVal) {
showAlert("errorStatistics", "Bitte Start- und Endtermin angeben.", "warning");
return;
}
if (startVal > endVal) {
showAlert("errorStatistics", "Starttermin darf nicht nach dem Endtermin liegen.", "warning");
return;
}
hideAlert("errorStatistics");
const content = $("#records_content");
// Spinner oberhalb des Ergebnisbereichs anzeigen, records_content NICHT anfassen
$("#statisticsSpinner").remove();
$("#records_content").before(
'<div id="statisticsSpinner" class="text-center my-3">'
+ '<div class="spinner-border text-primary" role="status">'
+ '<span class="visually-hidden">Lädt...</span></div></div>'
);
$("#records_content").hide();
$.ajax({
url: "ajax/getStatistics.php",
method: "GET",
dataType: "json",
data: {
dateStart: (startVal + " 00:00:00").trim(),
dateEnd: (endVal + " 23:59:59").trim()
},
success: function(data) {
$("#statisticsSpinner").remove();
if (data.status === 200) {
document.getElementById("statisticsBody").innerHTML = data.rows;
document.getElementById("statisticsFoot").innerHTML = data.foot;
document.getElementById("statisticsZeitraum").textContent = data.zeitraum;
content.show();
} else {
showAlert("errorStatistics", data.message || "Unbekannter Fehler.", "warning");
}
},
error: function(xhr, status, error) {
$("#statisticsSpinner").remove();
showAlert("errorStatistics", "Serverfehler: " + (error || status), "danger");
}
});
}
// ============================================================
// HILFSFUNKTIONEN UI
// ============================================================
function RecordContentHide() {
const el = document.getElementById("records_content");
el.style.display = 'none';
el.textContent = '';
}
function DisplayEnterDateSelection() {
document.getElementById("QueryStatistics").style.display = 'block';
}