Dynamische Daten in Seitentemplates eingefügt
This commit is contained in:
83
webseite/classes/SubjectData.php
Normal file
83
webseite/classes/SubjectData.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
require_once("Util.php");
|
||||
require_once("TopicData.php");
|
||||
|
||||
class SubjectData
|
||||
{
|
||||
public string $id;
|
||||
public string $displayName;
|
||||
public string $description;
|
||||
public string $color;
|
||||
public string $buttonText;
|
||||
public string $icon;
|
||||
public array $topics;
|
||||
|
||||
public static function getAll() : array
|
||||
{
|
||||
$result = array();
|
||||
|
||||
$subjectDirectory = "config/subjects";
|
||||
$subjectNames = scandir($subjectDirectory);
|
||||
foreach ($subjectNames as $subjectName) {
|
||||
if ($subjectName == "." || $subjectName == "..") {
|
||||
continue;
|
||||
}
|
||||
|
||||
$subjectData = SubjectData::fromName($subjectName);
|
||||
if(!isset($subjectData)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[] = $subjectData;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function fromName($subjectName) : SubjectData|null
|
||||
{
|
||||
$result = new SubjectData();
|
||||
|
||||
$subjectName = Util::removeIllegalCharacters($subjectName);
|
||||
|
||||
$subjectDirectory = "config/subjects/$subjectName";
|
||||
$filename = "$subjectDirectory/properties.json";
|
||||
$data = Util::parseJsonFromFile($filename);
|
||||
|
||||
$result->id = $subjectName;
|
||||
|
||||
if(isset($data->displayName)) {
|
||||
$result->displayName = $data->displayName;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(isset($data->description)) {
|
||||
$result->description = $data->description;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(isset($data->color)) {
|
||||
$result->color = $data->color;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(isset($data->buttonText)) {
|
||||
$result->buttonText = $data->buttonText;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(isset($data->icon)) {
|
||||
$result->icon = $data->icon;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
$result->topics = TopicData::getAll($subjectName);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
88
webseite/classes/TopicData.php
Normal file
88
webseite/classes/TopicData.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
require_once("Util.php");
|
||||
|
||||
class TopicData
|
||||
{
|
||||
public string $id;
|
||||
public string $subjectId;
|
||||
public string $displayName;
|
||||
public string $icon;
|
||||
public string $description;
|
||||
public array $relatedTopics;
|
||||
public array $files;
|
||||
public string $article;
|
||||
|
||||
public static function getAll($subjectName) : array
|
||||
{
|
||||
$result = array();
|
||||
|
||||
$topicDirectory = "config/subjects/$subjectName/topics";
|
||||
$topicNames = scandir($topicDirectory);
|
||||
foreach ($topicNames as $topicName) {
|
||||
if ($topicName == "." || $topicName == "..") {
|
||||
continue;
|
||||
}
|
||||
$topicData = TopicData::fromName($subjectName, $topicName);
|
||||
if(!isset($topicData)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[] = $topicData;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function fromName($subjectName, $topicName): TopicData|null
|
||||
{
|
||||
$result = new TopicData();
|
||||
|
||||
$subjectName = Util::removeIllegalCharacters($subjectName);
|
||||
$topicName = Util::removeIllegalCharacters($topicName);
|
||||
|
||||
$topicDirectory = "config/subjects/$subjectName/topics";
|
||||
$filename = "$topicDirectory/$topicName/properties.json";
|
||||
$data = Util::parseJsonFromFile($filename);
|
||||
|
||||
$result->id = $topicName;
|
||||
$result->subjectId = $subjectName;
|
||||
|
||||
if(isset($data->displayName)) {
|
||||
$result->displayName = $data->displayName;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(isset($data->icon)) {
|
||||
$result->icon = $data->icon;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(isset($data->description)) {
|
||||
$result->description = $data->description;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(isset($data->relatedTopics)) {
|
||||
$result->relatedTopics = $data->relatedTopics;
|
||||
} else {
|
||||
$result->relatedTopics = array();
|
||||
}
|
||||
if(isset($data->files)) {
|
||||
$result->files = $data->files;
|
||||
} else {
|
||||
$result->files = array();
|
||||
}
|
||||
|
||||
if(isset($data->article)) {
|
||||
$result->article = $data->article;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
18
webseite/classes/Util.php
Normal file
18
webseite/classes/Util.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
class Util
|
||||
{
|
||||
static function removeIllegalCharacters($string): string
|
||||
{
|
||||
return preg_replace("/[^a-zA-Z0-9_-]/", "", $string);
|
||||
}
|
||||
|
||||
static function parseJsonFromFile($filename): mixed
|
||||
{
|
||||
$file = fopen($filename, "r") or die("Unable to open file!");
|
||||
$fileContent = fread($file, filesize($filename));
|
||||
fclose($file);
|
||||
|
||||
return json_decode($fileContent);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user