getTopics()[$_GET['topicId']])) {
$editingTopic = $allSubjects[$_GET['subjectId']]->getTopics()[$_GET['topicId']];
$defaultValues['displayName'] = $editingTopic->getDisplayName();
$defaultValues['id'] = $editingTopic->getId();
$defaultValues['subjectId'] = $editingTopic->getSubjectId();
$defaultValues['description'] = $editingTopic->getDescription();
$defaultValues['icon'] = $editingTopic->getIcon();
$defaultValues['relatedTopics'] = implode(", ", $editingTopic->getRelatedTopics());
$defaultValues['article'] = str_replace("$$", "
$$
", $editingTopic->getFinishedArticle());
}
}
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['subjectId']) || trim($_POST['subjectId']) == "") {
$errors["subjectId"] = "Bitte geben Sie ein Fach an.";
}
if(!isset($_POST['description']) || trim($_POST['description']) == "") {
$errors["description"] = "Bitte geben Sie eine Beschreibung an.";
}
if(!isset($_POST['icon']) || trim($_POST['icon']) == "") {
$errors["icon"] = "Bitte geben Sie ein Icon an.";
}
if(!isset($_POST['article']) || trim($_POST['article']) == "") {
$errors["article"] = "Bitte geben Sie einen Erklärtext an.";
}
if(empty($errors)) {
$newTopic = false;
if(isset($allSubjects[$_POST['subjectId']]->getTopics()[$_POST['id']])) {
$newTopic = $allSubjects[$_POST['subjectId']]->getTopics()[$_POST['id']];
$newTopic->setDisplayName($_POST['displayName']);
$newTopic->setSubjectId($_POST['subjectId']);
$newTopic->setDescription($_POST['description']);
$newTopic->setIcon($_POST['icon']);
$relatedTopics = array();
foreach (explode(",", $_POST['relatedTopics']) as $relatedTopic) {
$relatedTopics[] = trim($relatedTopic);
}
$newTopic->setRelatedTopics($relatedTopics);
$newTopic->setArticle($_POST['article']);
} else {
$newTopic = TopicData::createNew($_POST['id'], $_POST['subjectId'], $_POST['displayName'], $_POST['icon'], $_POST['description'], $_POST['relatedTopics'], $_POST['article']);
}
if(!$newTopic) {
$errors["error"] = "Fehler beim Speichern des Themas.";
} else {
$newTopic->save();
header("Location: " . "topic.php?subject=" . $newTopic->getSubjectId() . "&topic=" . $newTopic->getId());
}
}
}
?>