Used CKEditor for being able to edit the post body. Added the ability to make a post and send it to the backend. Formatted post on backend by moving images into respective directory with the post name and unique ID. Showed message if errored or succeeded.

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
2023-06-26 03:54:25 +01:00
parent 88c5cc9508
commit 61b8d3a987
174 changed files with 895 additions and 187 deletions
+12 -26
View File
@@ -1,9 +1,11 @@
<?php
namespace api\project;
use api\utils\imgUtils;
use PDO;
use Psr\Http\Message\UploadedFileInterface;
require_once __DIR__ . "/../utils/config.php";
require_once __DIR__ . "/../utils/imgUtils.php";
/**
* Project Data Class
@@ -15,7 +17,7 @@ class projectData
* Get all project data
* @return array - Array of all project data or error message
*/
function getProjectData(): array
public function getProjectData(): array
{
$conn = dbConn();
$stmt = $conn->prepare("SELECT ID, title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects ORDER BY isMainProject DESC;");
@@ -43,7 +45,7 @@ class projectData
* @param string $gitLink - Link to the git repository
* @return bool|string - True if project was updated, false if not and there was an error, or an error string
*/
function updateProjectData(string $ID, string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): bool | string
public function updateProjectData(string $ID, string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): bool|string
{
$conn = dbConn();
@@ -82,7 +84,7 @@ class projectData
* @param int $ID - ID of the project in the database to delete
* @return string - True if project was deleted, false if not and there was an error
*/
function deleteProjectData(int $ID): string
public function deleteProjectData(int $ID): string
{
$conn = dbConn();
@@ -121,7 +123,7 @@ class projectData
* @param string $gitLink - Link to the github repository
* @return int|bool - ID of the project if it was added, false if not and there was an error
*/
function addProjectData(string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): int|bool
public function addProjectData(string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): int|bool
{
$conn = dbConn();
@@ -157,31 +159,15 @@ class projectData
public function uploadImage(int $ID, UploadedFileInterface $img): string | array
{
$targetDir = "../imgs/projects/";
$targetFile = $targetDir . basename($img->getClientFilename());
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
$imgUtils = new imgUtils();
$targetFile = $imgUtils->uploadFile($targetDir, $img);
// Check if file already exists
if (file_exists($targetFile))
if (!is_array($targetFile))
{
return "The file already exists";
return $targetFile;
}
// Check file size
if ($img->getSize() > 2000000)
{
return "The file is too large, max 2MB";
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif")
{
return "Only JPG, JPEG, PNG & GIF files are allowed";
}
$img->moveTo($targetFile);
if (file_exists($targetFile))
if (file_exists($targetFile["imgLocation"]))
{
$this->deleteImage($ID);
// update the database with the new image location
@@ -193,7 +179,7 @@ class projectData
if ($stmt->rowCount() > 0)
{
return array("imgLocation" => $targetFile);
return array("imgLocation" => $targetFile["imgLocation"]);
}
return "Couldn't update the database";