Beitrag erstellen #13

Merged
niklas.ortmann merged 164 commits from Beitrag-erstellen into dev 2026-05-29 13:22:11 +02:00
7 changed files with 145 additions and 110 deletions
Showing only changes of commit 0686e98384 - Show all commits
-18
View File
@@ -2,24 +2,6 @@
Seite: Beitrag erstellen
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">
<main class="editor-main">
+22 -8
View File
@@ -1,18 +1,32 @@
<?php
if (!isset($abs_path)) {
require_once "../../path.php";
}
require_once $abs_path . '/php/model/ArticleManager.php';
require_once $abs_path . '/php/model/Article.php';
require_once $abs_path . '/php/model/LocalArticleManager.php';
try {
$articleManager = new ArticleManager();
$articleManager = ArticleManager::getInstance();
}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();
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){
$_SESSION["message"] = "missing_parameters";
header("location:" . $abs_path . "/index.php?pfad=createArticle");
}
$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 $content;
private $author;
private $date;
private $creationDate;
private $category;
/**
@@ -22,16 +22,16 @@ class Article
* @param $content string Inhalt des Beitrags
* @param $author string der Autor des des Beitrages NID
* @param $category string Kategorie des Beitrages
* @param $date
* @param $creationDate
* @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->title = $title;
$this->content = $content;
$this->author = $author;
$this->date = $date;
$this->creationDate = $creationDate;
$this->category = $category;
$this->tags = $tags;
}
@@ -98,9 +98,9 @@ class Article
* Gibt das Veröffentlichungsdatum des Artikels zurück.
* @return mixed
*/
public function getDate()
public function getcreationDate()
{
return $this->date;
return $this->creationDate;
}
}
+10 -76
View File
@@ -1,84 +1,18 @@
<?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.
* Schreibt zunächst Daten lokal in das Verzeichnis und kann diese wieder auslesen.
* Die Klasse beinhaltet alle Methoden für die Operation mit den Artikel-Daten.
*
* @author Niklas Ortmann
*/
class ArticleManager implements ArticleManagerDAO {
private string $file = $abs_path . "data/articles.json";
/**
* Gibt die ArticleManager-Instanz zurück.
* @return ArticleManager
*/
class ArticleManager
{
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new ArticleManager();
}
return self::$instance;
return LocalArticleManager::getInstance(); // TODO: später durch DB-Implementation von ArticleManger ersetzen.
}
/**
* 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
/**
* 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
*/
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 : [];
}
}
?>