Speichern und löschen von Themen und Fächern eingebaut

This commit is contained in:
Matthias Grief
2024-12-06 22:12:47 +01:00
committed by Eric Blommel
parent 7a146d8982
commit e09438c6ea
3 changed files with 292 additions and 1 deletions

View File

@@ -119,4 +119,106 @@ class SubjectData
return $result;
}
/**
* Schreibt alle Daten in Dateien
* @return bool true, wenn erfolgreich, sonst false
*/
public function save(): bool
{
$data = array();
$data["displayName"] = $this->displayName;
$data["description"] = $this->description;
$data["color"] = $this->color;
$data["icon"] = $this->icon;
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
if (!$json) {
return false;
}
$subjectDirectory = Config::getSubjectDirectory($this->getId());
if (!is_dir($subjectDirectory)) {
mkdir($subjectDirectory, 0777, true);
}
if (!Util::writeFileContent($subjectDirectory . "properties.json", $json)) {
return false;
}
return true;
}
/**
* Löscht das Fach inklusive aller zugehörigen Themen
* @return bool true, wenn erfolgreich, sonst false
*/
public function delete(): bool
{
if(!Util::delete(Config::getSubjectDirectory($this->getId()))) {
return false;
}
return true;
}
public function getId(): string
{
return $this->id;
}
public function setId(string $id): void
{
$this->id = $id;
}
public function getDisplayName(): string
{
return $this->displayName;
}
public function setDisplayName(string $displayName): void
{
$this->displayName = $displayName;
}
public function getDescription(): string
{
return $this->description;
}
public function setDescription(string $description): void
{
$this->description = $description;
}
public function getColor(): string
{
return $this->color;
}
public function setColor(string $color): void
{
$this->color = $color;
}
public function getIcon(): string
{
return $this->icon;
}
public function setIcon(string $icon): void
{
$this->icon = $icon;
}
public function getTopics(): array
{
return $this->topics;
}
public function setTopics(array $topics): void
{
$this->topics = $topics;
}
}

View File

@@ -134,4 +134,135 @@ class TopicData
return $result;
}
/**
* Schreibt alle Daten in Dateien
* @return bool true, wenn erfolgreich, sonst false
*/
public function save(): bool
{
$data = array();
$data["displayName"] = $this->displayName;
$data["icon"] = $this->icon;
$data["description"] = $this->description;
$data["relatedTopics"] = $this->relatedTopics;
$data["files"] = $this->files;
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
if (!$json) {
return false;
}
if (!is_dir(Config::getSubjectDirectory($this->getSubjectId()))) {
return false;
}
$topicDirectory = Config::getTopicDirectory($this->getSubjectId(), $this->getId());
if (!is_dir($topicDirectory)) {
mkdir($topicDirectory, 0777, true);
}
if (!(Util::writeFileContent($topicDirectory . "properties.json", $json)
&& Util::writeFileContent($topicDirectory . "article.html", $json))
) {
return false;
}
return true;
}
/**
* Löscht das Thema inklusive aller zugehörigen Dateien
* @return bool true, wenn erfolgreich gelöscht, sonst false
*/
public function delete(): bool
{
if(!Util::delete(Config::getTopicDirectory($this->getSubjectId(), $this->getId()))) {
return false;
}
return true;
}
public function getId(): string
{
return $this->id;
}
public function setId(string $id): void
{
$this->id = $id;
}
public function getSubjectId(): string
{
return $this->subjectId;
}
public function setSubjectId(string $subjectId): void
{
$this->subjectId = $subjectId;
}
public function getDisplayName(): string
{
return $this->displayName;
}
public function setDisplayName(string $displayName): void
{
$this->displayName = $displayName;
}
public function getIcon(): string
{
return $this->icon;
}
public function setIcon(string $icon): void
{
$this->icon = $icon;
}
public function getDescription(): string
{
return $this->description;
}
public function setDescription(string $description): void
{
$this->description = $description;
}
public function getRelatedTopics(): array
{
return $this->relatedTopics;
}
public function setRelatedTopics(array $relatedTopics): void
{
$this->relatedTopics = $relatedTopics;
}
public function getFiles(): array
{
return $this->files;
}
public function setFiles(array $files): void
{
$this->files = $files;
}
public function getArticle(): string
{
return $this->article;
}
public function setArticle(string $article): void
{
$this->article = $article;
}
}

View File

@@ -15,15 +15,25 @@ class Util
return preg_replace("/[^a-zA-Z0-9_-]/", "", $string);
}
/**
* Prüft, ob Zeichen außer A-Z, a-z, 0-9 sowie - und _ enthalten sind
* @param string $string Zu prüfender Text
* @return bool true, wenn ungültige Zeichen enthalten sind, sonst false
*/
static function containsIllegalCharacters(string $string): bool
{
if(preg_match("/[^a-zA-Z0-9_-]/", $string)) {
if (preg_match("/[^a-zA-Z0-9_-]/", $string)) {
return true;
}
return false;
}
/**
* Liest den gesamten Text aus einer Datei aus
* @param string $filename Dateipfad
* @return string|null Der enthaltene Text oder null bei einem Fehler
*/
static function readFileContent(string $filename): string|null
{
if (!file_exists($filename)) {
@@ -42,6 +52,54 @@ class Util
return $fileContent;
}
/**
* Schreibt Inhalt in eine Datei. Überschreibt vorhandene Inhalte. Erstellt eine neue Datei, falls nötig.
* @param string $filename Dateipfad
* @param string $content Zu schreibender Text
* @return bool true, wenn erfolgreich, false, wenn ein Fehler aufgetreten ist
*/
static function writeFileContent(string $filename, string $content): bool
{
$file = fopen($filename, "w");
if (!$file) {
return false;
}
if (!fwrite($file, $content)) {
return false;
}
fclose($file);
return true;
}
/**
* Löscht eine Datei oder einen Ordner inklusive aller Inhalte
* @param string $path Pfad zur Datei oder Verzeichnis
* @return bool true, wenn gelöscht, sonst false
*/
static function delete(string $path): bool
{
if(is_file($path)) {
if(!unlink($path)) {
return false;
}
} else if(is_dir($path)) {
$entries = scandir($path);
foreach ($entries as $entry) {
if($entry == "." || $entry == "..") {
continue;
}
self::delete($path . "/" . $entry);
}
}
return true;
}
/**
* Öffnet eine Datei und gibt JSON-Inhalte als Array zurück
* @param string $filename Dateipfad