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:
Vendored
+74
-15
@@ -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);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Vendored
+81
-7
@@ -29,6 +29,38 @@ class blogRoutes implements routesInterface
|
||||
*/
|
||||
public function createRoutes(App $app): void
|
||||
{
|
||||
$app->get("/blog/categories", function (Request $request, Response $response)
|
||||
{
|
||||
$post = $this->blogData->getCategories();
|
||||
if (array_key_exists("errorMessage", $post))
|
||||
{
|
||||
$response->getBody()->write(json_encode($post));
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode($post));
|
||||
return $response;
|
||||
});
|
||||
|
||||
$app->get("/blog/categories/{category}", function (Request $request, Response $response, $args)
|
||||
{
|
||||
if ($args["category"] != null)
|
||||
{
|
||||
$post = $this->blogData->getPostsByCategory($args["category"]);
|
||||
if (array_key_exists("errorMessage", $post))
|
||||
{
|
||||
$response->getBody()->write(json_encode($post));
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode($post));
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(array("error" => "Please provide a category")));
|
||||
return $response->withStatus(400);
|
||||
});
|
||||
|
||||
$app->get("/blog/post", function (Request $request, Response $response)
|
||||
{
|
||||
$posts = $this->blogData->getBlogPosts();
|
||||
@@ -45,11 +77,37 @@ class blogRoutes implements routesInterface
|
||||
return $response;
|
||||
});
|
||||
|
||||
$app->get("/blog/post/{id}", function (Request $request, Response $response, $args)
|
||||
$app->get("/blog/post/{type}", function (Request $request, Response $response, $args)
|
||||
{
|
||||
if ($args["id"] != null)
|
||||
if ($args["type"] != null)
|
||||
{
|
||||
$post = $this->blogData->getBlogPost($args["id"]);
|
||||
if ($args["type"] == "latest")
|
||||
{
|
||||
$post = $this->blogData->getLatestBlogPost();
|
||||
if (array_key_exists("errorMessage", $post))
|
||||
{
|
||||
$response->getBody()->write(json_encode($post));
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode($post));
|
||||
return $response;
|
||||
}
|
||||
|
||||
if ($args["type"] == "featured")
|
||||
{
|
||||
$post = $this->blogData->getFeaturedBlogPost();
|
||||
if (array_key_exists("errorMessage", $post))
|
||||
{
|
||||
$response->getBody()->write(json_encode($post));
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode($post));
|
||||
return $response;
|
||||
}
|
||||
|
||||
$post = $this->blogData->getBlogPost($args["type"]);
|
||||
if (array_key_exists("errorMessage", $post))
|
||||
{
|
||||
$response->getBody()->write(json_encode($post));
|
||||
@@ -60,7 +118,7 @@ class blogRoutes implements routesInterface
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
|
||||
$response->getBody()->write(json_encode(array("error" => "Please provide a title")));
|
||||
return $response->withStatus(400);
|
||||
});
|
||||
|
||||
@@ -76,7 +134,14 @@ class blogRoutes implements routesInterface
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$message = $this->blogData->updatePost($args["id"], $data["title"], intval($data["featured"]), $data["body"], $data["dateModified"], $data["categories"]);
|
||||
if (!preg_match('/(?:^|,)(?=[^"]|(")?)"?((?(1)(?:[^"]|"")*|[^,"]*))"?(?=,|$)/mx', $data["categories"]))
|
||||
{
|
||||
// uh oh sent some empty data
|
||||
$response->getBody()->write(json_encode(array("error" => "Categories must be in a CSV format")));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$message = $this->blogData->updatePost($args["id"], $data["title"], intval($data["featured"]), $data["abstract"], $data["body"], $data["dateModified"], $data["categories"]);
|
||||
|
||||
if ($message === "post not found")
|
||||
{
|
||||
@@ -99,7 +164,9 @@ class blogRoutes implements routesInterface
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
$response->withStatus(201);
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
|
||||
@@ -144,20 +211,27 @@ class blogRoutes implements routesInterface
|
||||
$data = $request->getParsedBody();
|
||||
$files = $request->getUploadedFiles();
|
||||
$headerImg = $files["headerImg"];
|
||||
if (empty($data["title"]) || strlen($data["featured"]) == 0 || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["categories"]))
|
||||
if (empty($data["title"]) || strlen($data["featured"]) == 0 || empty($data["body"]) || empty($data["abstract"]) || empty($data["dateCreated"]) || empty($data["categories"]))
|
||||
{
|
||||
// uh oh sent some empty data
|
||||
$response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
if (!preg_match('/(?:^|,)(?=[^"]|(")?)"?((?(1)(?:[^"]|"")*|[^,"]*))"?(?=,|$)/mx', $data["categories"]))
|
||||
{
|
||||
// uh oh sent some empty data
|
||||
$response->getBody()->write(json_encode(array("error" => "Categories must be in a CSV format")));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
if (empty($files["headerImg"]))
|
||||
{
|
||||
$headerImg = null;
|
||||
}
|
||||
|
||||
$featured = $data["featured"] === "true";
|
||||
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $featured, $data["categories"], $headerImg);
|
||||
$insertedID = $this->blogData->createPost($data["title"], $data["abstract"], $data["body"], $data["dateCreated"], $featured, $data["categories"], $headerImg);
|
||||
if (!is_int($insertedID))
|
||||
{
|
||||
// uh oh something went wrong
|
||||
|
||||
Reference in New Issue
Block a user