Added the ability to edit and delete posts which includes uploading of images for the posts and managing those images
Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
+209
-43
@@ -21,7 +21,8 @@ class blogData
|
||||
public function getBlogPosts(): array
|
||||
{
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, body, categories FROM blog ORDER BY dateCreated DESC;");
|
||||
$stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, body, categories, featured
|
||||
FROM blog ORDER BY dateCreated;");
|
||||
$stmt->execute();
|
||||
|
||||
// set the resulting array to associative
|
||||
@@ -99,6 +100,164 @@ class blogData
|
||||
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
|
||||
* @return string - Success or error message
|
||||
*/
|
||||
public function deletePost(int $ID): string
|
||||
{
|
||||
$conn = dbConn();
|
||||
|
||||
$stmtCheckPost = $conn->prepare("SELECT * FROM blog WHERE ID = :ID");
|
||||
$stmtCheckPost->bindParam(":ID", $ID);
|
||||
$stmtCheckPost->execute();
|
||||
$result = $stmtCheckPost->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$result)
|
||||
{
|
||||
return "post not found";
|
||||
}
|
||||
|
||||
if ($result["featured"] === 1)
|
||||
{
|
||||
return "cannot delete";
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("DELETE FROM blog WHERE ID = :ID");
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
|
||||
if ($stmt->execute())
|
||||
{
|
||||
$imagUtils = new imgUtils();
|
||||
$imagUtils->deleteDirectory("../blog/imgs/" . $result["title"] . "_" . $result["folderID"] . "/");
|
||||
return "success";
|
||||
}
|
||||
|
||||
return "error";
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the blog post with the given ID
|
||||
* @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 $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
|
||||
{
|
||||
$conn = dbConn();
|
||||
|
||||
$stmtCheckPost = $conn->prepare("SELECT * FROM blog WHERE ID = :ID");
|
||||
$stmtCheckPost->bindParam(":ID", $ID);
|
||||
$stmtCheckPost->execute();
|
||||
$result = $stmtCheckPost->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$result)
|
||||
{
|
||||
return "post not found";
|
||||
}
|
||||
|
||||
if (!$featured && $result["featured"] === 1)
|
||||
{
|
||||
return "unset feature";
|
||||
}
|
||||
|
||||
if ($featured)
|
||||
{
|
||||
$stmtUnsetFeatured = $conn->prepare("UPDATE blog SET featured = 0 WHERE featured = 1;");
|
||||
$stmtUnsetFeatured->execute();
|
||||
}
|
||||
|
||||
$to = "../blog/imgs/" . $title . "_" . $result["folderID"] . "/";
|
||||
if ($result["title"] !== $title)
|
||||
{
|
||||
$from = "../blog/imgs/" . $result["title"] . "_" . $result["folderID"] . "/";
|
||||
mkdir($to, 0777, true);
|
||||
rename($result["headerImg"], $to . basename($result["headerImg"]));
|
||||
$body = $this->changeHTMLSrc($body, $to, $from);
|
||||
rmdir($from);
|
||||
}
|
||||
|
||||
$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->bindParam(":ID", $ID);
|
||||
$stmt->bindParam(":title", $title);
|
||||
$stmt->bindParam(":featured", $featured);
|
||||
$stmt->bindParam(":body", $newBody);
|
||||
$stmt->bindParam(":dateModified", $dateModified);
|
||||
$stmt->bindParam(":categories", $categories);
|
||||
|
||||
return $stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new post di rectory, uploads the header image and moves the images from the
|
||||
* 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 $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
|
||||
* @param string $categories - Categories of the blog post
|
||||
* @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
|
||||
{
|
||||
$conn = dbConn();
|
||||
$folderID = uniqid();
|
||||
$targetFile = array("imgLocation" => "../blog/imgs/placeholder.png");
|
||||
|
||||
$targetDir = "../blog/imgs/" . $title . "_" . $folderID . "/";
|
||||
mkdir($targetDir, 0777, true);
|
||||
|
||||
if ($headerImg !== null)
|
||||
{
|
||||
$imagUtils = new imgUtils();
|
||||
$targetFile = $imagUtils->uploadFile($targetDir, $headerImg);
|
||||
}
|
||||
|
||||
|
||||
if (!is_array($targetFile))
|
||||
{
|
||||
return $targetFile;
|
||||
}
|
||||
|
||||
$newBody = $this->changeHTMLSrc($body, $targetDir, "../blog/imgs/tmp/");
|
||||
|
||||
|
||||
if ($featured)
|
||||
{
|
||||
$stmtMainProject = $conn->prepare("UPDATE blog SET featured = 0 WHERE featured = 1;");
|
||||
$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->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(":body", $newBody);
|
||||
$stmt->bindParam(":categories", $categories);
|
||||
$stmt->bindParam(":folderID", $folderID);
|
||||
|
||||
if ($stmt->execute())
|
||||
{
|
||||
return intval($conn->lastInsertId());
|
||||
}
|
||||
|
||||
return "Error, couldn't create post";
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload the images in the post to temp folder and return image location
|
||||
* @param UploadedFileInterface $img - Image to upload
|
||||
@@ -131,37 +290,60 @@ class blogData
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new post directory, uploads the header image and moves the images from the
|
||||
* 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 $body - Body of the blog post
|
||||
* @param string $dateCreated - Date the blog post was created
|
||||
* @param string $featured - Whether the blog post is featured or not
|
||||
* @param string $categories - Categories of the blog post
|
||||
* @param UploadedFileInterface $headerImg - Header image of the blog post
|
||||
* @return int|string - ID of the blog post or error message
|
||||
* Upload the header image of the post and update the database
|
||||
* @param int $ID - ID of the post
|
||||
* @param UploadedFileInterface $img - Image to upload
|
||||
* @return string|array - String with error message or array with the location of the uploaded file
|
||||
*/
|
||||
public function createPost(string $title, string $body, string $dateCreated, string $featured, string $categories, UploadedFileInterface $headerImg): int|string
|
||||
public function uploadHeaderImage(int $ID, UploadedFileInterface $img): string|array
|
||||
{
|
||||
$conn = dbConn();
|
||||
$targetFile = "";
|
||||
$folderID = uniqid();
|
||||
if ($headerImg !== null)
|
||||
$stmt = $conn->prepare("SELECT * FROM blog WHERE ID = :ID;");
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$result)
|
||||
{
|
||||
$targetDir = "../blog/imgs/" . $title . "_" . $folderID . "/";
|
||||
mkdir($targetDir, 0777, true);
|
||||
$imagUtils = new imgUtils();
|
||||
$targetFile = $imagUtils->uploadFile($targetDir, $headerImg);
|
||||
return "Couldn't find the post";
|
||||
}
|
||||
|
||||
$targetFile = array("imgLocation" => ".../blog/imgs/placeholder.png");
|
||||
$targetDir = "../blog/imgs/" . $result["title"] . "_" . $result["folderID"] . "/";
|
||||
$imagUtils = new imgUtils();
|
||||
$targetFile = $imagUtils->uploadFile($targetDir, $img);
|
||||
|
||||
if (!is_array($targetFile))
|
||||
{
|
||||
return $targetFile;
|
||||
}
|
||||
|
||||
if (file_exists($targetFile["imgLocation"]))
|
||||
{
|
||||
unlink($result["headerImg"]);
|
||||
$stmt = $conn->prepare("UPDATE blog SET headerImg = :headerImg WHERE ID = :ID;");
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
$stmt->bindParam(":headerImg", $targetFile["imgLocation"]);
|
||||
$stmt->execute();
|
||||
if ($stmt->rowCount() > 0)
|
||||
{
|
||||
return $targetFile;
|
||||
}
|
||||
|
||||
return "Couldn't update the post";
|
||||
}
|
||||
|
||||
return "Couldn't upload the image";
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the HTML src of the images in the post to point to the new location
|
||||
* @param string $body - Body of the post
|
||||
* @param string $to - New location of the images
|
||||
* @param string $from - Old location of the images
|
||||
* @return string - Body of the post with the new image locations
|
||||
*/
|
||||
public function changeHTMLSrc(string $body, string $to, string $from): string
|
||||
{
|
||||
$htmlDoc = new DOMDocument();
|
||||
$htmlDoc->loadHTML($body, LIBXML_NOERROR);
|
||||
$doc = $htmlDoc->getElementsByTagName('body')->item(0);
|
||||
@@ -172,24 +354,25 @@ class blogData
|
||||
foreach ($imgs as $img)
|
||||
{
|
||||
$src = $img->getAttribute("src");
|
||||
$src = urldecode($src);
|
||||
$srcList[] = $src;
|
||||
$fileName = basename($src);
|
||||
|
||||
$img->setAttribute("src", $targetDir . $fileName);
|
||||
$img->setAttribute("src", $to . $fileName);
|
||||
}
|
||||
|
||||
$files = scandir("../blog/imgs/tmp/");
|
||||
$files = scandir($from);
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if ($file != "." && $file != "..")
|
||||
{
|
||||
if (!in_array("../blog/imgs/tmp/" . $file, $srcList))
|
||||
if (!in_array($from . $file, $srcList))
|
||||
{
|
||||
unlink("../blog/imgs/tmp/" . $file);
|
||||
unlink($from . $file);
|
||||
}
|
||||
else
|
||||
{
|
||||
rename("../blog/imgs/tmp/" . $file, $targetDir . $file);
|
||||
rename($from . $file, $to . $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -199,23 +382,6 @@ class blogData
|
||||
{
|
||||
$newBody .= $htmlDoc->saveHTML($node);
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("INSERT INTO blog (title, dateCreated, dateModified, featured, headerImg, body, categories, folderID)
|
||||
VALUES (:title, :dateCreated, :dateModified, :featured, :headerImg, :body, :categories, :folderID);");
|
||||
$stmt->bindParam(":title", $title);
|
||||
$stmt->bindParam(":dateCreated", $dateCreated);
|
||||
$stmt->bindParam(":dateModified", $dateCreated);
|
||||
$stmt->bindParam(":featured", $featured);
|
||||
$stmt->bindParam(":headerImg", $targetFile["imgLocation"]);
|
||||
$stmt->bindParam(":body", $newBody);
|
||||
$stmt->bindParam(":categories", $categories);
|
||||
$stmt->bindParam(":folderID", $folderID);
|
||||
|
||||
if ($stmt->execute())
|
||||
{
|
||||
return intval($conn->lastInsertId());
|
||||
}
|
||||
|
||||
return "Error, couldn't create post";
|
||||
return $newBody;
|
||||
}
|
||||
}
|
||||
+142
-4
@@ -29,12 +29,122 @@ class blogRoutes implements routesInterface
|
||||
*/
|
||||
public function createRoutes(App $app): void
|
||||
{
|
||||
$app->post("/blog/post", function (Request $request, Response $response, array $args)
|
||||
$app->get("/blog/post", function (Request $request, Response $response)
|
||||
{
|
||||
$posts = $this->blogData->getBlogPosts();
|
||||
|
||||
$json = json_encode($posts);
|
||||
|
||||
$response->getBody()->write($json);
|
||||
|
||||
if (array_key_exists("errorMessage", $posts))
|
||||
{
|
||||
$response->withStatus(404);
|
||||
}
|
||||
|
||||
return $response;
|
||||
});
|
||||
|
||||
$app->get("/blog/post/{id}", function (Request $request, Response $response, $args)
|
||||
{
|
||||
if ($args["id"] != null)
|
||||
{
|
||||
$post = $this->blogData->getBlogPost($args["id"]);
|
||||
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 an ID")));
|
||||
return $response->withStatus(400);
|
||||
});
|
||||
|
||||
$app->patch("/blog/post/{id}", function (Request $request, Response $response, $args)
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
if ($args["id"] != null)
|
||||
{
|
||||
if (empty($data["title"]) || strlen($data["featured"]) == 0 || empty($data["body"]) || empty($data["dateModified"]) || empty($data["categories"]))
|
||||
{
|
||||
// uh oh sent some empty data
|
||||
$response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$message = $this->blogData->updatePost($args["id"], $data["title"], intval($data["featured"]), $data["body"], $data["dateModified"], $data["categories"]);
|
||||
|
||||
if ($message === "post not found")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Error, post not found")));
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
if ($message === "unset featured")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Error, cannot unset featured post, try updating another post to be featured first")));
|
||||
return $response->withStatus(409);
|
||||
}
|
||||
|
||||
if (!is_bool($message) || $message === false)
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => $message)));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
|
||||
return $response->withStatus(400);
|
||||
});
|
||||
|
||||
$app->delete("/blog/post/{id}", function (Request $request, Response $response, $args)
|
||||
{
|
||||
if ($args["id"] != null)
|
||||
{
|
||||
$message = $this->blogData->deletePost($args["id"]);
|
||||
|
||||
if ($message === "post not found")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Error, post not found")));
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
if ($message === "error")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Error, something went wrong")));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
if ($message === "cannot delete")
|
||||
{
|
||||
// uh oh something went wrong
|
||||
$response->getBody()->write(json_encode(array("error" => "Error, cannot delete featured post")));
|
||||
return $response->withStatus(409);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
|
||||
return $response->withStatus(400);
|
||||
});
|
||||
|
||||
$app->post("/blog/post", function (Request $request, Response $response)
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
$files = $request->getUploadedFiles();
|
||||
$headerImg = $files["headerImg"];
|
||||
if (empty($data["title"]) || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["featured"]) || empty($data["categories"]))
|
||||
if (empty($data["title"]) || strlen($data["featured"]) == 0 || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["categories"]))
|
||||
{
|
||||
// uh oh sent some empty data
|
||||
$response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
|
||||
@@ -46,7 +156,8 @@ class blogRoutes implements routesInterface
|
||||
$headerImg = null;
|
||||
}
|
||||
|
||||
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $data["featured"], $data["categories"], $headerImg);
|
||||
$featured = $data["featured"] === "true";
|
||||
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $featured, $data["categories"], $headerImg);
|
||||
if (!is_int($insertedID))
|
||||
{
|
||||
// uh oh something went wrong
|
||||
@@ -74,9 +185,36 @@ class blogRoutes implements routesInterface
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
|
||||
$response->getBody()->write(json_encode($message));
|
||||
return $response->withStatus(201);
|
||||
});
|
||||
|
||||
$app->post("/blog/headerImage/{id}", function (Request $request, Response $response, $args)
|
||||
{
|
||||
$files = $request->getUploadedFiles();
|
||||
|
||||
if ($args["id"] != null)
|
||||
{
|
||||
if (empty($files))
|
||||
{
|
||||
// uh oh sent some empty data
|
||||
$response->getBody()->write(json_encode(array("error" => array("message" => "Error, empty data sent"))));
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$message = $this->blogData->uploadHeaderImage($args["id"], $files["headerImg"]);
|
||||
if (!is_array($message))
|
||||
{
|
||||
$response->getBody()->write(json_encode(array("error" => array("message" => $message))));
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode($message));
|
||||
return $response->withStatus(201);
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
|
||||
return $response->withStatus(400);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user