Lachenzelg/admin/formscontents-edit.php
2025-01-07 03:02:25 +01:00

205 lines
7.8 KiB
PHP

<?php include "includes/header.php"; ?>
<div>
<div class="card card-body p-4">
<?= alertMessage() ?>
<?php
$paramResult = checkParamId('id');
if(!is_numeric($paramResult)){
echo '<div class="alert alert-danger" role="alert">'.$paramResult.'</div>';
return false;
}
$formcontent = getById('tbl_formcontents',checkParamId('id'));
if($formcontent['status'] == 200)
{
$form = getById('tbl_forms',$formcontent["data"]["form_id"]);
?>
<div class="row">
<div class="col-6">
<h3 class="fw-semibold mb-4"><?= $formcontent['data']['name']; ?></h3>
</div>
<div class="col-6">
<a href="forms-edit.php?id=<?= $formcontent["data"]["form_id"] ?>" class="btn btn-light float-end"> <i class="ti ti-arrow-left"></i> Zurück zum Formular</a>
</div>
</div>
<form action="code.php" method="POST">
<input type="hidden" name="formcontent_id" value="<?= $formcontent["data"]['id'] ;?>" required>
<div class="row">
<div class="col-6">
<div class="mb-3">
<label class="form-label">Name</label>
<input type="text" name="name" value="<?= $formcontent['data']['name'] ;?>" required class="form-control">
</div>
</div>
<div class="col-auto">
<div class="mb-3">
<label class="form-label">Min. Auswahl</label>
<input type="number" name="min" value="<?= $formcontent['data']['min'] ;?>" class="form-control">
</div>
</div>
<div class="col-auto">
<div class="mb-3">
<label class="form-label">Max. Auswahl</label>
<input type="number" name="max" value="<?= $formcontent['data']['max'] ;?>" class="form-control">
</div>
</div>
</div>
<?php
$classesform = getClassesPerForm($form["data"]["id"]);
echo "<h5 class='fw-semibold mb-4 mt-3'>Klassenzuweisung</h5>";
echo "<div class='row'>";
$assignedClasses = getAssignedItems('tbl_assign_formcontents_classes', $formcontent['data']['id'], 'class_id', 'formcontent_id');
if (!empty($classesform)) {
$groupedClasses = array();
foreach ($classesform as $class) {
$level = $class['sek'];
$groupedClasses[$level][] = $class;
}
foreach ($groupedClasses as $level => $levelClasses) {
echo "<div class='col'>";
echo "<div class='card' style='border-radius:10px;'>";
echo "<div class='card-header fw-semibold bg-info-subtle'>";
echo "$level";
echo "</div>";
// Checkbox für "Alle Auswählen"
$idSafeLevel = str_replace(['.', ' '], '_', $level);
$idSafeLevel = preg_replace('/[^A-Za-z0-9_]/', '', $idSafeLevel);
echo "<div class='form-check ms-3 mt-2 mb-3'>";
echo "<input class='form-check-input' type='checkbox' id='select-all_$idSafeLevel' data-toggle='select-all' data-level='$level'>";
echo "<label class='form-check-label' for='select-all_$idSafeLevel' style='user-select: none; -webkit-user-select: none; -ms-user-select: none;' for=''>Alle auswählen</label>";
echo "</div>";
foreach ($levelClasses as $class) {
$classId = $class['id'];
$className = htmlspecialchars($class['classDisplayName']);
$isChecked = in_array($classId, $assignedClasses) ? 'checked' : '';
echo "<div class='form-check ms-3 mt-2 mb-3'>";
echo "<input class='form-check-input' type='checkbox' name='selected_classes[]' id='class_$classId' data-class-id='$level' value='$classId' $isChecked>";
echo "<label class='form-check-label' style='user-select: none; -webkit-user-select: none; -ms-user-select: none;' for='class_$classId'>$className</label>";
echo "</div>";
}
echo "</div>";
echo "</div>";
}
echo "</div>";
} else {
echo "<h6>Diesem Formular sind keine Klassen zugewiesen.</h6>";
}
?>
<div class="col-12">
<button type="submit" name="updateformcontent" class="btn btn-info mx-3 float-end"><i class="ti ti-check me-1"></i>Änderungen speichern</button>
</div>
</div>
</div>
</form>
<div class="card card-body p-4">
<div class="row">
<div class="col-6">
<h4 class="fw-semibold mb-4">Kurse</h4>
</div>
<div class="col-6">
<a href="formscourses-create.php?id=<?= $formcontent["data"]["id"] ?>" class="btn btn-info float-end"> <i class="ti ti-plus me-1"></i> Erstellen</a>
</div>
<div class="table-responsive">
<table class="table table-hover">
<tbody>
<?php
$formcontent_id = $formcontent['data']['id'];
$formcontents = getAll('id,name', 'tbl_formcourses', "WHERE formcontent_id='$formcontent_id'");
if(mysqli_num_rows($formcontents) > 0)
{
foreach($formcontents as $formcontentEntry)
{
?>
<tr>
<td><?= $formcontentEntry['name']; ?></td>
<td>
<a href="formscourses-edit.php?id=<?= $formcontentEntry['id']; ?>" class="btn btn-light btn-sm float-end mx-1"> <i class="ti ti-arrow-right"></i></a>
</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="7"> Keine Kurse vorhanden</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php
} else {
echo '<div class="alert alert-danger" role="alert">' .
$formcontent["message"] .
"</div>";
}
?>
<script>
$(document).ready(function() {
// Überwache Änderungen an den Klassen-Checkboxen
$('input[name="selected_classes[]"]').change(function() {
updateSelectAllCheckbox($(this).data('class-id'));
});
// Überwache Änderungen an den "Alle auswählen"-Checkboxen
$('input[type="checkbox"][data-toggle="select-all"]').change(function() {
var level = $(this).data('level');
var isChecked = $(this).prop('checked');
$('input[name="selected_classes[]"][data-class-id="' + level + '"]').prop('checked', isChecked);
});
// Initialisiere den Status der "Alle auswählen"-Checkboxen beim Laden der Seite
$('input[data-toggle="select-all"]').each(function() {
updateSelectAllCheckbox($(this).data('level'));
});
});
// Funktion zum Aktualisieren des Status der "Alle auswählen"-Checkboxen
function updateSelectAllCheckbox(level) {
var classCheckboxes = $('input[name="selected_classes[]"][data-class-id="' + level + '"]');
var selectAllCheckbox = $('input[data-toggle="select-all"][data-level="' + level + '"]');
var allChecked = true;
var anyChecked = false;
classCheckboxes.each(function() {
if (!$(this).prop('checked')) {
allChecked = false;
} else {
anyChecked = true;
}
});
selectAllCheckbox.prop('checked', allChecked);
selectAllCheckbox.prop('indeterminate', anyChecked && !allChecked);
}
</script>
<?php include "includes/footer.php"; ?>