2024-02-22 09:42:37 +01:00
|
|
|
<?php
|
2024-04-27 11:48:58 +02:00
|
|
|
declare(strict_types=1);
|
2024-02-22 09:42:37 +01:00
|
|
|
function getNewsArticles() :array
|
|
|
|
{
|
|
|
|
global $mysqli;
|
|
|
|
|
2024-02-22 13:57:05 +01:00
|
|
|
$output = ["Status" => "Fail"]; // Default Status is "Fail"
|
|
|
|
|
2024-02-22 09:42:37 +01:00
|
|
|
$articles = [];
|
2024-02-24 09:01:13 +01:00
|
|
|
$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 WHERE NewsArticles.PrivilegeLevel <= ?;");
|
2024-02-22 13:57:05 +01:00
|
|
|
$id = 0;
|
|
|
|
$writtenAt = "";
|
|
|
|
$writtenBy = 0;
|
|
|
|
$title = "";
|
|
|
|
$body = "";
|
|
|
|
$filelist = 0;
|
|
|
|
$writtenByName = "";
|
|
|
|
|
2024-02-24 09:01:13 +01:00
|
|
|
$stmt->bind_param("i", $_SESSION["privilege_level"]);
|
2024-02-22 13:57:05 +01:00
|
|
|
$stmt->bind_result($id, $writtenAt, $writtenBy, $title, $body, $filelist, $writtenByName);
|
|
|
|
|
|
|
|
$stmt->execute();
|
2024-02-22 09:42:37 +01:00
|
|
|
|
2024-02-22 13:57:05 +01:00
|
|
|
while ($stmt->fetch()) {
|
|
|
|
$articles[] = [
|
|
|
|
'ID' => $id,
|
|
|
|
'WrittenAt' => $writtenAt,
|
|
|
|
'Title' => $title,
|
|
|
|
'Body' => $body,
|
|
|
|
'WrittenByName' =>$writtenByName
|
|
|
|
];
|
2024-02-22 09:42:37 +01:00
|
|
|
}
|
2024-02-22 13:57:05 +01:00
|
|
|
|
|
|
|
// Check if any results were fetched
|
|
|
|
if (!empty($articles)) {
|
|
|
|
$output["Status"] = "Success";
|
|
|
|
$output["Articles"] = $articles;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
2024-02-22 10:05:09 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 09:01:13 +01:00
|
|
|
function addNewsArticle($title="Nazov", $body="Obsah", $privilegeLevel=0) :array
|
2024-02-22 10:05:09 +01:00
|
|
|
{
|
|
|
|
global $mysqli;
|
2024-02-24 09:01:13 +01:00
|
|
|
global $routerConfig;
|
|
|
|
|
|
|
|
|
|
|
|
if ($privilegeLevel == 0){
|
|
|
|
$privilegeLevel = $routerConfig['newsarticle']['default_permissions'];
|
|
|
|
}
|
2024-02-22 10:05:09 +01:00
|
|
|
|
|
|
|
$output = ["Status" => "Fail"]; // Default Status is "Fail"
|
2024-02-24 09:01:13 +01:00
|
|
|
if (isLoggedIn() && $privilegeLevel <= $_SESSION["privilege_level"]) {
|
|
|
|
$query = $mysqli->prepare("INSERT INTO NewsArticles (WrittenBy, Title, Body, FileList, PrivilegeLevel) VALUES (?, ?, ?, 0, ?);");
|
2024-02-25 20:34:29 +01:00
|
|
|
$minpriv = intval($privilegeLevel);
|
|
|
|
$query->bind_param("issi", $_SESSION["ID"], htmlspecialchars($title), htmlspecialchars($body), $minpriv);
|
2024-02-22 10:05:09 +01:00
|
|
|
$query->execute();
|
|
|
|
if ($query->affected_rows > 0) {
|
|
|
|
$output["Status"] = "Success";
|
|
|
|
}
|
2024-02-22 18:16:42 +01:00
|
|
|
$query->close();
|
2024-02-22 10:05:09 +01:00
|
|
|
}
|
|
|
|
return $output;
|
2024-02-22 09:42:37 +01:00
|
|
|
}
|