721 lines
38 KiB
PHP
721 lines
38 KiB
PHP
<?php
|
|
function kc_workshops_page() {
|
|
global $wpdb;
|
|
$prefix = $wpdb->prefix;
|
|
kc_admin_tabs('kc_workshops');
|
|
|
|
// Liste aller Teamer für Multi-Select (sortiert wie in Teamer-Übersicht: Vorname A-Z)
|
|
$teamer_liste = $wpdb->get_results("SELECT * FROM {$prefix}kc_teamer ORDER BY vorname ASC");
|
|
|
|
// Liste aller Wahlen für Auswahl
|
|
$wahlen_liste = $wpdb->get_results("SELECT * FROM {$prefix}kc_wahlen ORDER BY id DESC");
|
|
|
|
// Liste aller Phasen für Auswahl
|
|
$phasen_liste = $wpdb->get_results("SELECT * FROM {$prefix}kc_phasen ORDER BY nummer");
|
|
|
|
// Workshop löschen
|
|
if (isset($_GET['delete_workshop'])) {
|
|
$wid = intval($_GET['delete_workshop']);
|
|
$wpdb->delete("{$prefix}kc_workshops", ['id' => $wid]);
|
|
echo '<div class="notice notice-success">Workshop gelöscht!</div>';
|
|
}
|
|
|
|
// Workshop speichern (neu/ändern)
|
|
if (isset($_POST['kc_workshop_save'])) {
|
|
$wid = !empty($_POST['wid']) ? intval($_POST['wid']) : null;
|
|
|
|
// Nur Workshop-Daten speichern, wenn Name übergeben wurde (nicht bei reiner Teamer-Bearbeitung)
|
|
if (!empty($_POST['name'])) {
|
|
// Ensure DB has min_teilnehmer column
|
|
$col = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM {$prefix}kc_workshops LIKE %s", 'min_teilnehmer'));
|
|
if (empty($col)) {
|
|
$wpdb->query("ALTER TABLE {$prefix}kc_workshops ADD COLUMN min_teilnehmer INT DEFAULT 0");
|
|
}
|
|
|
|
$data = [
|
|
'name' => sanitize_text_field($_POST['name']),
|
|
'beschreibung' => sanitize_text_field($_POST['beschreibung']),
|
|
'max_teilnehmer' => intval($_POST['max_teilnehmer']),
|
|
'min_teilnehmer' => intval($_POST['min_teilnehmer'] ?? 0)
|
|
];
|
|
if ($wid) {
|
|
$wpdb->update("{$prefix}kc_workshops", $data, ['id'=> $wid]);
|
|
echo '<div class="notice notice-success">Workshop aktualisiert!</div>';
|
|
} else {
|
|
$wpdb->insert("{$prefix}kc_workshops", $data);
|
|
$wid = intval($wpdb->insert_id);
|
|
echo '<div class="notice notice-success">Workshop angelegt!</div>';
|
|
}
|
|
}
|
|
|
|
// Teamer-Zuordnungen speichern mit Wahl/Phase (auch für neue Workshops)
|
|
if (!empty($wid)) {
|
|
// Stelle sicher, dass wahl_id und phase Spalten existieren
|
|
$columns = $wpdb->get_col("SHOW COLUMNS FROM {$prefix}kc_workshop_teamer");
|
|
if (!in_array('wahl_id', $columns)) {
|
|
$wpdb->query("ALTER TABLE {$prefix}kc_workshop_teamer ADD COLUMN wahl_id INT(11) NULL DEFAULT NULL");
|
|
}
|
|
if (!in_array('phase', $columns)) {
|
|
$wpdb->query("ALTER TABLE {$prefix}kc_workshop_teamer ADD COLUMN phase INT(11) NULL DEFAULT NULL");
|
|
}
|
|
|
|
$is_feste = !empty($_POST['edit_feste']);
|
|
$wahl_id = !empty($_POST['select_wahl_id']) ? intval($_POST['select_wahl_id']) : null;
|
|
$phase = !empty($_POST['select_phase']) ? intval($_POST['select_phase']) : null;
|
|
|
|
// Lösche alte Zuordnungen (nur wenn Teamer vorhanden sind, für neue Workshops)
|
|
if (!empty($_POST['teamer_ids'])) {
|
|
if ($is_feste) {
|
|
$wpdb->query($wpdb->prepare(
|
|
"DELETE FROM {$prefix}kc_workshop_teamer WHERE workshop_id=%d AND wahl_id IS NULL AND phase IS NULL",
|
|
$wid
|
|
));
|
|
} elseif ($wahl_id !== null) {
|
|
if ($phase !== null) {
|
|
$wpdb->delete("{$prefix}kc_workshop_teamer", [
|
|
'workshop_id' => $wid,
|
|
'wahl_id' => $wahl_id,
|
|
'phase' => $phase
|
|
]);
|
|
} else {
|
|
$wpdb->query($wpdb->prepare(
|
|
"DELETE FROM {$prefix}kc_workshop_teamer WHERE workshop_id=%d AND wahl_id=%d AND phase IS NULL",
|
|
$wid, $wahl_id
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Neue Zuordnungen einfügen
|
|
if (!empty($_POST['teamer_ids']) && is_array($_POST['teamer_ids'])) {
|
|
foreach ($_POST['teamer_ids'] as $tid) {
|
|
$tid = intval($tid);
|
|
if ($tid > 0) {
|
|
if ($is_feste) {
|
|
$wpdb->insert("{$prefix}kc_workshop_teamer", [
|
|
'workshop_id' => $wid,
|
|
'teamer_id' => $tid,
|
|
'wahl_id' => null,
|
|
'phase' => null
|
|
]);
|
|
} else {
|
|
$wpdb->insert("{$prefix}kc_workshop_teamer", [
|
|
'workshop_id' => $wid,
|
|
'teamer_id' => $tid,
|
|
'wahl_id' => $wahl_id,
|
|
'phase' => $phase
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Redirect nach Speichern von festen Teamern zur Gesamtübersicht
|
|
if ($is_feste && empty($_POST['name'])) {
|
|
echo '<div class="notice notice-success">Feste Teamer gespeichert!</div>';
|
|
echo "<meta http-equiv='refresh' content='0;url=" . esc_url(admin_url('admin.php?page=kc_workshops')) . "'>";
|
|
return;
|
|
}
|
|
|
|
// Redirect nach Speichern von wahl-spezifischen Teamern zurück zur Workshop-Bearbeitung
|
|
if ($wahl_id !== null && empty($_POST['name'])) {
|
|
echo '<div class="notice notice-success">Teamer-Zuordnung gespeichert!</div>';
|
|
echo "<meta http-equiv='refresh' content='0;url=" . esc_url(admin_url('admin.php?page=kc_workshops&edit_workshop=' . $wid)) . "'>";
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Workshop bearbeiten
|
|
if (isset($_GET['edit_workshop'])) {
|
|
$wid = intval($_GET['edit_workshop']);
|
|
$ws = $wpdb->get_row("SELECT * FROM {$prefix}kc_workshops WHERE id=$wid");
|
|
if (!$ws) {
|
|
echo '<div class="kc-admin-table-wrap">';
|
|
echo '<div class="notice notice-error">Workshop nicht gefunden.</div>';
|
|
echo '<a href="?page=kc_workshops" class="kc-btn del" style="margin-top:12px;">Zur\u00fcck zur \u00dcbersicht</a>';
|
|
echo '</div>';
|
|
return;
|
|
}
|
|
|
|
echo '<div class="kc-admin-table-wrap">';
|
|
echo '<h2>Workshop bearbeiten: '.esc_html($ws->name).'</h2>';
|
|
|
|
// Hole Spalten-Info
|
|
$columns = $wpdb->get_col("SHOW COLUMNS FROM {$prefix}kc_workshop_teamer");
|
|
$has_wahl = in_array('wahl_id', $columns);
|
|
$has_phase = in_array('phase', $columns);
|
|
|
|
// Zeige Feste Teamer (gelten für alle Wahlen)
|
|
if ($has_wahl && $has_phase) {
|
|
$feste_teamer = $wpdb->get_results($wpdb->prepare(
|
|
"SELECT t.vorname, t.nachname FROM {$prefix}kc_workshop_teamer wt
|
|
JOIN {$prefix}kc_teamer t ON wt.teamer_id=t.id
|
|
WHERE wt.workshop_id=%d AND wt.wahl_id IS NULL AND wt.phase IS NULL
|
|
ORDER BY t.vorname",
|
|
$wid
|
|
));
|
|
|
|
if (!empty($feste_teamer)) {
|
|
$teamer_names = array_map(function($t) { return $t->vorname.' '.$t->nachname; }, $feste_teamer);
|
|
$teamer_str = implode(', ', array_map('esc_html', $teamer_names));
|
|
echo '<div style="background:#e8f5e9;border:1px solid #4CAF50;border-radius:6px;padding:12px;margin-bottom:16px;">
|
|
<strong>Feste Teamer (für alle Wahlen):</strong> '.$teamer_str.'
|
|
<a href="?page=kc_workshops&edit_workshop='.$wid.'&edit_feste=1" class="kc-btn edit" style="margin-left:12px;font-size:90%;padding:4px 10px;">Bearbeiten</a>
|
|
</div>';
|
|
}
|
|
}
|
|
|
|
// Zeige Wahl/Phase-Kombinationen mit ihren Teamern
|
|
echo '<p style="margin-bottom:12px;"><strong>Teamer pro Wahl:</strong></p>';
|
|
echo '<div style="margin-bottom:12px;">';
|
|
if ($has_wahl && $has_phase) {
|
|
echo '<a href="?page=kc_workshops&edit_workshop='.$wid.'&edit_feste=1" class="kc-btn" style="background:#4CAF50;">+ Feste Teamer festlegen</a>';
|
|
}
|
|
echo '</div>';
|
|
echo '<table class="kc-admin-table" style="width:100%;margin-bottom:20px;">';
|
|
echo '<thead><tr><th>Wahl</th><th>Teamer</th><th>Aktion</th></tr></thead><tbody>';
|
|
|
|
// Hole alle Wahlen, die diesem Workshop zugeordnet sind
|
|
$wahlen_for_workshop = $wpdb->get_results($wpdb->prepare(
|
|
"SELECT DISTINCT ww.wahl_id, w.name, w.anzahl_einheiten
|
|
FROM {$prefix}kc_wahl_workshops ww
|
|
JOIN {$prefix}kc_wahlen w ON ww.wahl_id=w.id
|
|
WHERE ww.workshop_id=%d
|
|
ORDER BY w.id",
|
|
$wid
|
|
));
|
|
|
|
if (empty($wahlen_for_workshop)) {
|
|
echo '<tr><td colspan="3"><em style="color:#999;">Dieser Workshop ist noch keiner Wahl zugeordnet.</em></td></tr>';
|
|
} else {
|
|
foreach($wahlen_for_workshop as $w) {
|
|
// Sammle Teamer für diese Wahl
|
|
if ($has_wahl && $has_phase) {
|
|
// Prüfe ob es wahl-spezifische Teamer gibt
|
|
$wahl_teamer = $wpdb->get_results($wpdb->prepare(
|
|
"SELECT DISTINCT t.vorname, t.nachname FROM {$prefix}kc_workshop_teamer wt
|
|
JOIN {$prefix}kc_teamer t ON wt.teamer_id=t.id
|
|
WHERE wt.workshop_id=%d AND wt.wahl_id=%d
|
|
ORDER BY t.vorname",
|
|
$wid, $w->wahl_id
|
|
));
|
|
|
|
// Wenn keine wahl-spezifischen Teamer, nutze die Festen Teamer
|
|
if (empty($wahl_teamer) && !empty($feste_teamer)) {
|
|
$teamer = $feste_teamer;
|
|
} else {
|
|
$teamer = $wahl_teamer;
|
|
}
|
|
} else {
|
|
$teamer = [];
|
|
}
|
|
|
|
$teamer_names = array_map(function($t) { return $t->vorname.' '.$t->nachname; }, $teamer);
|
|
$teamer_str = !empty($teamer_names) ? implode(', ', array_map('esc_html', $teamer_names)) : '<em style="color:#999;">Keine</em>';
|
|
|
|
echo '<tr>
|
|
<td>'.esc_html($w->name).'</td>
|
|
<td>'.$teamer_str.'</td>
|
|
<td><a class="kc-btn edit" href="?page=kc_workshops&edit_workshop='.$wid.'&select_wahl='.$w->wahl_id.'">Bearbeiten</a></td>
|
|
</tr>';
|
|
}
|
|
}
|
|
|
|
echo '</tbody></table>';
|
|
|
|
// Wenn Feste Teamer bearbeitet werden sollen
|
|
if (isset($_GET['edit_feste'])) {
|
|
echo '<h3 style="margin-top:20px;">Feste Teamer (gelten für alle Wahlen)</h3>
|
|
<form method="post" style="max-width:600px;">
|
|
<input type="hidden" name="wid" value="'.intval($ws->id).'">
|
|
<input type="hidden" name="edit_feste" value="1">
|
|
<label style="display:block;margin:8px 0 4px 0;font-weight:700;">Teamer auswählen</label>
|
|
<input type="text" id="teamer-search" placeholder="Suchen..." style="width:100%;padding:8px;margin-bottom:8px;border:1px solid #ddd;border-radius:4px;">
|
|
<div id="teamer-list" style="border:1px solid #ddd;border-radius:6px;padding:12px;margin-bottom:10px;max-height:300px;overflow-y:auto;background:#fafafa;">';
|
|
|
|
// Feste Teamer auslesen
|
|
if ($has_wahl && $has_phase) {
|
|
$assigned = $wpdb->get_col($wpdb->prepare(
|
|
"SELECT teamer_id FROM {$prefix}kc_workshop_teamer WHERE workshop_id=%d AND wahl_id IS NULL AND phase IS NULL",
|
|
$wid
|
|
));
|
|
} else {
|
|
$assigned = [];
|
|
}
|
|
|
|
foreach($teamer_liste as $t) {
|
|
$checked = in_array($t->id, (array)$assigned) ? ' checked' : '';
|
|
echo '<label class="teamer-item" style="display:block;margin-bottom:6px;cursor:pointer;" data-name="'.esc_attr(strtolower($t->vorname.' '.$t->nachname)).'"><input type="checkbox" name="teamer_ids[]" value="'.intval($t->id).'"'.$checked.' style="margin-right:8px;"> '.esc_html($t->vorname.' '.$t->nachname).'</label>';
|
|
}
|
|
|
|
echo '</div>
|
|
<script>
|
|
document.getElementById("teamer-search").addEventListener("input", function(e) {
|
|
var search = e.target.value.toLowerCase().trim();
|
|
var items = document.querySelectorAll(".teamer-item");
|
|
|
|
if (search === "") {
|
|
items.forEach(function(item) { item.style.display = "block"; });
|
|
return;
|
|
}
|
|
|
|
var searchTerms = search.split(",").map(function(term) { return term.trim(); }).filter(function(term) { return term.length > 0; });
|
|
|
|
items.forEach(function(item) {
|
|
var name = item.getAttribute("data-name");
|
|
var matches = false;
|
|
|
|
for (var i = 0; i < searchTerms.length; i++) {
|
|
if (name.indexOf(searchTerms[i]) > -1) {
|
|
matches = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
item.style.display = matches ? "block" : "none";
|
|
});
|
|
});
|
|
</script>
|
|
<button name="kc_workshop_save" class="kc-btn">Speichern</button>
|
|
<a href="?page=kc_workshops&edit_workshop='.$wid.'" class="kc-btn del" style="margin-left:24px;">Abbrechen</a>
|
|
</form>';
|
|
}
|
|
|
|
// Wenn eine Wahl ausgewählt wurde, zeige Phase-Auswahl oder direkt Teamer-Bearbeitung
|
|
elseif (isset($_GET['select_wahl']) && !isset($_GET['edit_wahl'])) {
|
|
$select_wahl_id = intval($_GET['select_wahl']);
|
|
$wahl_info = $wpdb->get_row($wpdb->prepare("SELECT name, anzahl_einheiten FROM {$prefix}kc_wahlen WHERE id=%d", $select_wahl_id));
|
|
if (!$wahl_info) {
|
|
echo '<div style="background:#fee;border:1px solid #e57373;border-radius:8px;padding:12px;margin-top:12px;max-width:600px;">Wahl nicht gefunden.</div>';
|
|
echo '<a href="?page=kc_workshops&edit_workshop='.$wid.'" class="kc-btn del" style="margin-left:12px;margin-top:12px;display:inline-block;">Abbrechen</a>';
|
|
return;
|
|
}
|
|
|
|
if (intval($wahl_info->anzahl_einheiten) > 1) {
|
|
// Mehrere Phasen: Frage stellen unter Berücksichtigung der tatsächlichen Zuordnung
|
|
// Ermittle, welchen Phasen dieser Workshop in dieser Wahl zugeordnet ist
|
|
$assigned_phases = $wpdb->get_col($wpdb->prepare(
|
|
"SELECT DISTINCT phase FROM {$prefix}kc_wahl_workshops WHERE workshop_id=%d AND wahl_id=%d AND phase IS NOT NULL",
|
|
$wid, $select_wahl_id
|
|
));
|
|
$assigned_phases = array_map('intval', (array)$assigned_phases);
|
|
sort($assigned_phases);
|
|
|
|
echo '<div style="background:#f9f9f9;border:1px solid #ddd;border-radius:8px;padding:20px;margin-top:20px;max-width:600px;">';
|
|
echo '<h3 style="margin-top:0;">Teamer für: '.esc_html($wahl_info->name).'</h3>';
|
|
echo '<p style="margin-bottom:16px;">Sollen die Teamer für eine <strong>bestimmte Phase</strong> gelten?';
|
|
echo ' <span style="color:#666;">(Dieser Workshop ist zugeordnet: '.(!empty($assigned_phases) ? 'Phase '.esc_html(implode(', ', $assigned_phases)) : 'keiner Phase').')</span></p>';
|
|
echo '<div style="display:flex;gap:12px;flex-wrap:wrap;">';
|
|
|
|
// "Für alle Phasen" nur anbieten, wenn wirklich allen Phasen dieser Wahl zugeordnet
|
|
if (!empty($assigned_phases) && count($assigned_phases) === intval($wahl_info->anzahl_einheiten)) {
|
|
echo '<a href="?page=kc_workshops&edit_workshop='.$wid.'&edit_wahl='.$select_wahl_id.'" class="kc-btn" style="background:#4CAF50;">Für alle Phasen</a>';
|
|
}
|
|
|
|
// Buttons nur für tatsächlich zugeordnete Phasen
|
|
foreach ($assigned_phases as $p) {
|
|
echo '<a href="?page=kc_workshops&edit_workshop='.$wid.'&edit_wahl='.$select_wahl_id.'&edit_phase='.$p.'" class="kc-btn edit">Phase '.$p.'</a>';
|
|
}
|
|
|
|
if (empty($assigned_phases)) {
|
|
echo '<span style="color:#a33;">Dieser Workshop ist in dieser Wahl aktuell keiner Phase zugeordnet.</span>';
|
|
}
|
|
|
|
echo '</div>';
|
|
echo '<a href="?page=kc_workshops&edit_workshop='.$wid.'" class="kc-btn del" style="margin-left:12px;margin-top:12px;display:inline-block;">Abbrechen</a>';
|
|
echo '</div>';
|
|
} else {
|
|
// Nur eine Phase: direkt weiterleiten
|
|
echo "<meta http-equiv='refresh' content='0;url=" . esc_url(admin_url('admin.php?page=kc_workshops&edit_workshop=' . $wid . '&edit_wahl=' . $select_wahl_id . '&edit_phase=1')) . "'>";
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Wenn eine Wahl ausgewählt wurde (mit oder ohne Phase), zeige Bearbeitung
|
|
elseif (isset($_GET['edit_wahl'])) {
|
|
$edit_wahl_id = intval($_GET['edit_wahl']);
|
|
$edit_phase = isset($_GET['edit_phase']) ? intval($_GET['edit_phase']) : null;
|
|
$edit_wahl = $wpdb->get_row($wpdb->prepare("SELECT name FROM {$prefix}kc_wahlen WHERE id=%d", $edit_wahl_id));
|
|
if (!$edit_wahl) {
|
|
echo '<div style="background:#fee;border:1px solid #e57373;border-radius:8px;padding:12px;margin-top:12px;max-width:600px;">Wahl nicht gefunden.</div>';
|
|
echo '<a href="?page=kc_workshops&edit_workshop='.$wid.'" class="kc-btn del" style="margin-left:12px;margin-top:12px;display:inline-block;">Abbrechen</a>';
|
|
return;
|
|
}
|
|
|
|
if ($edit_phase !== null) {
|
|
$title = 'Teamer für '.esc_html($edit_wahl->name).' Phase '.$edit_phase;
|
|
} else {
|
|
$title = 'Teamer für '.esc_html($edit_wahl->name).' (Alle Phasen)';
|
|
}
|
|
|
|
echo '<h3 style="margin-top:20px;">'.$title.'</h3>
|
|
<form method="post" style="max-width:600px;">
|
|
<input type="hidden" name="wid" value="'.intval($ws->id).'">
|
|
<input type="hidden" name="select_wahl_id" value="'.intval($edit_wahl_id).'">';
|
|
|
|
if ($edit_phase !== null) {
|
|
echo '<input type="hidden" name="select_phase" value="'.intval($edit_phase).'">';
|
|
}
|
|
|
|
echo '<label style="display:block;margin:8px 0 4px 0;font-weight:700;">Teamer auswählen</label>
|
|
<input type="text" id="teamer-search" placeholder="Suchen..." style="width:100%;padding:8px;margin-bottom:8px;border:1px solid #ddd;border-radius:4px;">
|
|
<div id="teamer-list" style="border:1px solid #ddd;border-radius:6px;padding:12px;margin-bottom:10px;max-height:300px;overflow-y:auto;background:#fafafa;">';
|
|
|
|
// Stelle sicher, dass Spalten existieren
|
|
$columns = $wpdb->get_col("SHOW COLUMNS FROM {$prefix}kc_workshop_teamer");
|
|
$has_wahl = in_array('wahl_id', $columns);
|
|
$has_phase = in_array('phase', $columns);
|
|
|
|
// Teamer für diese Wahl/Phase auslesen
|
|
if ($has_wahl && $has_phase) {
|
|
if ($edit_phase !== null) {
|
|
$assigned = $wpdb->get_col($wpdb->prepare(
|
|
"SELECT teamer_id FROM {$prefix}kc_workshop_teamer WHERE workshop_id=%d AND wahl_id=%d AND phase=%d",
|
|
$wid, $edit_wahl_id, $edit_phase
|
|
));
|
|
} else {
|
|
$assigned = $wpdb->get_col($wpdb->prepare(
|
|
"SELECT teamer_id FROM {$prefix}kc_workshop_teamer WHERE workshop_id=%d AND wahl_id=%d AND phase IS NULL",
|
|
$wid, $edit_wahl_id
|
|
));
|
|
}
|
|
} else {
|
|
$assigned = [];
|
|
}
|
|
|
|
foreach($teamer_liste as $t) {
|
|
$checked = in_array($t->id, (array)$assigned) ? ' checked' : '';
|
|
echo '<label class="teamer-item" style="display:block;margin-bottom:6px;cursor:pointer;" data-name="'.esc_attr(strtolower($t->vorname.' '.$t->nachname)).'"><input type="checkbox" name="teamer_ids[]" value="'.intval($t->id).'"'.$checked.' style="margin-right:8px;"> '.esc_html($t->vorname.' '.$t->nachname).'</label>';
|
|
}
|
|
|
|
echo '</div>
|
|
<script>
|
|
document.getElementById("teamer-search").addEventListener("input", function(e) {
|
|
var search = e.target.value.toLowerCase().trim();
|
|
var items = document.querySelectorAll(".teamer-item");
|
|
|
|
if (search === "") {
|
|
items.forEach(function(item) { item.style.display = "block"; });
|
|
return;
|
|
}
|
|
|
|
// Teile Suchbegriffe an Kommas
|
|
var searchTerms = search.split(",").map(function(term) { return term.trim(); }).filter(function(term) { return term.length > 0; });
|
|
|
|
items.forEach(function(item) {
|
|
var name = item.getAttribute("data-name");
|
|
var matches = false;
|
|
|
|
// Prüfe ob einer der Suchbegriffe im Namen vorkommt
|
|
for (var i = 0; i < searchTerms.length; i++) {
|
|
if (name.indexOf(searchTerms[i]) > -1) {
|
|
matches = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
item.style.display = matches ? "block" : "none";
|
|
});
|
|
});
|
|
</script>
|
|
<button name="kc_workshop_save" class="kc-btn">Speichern</button>
|
|
<a href="?page=kc_workshops&edit_workshop='.$wid.'" class="kc-btn del" style="margin-left:24px;">Abbrechen</a>
|
|
</form>';
|
|
} else {
|
|
// Workshop-Details bearbeiten
|
|
echo '<h3 style="margin-top:20px;">Workshop-Details</h3>
|
|
<form method="post" style="max-width:600px;">
|
|
<input type="hidden" name="wid" value="'.intval($ws->id).'">
|
|
<input type="text" name="name" placeholder="Name" value="'.esc_attr($ws->name).'" required style="margin-bottom:8px;width:100%;padding:7px;">
|
|
<input type="text" name="beschreibung" placeholder="Beschreibung (Bitte KC eintragen)" value="'.esc_attr($ws->beschreibung).'" style="margin-bottom:8px;width:100%;padding:7px;">
|
|
<div style="display:flex;gap:8px;margin-bottom:8px;">
|
|
<input type="number" name="min_teilnehmer" placeholder="Min. Teilnehmer" value="'.(isset($ws->min_teilnehmer)?intval($ws->min_teilnehmer):0).'" style="width:120px;padding:7px;">
|
|
<input type="number" name="max_teilnehmer" placeholder="Max. Teilnehmer" value="'.intval($ws->max_teilnehmer).'" required style="width:120px;padding:7px;">
|
|
</div>
|
|
<button name="kc_workshop_save" class="kc-btn">Speichern</button>
|
|
<a href="?page=kc_workshops" class="kc-btn del" style="margin-left:24px;">Abbrechen</a>
|
|
</form>';
|
|
}
|
|
|
|
echo '</div>';
|
|
return;
|
|
}
|
|
|
|
// Neuer Workshop anlegen
|
|
if (isset($_GET['new'])) {
|
|
echo '<div class="kc-admin-table-wrap">';
|
|
echo '<h2>Neuen Workshop anlegen</h2>';
|
|
|
|
echo '<form method="post" id="kc-new-workshop-form" style="max-width:600px;">';
|
|
echo '<input type="text" name="name" placeholder="Name" required style="margin-bottom:8px;width:100%;padding:7px;" id="ws-name">';
|
|
echo '<input type="text" name="beschreibung" placeholder="Beschreibung (Bitte KC eintragen)" style="margin-bottom:8px;width:100%;padding:7px;" id="ws-desc">';
|
|
|
|
echo '<div style="display:flex;gap:8px;margin-bottom:8px;">';
|
|
echo '<input type="number" name="min_teilnehmer" placeholder="Min. Teilnehmer" value="0" style="width:120px;padding:7px;" id="ws-min">';
|
|
echo '<input type="number" name="max_teilnehmer" placeholder="Max. Teilnehmer" required style="width:120px;padding:7px;" id="ws-max">';
|
|
echo '</div>';
|
|
|
|
echo '<button name="kc_workshop_save" class="kc-btn">Speichern</button>';
|
|
echo '<a href="?page=kc_workshops" class="kc-btn del" style="margin-left:24px;">Abbrechen</a>';
|
|
echo '</form>';
|
|
|
|
echo '</div>';
|
|
return;
|
|
}
|
|
|
|
// Übersicht
|
|
echo '<div class="kc-admin-table-wrap">';
|
|
echo '<h2 style="margin-top:0;">Alle Workshops</h2>';
|
|
echo '<a class="kc-btn" style="float:right;margin-bottom:12px;" href="?page=kc_workshops&new=1">+ Neuer Workshop</a>';
|
|
echo '<div style="margin:12px 0;clear:both;display:flex;gap:8px;align-items:center;flex-wrap:wrap;">'
|
|
.'<label for="kc-desc-filter" style="font-weight:600;">Beschreibung filtern:</label>'
|
|
.'<input type="text" id="kc-desc-filter" placeholder="Text in Beschreibung suchen..." style="padding:6px 8px;min-width:220px;border:1px solid #ccc;border-radius:4px;">'
|
|
.'<span id="kc-desc-filter-count" style="color:#666;font-size:90%;">Alle anzeigen</span>'
|
|
.'<div style="display:flex;gap:6px;flex-wrap:wrap;">'
|
|
.'<button type="button" class="kc-btn kc-desc-quick-btn" style="padding:6px 10px;" data-desc-preset="">Alle</button>'
|
|
.'<button type="button" class="kc-btn kc-desc-quick-btn" style="padding:6px 10px;" data-desc-preset="(zu ändern)">(zu ändern)</button>'
|
|
.'<button type="button" class="kc-btn kc-desc-quick-btn" style="padding:6px 10px;" data-desc-preset="KC1">KC1</button>'
|
|
.'<button type="button" class="kc-btn kc-desc-quick-btn" style="padding:6px 10px;" data-desc-preset="KC2">KC2</button>'
|
|
.'<button type="button" class="kc-btn kc-desc-quick-btn" style="padding:6px 10px;" data-desc-preset="KC3">KC3</button>'
|
|
.'<button type="button" class="kc-btn kc-desc-quick-btn" style="padding:6px 10px;" data-desc-preset="NOTEAMER">Ohne Teamer</button>'
|
|
.'</div>'
|
|
.'</div>';
|
|
echo '<div style="margin:12px 0;display:flex;gap:8px;align-items:center;flex-wrap:wrap;">'
|
|
.'<label for="kc-wahl-filter" style="font-weight:600;">Wahlen filtern:</label>'
|
|
.'<input type="text" id="kc-wahl-filter" placeholder="Wahl suchen..." style="padding:6px 8px;min-width:220px;border:1px solid #ccc;border-radius:4px;">'
|
|
.'<span id="kc-wahl-filter-count" style="color:#666;font-size:90%;">Alle anzeigen</span>'
|
|
.'</div>';
|
|
|
|
// Sortierung
|
|
$sort = isset($_GET['sort']) ? sanitize_text_field($_GET['sort']) : 'id';
|
|
$order = isset($_GET['order']) ? (($_GET['order'] === 'desc') ? 'DESC' : 'ASC') : 'DESC';
|
|
$allowed_sort = ['id', 'name', 'beschreibung'];
|
|
if (!in_array($sort, $allowed_sort)) {
|
|
$sort = 'id';
|
|
}
|
|
|
|
// Sortier-Links
|
|
$name_order = ($sort === 'name' && $order === 'ASC') ? 'desc' : 'asc';
|
|
$beschreibung_order = ($sort === 'beschreibung' && $order === 'ASC') ? 'desc' : 'asc';
|
|
$name_arrow = ($sort === 'name') ? ($order === 'ASC' ? ' ▲' : ' ▼') : '';
|
|
$beschreibung_arrow = ($sort === 'beschreibung') ? ($order === 'ASC' ? ' ▲' : ' ▼') : '';
|
|
|
|
echo '<table class="kc-admin-table">';
|
|
echo '<thead><tr>';
|
|
echo '<th><a href="?page=kc_workshops&sort=name&order='.$name_order.'" style="text-decoration:none;color:inherit;">Name'.$name_arrow.'</a></th>';
|
|
echo '<th><a href="?page=kc_workshops&sort=beschreibung&order='.$beschreibung_order.'" style="text-decoration:none;color:inherit;">Beschreibung'.$beschreibung_arrow.'</a></th>';
|
|
echo '<th>Teilnehmer (Min - Max)</th>';
|
|
echo '<th>Teamer</th>';
|
|
echo '<th>Wahlen</th>';
|
|
echo '<th>Aktion</th>';
|
|
echo '</tr></thead><tbody>';
|
|
|
|
$workshops = $wpdb->get_results("SELECT * FROM {$prefix}kc_workshops ORDER BY {$sort} {$order}");
|
|
foreach ($workshops as $ws) {
|
|
// Teamer anzeigen
|
|
$trows = $wpdb->get_results($wpdb->prepare("SELECT t.vorname, t.nachname FROM {$prefix}kc_workshop_teamer wt JOIN {$prefix}kc_teamer t ON wt.teamer_id=t.id WHERE wt.workshop_id=%d", $ws->id));
|
|
$tnames = array_map(function($r){ return $r->vorname.' '.$r->nachname; }, $trows);
|
|
$tnames_str = implode(', ', array_map('esc_html', $tnames));
|
|
if (empty($tnames_str)) $tnames_str = '<em style="color:#999;">Keine</em>';
|
|
|
|
// Wahlen und Phasen anzeigen
|
|
$wahl_rows = $wpdb->get_results($wpdb->prepare(
|
|
"SELECT w.name, ww.phase
|
|
FROM {$prefix}kc_wahl_workshops ww
|
|
JOIN {$prefix}kc_wahlen w ON ww.wahl_id=w.id
|
|
WHERE ww.workshop_id=%d
|
|
ORDER BY w.id, ww.phase",
|
|
$ws->id
|
|
));
|
|
$wahl_info = [];
|
|
foreach($wahl_rows as $wr) {
|
|
$wahl_info[] = esc_html($wr->name) . ($wr->phase ? ' (P'.intval($wr->phase).')' : '');
|
|
}
|
|
$wahl_str = !empty($wahl_info) ? implode(', ', $wahl_info) : '<em style="color:#999;">Keine</em>';
|
|
|
|
echo "<tr>
|
|
<td>".esc_html($ws->name)."</td>
|
|
<td>".esc_html($ws->beschreibung)."</td>
|
|
<td>".(isset($ws->min_teilnehmer)?intval($ws->min_teilnehmer):0)." - ".intval($ws->max_teilnehmer)."</td>
|
|
<td>". $tnames_str ."</td>
|
|
<td>". $wahl_str ."</td>
|
|
<td class='kc-actions'>
|
|
<a class='kc-btn edit' href='?page=kc_workshops&edit_workshop={$ws->id}'>Bearbeiten</a>
|
|
<a class='kc-btn del' href='?page=kc_workshops&delete_workshop={$ws->id}' onclick=\"return confirm('Wirklich loeschen?');\">Loeschen</a>
|
|
</td>
|
|
</tr>";
|
|
}
|
|
echo '</tbody></table>';
|
|
echo '<script>
|
|
(function() {
|
|
var descInput = document.getElementById("kc-desc-filter");
|
|
var wahlInput = document.getElementById("kc-wahl-filter");
|
|
if (!descInput || !wahlInput) return;
|
|
|
|
var rows = Array.prototype.slice.call(document.querySelectorAll(".kc-admin-table tbody tr"));
|
|
var descStatus = document.getElementById("kc-desc-filter-count");
|
|
var wahlStatus = document.getElementById("kc-wahl-filter-count");
|
|
var total = rows.length;
|
|
var quickBtns = Array.prototype.slice.call(document.querySelectorAll(".kc-desc-quick-btn"));
|
|
|
|
// Lade gespeicherte Filter-Werte aus localStorage
|
|
var savedDescValue = localStorage.getItem("kc_workshops_desc_filter") || "";
|
|
var savedWahlValue = localStorage.getItem("kc_workshops_wahl_filter") || "";
|
|
var savedQuickFilters = (localStorage.getItem("kc_workshops_quick_filters") || "").split(",").filter(function(v) { return v.length > 0; });
|
|
|
|
descInput.value = savedDescValue;
|
|
wahlInput.value = savedWahlValue;
|
|
|
|
// Restore active buttons
|
|
quickBtns.forEach(function(btn) {
|
|
if (savedQuickFilters.indexOf(btn.getAttribute("data-desc-preset")) !== -1) {
|
|
btn.style.fontWeight = "bold";
|
|
btn.style.border = "2px solid #333";
|
|
}
|
|
});
|
|
|
|
function applyFilters() {
|
|
var descQuery = descInput.value.toLowerCase().trim();
|
|
var wahlQuery = wahlInput.value.toLowerCase().trim();
|
|
|
|
// Sammle aktive Quick-Filter (ausser "zu ändern" und "Ohne Teamer")
|
|
var activeQuickFilters = [];
|
|
var zuAendernActive = false;
|
|
var noTeamerActive = false;
|
|
quickBtns.forEach(function(btn) {
|
|
var preset = btn.getAttribute("data-desc-preset");
|
|
if (btn.style.fontWeight === "bold") {
|
|
if (preset === "NOTEAMER") {
|
|
noTeamerActive = true;
|
|
} else if (preset === "(zu ändern)") {
|
|
zuAendernActive = true;
|
|
} else if (preset !== "") {
|
|
activeQuickFilters.push(preset.toLowerCase());
|
|
}
|
|
}
|
|
});
|
|
|
|
var shown = 0;
|
|
|
|
rows.forEach(function(row) {
|
|
var descCell = row.querySelector("td:nth-child(2)");
|
|
var teamerCell = row.querySelector("td:nth-child(4)");
|
|
var wahlCell = row.querySelector("td:nth-child(5)");
|
|
if (!descCell || !teamerCell || !wahlCell) return;
|
|
|
|
var descText = descCell.textContent.toLowerCase();
|
|
var wahlText = wahlCell.textContent.toLowerCase();
|
|
var teamerText = teamerCell.textContent.toLowerCase();
|
|
|
|
// Text-Filter
|
|
var descMatch = descQuery === "" || descText.indexOf(descQuery) > -1;
|
|
var wahlMatch = wahlQuery === "" || wahlText.indexOf(wahlQuery) > -1;
|
|
|
|
// Quick-Filter (KC1/KC2/KC3 mit OR, "zu ändern" und "Ohne Teamer" mit UND)
|
|
var quickMatch = true;
|
|
if (activeQuickFilters.length > 0 || zuAendernActive || noTeamerActive) {
|
|
quickMatch = false;
|
|
|
|
// Prüfe normale Quick-Filter (OR - eine muss passen)
|
|
if (activeQuickFilters.length > 0) {
|
|
for (var i = 0; i < activeQuickFilters.length; i++) {
|
|
if (descText.indexOf(activeQuickFilters[i]) > -1) {
|
|
quickMatch = true;
|
|
break;
|
|
}
|
|
}
|
|
} else if (zuAendernActive || noTeamerActive) {
|
|
// Wenn nur "zu ändern" oder "Ohne Teamer" aktiv sind
|
|
quickMatch = true;
|
|
}
|
|
|
|
// Prüfe "zu ändern" Filter (UND)
|
|
if (quickMatch && zuAendernActive && descText.indexOf("(zu ändern)") === -1) {
|
|
quickMatch = false;
|
|
}
|
|
|
|
// Prüfe "Ohne Teamer" Filter (UND)
|
|
if (quickMatch && noTeamerActive && teamerText.indexOf("keine") === -1) {
|
|
quickMatch = false;
|
|
}
|
|
}
|
|
|
|
var show = descMatch && wahlMatch && quickMatch;
|
|
row.style.display = show ? "" : "none";
|
|
if (show) shown++;
|
|
});
|
|
|
|
if (descStatus) {
|
|
descStatus.textContent = descQuery === "" && activeQuickFilters.length === 0 && !zuAendernActive && !noTeamerActive ? "Alle anzeigen (" + total + ")" : shown + " von " + total + " Treffer";
|
|
}
|
|
if (wahlStatus) {
|
|
wahlStatus.textContent = wahlQuery === "" ? "Alle anzeigen (" + total + ")" : shown + " von " + total + " Treffer";
|
|
}
|
|
|
|
// Speichere Filter-Werte in localStorage
|
|
localStorage.setItem("kc_workshops_desc_filter", descInput.value);
|
|
localStorage.setItem("kc_workshops_wahl_filter", wahlInput.value);
|
|
var activeFilters = [];
|
|
quickBtns.forEach(function(btn) {
|
|
if (btn.style.fontWeight === "bold") {
|
|
activeFilters.push(btn.getAttribute("data-desc-preset"));
|
|
}
|
|
});
|
|
localStorage.setItem("kc_workshops_quick_filters", activeFilters.join(","));
|
|
}
|
|
|
|
descInput.addEventListener("input", applyFilters);
|
|
wahlInput.addEventListener("input", applyFilters);
|
|
|
|
quickBtns.forEach(function(btn) {
|
|
btn.addEventListener("click", function(e) {
|
|
e.preventDefault();
|
|
var preset = btn.getAttribute("data-desc-preset");
|
|
|
|
// "Alle" Button setzt alles zurück
|
|
if (preset === "") {
|
|
quickBtns.forEach(function(b) {
|
|
b.style.fontWeight = "";
|
|
b.style.border = "";
|
|
});
|
|
descInput.value = "";
|
|
wahlInput.value = "";
|
|
localStorage.removeItem("kc_workshops_desc_filter");
|
|
localStorage.removeItem("kc_workshops_wahl_filter");
|
|
localStorage.removeItem("kc_workshops_quick_filters");
|
|
applyFilters();
|
|
descInput.focus();
|
|
} else if (preset === "KC1" || preset === "KC2" || preset === "KC3") {
|
|
// KC-Buttons sind exklusiv: deaktiviere andere KC-Buttons
|
|
quickBtns.forEach(function(b) {
|
|
var bpreset = b.getAttribute("data-desc-preset");
|
|
if (bpreset === "KC1" || bpreset === "KC2" || bpreset === "KC3") {
|
|
b.style.fontWeight = "";
|
|
b.style.border = "";
|
|
}
|
|
});
|
|
// Aktiviere den angeklickten Button
|
|
btn.style.fontWeight = "bold";
|
|
btn.style.border = "2px solid #333";
|
|
applyFilters();
|
|
} else {
|
|
// Toggle für "(zu ändern)" und "Ohne Teamer"
|
|
if (btn.style.fontWeight === "bold") {
|
|
btn.style.fontWeight = "";
|
|
btn.style.border = "";
|
|
} else {
|
|
btn.style.fontWeight = "bold";
|
|
btn.style.border = "2px solid #333";
|
|
}
|
|
applyFilters();
|
|
}
|
|
});
|
|
});
|
|
|
|
applyFilters();
|
|
})();
|
|
</script>';
|
|
echo '</div>';
|
|
}
|
|
?>
|