Files
awo-hamburg-intranet/module/knowledgecenter_update/Views/post_cardform_settings.php
Moritz Weinmann 8aadf65cc3 feat(einricht): Verknüpfte Einrichtungen bei Mehrfachauswahl automatisch mitselektieren
Wenn eine Einrichtung mit hinterlegten Links ausgewählt wird, werden alle
verknüpften Einrichtungen (aus organigramm_einricht_link) automatisch
mitausgewählt. Gilt für alle 5 Einrichtungs-Multiselects (3× Semantic UI
Dropdown, 2× Tagify).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-29 10:07:25 +02:00

577 lines
24 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.
<?php
// post_cardform_settings.php
// Sicherstellen, dass $postId gesetzt ist andernfalls neuen Beitrag (0) annehmen
if (!isset($postId)) {
$postId = 0;
}
// Falls $languageId noch nicht definiert ist, Standard (z.B. 1) verwenden
if (!isset($languageId)) {
$languageId = 1;
}
// Load current linked form for this post (if any)
$currentFormId = 0;
$res = mysqli_query($GLOBALS['mysql_con'], "
SELECT form_id
FROM knowledgecenter_post_form
WHERE post_id = $postId
LIMIT 1
");
if ($row = mysqli_fetch_assoc($res)) {
$currentFormId = (int)$row['form_id'];
}
// Build <option> list from contactform_header with 'selected' on current form
$formOptions = "";
$query = "SELECT id, description FROM contactform_header ORDER BY id ASC";
$result = mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = mysqli_fetch_assoc($result)) {
// Mark the currently linked form as selected
$selected = ($row['id'] == $currentFormId) ? 'selected' : '';
$formOptions .= "<option value='" . $row['id'] . "' $selected>"
. htmlspecialchars($row['description']) . "</option>";
}
// -------------------- Kategorien --------------------
// Erzeuge die Whitelist für alle verfügbaren Kategorien (mit Übersetzung)
// Betriebsrat sieht nur Kategorien, die für ihn freigegeben sind (direkt oder über Elternkategorie)
$categoriesData = [];
$query = "SELECT c.id, t.title, c.enable_post_color_pickers
FROM knowledgecenter_categories_update AS c
LEFT JOIN knowledgecenter_category_translations AS t
ON c.id = t.category_id
WHERE t.language_id = $languageId";
$result = mysqli_query($GLOBALS['mysql_con'], $query);
$isBrOnly = !is_wiki_redakteur() && is_wiki_betriebsrat();
while ($row = mysqli_fetch_assoc($result)) {
if ($isBrOnly && !is_category_betriebsrat_editable((int)$row['id'])) {
continue;
}
$categoriesData[] = [
'value' => $row['id'],
'name' => $row['title'],
'enable_post_color_pickers' => (int) ($row['enable_post_color_pickers'] ?? 0)
];
}
// Bereits zugeordnete Kategorien abrufen (falls vorhanden)
$selectedCategories = [];
if ($postId != 0) {
$querySelected = "SELECT c.id, t.title
FROM knowledgecenter_category_post AS pc
JOIN knowledgecenter_categories_update AS c ON pc.category_id = c.id
LEFT JOIN knowledgecenter_category_translations AS t
ON c.id = t.category_id
WHERE pc.post_id = $postId AND t.language_id = $languageId";
$resultSelected = mysqli_query($GLOBALS['mysql_con'], $querySelected);
while ($row = mysqli_fetch_assoc($resultSelected)) {
$selectedCategories[] = ['value' => $row['id'], 'name' => $row['title']];
}
}
// -------------------- Mandanten für Post --------------------
$postMandantsData = [];
$query = "SELECT id, description FROM main_mandant";
$result = mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = mysqli_fetch_assoc($result)) {
// Hier verwenden wir 'name' als Anzeige-Text, der die description enthält.
$postMandantsData[] = ['value' => $row['id'], 'name' => $row['description']];
}
$selectedPostMandants = [];
if ($postId != 0) {
$querySelected = "SELECT m.id, m.description
FROM knowledgecenter_post_mandant AS pm
JOIN main_mandant AS m ON pm.mandant_id = m.id
WHERE pm.post_id = $postId";
$resultSelected = mysqli_query($GLOBALS['mysql_con'], $querySelected);
while ($row = mysqli_fetch_assoc($resultSelected)) {
$selectedPostMandants[] = ['value' => $row['id'], 'name' => $row['description']];
}
}
// -------------------- Departments für Post --------------------
$postDepartmentsData = [];
$query = "SELECT id, description FROM main_department";
$result = mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = mysqli_fetch_assoc($result)) {
$postDepartmentsData[] = ['value' => $row['id'], 'name' => $row['description']];
}
$selectedPostDepartments = [];
if ($postId != 0) {
$querySelected = "SELECT d.id, d.description
FROM knowledgecenter_post_department AS pd
JOIN main_department AS d ON pd.department_id = d.id
WHERE pd.post_id = $postId";
$resultSelected = mysqli_query($GLOBALS['mysql_con'], $querySelected);
while ($row = mysqli_fetch_assoc($resultSelected)) {
$selectedPostDepartments[] = ['value' => $row['id'], 'name' => $row['description']];
}
}
// -------------------- Rollen für Post --------------------
$postRolesData = [];
$query = "SELECT id, description FROM main_role";
$result = mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = mysqli_fetch_assoc($result)) {
$postRolesData[] = ['value' => $row['id'], 'name' => $row['description']];
}
$selectedPostRoles = [];
if ($postId != 0) {
$querySelected = "SELECT r.id, r.description
FROM knowledgecenter_post_role AS pr
JOIN main_role AS r ON pr.role_id = r.id
WHERE pr.post_id = $postId";
$resultSelected = mysqli_query($GLOBALS['mysql_con'], $querySelected);
while ($row = mysqli_fetch_assoc($resultSelected)) {
$selectedPostRoles[] = ['value' => $row['id'], 'name' => $row['description']];
}
}
// -------------------- Einrichtungen für Post --------------------
$postEinrichtsData = [];
$query = "SELECT id, description FROM organigramm_einricht ORDER BY description ASC";
$result = mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = mysqli_fetch_assoc($result)) {
$postEinrichtsData[] = ['value' => $row['id'], 'name' => $row['description']];
}
// Verknüpfungskarte für Einrichtungen (Gruppen → Mitglieder)
$postEinrichtLinks = [];
$linkRes = mysqli_query($GLOBALS['mysql_con'], "SELECT einricht_id, linked_einricht_id FROM organigramm_einricht_link");
while ($lr = mysqli_fetch_assoc($linkRes)) {
$postEinrichtLinks[strval($lr['einricht_id'])][] = strval($lr['linked_einricht_id']);
}
$selectedPostEinrichts = [];
if ($postId != 0) {
$querySelected = "SELECT e.id, e.description
FROM knowledgecenter_post_einricht AS pe
JOIN organigramm_einricht AS e ON pe.einricht_id = e.id
WHERE pe.post_id = $postId";
$resultSelected = mysqli_query($GLOBALS['mysql_con'], $querySelected);
while ($row = mysqli_fetch_assoc($resultSelected)) {
$selectedPostEinrichts[] = ['value' => $row['id'], 'name' => $row['description']];
}
}
// -------------------- Fachbereiche für Post --------------------
$postFachbereicheData = [];
$query = "SELECT id, description FROM organogramm_space ORDER BY description ASC";
$result = mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = mysqli_fetch_assoc($result)) {
$postFachbereicheData[] = ['value' => $row['id'], 'name' => $row['description']];
}
$selectedPostFachbereiche = [];
if ($postId != 0) {
$querySelected = "SELECT s.id, s.description
FROM knowledgecenter_post_fachbereich AS pfb
JOIN organogramm_space AS s ON pfb.fachbereich_id = s.id
WHERE pfb.post_id = $postId";
$resultSelected = mysqli_query($GLOBALS['mysql_con'], $querySelected);
while ($row = mysqli_fetch_assoc($resultSelected)) {
$selectedPostFachbereiche[] = ['value' => $row['id'], 'name' => $row['description']];
}
}
$postBackgroundColor = '#FFFFFF';
if (!empty($post['background_color'])) {
$postBackgroundColor = $post['background_color'];
} elseif (!empty($currentVersion['background_color'])) {
$postBackgroundColor = $currentVersion['background_color'];
}
$postTextColor = '#000000';
if (!empty($post['text_color'])) {
$postTextColor = $post['text_color'];
} elseif (!empty($currentVersion['text_color'])) {
$postTextColor = $currentVersion['text_color'];
}
?>
<button id="message" style="opacity:0; position: absolute;"></button>
<div class="titlebar_small">
<h2><?=$translation->get("settings")?></h2>
<?php if ($canEditPost ?? false) { ?>
<span id="saveAllBtn" class="post-action-btn-small green"><?= $saveIcon ?></span>
<?php } ?>
</div>
<hr>
<div class="settings">
<form id="postSettingsForm">
<input type="hidden" id="post_id" name="post_id" value="<?php echo $postId; ?>" />
<h3><?=$translation->get("categories")?>
</h3>
<div class="settings_container">
<input type="text" id="categoryTags" name="categories" placeholder="Kategorie hinzufügen…" />
</div>
<div id="postColorPickersSection" class="post-color-grid" style="display:none;">
<div class="post-color-item">
<label for="postBackgroundColor"><?= $translation->get("post_background_color") ?></label>
<input type="color" id="postBackgroundColor" name="background_color"
value="<?= htmlspecialchars($postBackgroundColor, ENT_QUOTES, 'UTF-8') ?>">
</div>
<div class="post-color-item">
<label for="postTextColor"><?= $translation->get("post_text_color") ?></label>
<input type="color" id="postTextColor" name="text_color"
value="<?= htmlspecialchars($postTextColor, ENT_QUOTES, 'UTF-8') ?>">
</div>
</div>
<h3><?=$translation->get("Geschäftsstellen")?>
</h3>
<div class="settings_container">
<input type="text" id="postMandantTags" name="postMandants" placeholder="<?=$translation->get("Geschäftsstellen")?>…" />
</div>
<h3><?=$translation->get("departments")?>
</h3>
<div class="settings_container">
<input type="text" id="postDepartmentTags" name="postDepartments" placeholder="<?=$translation->get("departments")?>…" />
</div>
<h3><?=$translation->get("roles")?>
</h3>
<div class="settings_container">
<input type="text" id="postRoleTags" name="postRoles" placeholder="<?=$translation->get("roles")?>…" />
</div>
<h3>Einrichtungen</h3>
<div class="settings_container">
<input type="text" id="postEinrichtTags" name="postEinrichts" placeholder="Einrichtung hinzufügen…" />
</div>
<h3>Fachbereiche</h3>
<div class="settings_container">
<input type="text" id="postFachbereichTags" name="postFachbereiche" placeholder="Fachbereich hinzufügen…" />
</div>
<h3>Formular auswählen</h3>
<div class="settings_container">
<select name="postFormSelect" id="postFormSelect">
<option value="">Bitte wählen…</option>
<?php echo $formOptions; ?>
</select>
</div>
<hgroup>
<h3><?=$translation->get("pdf_inline_preview")?></h3>
<small><?=$translation->get("pdf_inline_preview_explanation")?></small>
</hgroup>
<div class="settings_container">
<label>
<input type="checkbox" id="pdfInlineCheckbox" name="pdf_inline" value="1"
<?php echo (!empty($post['preview_inline']) && $post['preview_inline'] == 1) ? 'checked' : ''; ?>>
<?=$translation->get("pdf_inline_preview_label")?>
</label>
</div>
<h3><?=$translation->get("delete_post")?></h3>
<div class="settings_container">
<div id="deletePostBtn" class="post-action-btn-text red">
<?=$translation->get("delete_post")?>
</div>
</div>
</form>
</div>
<script>
// Übergabe der Whitelist-Daten aus PHP
var categoriesData = <?php echo json_encode($categoriesData); ?>;
var postMandantsData = <?php echo json_encode($postMandantsData); ?>;
var postDepartmentsData = <?php echo json_encode($postDepartmentsData); ?>;
var postRolesData = <?php echo json_encode($postRolesData); ?>;
var postEinrichtsData = <?php echo json_encode($postEinrichtsData); ?>;
var postFachbereicheData = <?php echo json_encode($postFachbereicheData); ?>;
var selectedCategories = <?php echo json_encode($selectedCategories); ?>;
var selectedPostMandants = <?php echo json_encode($selectedPostMandants); ?>;
var selectedPostDepartments = <?php echo json_encode($selectedPostDepartments); ?>;
var selectedPostRoles = <?php echo json_encode($selectedPostRoles); ?>;
var selectedPostEinrichts = <?php echo json_encode($selectedPostEinrichts); ?>;
var selectedPostFachbereiche = <?php echo json_encode($selectedPostFachbereiche); ?>;
var categoryEnableColorMap = {};
categoriesData.forEach(function (cat) {
categoryEnableColorMap[String(cat.value)] = parseInt(cat.enable_post_color_pickers || 0, 10) === 1;
});
function updatePostColorPickerVisibility() {
var selectedValues = tagifyCategories.value.map(function (tag) {
return String(tag.value);
});
var hasEnabledCategory = selectedValues.some(function (catId) {
return categoryEnableColorMap[catId] === true;
});
if (hasEnabledCategory) {
$("#postColorPickersSection").css("display", "grid");
} else {
$("#postColorPickersSection").hide();
}
}
// Kategorien
var tagifyCategories = new Tagify(document.getElementById('categoryTags'), {
whitelist: categoriesData,
tagTextProp: 'name',
enforceWhitelist: true,
dropdown: {
enabled: 0,
maxItems: Infinity,
searchKeys: ["name"]
},
templates: {
dropdownItem: function (tagData) {
return `<div ${this.getAttributes(tagData)} class="tagify__dropdown__item">
<span>${tagData.name}</span>
</div>`;
}
}
});
if (selectedCategories.length) {
tagifyCategories.addTags(selectedCategories);
}
tagifyCategories.on('add', updatePostColorPickerVisibility);
tagifyCategories.on('remove', updatePostColorPickerVisibility);
updatePostColorPickerVisibility();
// Mandanten für Post
var tagifyPostMandants = new Tagify(document.getElementById('postMandantTags'), {
whitelist: postMandantsData,
tagTextProp: 'name',
enforceWhitelist: true,
dropdown: { enabled: 0, maxItems: Infinity, searchKeys: ["name"] },
templates: {
dropdownItem: function (tagData) {
return `<div ${this.getAttributes(tagData)} class="tagify__dropdown__item">
<span>${tagData.name}</span>
</div>`;
}
}
});
if (selectedPostMandants.length) {
tagifyPostMandants.addTags(selectedPostMandants);
}
// Departments für Post
var tagifyPostDepartments = new Tagify(document.getElementById('postDepartmentTags'), {
whitelist: postDepartmentsData,
tagTextProp: 'name',
enforceWhitelist: true,
dropdown: { enabled: 0, maxItems: Infinity, searchKeys: ["name"] },
templates: {
dropdownItem: function (tagData) {
return `<div ${this.getAttributes(tagData)} class="tagify__dropdown__item">
<span>${tagData.name}</span>
</div>`;
}
}
});
if (selectedPostDepartments.length) {
tagifyPostDepartments.addTags(selectedPostDepartments);
}
// Rollen für Post
var tagifyPostRoles = new Tagify(document.getElementById('postRoleTags'), {
whitelist: postRolesData,
tagTextProp: 'name',
enforceWhitelist: true,
dropdown: { enabled: 0, maxItems: Infinity, searchKeys: ["name"] },
templates: {
dropdownItem: function (tagData) {
return `<div ${this.getAttributes(tagData)} class="tagify__dropdown__item">
<span>${tagData.name}</span>
</div>`;
}
}
});
if (selectedPostRoles.length) {
tagifyPostRoles.addTags(selectedPostRoles);
}
// Einrichtungen für Post
var tagifyPostEinrichts = new Tagify(document.getElementById('postEinrichtTags'), {
whitelist: postEinrichtsData,
tagTextProp: 'name',
enforceWhitelist: true,
dropdown: { enabled: 0, maxItems: Infinity, searchKeys: ["name"] },
templates: {
dropdownItem: function (tagData) {
return `<div ${this.getAttributes(tagData)} class="tagify__dropdown__item">
<span>${tagData.name}</span>
</div>`;
}
}
});
if (selectedPostEinrichts.length) {
tagifyPostEinrichts.addTags(selectedPostEinrichts);
}
var postEinrichtLinks = <?= json_encode($postEinrichtLinks) ?>;
tagifyPostEinrichts.on('add', function(e) {
var addedId = String(e.detail.data.value);
(postEinrichtLinks[addedId] || []).forEach(function(lid) {
var exists = tagifyPostEinrichts.value.some(function(t) { return String(t.value) === lid; });
if (!exists) {
var item = postEinrichtsData.find(function(d) { return String(d.value) === lid; });
if (item) tagifyPostEinrichts.addTags([item]);
}
});
});
// Fachbereiche für Post
var tagifyPostFachbereiche = new Tagify(document.getElementById('postFachbereichTags'), {
whitelist: postFachbereicheData,
tagTextProp: 'name',
enforceWhitelist: true,
dropdown: { enabled: 0, maxItems: Infinity, searchKeys: ["name"] },
templates: {
dropdownItem: function (tagData) {
return `<div ${this.getAttributes(tagData)} class="tagify__dropdown__item">
<span>${tagData.name}</span>
</div>`;
}
}
});
if (selectedPostFachbereiche.length) {
tagifyPostFachbereiche.addTags(selectedPostFachbereiche);
}
<?php if ($canEditPost ?? false) { ?>
$("#saveAllBtn").on("click", function () {
// Während des Speicherns blurren
$("#tab-settings").addClass("blurred");
var postId = $("#post_id").val();
var pdfInline = $("#pdfInlineCheckbox").prop("checked") ? 1 : 0;
var selectedCat = tagifyCategories.value.map(tag => tag.value);
var selectedMandants = tagifyPostMandants.value.map(tag => tag.value);
var selectedDepartments = tagifyPostDepartments.value.map(tag => tag.value);
var selectedRoles = tagifyPostRoles.value.map(tag => tag.value);
var selectedEinrichts = tagifyPostEinrichts.value.map(tag => tag.value);
var selectedFachbereiche = tagifyPostFachbereiche.value.map(tag => tag.value);
var selectedFormId = $("#postFormSelect").val();
var postBackgroundColor = $("#postBackgroundColor").val();
var postTextColor = $("#postTextColor").val();
$.when(
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_categories",
post_id: postId,
categories: JSON.stringify(selectedCat)
}, "json"),
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_mandants",
post_id: postId,
mandants: JSON.stringify(selectedMandants)
}, "json"),
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_form",
post_id: postId,
form_id: selectedFormId
}, "json"),
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_departments",
post_id: postId,
departments: JSON.stringify(selectedDepartments)
}, "json"),
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_roles",
post_id: postId,
roles: JSON.stringify(selectedRoles)
}, "json"),
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_einrichts",
post_id: postId,
einrichts: JSON.stringify(selectedEinrichts)
}, "json"),
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_fachbereiche",
post_id: postId,
fachbereiche: JSON.stringify(selectedFachbereiche)
}, "json"),
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_pdf_inline",
post_id: postId,
pdf_inline: pdfInline
}, "json"),
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "update_post_colors",
post_id: postId,
background_color: postBackgroundColor,
text_color: postTextColor
}, "json")
).done(function (categoriesResponse, mandantsResponse, formResponse, departmentsResponse, rolesResponse, einrichtsResponse, fachbereicheResponse, pdfInlineResponse, postColorsResponse) {
var catSuccess = categoriesResponse[0].success;
var mandantsSuccess = mandantsResponse[0].success;
var formSuccess = formResponse[0].success;
var departmentsSuccess = departmentsResponse[0].success;
var rolesSuccess = rolesResponse[0].success;
var einrichtsSuccess = einrichtsResponse[0].success;
var fachbereicheSuccess = fachbereicheResponse[0].success;
var pdfInlineSuccess = pdfInlineResponse[0].success;
var postColorsSuccess = postColorsResponse[0].success;
if (catSuccess && mandantsSuccess && formSuccess && departmentsSuccess && rolesSuccess && einrichtsSuccess && fachbereicheSuccess && pdfInlineSuccess && postColorsSuccess) {
showMessage("Alle Daten erfolgreich gespeichert!", true);
} else {
var errorMsg = "Fehler beim Speichern:";
if (!catSuccess) errorMsg += " Kategorien";
if (!mandantsSuccess) errorMsg += " Mandanten";
if (!formSuccess) errorMsg += " Formular";
if (!departmentsSuccess) errorMsg += " Departments";
if (!rolesSuccess) errorMsg += " Rollen";
if (!einrichtsSuccess) errorMsg += " Einrichtungen";
if (!fachbereicheSuccess) errorMsg += " Fachbereiche";
if (!pdfInlineSuccess) errorMsg += " PDF Inline Vorschau";
if (!postColorsSuccess) errorMsg += " Farben";
showMessage(errorMsg, false);
}
}).fail(function () {
showMessage("Es ist ein Fehler beim Speichern aufgetreten!", false);
}).always(function () {
$("#tab-settings").removeClass("blurred");
});
});
<?php } ?>
// Post löschen
$("#deletePostBtn").on("click", function() {
if (!confirm("<?= addslashes($translation->get("delete_post_confirm")) ?>")) {
return;
}
// Ajax-Aufruf an Deinen neuen Case
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: "delete_post",
post_id: <?=$postId?>
}, function(response) {
if (response.success) {
// Weiterleitung nach dem Löschen, z.B. zurück zur Übersicht
window.location.href = "?action=Category";
} else {
showMessage("Fehler beim Löschen: " + (response.error || "Unbekannter Fehler"), false);
}
}, "json").fail(function() {
showMessage("Serverfehler beim Löschen.", false);
});
});
</script>