add-to-blog #34

Merged
rodude123 merged 3 commits from add-to-blog into master 2023-06-26 17:05:57 +01:00
7 changed files with 48 additions and 17 deletions
Showing only changes of commit 7b637dac7b - Show all commits

View File

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

View File

@ -33,14 +33,20 @@ class blogRoutes implements routesInterface
{ {
$data = $request->getParsedBody(); $data = $request->getParsedBody();
$files = $request->getUploadedFiles(); $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 // uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Error, empty data sent"))); $response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
return $response->withStatus(400); 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)) if (!is_int($insertedID))
{ {
// uh oh something went wrong // 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 public function createPost(string $title, string $body, string $dateCreated, string $featured, string $categories, UploadedFileInterface $headerImg): int|string
{ {
$conn = dbConn(); $conn = dbConn();
$targetDir = "../blog/imgs/" . $title . "_" . uniqid() . "/"; $targetFile = "";
$folderID = uniqid();
if ($headerImg !== null)
{
$targetDir = "../blog/imgs/" . $title . "_" . $folderID . "/";
mkdir($targetDir, 0777, true); mkdir($targetDir, 0777, true);
$imagUtils = new imgUtils(); $imagUtils = new imgUtils();
$targetFile = $imagUtils->uploadFile($targetDir, $headerImg); $targetFile = $imagUtils->uploadFile($targetDir, $headerImg);
}
$targetFile = array("imgLocation" => ".../blog/imgs/placeholder.png");
if (!is_array($targetFile)) if (!is_array($targetFile))
{ {
return $targetFile; return $targetFile;
@ -192,8 +200,8 @@ class blogData
$newBody .= $htmlDoc->saveHTML($node); $newBody .= $htmlDoc->saveHTML($node);
} }
$stmt = $conn->prepare("INSERT INTO blog (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);"); VALUES (:title, :dateCreated, :dateModified, :featured, :headerImg, :body, :categories, :folderID);");
$stmt->bindParam(":title", $title); $stmt->bindParam(":title", $title);
$stmt->bindParam(":dateCreated", $dateCreated); $stmt->bindParam(":dateCreated", $dateCreated);
$stmt->bindParam(":dateModified", $dateCreated); $stmt->bindParam(":dateModified", $dateCreated);
@ -201,6 +209,7 @@ class blogData
$stmt->bindParam(":headerImg", $targetFile["imgLocation"]); $stmt->bindParam(":headerImg", $targetFile["imgLocation"]);
$stmt->bindParam(":body", $newBody); $stmt->bindParam(":body", $newBody);
$stmt->bindParam(":categories", $categories); $stmt->bindParam(":categories", $categories);
$stmt->bindParam(":folderID", $folderID);
if ($stmt->execute()) if ($stmt->execute())
{ {

View File

@ -33,14 +33,20 @@ class blogRoutes implements routesInterface
{ {
$data = $request->getParsedBody(); $data = $request->getParsedBody();
$files = $request->getUploadedFiles(); $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 // uh oh sent some empty data
$response->getBody()->write(json_encode(array("error" => "Error, empty data sent"))); $response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
return $response->withStatus(400); 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)) if (!is_int($insertedID))
{ {
// uh oh something went wrong // uh oh something went wrong

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB