Created blog post page and updated a few things here and there to work with the blog post page. Has comments and a sidebar.

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
2023-10-18 00:28:34 +01:00
parent 74d1ea35c1
commit 57aa831cdf
159 changed files with 3476 additions and 11912 deletions
+74 -15
View File
@@ -21,8 +21,7 @@ class blogData
public function getBlogPosts(): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, body, categories, featured
FROM blog ORDER BY dateCreated;");
$stmt = $conn->prepare("SELECT * FROM blog ORDER BY dateCreated DESC;");
$stmt->execute();
// set the resulting array to associative
@@ -38,14 +37,14 @@ class blogData
/**
* Get a blog post with the given ID
* @param string $ID - ID of the blog post to get
* @param string $title - Title of the blog post
* @return array - Array of all blog posts or error message
*/
public function getBlogPost(string $ID): array
public function getBlogPost(string $title): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, featured, headerImg, body, categories FROM blog WHERE ID = :ID;");
$stmt->bindParam(":ID", $ID);
$stmt = $conn->prepare("SELECT * FROM blog WHERE title = :title;");
$stmt->bindParam(":title", $title);
$stmt->execute();
// set the resulting array to associative
@@ -66,7 +65,7 @@ class blogData
public function getLatestBlogPost(): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, featured, headerImg, body, categories FROM blog ORDER BY dateCreated DESC LIMIT 1;");
$stmt = $conn->prepare("SELECT * FROM blog ORDER BY dateCreated DESC LIMIT 1;");
$stmt->execute();
// set the resulting array to associative
@@ -87,7 +86,7 @@ class blogData
public function getFeaturedBlogPost(): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, featured, headerImg, body, categories FROM blog WHERE featured = 1;");
$stmt = $conn->prepare("SELECT * FROM blog WHERE featured = 1;");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
@@ -100,6 +99,46 @@ class blogData
return array("errorMessage" => "Error, blog post could not found");
}
/**
* Get the blog posts with the given category
* @param string $category - Category of the blog post
* @return array - Array of the blog posts with the given category or error message
*/
public function getBlogPostsWithCategory(string $category): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT * FROM blog WHERE categories LIKE :category;");
$stmt->bindParam(":category", $category);
$stmt->execute();
// set the resulting array to associative
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($result)
{
return $result;
}
return array("errorMessage" => "Error, blog post could not found");
}
public function getCategories(): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT categories FROM blog;");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($result)
{
return $result;
}
return array("errorMessage" => "Error, blog post could not found");
}
/**
* Delete a blog post with the given ID
* @param int $ID - ID of the blog post to delete
@@ -142,12 +181,13 @@ class blogData
* @param int $ID - ID of the blog post to update
* @param string $title - Title of the blog post
* @param bool $featured - Whether the blog post is featured or not
* @param string $abstract - Abstract of the blog post i.e. a short description
* @param string $body - Body of the blog post
* @param string $dateModified - Date the blog post was modified
* @param string $categories - Categories of the blog post
* @return bool|string - Success or error message
*/
public function updatePost(int $ID, string $title, bool $featured, string $body, string $dateModified, string $categories): bool|string
public function updatePost(int $ID, string $title, bool $featured, string $abstract, string $body, string $dateModified, string $categories): bool|string
{
$conn = dbConn();
@@ -185,10 +225,11 @@ class blogData
$from = "../blog/imgs/tmp/";
$newBody = $this->changeHTMLSrc($body, $to, $from);
$stmt = $conn->prepare("UPDATE blog SET title = :title, featured = :featured, body = :body, dateModified = :dateModified, categories = :categories WHERE ID = :ID;");
$stmt = $conn->prepare("UPDATE blog SET title = :title, featured = :featured, abstract = :abstract, body = :body, dateModified = :dateModified, categories = :categories WHERE ID = :ID;");
$stmt->bindParam(":ID", $ID);
$stmt->bindParam(":title", $title);
$stmt->bindParam(":featured", $featured);
$stmt->bindParam(":abstract", $abstract);
$stmt->bindParam(":body", $newBody);
$stmt->bindParam(":dateModified", $dateModified);
$stmt->bindParam(":categories", $categories);
@@ -201,6 +242,7 @@ class blogData
* temp folder to the new folder, then updates the post html to point to the new images, finally
* it creates the post in the database
* @param string $title - Title of the blog post
* @param string $abstract - Abstract of the blog post i.e. a short description
* @param string $body - Body of the blog post
* @param string $dateCreated - Date the blog post was created
* @param bool $featured - Whether the blog post is featured or not
@@ -208,7 +250,7 @@ class blogData
* @param UploadedFileInterface $headerImg - Header image of the blog post
* @return int|string - ID of the blog post or error message
*/
public function createPost(string $title, string $body, string $dateCreated, bool $featured, string $categories, UploadedFileInterface $headerImg): int|string
public function createPost(string $title, string $abstract, string $body, string $dateCreated, bool $featured, string $categories, UploadedFileInterface $headerImg): int|string
{
$conn = dbConn();
$folderID = uniqid();
@@ -238,14 +280,15 @@ class blogData
$stmtMainProject->execute();
}
$stmt = $conn->prepare("INSERT INTO blog (title, dateCreated, dateModified, featured, headerImg, body, categories, folderID)
VALUES (:title, :dateCreated, :dateModified, :featured, :headerImg, :body, :categories, :folderID);");
$stmt = $conn->prepare("INSERT INTO blog (title, dateCreated, dateModified, featured, headerImg, abstract, body, categories, folderID)
VALUES (:title, :dateCreated, :dateModified, :featured, :headerImg, :abstract, :body, :categories, :folderID);");
$stmt->bindParam(":title", $title);
$stmt->bindParam(":dateCreated", $dateCreated);
$stmt->bindParam(":dateModified", $dateCreated);
$isFeatured = $featured ? 1 : 0;
$stmt->bindParam(":featured", $isFeatured);
$stmt->bindParam(":headerImg", $targetFile["imgLocation"]);
$stmt->bindParam(":abstract", $abstract);
$stmt->bindParam(":body", $newBody);
$stmt->bindParam(":categories", $categories);
$stmt->bindParam(":folderID", $folderID);
@@ -358,7 +401,7 @@ class blogData
$srcList[] = $src;
$fileName = basename($src);
$img->setAttribute("src", $to . $fileName);
$img->setAttribute("src", substr($to, 2) . $fileName);
}
$files = scandir($from);
@@ -372,7 +415,7 @@ class blogData
}
else
{
rename($from . $file, $to . $file);
rename($from . $file,$to . $file);
}
}
}
@@ -384,4 +427,20 @@ class blogData
}
return $newBody;
}
/**
* Get all posts with the given category
* @param mixed $category - Category of the post
* @return array - Array of all posts with the given category or error message
*/
public function getPostsByCategory(mixed $category)
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT * FROM blog WHERE categories = :category;");
$stmt->bindParam(":category", $category);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}