first test

This commit is contained in:
NOrtmann1
2026-05-27 16:52:06 +02:00
parent 7d9bef932f
commit 0686e98384
7 changed files with 145 additions and 110 deletions
-18
View File
@@ -2,24 +2,6 @@
Seite: Beitrag erstellen Seite: Beitrag erstellen
Inhalt: Formular für die Erstellung eines neuen Beitrags Inhalt: Formular für die Erstellung eines neuen Beitrags
--> -->
<?php
include_once $abs_path . '/php/model/ArticleManagerDAO.php';
include_once $abs_path . '/php/model/ArticleManager.php';
try {
$articleManager = new ArticleManager();
}catch (Exception $e){
die("Fehler bei der Initialisierung des Artikel-Managers: " . $e->getMessage());
}
if(isset($_POST['title']) && isset($_POST['content']) && isset($_POST['category'])) {
}else{
// TODO: Exception werfen.
echo"Fehler!";
}
?>
<form method="post" action="php/controller/createArticle-controller.php" id="editor-form" class="wp-editor-scope editor-container"> <form method="post" action="php/controller/createArticle-controller.php" id="editor-form" class="wp-editor-scope editor-container">
<main class="editor-main"> <main class="editor-main">
+22 -8
View File
@@ -1,18 +1,32 @@
<?php <?php
if (!isset($abs_path)) {
require_once "../../path.php";
}
require_once $abs_path . '/php/model/ArticleManager.php'; require_once $abs_path . '/php/model/LocalArticleManager.php';
require_once $abs_path . '/php/model/Article.php';
try { try {
$articleManager = new ArticleManager(); $articleManager = ArticleManager::getInstance();
}catch (Exception $e){ }catch (Exception $e){
die("Fehler bei der Initialisierung des Artikel-Managers: " . $e->getMessage()); die("Fehler bei der Initialisierung des Artikel-Managers: " . $e->getMessage());
} }
try { if ($_SERVER["REQUEST_METHOD"] === "POST") {
$author = "max.mustermann@web.de"; // wird später aus session bezogen. if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){
$articleManager->addArticle($_Post['title'], $_POST['post-content'], $author); $_SESSION["message"] = "missing_parameters";
} catch (Exception $e) { header("location:" . $abs_path . "/index.php?pfad=createArticle");
echo "Fehler beim Erstellen des Beitrags: " . $e->getMessage(); }
$title = $_POST["title"];
$content = $_POST["content"];
$category = $_POST["category"];
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
$tags = $_POST["tags"];
$articleManager->addArticle($title, $content, $category, $author, $tags);
// Weiterleitung zur Homepage
header("location:". $abs_path . "/index.php");
exit();
} }
?> ?>
@@ -0,0 +1,18 @@
<?php
require_once $abs_path . '/php/model/LocalArticleManager.php';
require_once $abs_path . '/php/model/Article.php';
try {
$articleManager = new localArticleManager();
}catch (Exception $e){
die("Fehler bei der Initialisierung des Artikel-Managers: " . $e->getMessage());
}
try {
$author = "max.mustermann@web.de"; // wird später aus session bezogen.
$articleManager->addArticle($_Post['title'], $_POST['post-content'], $author);
} catch (Exception $e) {
echo "Fehler beim Erstellen des Beitrags: " . $e->getMessage();
}
?>
+6 -6
View File
@@ -11,7 +11,7 @@ class Article
private $title; private $title;
private $content; private $content;
private $author; private $author;
private $date; private $creationDate;
private $category; private $category;
/** /**
@@ -22,16 +22,16 @@ class Article
* @param $content string Inhalt des Beitrags * @param $content string Inhalt des Beitrags
* @param $author string der Autor des des Beitrages NID * @param $author string der Autor des des Beitrages NID
* @param $category string Kategorie des Beitrages * @param $category string Kategorie des Beitrages
* @param $date * @param $creationDate
* @param $tags string[] optionale Schlagworte für eine bessere Suche * @param $tags string[] optionale Schlagworte für eine bessere Suche
*/ */
public function __construct($id, $title, $content, $author, $date, $category, array $tags) public function __construct($id, $title, $content, $author, $creationDate, $category, array $tags)
{ {
$this->id = $id; $this->id = $id;
$this->title = $title; $this->title = $title;
$this->content = $content; $this->content = $content;
$this->author = $author; $this->author = $author;
$this->date = $date; $this->creationDate = $creationDate;
$this->category = $category; $this->category = $category;
$this->tags = $tags; $this->tags = $tags;
} }
@@ -98,9 +98,9 @@ class Article
* Gibt das Veröffentlichungsdatum des Artikels zurück. * Gibt das Veröffentlichungsdatum des Artikels zurück.
* @return mixed * @return mixed
*/ */
public function getDate() public function getcreationDate()
{ {
return $this->date; return $this->creationDate;
} }
} }
+10 -76
View File
@@ -1,84 +1,18 @@
<?php <?php
require_once $abs_path . 'ArticleManagerDAO.php'; if (!isset($abs_path)) {
require_once "../../path.php";
}
require_once $abs_path . '/php/model/LocalArticleManager.php';
/** /**
* Klasse: Eine lokale Lösung des ArticleManagerDAO. * Die Klasse beinhaltet alle Methoden für die Operation mit den Artikel-Daten.
* Schreibt zunächst Daten lokal in das Verzeichnis und kann diese wieder auslesen.
* *
* @author Niklas Ortmann * @author Niklas Ortmann
*/ */
class ArticleManager implements ArticleManagerDAO { class ArticleManager
{
private string $file = $abs_path . "data/articles.json";
/**
* Gibt die ArticleManager-Instanz zurück.
* @return ArticleManager
*/
public static function getInstance() public static function getInstance()
{ {
if (self::$instance == null) { return LocalArticleManager::getInstance(); // TODO: später durch DB-Implementation von ArticleManger ersetzen.
self::$instance = new ArticleManager();
}
return self::$instance;
} }
}
/**
* Speichert alle Artikel/Beiträge in der Datei.
* @param $articles
* @return void
*/
public function saveArticle($articles)
{
file_put_contents(
$this->file,
json_encode($articles, JSON_PRETTY_PRINT)
);
}
public function addArticle($title, $content, $author, $category, array $tags)
{
$articles = $this->getAllArticles();
$articles[] = [
"id" => count($articles)+1,
"title" => $title,
"content" => $content,
"author" => $author,
"category" => $category,
"tags" => $tags
];
$this->saveArticle($articles);
}
public function updateArticle($id, $title, $content, $author)
{
// TODO: Implement updateArticle() method.
}
public function deleteArticle($id)
{
// TODO: Implement deleteArticle() method.
}
public function getArticle($id)
{
// TODO: Implement getArticle() method.
}
public function getAllArticles()
{
if (!file_exists($this->file)) {
return [];
}
$json = file_get_contents($this->file);
$articles = json_decode($json, true);
return is_array($articles) ? $articles : [];
}
}
?>
+1 -2
View File
@@ -1,10 +1,9 @@
<?php <?php
/** /**
* Die Klasse beinhaltet alle Methoden für die Operation auf der Datenbank. * Die Klasse beinhaltet alle Methoden für die Operation mit den Artikel-Daten.
* *
* @author Niklas Ortmann * @author Niklas Ortmann
*/ */
interface ArticleManagerDAO interface ArticleManagerDAO
{ {
/** /**
+88
View File
@@ -0,0 +1,88 @@
<?php
if (!isset($abs_path)) {
require_once "../../path.php";
}
require_once $abs_path . '/php/model/ArticleManagerDAO.php';
/**
* Klasse: Eine lokale Lösung des ArticleManagerDAO.
* Schreibt zunächst Daten lokal in das Verzeichnis und kann diese wieder auslesen.
*
* @author Niklas Ortmann
*/
class LocalArticleManager implements ArticleManagerDAO {
private string $file = "../../data/articles.json";
/**
* Gibt die LocalArticleManager-Instanz zurück.
* @return LocalArticleManager
*/
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new LocalArticleManager();
}
return self::$instance;
}
/**
* Speichert alle Artikel/Beiträge in der Datei.
* @param $articles
* @return void
*/
public function saveArticle($articles)
{
file_put_contents(
$this->file,
json_encode($articles, JSON_PRETTY_PRINT)
);
}
public function addArticle($title, $content, $author, $category, array $tags)
{
$articles = $this->getAllArticles();
$articles[] = [
"id" => count($articles)+1,
"title" => $title,
"content" => $content,
"author" => $author,
"category" => $category,
"tags" => $tags,
"creationDate" => date("Y-m-d H:i:s")
];
$this->saveArticle($articles);
}
public function updateArticle($id, $title, $content, $author)
{
// TODO: Implement updateArticle() method.
}
public function deleteArticle($id)
{
// TODO: Implement deleteArticle() method.
}
public function getArticle($id)
{
// TODO: Implement getArticle() method.
}
public function getAllArticles()
{
if (!file_exists($this->file)) {
return [];
}
$json = file_get_contents($this->file);
$articles = json_decode($json, true);
return is_array($articles) ? $articles : [];
}
}
?>