Added in the ability not upload a headerImage and instead use a placeholder. Folder ID is now stored in the DB

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
Rohit Pai 2023-06-26 16:52:48 +01:00
parent bae523b62b
commit 7b637dac7b
7 changed files with 48 additions and 17 deletions

View File

@ -14,6 +14,7 @@
"slim/slim": "^4.10",
"rbdwllr/psr-jwt": "^2.0",
"tuupola/slim-jwt-auth": "^3.6",
"ext-dom": "*"
"ext-dom": "*",
"ext-libxml": "*"
}
}

View File

@ -145,10 +145,18 @@ class blogData
public function createPost(string $title, string $body, string $dateCreated, string $featured, string $categories, UploadedFileInterface $headerImg): int|string
{
$conn = dbConn();
$targetDir = "../blog/imgs/" . $title . "_" . uniqid() . "/";
$targetFile = "";
$folderID = uniqid();
if ($headerImg !== null)
{
$targetDir = "../blog/imgs/" . $title . "_" . $folderID . "/";
mkdir($targetDir, 0777, true);
$imagUtils = new imgUtils();
$targetFile = $imagUtils->uploadFile($targetDir, $headerImg);
}
$targetFile = array("imgLocation" => ".../blog/imgs/placeholder.png");
if (!is_array($targetFile))
{
return $targetFile;
@ -192,8 +200,8 @@ class blogData
$newBody .= $htmlDoc->saveHTML($node);
}
$stmt = $conn->prepare("INSERT INTO blog (title, dateCreated, dateModified, featured, headerImg, body, categories)
VALUES (:title, :dateCreated, :dateModified, :featured, :headerImg, :body, :categories);");
$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);
@ -201,6 +209,7 @@ class blogData
$stmt->bindParam(":headerImg", $targetFile["imgLocation"]);
$stmt->bindParam(":body", $newBody);
$stmt->bindParam(":categories", $categories);
$stmt->bindParam(":folderID", $folderID);
if ($stmt->execute())
{

View File

@ -33,14 +33,20 @@ class blogRoutes implements routesInterface
{
$data = $request->getParsedBody();
$files = $request->getUploadedFiles();
if (empty($data["title"]) || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["featured"]) || empty($data["categories"]) || empty($files["headerImg"]))
$headerImg = $files["headerImg"];
if (empty($data["title"]) || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["featured"]) || empty($data["categories"]))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
return $response->withStatus(400);
}
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $data["featured"], $data["categories"], $files["headerImg"]);
if (empty($files["headerImg"]))
{
$headerImg = null;
}
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $data["featured"], $data["categories"], $headerImg);
if (!is_int($insertedID))
{
// uh oh something went wrong

BIN
dist/blog/imgs/placeholder.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

View File

@ -145,10 +145,18 @@ class blogData
public function createPost(string $title, string $body, string $dateCreated, string $featured, string $categories, UploadedFileInterface $headerImg): int|string
{
$conn = dbConn();
$targetDir = "../blog/imgs/" . $title . "_" . uniqid() . "/";
$targetFile = "";
$folderID = uniqid();
if ($headerImg !== null)
{
$targetDir = "../blog/imgs/" . $title . "_" . $folderID . "/";
mkdir($targetDir, 0777, true);
$imagUtils = new imgUtils();
$targetFile = $imagUtils->uploadFile($targetDir, $headerImg);
}
$targetFile = array("imgLocation" => ".../blog/imgs/placeholder.png");
if (!is_array($targetFile))
{
return $targetFile;
@ -192,8 +200,8 @@ class blogData
$newBody .= $htmlDoc->saveHTML($node);
}
$stmt = $conn->prepare("INSERT INTO blog (title, dateCreated, dateModified, featured, headerImg, body, categories)
VALUES (:title, :dateCreated, :dateModified, :featured, :headerImg, :body, :categories);");
$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);
@ -201,6 +209,7 @@ class blogData
$stmt->bindParam(":headerImg", $targetFile["imgLocation"]);
$stmt->bindParam(":body", $newBody);
$stmt->bindParam(":categories", $categories);
$stmt->bindParam(":folderID", $folderID);
if ($stmt->execute())
{

View File

@ -33,14 +33,20 @@ class blogRoutes implements routesInterface
{
$data = $request->getParsedBody();
$files = $request->getUploadedFiles();
if (empty($data["title"]) || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["featured"]) || empty($data["categories"]) || empty($files["headerImg"]))
$headerImg = $files["headerImg"];
if (empty($data["title"]) || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["featured"]) || empty($data["categories"]))
{
// uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
return $response->withStatus(400);
}
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $data["featured"], $data["categories"], $files["headerImg"]);
if (empty($files["headerImg"]))
{
$headerImg = null;
}
$insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $data["featured"], $data["categories"], $headerImg);
if (!is_int($insertedID))
{
// uh oh something went wrong

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB