diff --git a/webseite/classes/SubjectData.php b/webseite/classes/SubjectData.php index 49fa157..d957308 100644 --- a/webseite/classes/SubjectData.php +++ b/webseite/classes/SubjectData.php @@ -41,22 +41,57 @@ class SubjectData public array $topics; /** - * Erstellt ein neues Fach. Es wird noch nichts gespeichert + * Erstellt ein neues Fach * @param string $id Ein eindeutiger Bezeichner für das Fach, darf nur A-Z, a-Z, 0-9 sowie _ und - enthalten * @param string $displayName Der für User angezeigte Name des Faches, nur reiner Text * @param string $description Eine kurze Beschreibung des Faches, z.B. für den Text auf der Startseite, kann HTML enthalten * @param string $color Themenfarbe des Faches als hexcode * @param string $icon Icon des Faches als Font-Awesome CSS-Klasse - * @param array $topics Alle Themen des Faches als TopicData Objekt + * @param array $topics Alle Themen des Faches als TopicData Object + * @return SubjectData|false Neues Fach oder false, wenn ein Fehler auftritt */ - public function __construct(string $id, string $displayName, string $description, string $color, string $icon, array $topics) + public static function createNew(string $id, string $displayName, string $description, string $color, string $icon, array $topics): SubjectData|false { - $this->id = $id; - $this->displayName = $displayName; - $this->description = $description; - $this->color = $color; - $this->icon = $icon; - $this->topics = $topics; + $result = new SubjectData(); + + if(Util::containsIllegalCharacters($id)) { + return false; + } + if(self::exists($id)) { + return false; + } + $result->id = $id; + + $result->displayName = $displayName; + + $result->description = $description; + + $result->color = $color; + + $result->icon = $icon; + + $result->topics = $topics; + + if(!$result->save()) { + return false; + } + + return $result; + } + + /** + * Prüft, ob das Thema zu den angegebenen IDs existiert + * @param string $subjectId ID des Faches + * @param string $topicId ID des Themas + * @return bool true, wenn es existiert, sonst false + */ + public static function exists(string $subjectId): bool + { + if(!is_dir(Config::getSubjectDirectory($subjectId))) { + return false; + } + + return true; } /** @@ -96,9 +131,12 @@ class SubjectData */ public static function fromName(string $subjectId): SubjectData|null { + $result = new SubjectData(); + if (Util::containsIllegalCharacters($subjectId)) { return null; } + $result->id = $subjectId; $filename = Config::getSubjectDirectory($subjectId) . "properties.json"; $data = Util::parseJsonFromFile($filename); @@ -109,20 +147,26 @@ class SubjectData if (!isset($data->displayName)) { return null; } + $result->displayName = $data->displayName; if (!isset($data->description)) { return null; } + $result->description = $data->description; if (!isset($data->color)) { return null; } + $result->color = $data->color; if (!isset($data->icon)) { return null; } + $result->icon = $data->icon; - return new SubjectData($subjectId, $data->displayName, $data->description, $data->color, $data->icon, TopicData::getAll($subjectId)); + $result->topics = TopicData::getAll($subjectId); + + return $result; } /** diff --git a/webseite/classes/TopicData.php b/webseite/classes/TopicData.php index b13d48f..06cad94 100644 --- a/webseite/classes/TopicData.php +++ b/webseite/classes/TopicData.php @@ -1,4 +1,8 @@ id = $id; - $this->subjectId = $subjectId; - $this->displayName = $displayName; - $this->icon = $icon; - $this->description = $description; - $this->relatedTopics = $relatedTopics; - $this->files = $files; - $this->article = $article; + $result = new TopicData(); + + if(Util::containsIllegalCharacters($subjectId)) { + return false; + } + if(!SubjectData::exists($subjectId)) { + return false; + } + $result->subjectId = $subjectId; + + if(Util::containsIllegalCharacters($id)) { + return false; + } + if(self::exists($subjectId, $id)) { + return false; + } + $result->id = $id; + + $result->displayName = $displayName; + + $result->icon = $icon; + + $result->description = $description; + + $result->relatedTopics = $relatedTopics; + + $result->files = $files; + + $result->article = $article; + + if(!$result->save()) { + return false; + } + + return $result; + } + + /** + * Prüft, ob das Thema zu den angegebenen IDs existiert + * @param string $subjectId ID des Faches + * @param string $topicId ID des Themas + * @return bool true, wenn es existiert, sonst false + */ + public static function exists(string $subjectId, string $topicId): bool + { + if(!is_dir(Config::getTopicDirectory($subjectId, $topicId))) { + return false; + } + + return true; } /** @@ -105,12 +152,16 @@ class TopicData */ public static function fromName(string $subjectId, string $topicId): TopicData|null { + $result = new TopicData(); + if (Util::containsIllegalCharacters($subjectId)) { return null; } if (Util::containsIllegalCharacters($topicId)) { return null; } + $result->id = $topicId; + $result->subjectId = $subjectId; $data = Util::parseJsonFromFile(Config::getTopicDirectory($subjectId, $topicId) . "properties.json"); if (!isset($data)) { @@ -120,19 +171,23 @@ class TopicData if (!isset($data->displayName)) { return null; } + $result->displayName = $data->displayName; if (!isset($data->icon)) { return null; } + $result->icon = $data->icon; if (!isset($data->description)) { return null; } + $result->description = $data->description; $relatedTopics = array(); if (isset($data->relatedTopics)) { $relatedTopics = $data->relatedTopics; } + $result->relatedTopics = $relatedTopics; $files = array(); $downloadDirectory = Config::getTopicDirectory($subjectId, $topicId) . "downloads/"; @@ -146,14 +201,15 @@ class TopicData $files[] = $fileName; } } + $result->files = $files; $article = Util::readFileContent(Config::getTopicDirectory($subjectId, $topicId) . "article.html"); if (!isset($article)) { $article = "Kein Erklärtext vorhanden"; } - $article = str_replace('$TOPICPATH', Config::getTopicDirectory($subjectId, $topicId) . "images", $article); + $result->article = str_replace('$TOPICPATH', Config::getTopicDirectory($subjectId, $topicId) . "images", $article); - return new TopicData($topicId, $subjectId, $data->displayName, $data->icon, $data->description, $relatedTopics, $files, $article); + return $result; } /**