This commit is contained in:
Bruno Rybársky 2024-02-22 13:57:05 +01:00
parent c74feeb8cd
commit 4b288b48c8
5 changed files with 73 additions and 24 deletions

@ -179,16 +179,24 @@ async function togglearticlecreate(){
articleContainerElement.classList.toggle("hidden"); articleContainerElement.classList.toggle("hidden");
} }
async function renderarticle(articledata){
}
async function submitarticle(){ async function submitarticle(){
let articleTitleElement = document.getElementById("articletitleinput"); let articleTitleElement = document.getElementById("articletitleinput");
let articleBodyElement = document.getElementById("articlebodyinput"); let articleBodyElement = document.getElementById("articlebodyinput");
await doAction("/newsarticle", { await doAction(
"/newsarticle",
{
action: "addNewsArticle", action: "addNewsArticle",
title: articleTitleElement.value, title: articleTitleElement.value,
body: articleBodyElement.value body: articleBodyElement.value
}, "Článok úspešne pridaný", },
"Článok úspešne pridaný",
"Nastala chyba pri pridávaní článku", "Nastala chyba pri pridávaní článku",
false); false
);
} }
async function articleInit(){ async function articleInit(){

@ -4,19 +4,43 @@ function getNewsArticles() :array
{ {
global $mysqli; global $mysqli;
$output = ["Status" => "Fail"]; // Default Status is "Fail"
$articles = []; $articles = [];
if (isLoggedIn()) { if (isLoggedIn()) {
$result = $mysqli->query("SELECT NewsArticles.ID, NewsArticles.WrittenAt, NewsArticles.WrittenBy, NewsArticles.Title, NewsArticles.Body, NewsArticles.FileList, Users.Nickname FROM NewsArticles INNER JOIN Users ON NewsArticles.WrittenBy = Users.ID; "); $stmt = $mysqli->prepare("SELECT NewsArticles.ID, NewsArticles.WrittenAt, NewsArticles.WrittenBy, NewsArticles.Title, NewsArticles.Body, NewsArticles.FileList, Users.Nickname FROM NewsArticles INNER JOIN Users ON NewsArticles.WrittenBy = Users.ID; ");
// Check if the query executed Successfully $id = 0;
if ($result) { $writtenAt = "";
while ($row = $result->fetch_assoc()) { $writtenBy = 0;
$articles[] = $row; $title = "";
} $body = "";
} $filelist = 0;
$writtenByName = "";
$stmt->bind_result($id, $writtenAt, $writtenBy, $title, $body, $filelist, $writtenByName);
$stmt->execute();
while ($stmt->fetch()) {
$articles[] = [
'ID' => $id,
'WrittenAt' => $writtenAt,
'Title' => $title,
'Body' => $body,
'WrittenByName' =>$writtenByName
];
} }
return $articles; // Check if any results were fetched
if (!empty($articles)) {
$output["Status"] = "Success";
$output["Articles"] = $articles;
}
}
return $output;
} }
function addNewsArticle($title="Nazov", $body="Obsah") :array function addNewsArticle($title="Nazov", $body="Obsah") :array

@ -8,8 +8,13 @@ global $routerConfig;
$output = file_get_contents($routerConfig["template_dir"] . "newsArticles.html"); $output = file_get_contents($routerConfig["template_dir"] . "newsArticles.html");
$articles_out = ""; $articles_out = "";
$articles = getNewsArticles(); $articles = [];
$articles_tmp = getNewsArticles();
if($articles_tmp['Status'] == "Success"){
$articles = $articles_tmp["Articles"];
}
$articleTemplate = file_get_contents($routerConfig["template_dir"] . "newsArticle.html");
foreach ($articles as $article){ foreach ($articles as $article){
$articleTitle = htmlspecialchars($article["Title"]); $articleTitle = htmlspecialchars($article["Title"]);
$articleBody = htmlspecialchars($article["Body"]); $articleBody = htmlspecialchars($article["Body"]);
@ -18,18 +23,15 @@ foreach ($articles as $article){
$articleWrittenAt = htmlspecialchars($article["WrittenAt"]); $articleWrittenAt = htmlspecialchars($article["WrittenAt"]);
$articleWrittenByName = htmlspecialchars($article["Nickname"]); $articleWrittenByName = htmlspecialchars($article["Nickname"]);
$articles_out .= "<article> $articleTemplate = str_replace("__TEMPLATE_ARTICLE_TITLE__", $articleTitle, $articleTemplate);
<h2 class='newstitle'>$articleTitle</h2> $articleTemplate = str_replace("__TEMPLATE_ARTICLE_AUTHOR__", $articleWrittenByName, $articleTemplate);
<br> $articleTemplate = str_replace("__TEMPLATE_ARTICLE_DATE__", $articleWrittenAt, $articleTemplate);
<p class='newsauthor'>$articleWrittenByName</p> $articleTemplate = str_replace("__TEMPLATE_ARTICLE_BODY__", $articleBody, $articleTemplate);
<p class='newsdate'>$articleWrittenAt</p>
<hr> $articles_out .= $articleTemplate;
<div class='newsbody'>
$articleBody
</div>
</article>";
} }
$output = str_replace("__TEMPLATE_FOR_ARTICLE_CONTENT__", $articleTemplate, $output);
$output = str_replace("__TEMPLATE__ARTICLES_HERE__", $articles_out, $output); $output = str_replace("__TEMPLATE__ARTICLES_HERE__", $articles_out, $output);
return [ return [

@ -0,0 +1,10 @@
<article>
<h2 class='newstitle'>__TEMPLATE_ARTICLE_TITLE__</h2>
<br>
<p class='newsauthor'>__TEMPLATE_ARTICLE_AUTHOR__</p>
<p class='newsdate'>__TEMPLATE_ARTICLE_DATE__</p>
<hr>
<div class='newsbody'>
__TEMPLATE_ARTICLE_BODY__
</div>
</article>

@ -4,6 +4,11 @@
<button id="articlecreateopen" onclick="togglearticlecreate()"><i class="ri-add-circle-line"></i></button> <button id="articlecreateopen" onclick="togglearticlecreate()"><i class="ri-add-circle-line"></i></button>
<hr> <hr>
</header> </header>
<template data-template-name="article">
__TEMPLATE_FOR_ARTICLE_CONTENT__
</template>
<div id="articleslist"> <div id="articleslist">
__TEMPLATE__ARTICLES_HERE__ __TEMPLATE__ARTICLES_HERE__
</div> </div>