Added in ability to add a new project with and with an image and update a project with and without image. If a project is the main project then it cannot be deleted and when a new main project is selected it gets moved to the top.
Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
Vendored
+62
-21
@@ -18,7 +18,7 @@ class projectData
|
||||
function getProjectData(): array
|
||||
{
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("SELECT ID, title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects;");
|
||||
$stmt = $conn->prepare("SELECT ID, title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects ORDER BY isMainProject DESC;");
|
||||
$stmt->execute();
|
||||
|
||||
// set the resulting array to associative
|
||||
@@ -37,52 +37,66 @@ class projectData
|
||||
* Update project data in the database with the given ID
|
||||
* @param string $ID - ID of the project in the database to update
|
||||
* @param string $title - Title of the project
|
||||
* @param bool $isMainProject - Is the project a main project or not
|
||||
* @param string $isMainProject - Is the project a main project or not
|
||||
* @param string $information - Information about the project
|
||||
* @param string $imgLocation - Location of the image
|
||||
* @param string $projectLink - Link to the project
|
||||
* @param string $gitLink - Link to the github repository
|
||||
* @param string $gitLink - Link to the git repository
|
||||
* @return bool - True if project was updated, false if not and there was an error
|
||||
*/
|
||||
function updateProjectData(string $ID, string $title, bool $isMainProject, string $information, string $imgLocation, string $projectLink, string $gitLink): bool
|
||||
function updateProjectData(string $ID, string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): bool
|
||||
{
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("UPDATE projects SET title = :title, isMainProject = :isMainProject, information = :information, imgLocation = :imgLocation, projectLink = :projectLink, gitLink = :gitLink WHERE ID = :ID");
|
||||
|
||||
if ($isMainProject === "true")
|
||||
{
|
||||
$stmtMainProject = $conn->prepare("UPDATE projects SET isMainProject = 0;");
|
||||
$stmtMainProject->execute();
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("UPDATE projects SET title = :title, isMainProject = :isMainProject, information = :information, projectLink = :projectLink, gitLink = :gitLink WHERE ID = :ID");
|
||||
$stmt->bindParam(":title", $title);
|
||||
$stmt->bindParam(":isMainProject", $isMainProject);
|
||||
$isMainProj = ($isMainProject) ? 1 : 0;
|
||||
$stmt->bindParam(":isMainProject", $isMainProj);
|
||||
$stmt->bindParam(":information", $information);
|
||||
$stmt->bindParam(":imgLocation", $imgLocation);
|
||||
$stmt->bindParam(":projectLink", $projectLink);
|
||||
$stmt->bindParam(":gitLink", $gitLink);
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $stmt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete project data from the database
|
||||
* @param int $ID - ID of the project in the database to delete
|
||||
* @return bool - True if project was deleted, false if not and there was an error
|
||||
* @return string - True if project was deleted, false if not and there was an error
|
||||
*/
|
||||
function deleteProjectData(int $ID): bool
|
||||
function deleteProjectData(int $ID): string
|
||||
{
|
||||
$conn = dbConn();
|
||||
|
||||
// check if the project is a main project if it is return false
|
||||
|
||||
$stmtMainProject = $conn->prepare("SELECT isMainProject FROM projects WHERE ID = :ID");
|
||||
$stmtMainProject->bindParam(":ID", $ID);
|
||||
$stmtMainProject->execute();
|
||||
$result = $stmtMainProject->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($result["isMainProject"] === "1")
|
||||
{
|
||||
return "cannot delete";
|
||||
}
|
||||
|
||||
$this->deleteImage($ID);
|
||||
|
||||
$stmt = $conn->prepare("DELETE FROM projects WHERE ID = :ID");
|
||||
$stmt->bindParam(":ID", $ID);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0)
|
||||
{
|
||||
return true;
|
||||
return "ok";
|
||||
}
|
||||
|
||||
return false;
|
||||
return "error";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,10 +110,18 @@ class projectData
|
||||
*/
|
||||
function addProjectData(string $title, string $isMainProject, string $information, string $projectLink, string $gitLink): int|bool
|
||||
{
|
||||
|
||||
$conn = dbConn();
|
||||
if ($isMainProject === "true")
|
||||
{
|
||||
$stmtMainProject = $conn->prepare("UPDATE projects SET isMainProject = 0;");
|
||||
$stmtMainProject->execute();
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("INSERT INTO projects (title, isMainProject, information, projectLink, gitLink) VALUES (:title, :isMainProject, :information, :projectLink, :gitLink)");
|
||||
$stmt->bindParam(":title", $title);
|
||||
$stmt->bindParam(":isMainProject", $isMainProject);
|
||||
$isMainProj = ($isMainProject) ? 1 : 0;
|
||||
$stmt->bindParam(":isMainProject", $isMainProj);
|
||||
$stmt->bindParam(":information", $information);
|
||||
$stmt->bindParam(":projectLink", $projectLink);
|
||||
$stmt->bindParam(":gitLink", $gitLink);
|
||||
@@ -148,6 +170,7 @@ class projectData
|
||||
|
||||
if (file_exists($targetFile))
|
||||
{
|
||||
$this->deleteImage($ID);
|
||||
// update the database with the new image location
|
||||
$conn = dbConn();
|
||||
$stmt = $conn->prepare("UPDATE projects SET imgLocation = :imgLocation WHERE ID = :ID");
|
||||
@@ -165,4 +188,22 @@ class projectData
|
||||
|
||||
return "Couldn't upload the image";
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the image from the server
|
||||
* @param int $ID - ID of the project in the database
|
||||
*/
|
||||
private function deleteImage(int $ID): void
|
||||
{
|
||||
$conn = dbConn();
|
||||
$imgStmt = $conn->prepare("SELECT imgLocation FROM projects WHERE ID = :ID");
|
||||
$imgStmt->bindParam(":ID", $ID);
|
||||
$imgStmt->execute();
|
||||
$imgLocation = $imgStmt->fetch(PDO::FETCH_ASSOC)["imgLocation"];
|
||||
|
||||
if ($imgLocation != null)
|
||||
{
|
||||
unlink($imgLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user