Skip to content
Snippets Groups Projects
Commit 7db5af93 authored by Silas Meister's avatar Silas Meister
Browse files

News now being loaded from the database

parent c8595226
No related branches found
No related tags found
No related merge requests found
img/uploads/astronaut.jpg

69.9 KiB

img/uploads/nasa_launch.jpg

40.2 KiB

......@@ -6,15 +6,21 @@ require_once 'constants.php';
class Admin extends Page
{
private const FILE = 'file';
private const IMAGES = 'images';
private const IMAGES_WITH_BRACKETS = 'images[]';
private const EXPIRES = 'EXPIRES';
private const TITLE = 'title';
private const IMGURL = 'imgurl';
private const CONTENT = 'content';
protected function __construct()
{
parent::__construct();
session_start();
if (!isset($_SESSION[EXPIRES]) || time() - $_SESSION[EXPIRES] > 60) {
if (!isset($_SESSION[EXPIRES]) || time() - $_SESSION[EXPIRES] > 300) {
session_unset();
session_destroy();
header("Location: Login.php");
......@@ -37,6 +43,8 @@ class Admin extends Page
$this->generatePageHeader('NASA Admin');
$file = self::FILE;
$images = self::IMAGES;
$imagesWithBrackets = self::IMAGES_WITH_BRACKETS;
echo <<<EOT
<body>
......@@ -44,8 +52,15 @@ class Admin extends Page
<form class="admin-form" action="Admin.php" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
<fieldset>
<legend>Select a News file</legend>
<input id="$file" name="$file" type="file" accept=".json" required />
<legend>Upload</legend>
<div>
<label for="$file">News file:</label>
<input style="backgorund-color: red" id="$file" name="$file" type="file" accept=".json" required />
</div>
<div>
<label for="$images">Images:</label>
<input id="$images" name="$imagesWithBrackets" type="file" accept="image/*" multiple />
</div>
<input type="submit" value="Apply" />
</fieldset>
</form>
......@@ -60,17 +75,60 @@ EOT;
parent::processReceivedData();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_FILES[self::FILE])) {
$name = $_FILES[self::FILE]["name"];
$tmp = $_FILES[self::FILE]["tmp_name"];
$target = "../data/uploads/" . $name;
move_uploaded_file($tmp, $target);
$this->processNews();
$this->uploadImages();
$_SESSION[NEWS_FILE] = "../data/uploads/news_01.json";
header("Location: Index.php");
die();
}
}
private function processNews() {
if (!isset($_FILES[self::FILE]))
return;
$name = $_FILES[self::FILE]["name"];
$path = $_FILES[self::FILE]["tmp_name"];
$jsonString = file_get_contents($path);
$json = json_decode($jsonString, true);
$this->insertNews($json);
}
private function insertNews(array $json):void {
$listOfNews = $json['news'];
$stmt = $this->database->prepare("INSERT INTO news(title, imgurl, content) VALUES (?, ?, ?);");
$stmt->bind_param("sss", $title, $imgurl, $content);
foreach ($listOfNews as $news) {
$title = $news[self::TITLE];
$imgurl = $news[self::IMGURL];
$content = $news[self::CONTENT];
$stmt->execute();
}
$stmt->close();
}
private function uploadImages():void {
if (!isset($_FILES[self::IMAGES]))
return;
$count = count($_FILES[self::IMAGES]['name']);
$_SESSION[NEWS_FILE] = $target;
for ($i = 0; $i < $count; ++$i) {
$tmpFilePath = $_FILES[self::IMAGES]['tmp_name'][$i];
$name = $_FILES[self::IMAGES]['name'][$i];
$target = "../img/uploads/" . $name;
header("Location: Index.php");
die();
}
move_uploaded_file($tmpFilePath, $target);
}
}
......
......@@ -6,6 +6,10 @@ require_once 'data/News.php';
class Index extends Page {
private const TITLE = 'title';
private const IMGURL = 'imgurl';
private const CONTENT = 'content';
protected function __construct()
{
parent::__construct();
......@@ -20,32 +24,17 @@ class Index extends Page {
protected function getViewData():array
{
$file = DEFAULT_NEWS_FILE;
if (isset($_SESSION[NEWS_FILE])) {
$file = $_SESSION[NEWS_FILE];
}
if (!file_exists($file)) {
die("File not found");
}
$fileContents = file_get_contents($file);
if ($fileContents === NULL)
return array();
$json = json_decode($fileContents, true);
$news = array();
foreach ($json["news"] as $elem) {
$title = htmlspecialchars($elem["title"]);
$imgurl = htmlspecialchars($elem["imgurl"]);
$content = htmlspecialchars($elem["content"]);
$resultSet = $this->database->query("SELECT n.title, n.imgurl, n.content FROM news n ORDER BY n.date DESC LIMIT 5");
$news[] = new News($title, $imgurl, $content);
}
while ($row = $resultSet->fetch_assoc()) {
$title = htmlspecialchars($row[self::TITLE]);
$imgurl = htmlspecialchars($row[self::IMGURL]);
$content = htmlspecialchars($row[self::CONTENT]);
$news[] = new News($title, $imgurl, $content);
}
return $news;
}
......
......@@ -7,7 +7,7 @@ abstract class Page {
protected function __construct() {
error_reporting(E_ALL);
$this->database = new mysqli("localhost","root","","nasa");
$this->database = new mysqli("localhost","root", "","nasa");
// Check connection
if ($this->database->connect_errno) {
......
......@@ -458,6 +458,19 @@ main {
display: inline;
}
.admin-form > fieldset > div {
display: flex;
width: 100%;
justify-content: space-around;
align-items: center;
column-gap: 1rem;
padding: 0.5rem 0;
}
.admin-form > fieldset > div > label {
font-size: 0.8rem;
}
.admin-form input[type="submit"] {
background-color: lightgray;
cursor: pointer;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment