Funktionalität Facheditor

This commit is contained in:
Matthias Grief
2024-12-23 16:09:27 +01:00
parent f9e4066352
commit 438fd9cb25
4 changed files with 112 additions and 21 deletions

View File

@@ -127,7 +127,14 @@ class TopicData
{
$result = array();
if(!is_dir(Config::getTopicsDirectory($subjectId))) {
return array();
}
$topicNames = scandir(Config::getTopicsDirectory($subjectId));
if(!$topicNames) {
return array();
}
usort($topicNames, function ($a, $b) {
return strcmp($a, $b);

View File

@@ -1,6 +1,6 @@
{
"displayName": "Mathe",
"description": "Mathe ist blau",
"color": "#3b82f6",
"description": "Mathe ist rot",
"color": "#626cd0",
"icon": "fa-square-root-alt"
}

View File

@@ -0,0 +1,6 @@
{
"displayName": "Physik",
"description": "Noch ein Fach",
"color": "#363cfc",
"icon": "fa-square-root-alt"
}

View File

@@ -6,8 +6,70 @@ require_once("classes/TopicData.php");
$allSubjects = SubjectData::getAll();
$editingSubject = null;
$defaultValues = array();
$defaultValues['displayName'] = "";
$defaultValues['id'] = "";
$defaultValues['description'] = "";
$defaultValues['color'] = "#3b82f6";
$defaultValues['icon'] = "";
$errors = array();
if (isset($_GET['subjectId'])) {
$editingSubject = $allSubjects[$_GET['subjectId']];
$defaultValues['displayName'] = $editingSubject->getDisplayName();
$defaultValues['id'] = $editingSubject->getId();
$defaultValues['description'] = $editingSubject->getDescription();
$defaultValues['color'] = $editingSubject->getColor();
$defaultValues['icon'] = $editingSubject->getIcon();
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
foreach ($defaultValues as $key => $value) {
$defaultValues[$key] = $_POST[$key];
}
if(!isset($_POST['displayName']) || trim($_POST['displayName']) == "") {
$errors["displayName"] = "Bitte geben Sie einen Namen an.";
}
if(!isset($_POST['id']) || trim($_POST['id']) == "") {
$errors["id"] = "Bitte geben Sie eine ID an.";
}
if(!isset($_POST['description']) || trim($_POST['description']) == "") {
$errors["description"] = "Bitte geben Sie eine Beschreibung an.";
}
if(!isset($_POST['color']) || trim($_POST['color']) == "") {
$errors["color"] = "Bitte geben Sie eine Farbe an.";
}
if(!isset($_POST['icon']) || trim($_POST['icon']) == "") {
$errors["icon"] = "Bitte geben Sie ein Icon an.";
}
if(empty($errors)) {
$newSubject = false;
if(isset($allSubjects[$_POST['id']])) {
$newSubject = $allSubjects[$_POST['id']];
$newSubject->setDisplayName($_POST['displayName']);
$newSubject->setDescription($_POST['description']);
$newSubject->setColor($_POST['color']);
$newSubject->setIcon($_POST['icon']);
} else {
$newSubject = SubjectData::createNew($_POST['id'], $_POST['displayName'], $_POST['description'], $_POST['color'], $_POST['icon'], array());
}
if(!$newSubject) {
$errors["error"] = "Fehler beim Speichern des Faches.";
} else {
$newSubject->save();
header("Location: " . "subject.php?subject=" . $newSubject->getId());
}
}
}
?>
@@ -29,7 +91,7 @@ if (isset($_GET['subjectId'])) {
<body class="min-h-screen bg-gray-50">
<form id="subjectForm" onsubmit="handleSubjectSubmit(event)">
<form id="subjectForm" method="post" onsubmit="handleSubjectSubmit(event)">
<div class="space-y-6">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Name des Fachs</label>
@@ -38,11 +100,17 @@ if (isset($_GET['subjectId'])) {
required
class="w-full px-4 py-3 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-lg enabled:hover:border-gray-400"
placeholder="z.B. Mathematik"
value="<?php
if (isset($editingSubject)) {
echo $editingSubject->getDisplayName();
}
?>">
value="<?php echo $defaultValues['displayName']?>">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">ID des Fachs</label>
<input type="text"
name="id"
required
class="w-full px-4 py-3 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-lg enabled:hover:border-gray-400"
placeholder="z.B. mathe"
value="<?php echo $defaultValues['id']?>">
</div>
<div>
@@ -51,26 +119,23 @@ if (isset($_GET['subjectId'])) {
required
class="w-full px-4 py-3 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-lg enabled:hover:border-gray-400"
rows="4"
placeholder="Kurze Beschreibung des Fachs"><?php
if (isset($editingSubject)) {
echo $editingSubject->getDescription();
}
?></textarea>
placeholder="Kurze Beschreibung des Fachs"><?php echo $defaultValues['description']?></textarea>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Farbe</label>
<input type="color" name="color" required
class="w-full h-14 px-1 py-1 rounded-lg"
value="<?php
if (isset($editingSubject)) {
echo $editingSubject->getColor();
} else {
echo "#3b82f6";
}
?>">
value="<?php echo $defaultValues['color']?>">
</div>
<div>
<label for="iconSelect" class="block text-sm font-medium text-gray-700">Icon</label>
<input class="mt-1 block w-full rounded-lg border-gray-300 shadow-sm" type="text" name="icon"
id="iconSelect" value="<?php echo $defaultValues['icon']?>">
</div>
<!--
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Icon auswählen</label>
<div class="grid grid-cols-6 gap-4 p-4 border rounded-lg bg-gray-50">
@@ -95,6 +160,10 @@ if (isset($_GET['subjectId'])) {
'fa-tablets',
];
if(isset($editingSubject)) {
$icons[] = $editingSubject->getIcon();
}
foreach ($icons as $icon) {
echo "<div class='icon-option cursor-pointer p-4 rounded-lg hover:bg-white hover:shadow transition-all text-center'
onclick='selectIcon(this, \"$icon\")' data-icon='$icon'>
@@ -105,8 +174,17 @@ if (isset($_GET['subjectId'])) {
</div>
<input type="hidden" name="icon" id="selectedIcon">
</div>
-->
<?php
foreach ($errors as $error) {
echo "<p class='text-red-800'>$error</p>";
}
?>
</div>
<div class="mt-8 flex justify-end gap-3">
<button type="button" onclick="closeModal()"
class="px-6 py-3 text-gray-600 hover:text-gray-800 text-lg">
@@ -114,7 +192,7 @@ if (isset($_GET['subjectId'])) {
</button>
<button type="submit"
class="px-6 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-lg">
Fach erstellen
Speichern
</button>
</div>
</form>